KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > util > HttpUtil


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Apr 20, 2005
14  * @author James Dixon
15  *
16  */

17
18 package org.pentaho.util;
19
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.net.HttpURLConnection JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29
30 import org.apache.commons.httpclient.HttpClient;
31 import org.apache.commons.httpclient.HttpConnectionManager;
32 import org.apache.commons.httpclient.SimpleHttpConnectionManager;
33 import org.apache.commons.httpclient.methods.GetMethod;
34 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
35 import org.pentaho.messages.Messages;
36 import org.pentaho.util.logging.Logger;
37
38 import java.util.Map JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.StringTokenizer JavaDoc;
41
42 public class HttpUtil {
43
44     public static HttpClient getClient( ) {
45         
46         int connectionTimeout = 3000;
47         int pageTimeout = 7000;
48         HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
49         HttpConnectionManagerParams connectionParams = connectionManager.getParams();
50         connectionParams.setConnectionTimeout( connectionTimeout );
51         connectionParams.setSoTimeout( pageTimeout );
52
53         HttpClient httpClient = null;
54         if( connectionManager != null ) {
55             httpClient = new HttpClient( connectionManager );
56         }
57         return httpClient;
58
59     }
60     
61     public static boolean getURLContent( String JavaDoc url, StringBuffer JavaDoc content ) throws MalformedURLException JavaDoc, IOException JavaDoc {
62
63         HttpClient httpClient = getClient( );
64
65         try {
66
67             GetMethod call = new GetMethod( url );
68             
69             int status = httpClient.executeMethod( call );
70             if( status == 200 ) {
71                 InputStream JavaDoc response = call.getResponseBodyAsStream();
72                 try {
73                         byte buffer[] = new byte[2048];
74                         int size = response.read( buffer );
75                         while( size > 0 ) {
76                             for( int idx=0; idx<size; idx++ ) {
77                                 content.append( (char) buffer[idx] );
78                             }
79                             size = response.read( buffer );
80                         }
81                 } catch (Exception JavaDoc e) {
82                     // we can ignore this because the content comparison will fail
83
}
84             }
85         } catch (Throwable JavaDoc e) {
86             StringWriter JavaDoc writer = new StringWriter JavaDoc();
87             PrintWriter JavaDoc writer2 = new PrintWriter JavaDoc( writer );
88             e.printStackTrace( writer2 );
89             content.append( writer.getBuffer() );
90             return false;
91         }
92         return true;
93
94     }
95     public static void getURLContent_old(String JavaDoc uri, StringBuffer JavaDoc content) throws MalformedURLException JavaDoc, IOException JavaDoc {
96
97         URL JavaDoc url = new URL JavaDoc(uri);
98         HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc) url.openConnection();
99         connection.connect();
100         InputStream JavaDoc in = connection.getInputStream();
101         byte buffer[] = new byte[300];
102         int n = buffer.length;
103         while (n > 0) {
104             n = in.read(buffer);
105             for (int i = 0; i < n; i++) {
106                 content.append((char) buffer[i]);
107             }
108         }
109         n = in.read(buffer);
110     }
111
112     public static String JavaDoc getURLContent(String JavaDoc uri) {
113
114         try {
115                 StringBuffer JavaDoc content = new StringBuffer JavaDoc();
116                 getURLContent( uri, content );
117             return content.toString();
118         } catch (Exception JavaDoc e) {
119             // TODO: handle this error
120
Logger.error("org.pentaho.util.HttpUtil", Messages.getErrorString("HttpUtil.ERROR_0001_URL_ERROR", e.getMessage()), e); //$NON-NLS-1$ //$NON-NLS-2$
121
return null;
122         }
123     }
124
125     public static InputStream JavaDoc getURLInputStream(String JavaDoc uri) {
126
127         try {
128             URL JavaDoc url = new URL JavaDoc(uri);
129             HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc) url.openConnection();
130             connection.connect();
131             InputStream JavaDoc in = connection.getInputStream();
132             return in;
133         } catch (Exception JavaDoc e) {
134             // TODO: handle this error
135
Logger.error("org.pentaho.util.HttpUtil", Messages.getErrorString("HttpUtil.ERROR_0001_URL_ERROR", e.getMessage()), e); //$NON-NLS-1$ //$NON-NLS-2$
136
return null;
137         }
138
139     }
140
141     public static Reader JavaDoc getURLReader(String JavaDoc uri) {
142
143         try {
144             URL JavaDoc url = new URL JavaDoc(uri);
145             HttpURLConnection JavaDoc connection = (HttpURLConnection JavaDoc) url.openConnection();
146             connection.connect();
147             InputStream JavaDoc in = connection.getInputStream();
148             return new InputStreamReader JavaDoc(in);
149         } catch (Exception JavaDoc e) {
150             // TODO: handle this error
151
Logger.error(HttpUtil.class.getName(), Messages.getErrorString("HttpUtil.ERROR_0001_URL_ERROR", e.getMessage()), e); //$NON-NLS-1$
152
return null;
153         }
154
155     }
156
157     //
158
// The code in the next two methods is based on the code in HttpUtils.java
159
// from
160
// javax.servlet.http. HttpUtils is deprecated - so I updated the methods to
161
// be a bit smarter
162
// and use Map instead of Hashtable
163
//
164
public static Map JavaDoc parseQueryString(String JavaDoc s) {
165         String JavaDoc valArray[] = null;
166         if (s == null)
167             throw new IllegalArgumentException JavaDoc();
168         Map JavaDoc rtn = new HashMap JavaDoc();
169         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
170         String JavaDoc key;
171         for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, "&"); st.hasMoreTokens(); rtn.put(key, valArray)) { //$NON-NLS-1$
172
String JavaDoc pair = st.nextToken();
173             int pos = pair.indexOf('=');
174             if (pos == -1) {
175                 throw new IllegalArgumentException JavaDoc();
176             }
177             key = parseName(pair.substring(0, pos), sb);
178             String JavaDoc val = parseName(pair.substring(pos + 1, pair.length()), sb);
179             if (rtn.containsKey(key)) {
180                 String JavaDoc oldVals[] = (String JavaDoc[]) rtn.get(key);
181                 valArray = new String JavaDoc[oldVals.length + 1];
182                 System.arraycopy(oldVals, 0, valArray, 0, oldVals.length);
183                 valArray[oldVals.length] = val;
184             } else {
185                 valArray = new String JavaDoc[1];
186                 valArray[0] = val;
187             }
188         }
189         return rtn;
190     }
191
192     private static String JavaDoc parseName(String JavaDoc s, StringBuffer JavaDoc sb) {
193         sb.setLength(0);
194         char c;
195         for (int i = 0; i < s.length(); i++) {
196             c = s.charAt(i);
197             switch (c) {
198             case 43: { // '+'
199
sb.append(' ');
200                 break;
201             }
202             case 37: { // '%'
203
try {
204                     sb.append((char) Integer.parseInt(s.substring(i + 1, i + 3), 16));
205                     i += 2;
206                     break;
207                 } catch (NumberFormatException JavaDoc numberformatexception) {
208                     throw new IllegalArgumentException JavaDoc();
209                 } catch (StringIndexOutOfBoundsException JavaDoc oob) {
210                     String JavaDoc rest = s.substring(i);
211                     sb.append(rest);
212                     if (rest.length() == 2) {
213                         i++;
214                     }
215                 }
216                 break;
217             }
218             default: {
219                 sb.append(c);
220                 break;
221             }
222             }
223         }
224         return sb.toString();
225     }
226
227 }
228
Popular Tags