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 * @param template path 018 */ 019 public URLTemplate(String template) { 020 this.template = template; 021 } 022 023 /** 024 * Build a URL with numeric URL Parameters. 025 * @param base base URL 026 * @param values URL parameters 027 * @return URL 028 */ 029 public URL build(String base, Object... values) { 030 for (Object value: values) { 031 String valueString = String.valueOf(value); 032 Boolean test = NUMERIC.matcher(valueString).matches(); 033 if (!NUMERIC.matcher(valueString).matches()) { 034 throw new BoxAPIException("An invalid path parameter passed in. It must be numeric."); 035 } 036 } 037 String urlString = String.format(base + this.template, values); 038 039 URL url = null; 040 try { 041 url = new URL(urlString); 042 } catch (MalformedURLException e) { 043 throw new BoxAPIException("An invalid path parameter passed in. It must be numeric."); 044 } 045 046 return url; 047 } 048 049 /** 050 * Build a URL with alphanumeric URL Parameters. 051 * @param base base URL 052 * @param values URL parameters 053 * @return URL 054 */ 055 public URL buildAlpha(String base, Object... values) { 056 for (Object value: values) { 057 String valueString = String.valueOf(value); 058 Boolean test = ALPHA_NUMERIC.matcher(valueString).matches(); 059 if (!ALPHA_NUMERIC.matcher(valueString).matches()) { 060 throw new BoxAPIException("An invalid path parameter passed in. It must be alphanumeric."); 061 } 062 } 063 String urlString = String.format(base + this.template, values); 064 065 URL url = null; 066 try { 067 url = new URL(urlString); 068 } catch (MalformedURLException e) { 069 throw new BoxAPIException("A valid URL could not be constructed from the provided parameters."); 070 } 071 072 return url; 073 } 074 075 /** 076 * Build a URL with Query String and numeric URL Parameters. 077 * @param base base URL 078 * @param queryString query string 079 * @param values URL Parameters 080 * @return URL 081 */ 082 public URL buildWithQuery(String base, String queryString, Object... values) { 083 for (Object value: values) { 084 String valueString = String.valueOf(value); 085 if (!NUMERIC.matcher(valueString).matches()) { 086 throw new BoxAPIException("An invalid path param passed in. It must be numeric."); 087 } 088 } 089 String urlString = String.format(base + this.template, values) + queryString; 090 URL url = null; 091 try { 092 url = new URL(urlString); 093 } catch (MalformedURLException e) { 094 throw new BoxAPIException("A valid URL could not be constructed from the provided parameters."); 095 } 096 097 return url; 098 } 099 100 /** 101 * Build a URL with Query String and alphanumeric URL Parameters. 102 * @param base base URL 103 * @param queryString query string 104 * @param values URL Parameters 105 * @return URL 106 */ 107 public URL buildAlphaWithQuery(String base, String queryString, Object... values) { 108 for (Object value: values) { 109 String valueString = String.valueOf(value); 110 if (!ALPHA_NUMERIC.matcher(valueString).matches()) { 111 throw new BoxAPIException("An invalid path param passed in. It must be alphanumeric."); 112 } 113 } 114 String urlString = String.format(base + this.template, values) + queryString; 115 URL url = null; 116 try { 117 url = new URL(urlString); 118 } catch (MalformedURLException e) { 119 throw new BoxAPIException("A valid URL could not be constructed from the provided parameters."); 120 } 121 122 return url; 123 } 124}