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/** A template class to build URLs from base URL, path, URL parameters and Query String. */
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 final 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      if (!NUMERIC.matcher(valueString).matches()) {
035        throw new BoxAPIException("An invalid path parameter passed in. It must be numeric.");
036      }
037    }
038    try {
039      return new URL(format(fullTemplate(base), values));
040    } catch (MalformedURLException e) {
041      throw new BoxAPIException(e.getMessage());
042    }
043  }
044
045  /**
046   * Build a URL with alphanumeric URL Parameters.
047   *
048   * @param base base URL
049   * @param values URL parameters
050   * @return URL
051   */
052  public URL buildAlpha(String base, Object... values) {
053    for (Object value : values) {
054      String valueString = String.valueOf(value);
055      Boolean test = ALPHA_NUMERIC.matcher(valueString).matches();
056      if (!ALPHA_NUMERIC.matcher(valueString).matches()) {
057        throw new BoxAPIException("An invalid path parameter passed in. It must be alphanumeric.");
058      }
059    }
060    try {
061      return new URL(format(fullTemplate(base), values));
062    } catch (MalformedURLException e) {
063      throw new BoxAPIException(e.getMessage());
064    }
065  }
066
067  /**
068   * Build a URL with Query String and numeric URL Parameters.
069   *
070   * @param base base URL
071   * @param queryString query string
072   * @param values URL Parameters
073   * @return URL
074   */
075  public URL buildWithQuery(String base, String queryString, Object... values) {
076    for (Object value : values) {
077      String valueString = String.valueOf(value);
078      if (!NUMERIC.matcher(valueString).matches()) {
079        throw new BoxAPIException("An invalid path param passed in. It must be numeric.");
080      }
081    }
082    try {
083      String urlString = format(fullTemplate(base), values) + queryString;
084      return new URL(urlString);
085    } catch (MalformedURLException e) {
086      throw new BoxAPIException(e.getMessage());
087    }
088  }
089
090  /**
091   * Build a URL with Query String and alphanumeric URL Parameters.
092   *
093   * @param base base URL
094   * @param queryString query string
095   * @param values URL Parameters
096   * @return URL
097   */
098  public URL buildAlphaWithQuery(String base, String queryString, Object... values) {
099    for (Object value : values) {
100      String valueString = String.valueOf(value);
101      if (!ALPHA_NUMERIC.matcher(valueString).matches()) {
102        throw new BoxAPIException("An invalid path param passed in. It must be alphanumeric.");
103      }
104    }
105    try {
106      String urlString = format(fullTemplate(base), values) + queryString;
107      return new URL(urlString);
108    } catch (MalformedURLException e) {
109      throw new BoxAPIException(e.getMessage());
110    }
111  }
112
113  private String fullTemplate(String path) {
114    return path + this.template;
115  }
116}