示例#1
0
  /**
   * @param words
   * @param separator
   * @return String of words concatonated with the separator
   */
  public static String join(double[] words, String separator) {

    if (ArrayUtils.isEmpty(words)) return EMPTY;

    StringBuilder ret = new StringBuilder();
    for (int i = 0; i < words.length; i++) {
      if (i > 0) {
        ret.append(separator);
      }
      ret.append(words[i]);
    }
    return ret.toString();
  }
示例#2
0
  /**
   * Returns true if the parameter string prior to the "sig" parameter match the "sig" parameter
   * when combined with the key and hashed. For post requests, the parameter order is not guaranteed
   * and so is assumed to be sorted alphabetically by key.
   *
   * @param req
   * @param secret
   * @return
   */
  public static boolean isAuthenticatedRequest(HttpServletRequest req, String secret) {
    String query, sig = getStringParameter(req, "sig"), method = req.getMethod();
    if (method.equalsIgnoreCase("GET")) {
      query = req.getQueryString();

      if (StringUtils.isBlank(query) || StringUtils.isBlank(sig)) return false;

      query = query.replace("&sig=" + sig, EMPTY);
    } else if (method.equalsIgnoreCase("POST")) {
      TreeMap<String, String[]> params = new TreeMap(req.getParameterMap());
      params.remove("sig"); // remove the signature

      StringBuilder buf = new StringBuilder();
      for (Map.Entry<String, String[]> entry : params.entrySet()) {
        if (buf.length() > 0) buf.append("&");

        buf.append(entry.getKey());
        buf.append('=');
        buf.append(entry.getValue()[0]);
      }
      query = buf.toString();
    } else // We're not supporting auth on non GET or POST requests
    return false;

    return signQueryString(query, secret).equals(sig);
  }
示例#3
0
  /**
   * @param <T>
   * @param words to join
   * @param separator
   * @return String of words concatonated with the separator
   */
  public static <T extends Object> String join(Collection<T> words, String separator) {

    if (words == null || words.isEmpty()) {
      return EMPTY;
    }

    StringBuilder ret = new StringBuilder();
    Iterator<T> itr = words.iterator();
    int i = 0;
    while (itr.hasNext()) {
      if (i > 0) {
        ret.append(separator);
      }
      ret.append(itr.next().toString());
      i++;
    }
    return ret.toString();
  }
示例#4
0
  /**
   * @param source
   * @return
   */
  public static String toCamelCase(String source) {

    if (StringUtils.isEmpty(source)) return EMPTY;
    if (StringUtils.isBlank(source)) return source;

    source = source.replaceAll("-|_|\\.", SPACE);

    String[] parts = source.split(SPACE);

    StringBuilder ret = new StringBuilder();

    for (int i = 0; i < parts.length; i++) {
      if (i == 0) {
        ret.append(parts[i]);
      } else {
        ret.append(capitalizeFirst(parts[i]));
      }
    }

    return ret.toString();
  }
示例#5
0
  /**
   * @param req
   * @return
   */
  public static String describeRequest(HttpServletRequest req) {

    if (req == null) {
      return EMPTY;
    }

    HttpSession session = null;
    try {
      session = req.getSession();
    } catch (Exception e) {
    }

    StringBuilder body = new StringBuilder();
    body.append("Browser: " + req.getHeader("User-Agent"));

    body.append("\n\nRequest Info");
    body.append("\nRequest URI: " + req.getRequestURI());
    body.append("\nRequest URL: " + req.getRequestURL().toString());
    body.append("\nPath Info: " + req.getPathInfo());
    body.append("\nQuery String: " + req.getQueryString());

    if (session != null) {
      body.append("\n\nSession Info");
      body.append("\nSession ID: " + session.getId());
      body.append("\nSession Created: " + new Date(session.getCreationTime()).toString());
      body.append("\nSession Last Accessed: " + new Date(session.getLastAccessedTime()).toString());
    }

    body.append("\n\nUser Info");
    body.append("\nRemote User: "******"\nUser Principal: " + req.getUserPrincipal());

    body.append("\n\nServer Info");
    String hostname = "", serverInstance = "", ip = "";
    try {
      hostname = java.net.InetAddress.getLocalHost().getHostName();
      serverInstance = System.getProperty("com.sun.aas.instanceName");
      ip = java.net.InetAddress.getLocalHost().getHostAddress();
      body.append("\nInstance: " + serverInstance + " : " + ip + " : " + hostname);
    } catch (Exception e) {
    }

    return body.toString();
  }