2 * Copyright (C) 2010 Prasanta Paul, http://prasanta-paul.blogspot.com
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package com.pras.conn;
\r
19 import java.io.BufferedReader;
\r
20 import java.io.InputStream;
\r
21 import java.io.InputStreamReader;
\r
22 import java.io.OutputStream;
\r
23 import java.net.HttpURLConnection;
\r
24 import java.net.URL;
\r
25 import java.net.URLEncoder;
\r
26 import java.util.HashMap;
\r
27 import java.util.Iterator;
\r
29 import com.pras.Log;
\r
32 * HTTP Connection handler, supports GET, POST, PUT and DELETE.
\r
34 * @author Prasanta Paul
\r
36 public class HttpConHandler {
\r
41 public static final String AUTHORIZATION_HTTP_HEADER = "Authorization";
\r
42 public static final String GDATA_VERSION_HTTP_HEADER = "GData-Version";
\r
43 public static final String CONTENT_LENGTH_HTTP_HEADER = "Content-Length";
\r
44 public static final String CONTENT_TYPE_HTTP_HEADER = "Content-Type";
\r
46 public static final int HTTP_GET = 0xA1;
\r
47 public static final int HTTP_POST = 0xA2;
\r
48 public static final int HTTP_DELETE = 0xA3;
\r
49 public static final int HTTP_PUT = 0xA4;
\r
52 * @param urlStr HTTP URL
\r
53 * @param type Type of Connection (POST, GET, PUT or DELETE)
\r
54 * @param httpHeaders HTTP headers
\r
55 * @param postData Data to be sent as a part of POST/PUT request
\r
57 * @return ATOM XML feed and Response/Error message
\r
59 public Response doConnect(String urlStr, int type, HashMap<String, String> httpHeaders, String postData) {
\r
62 HttpURLConnection con = null;
\r
63 Response response = new Response();
\r
64 String TAG = "HttpConHandler";
\r
69 * User SHOULD provide URL Encoded Parms
\r
71 Log.p(TAG, "URL="+ urlStr);
\r
72 // TODO: Remove proxy
\r
73 // Somehow Eclipse is not detecting Proxy
\r
75 System.getProperties().put("http.proxyHost", "168.219.61.250");
\r
76 System.getProperties().put("http.proxyPort", "8080");
\r
78 System.getProperties().put("https.proxyHost", "168.219.61.252");
\r
79 System.getProperties().put("https.proxyPort", "8080");
\r
80 URL url = new URL(urlStr);
\r
81 con = (HttpURLConnection) url.openConnection();
\r
83 //con.setInstanceFollowRedirects(false);
\r
85 OutputStream out = null;
\r
89 * All subsequent request to Google Spreadsheet/Data API
\r
90 * should include following 2 Headers
\r
92 //con.setRequestProperty("Authorization", "GoogleLogin auth="+ authToken);
\r
93 //con.setRequestProperty("GData-Version", "3.0");
\r
95 if(httpHeaders != null){
\r
96 //System.out.println("Number of HTTP Headers: "+ httpHeaders.size());
\r
97 Iterator<String> keys = httpHeaders.keySet().iterator();
\r
98 while(keys.hasNext()){
\r
99 String k = keys.next();
\r
100 con.setRequestProperty(k, httpHeaders.get(k));
\r
104 if(type == HTTP_POST){
\r
105 con.setDoOutput(true);
\r
106 out = con.getOutputStream();
\r
107 out.write(postData.getBytes());
\r
110 else if(type == HTTP_GET){
\r
111 con.setDoInput(true);
\r
113 else if(type == HTTP_DELETE){
\r
114 con.setRequestMethod("DELETE");
\r
117 else if(type == HTTP_PUT){
\r
118 con.setRequestMethod("PUT");
\r
120 con.setDoOutput(true);
\r
121 out = con.getOutputStream();
\r
122 out.write(postData.getBytes());
\r
126 // Read Response Code
\r
127 response.setResponseCode(""+ con.getResponseCode());
\r
128 response.setResponseMessage(con.getResponseMessage());
\r
130 // Read InputStream
\r
131 BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
\r
132 StringBuffer strBuf = new StringBuffer();
\r
134 while((line = reader.readLine()) != null)
\r
135 strBuf.append(line);
\r
142 res = strBuf.toString();
\r
144 response.setOutput(res);
\r
146 Log.p(TAG, "Response from Google Server: \n"+ res);
\r
148 }catch(Exception ex){
\r
150 Log.p(TAG, "Error in connection: "+ ex.toString());
\r
152 response.setError(true);
\r
155 response.setException(ex);
\r
160 InputStream error_in = con.getErrorStream();
\r
162 if(error_in == null)
\r
165 // Read the error stream
\r
166 BufferedReader reader = new BufferedReader(new InputStreamReader(error_in));
\r
170 StringBuffer errStrBuf = new StringBuffer();
\r
174 while((line = reader.readLine()) != null)
\r
175 errStrBuf.append(line);
\r
177 // Set Error Stream Message
\r
178 response.setErrorStreamMsg(errStrBuf.toString());
\r
182 // Display error on logging console
\r
183 response.printErrorLog();
\r
185 }catch(Exception e){
\r
186 Log.p(TAG, "Error in reading Stream: "+ e.getMessage());
\r
193 * Encode URL parameters in UTF-8 format
\r
195 * @param str String to be URL encoded
\r
199 public static String encode(String str){
\r
201 str = URLEncoder.encode(str, "UTF-8");
\r
202 }catch(Exception ex){ex.printStackTrace();}
\r