001package com.box.sdk; 002 003import java.net.MalformedURLException; 004import java.net.URL; 005import java.util.regex.Pattern; 006 007/** 008 * A template class to build URLs from base URL, path, URL parameters and Query String. 009 */ 010public class URLTemplate { 011 private static final Pattern NUMERIC = Pattern.compile("^[0-9]*$"); 012 private static final Pattern ALPHA_NUMERIC = Pattern.compile("^[a-zA-Z0-9!@#$%^&*()_+\\-]*$"); 013 private String template; 014 015 /** 016 * Construct an URL Template object from path. 017 * 018 * @param template path 019 */ 020 public URLTemplate(String template) { 021 this.template = template; 022 } 023 024 /** 025 * Build a URL with numeric URL Parameters. 026 * 027 * @param base base URL 028 * @param values URL parameters 029 * @return URL 030 */ 031 public URL build(String base, Object... values) { 032 for (Object value : values) { 033 String valueString = String.valueOf(value); 034 Boolean test = NUMERIC.matcher(valueString).matches(); 035 if (!NUMERIC.matcher(valueString).matches()) { 036 throw new BoxAPIException("An invalid path parameter passed in. It must be numeric."); 037 } 038 } 039 String urlString = String.format(base + this.template, values); 040 041 URL url = null; 042 try { 043 url = new URL(urlString); 044 } catch (MalformedURLException e) { 045 throw new BoxAPIException("An invalid path parameter passed in. It must be numeric."); 046 } 047 048 return url; 049 } 050 051 /** 052 * Build a URL with alphanumeric URL Parameters. 053 * 054 * @param base base URL 055 * @param values URL parameters 056 * @return URL 057 */ 058 public URL buildAlpha(String base, Object... values) { 059 for (Object value : values) { 060 String valueString = String.valueOf(value); 061 Boolean test = ALPHA_NUMERIC.matcher(valueString).matches(); 062 if (!ALPHA_NUMERIC.matcher(valueString).matches()) { 063 throw new BoxAPIException("An invalid path parameter passed in. It must be alphanumeric."); 064 } 065 } 066 String urlString = String.format(base + this.template, values); 067 068 URL url = null; 069 try { 070 url = new URL(urlString); 071 } catch (MalformedURLException e) { 072 throw new BoxAPIException("A valid URL could not be constructed from the provided parameters."); 073 } 074 075 return url; 076 } 077 078 /** 079 * Build a URL with Query String and numeric URL Parameters. 080 * 081 * @param base base URL 082 * @param queryString query string 083 * @param values URL Parameters 084 * @return URL 085 */ 086 public URL buildWithQuery(String base, String queryString, Object... values) { 087 for (Object value : values) { 088 String valueString = String.valueOf(value); 089 if (!NUMERIC.matcher(valueString).matches()) { 090 throw new BoxAPIException("An invalid path param passed in. It must be numeric."); 091 } 092 } 093 String urlString = String.format(base + this.template, values) + queryString; 094 URL url = null; 095 try { 096 url = new URL(urlString); 097 } catch (MalformedURLException e) { 098 throw new BoxAPIException("A valid URL could not be constructed from the provided parameters."); 099 } 100 101 return url; 102 } 103 104 /** 105 * Build a URL with Query String and alphanumeric URL Parameters. 106 * 107 * @param base base URL 108 * @param queryString query string 109 * @param values URL Parameters 110 * @return URL 111 */ 112 public URL buildAlphaWithQuery(String base, String queryString, Object... values) { 113 for (Object value : values) { 114 String valueString = String.valueOf(value); 115 if (!ALPHA_NUMERIC.matcher(valueString).matches()) { 116 throw new BoxAPIException("An invalid path param passed in. It must be alphanumeric."); 117 } 118 } 119 String urlString = String.format(base + this.template, values) + queryString; 120 URL url = null; 121 try { 122 url = new URL(urlString); 123 } catch (MalformedURLException e) { 124 throw new BoxAPIException("A valid URL could not be constructed from the provided parameters."); 125 } 126 127 return url; 128 } 129}