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