HttpUtil.java :  » Facebook » socialauth » org » brickred » socialauth » util » Java Open Source

Java Open Source » Facebook » socialauth 
socialauth » org » brickred » socialauth » util » HttpUtil.java
/*
 ===========================================================================
 Copyright (c) 2010 BrickRed Technologies Limited

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ===========================================================================

 */
package org.brickred.socialauth.util;

import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.brickred.socialauth.exception.SocialAuthException;

/**
 * This class is used to make HTTP requests. We did try NOT writing this class
 * and instead use Apache Commons HTTP Client. However it has been reported by
 * users that Commons HTTP Client does not work on Google AppEngine. Hence we
 * have handcoded this class using HTTPURLConnection and incorporated only as
 * much functionality as is needed for OAuth clients.
 * 
 * This class may be completed rewritten in future, or may be removed if a
 * version of Commons HTTP Client compatible with AppEngine is released.
 * 
 * @author tarunn@brickred.com
 * 
 */
public class HttpUtil {

  /**
   * Makes HTTP request using java.net.HTTPURLConnection
   * 
   * @param urlStr
   *            the URL String
   * @param requestMethod
   *            Method type
   * @param body
   *            Body to pass in request.
   * @param header
   *            Header parameters
   * @return Response Object
   * @throws Exception
   */
  public static Response doHttpRequest(final String urlStr,
      final String requestMethod, final String body,
      final Map<String, String> header) throws Exception {
    HttpURLConnection conn;
    try {
      URL url = new URL(urlStr);
      conn = (HttpURLConnection) url.openConnection();
      conn.setDoOutput(true);
      conn.setDoInput(true);
      conn.setInstanceFollowRedirects(true);
      if (requestMethod != null) {
        conn.setRequestMethod(requestMethod);
      }
      if (header != null) {
        for (String key : header.keySet()) {
          conn.setRequestProperty(key, header.get(key));
        }
      }

      // If use POST or PUT must use this
      OutputStreamWriter wr = null;
      if (body != null) {
        if (requestMethod != null
            && !MethodType.GET.toString().equals(requestMethod)
            && !MethodType.DELETE.toString().equals(requestMethod)) {
          wr = new OutputStreamWriter(conn.getOutputStream());
          wr.write(body);
          wr.flush();
        }
      }
      conn.connect();
    } catch (Exception e) {
      throw new SocialAuthException(e);
    }
    return new Response(conn);

  }

  /**
   * Generates a query string from given Map while sorting the parameters in
   * the canonical order as required by oAuth before signing
   * 
   * @param params
   *            Parameters Map to generate query string
   * @return String
   * @throws Exception
   */
  public static String buildParams(final Map<String, String> params)
      throws Exception {
    List<String> argList = new ArrayList<String>();

    for (String key : params.keySet()) {
      String val = params.get(key);
      if (val != null && val.length() > 0) {
        String arg = key + "=" + encodeURIComponent(val);
        argList.add(arg);
      }
    }
    Collections.sort(argList);
    StringBuilder s = new StringBuilder();
    for (int i = 0; i < argList.size(); i++) {
      s.append(argList.get(i));
      if (i != argList.size() - 1) {
        s.append("&");
      }
    }
    return s.toString();
  }

  private static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";

  public static String encodeURIComponent(final String value)
      throws Exception {
    if (value == null) {
      return "";
    }

    try {
      return URLEncoder.encode(value, "utf-8")
          // OAuth encodes some characters differently:
          .replace("+", "%20").replace("*", "%2A")
          .replace("%7E", "~");
      // This could be done faster with more hand-crafted code.
    } catch (UnsupportedEncodingException wow) {
      throw new SocialAuthException(wow.getMessage(), wow);
    }
  }

  /*
   * public static String encodeURIComponent(final String input) { if (input
   * == null || input.trim().length() == 0) { return input; }
   * 
   * int l = input.length(); StringBuilder o = new StringBuilder(l * 3); try {
   * for (int i = 0; i < l; i++) { String e = input.substring(i, i + 1); if
   * (ALLOWED_CHARS.indexOf(e) == -1) { byte[] b = e.getBytes("utf-8");
   * o.append(getHex(b)); continue; } o.append(e); } return o.toString(); }
   * catch (UnsupportedEncodingException e) { e.printStackTrace(); } return
   * input; }
   */

  private static String getHex(final byte buf[]) {
    StringBuilder o = new StringBuilder(buf.length * 3);
    for (int i = 0; i < buf.length; i++) {
      int n = buf[i] & 0xff;
      o.append("%");
      if (n < 0x10) {
        o.append("0");
      }
      o.append(Long.toString(n, 16).toUpperCase());
    }
    return o.toString();
  }

  /**
   * It decodes the given string
   * 
   * @param encodedURI
   * @return
   */
  public static String decodeURIComponent(final String encodedURI) {
    char actualChar;

    StringBuffer buffer = new StringBuffer();

    int bytePattern, sumb = 0;

    for (int i = 0, more = -1; i < encodedURI.length(); i++) {
      actualChar = encodedURI.charAt(i);

      switch (actualChar) {
      case '%': {
        actualChar = encodedURI.charAt(++i);
        int hb = (Character.isDigit(actualChar) ? actualChar - '0'
            : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
        actualChar = encodedURI.charAt(++i);
        int lb = (Character.isDigit(actualChar) ? actualChar - '0'
            : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
        bytePattern = (hb << 4) | lb;
        break;
      }
      case '+': {
        bytePattern = ' ';
        break;
      }
      default: {
        bytePattern = actualChar;
      }
      }

      if ((bytePattern & 0xc0) == 0x80) { // 10xxxxxx
        sumb = (sumb << 6) | (bytePattern & 0x3f);
        if (--more == 0) {
          buffer.append((char) sumb);
        }
      } else if ((bytePattern & 0x80) == 0x00) { // 0xxxxxxx
        buffer.append((char) bytePattern);
      } else if ((bytePattern & 0xe0) == 0xc0) { // 110xxxxx
        sumb = bytePattern & 0x1f;
        more = 1;
      } else if ((bytePattern & 0xf0) == 0xe0) { // 1110xxxx
        sumb = bytePattern & 0x0f;
        more = 2;
      } else if ((bytePattern & 0xf8) == 0xf0) { // 11110xxx
        sumb = bytePattern & 0x07;
        more = 3;
      } else if ((bytePattern & 0xfc) == 0xf8) { // 111110xx
        sumb = bytePattern & 0x03;
        more = 4;
      } else { // 1111110x
        sumb = bytePattern & 0x01;
        more = 5;
      }
    }
    return buffer.toString();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.