001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Apr 20, 2005
014: * @author James Dixon
015: *
016: */
017:
018: package org.pentaho.util;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.InputStreamReader;
023: import java.io.PrintWriter;
024: import java.io.Reader;
025: import java.io.StringWriter;
026: import java.net.HttpURLConnection;
027: import java.net.MalformedURLException;
028: import java.net.URL;
029:
030: import org.apache.commons.httpclient.HttpClient;
031: import org.apache.commons.httpclient.HttpConnectionManager;
032: import org.apache.commons.httpclient.SimpleHttpConnectionManager;
033: import org.apache.commons.httpclient.methods.GetMethod;
034: import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
035: import org.pentaho.messages.Messages;
036: import org.pentaho.util.logging.Logger;
037:
038: import java.util.Map;
039: import java.util.HashMap;
040: import java.util.StringTokenizer;
041:
042: public class HttpUtil {
043:
044: public static HttpClient getClient() {
045:
046: int connectionTimeout = 3000;
047: int pageTimeout = 7000;
048: HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
049: HttpConnectionManagerParams connectionParams = connectionManager
050: .getParams();
051: connectionParams.setConnectionTimeout(connectionTimeout);
052: connectionParams.setSoTimeout(pageTimeout);
053:
054: HttpClient httpClient = null;
055: if (connectionManager != null) {
056: httpClient = new HttpClient(connectionManager);
057: }
058: return httpClient;
059:
060: }
061:
062: public static boolean getURLContent(String url, StringBuffer content)
063: throws MalformedURLException, IOException {
064:
065: HttpClient httpClient = getClient();
066:
067: try {
068:
069: GetMethod call = new GetMethod(url);
070:
071: int status = httpClient.executeMethod(call);
072: if (status == 200) {
073: InputStream response = call.getResponseBodyAsStream();
074: try {
075: byte buffer[] = new byte[2048];
076: int size = response.read(buffer);
077: while (size > 0) {
078: for (int idx = 0; idx < size; idx++) {
079: content.append((char) buffer[idx]);
080: }
081: size = response.read(buffer);
082: }
083: } catch (Exception e) {
084: // we can ignore this because the content comparison will fail
085: }
086: }
087: } catch (Throwable e) {
088: StringWriter writer = new StringWriter();
089: PrintWriter writer2 = new PrintWriter(writer);
090: e.printStackTrace(writer2);
091: content.append(writer.getBuffer());
092: return false;
093: }
094: return true;
095:
096: }
097:
098: public static void getURLContent_old(String uri,
099: StringBuffer content) throws MalformedURLException,
100: IOException {
101:
102: URL url = new URL(uri);
103: HttpURLConnection connection = (HttpURLConnection) url
104: .openConnection();
105: connection.connect();
106: InputStream in = connection.getInputStream();
107: byte buffer[] = new byte[300];
108: int n = buffer.length;
109: while (n > 0) {
110: n = in.read(buffer);
111: for (int i = 0; i < n; i++) {
112: content.append((char) buffer[i]);
113: }
114: }
115: n = in.read(buffer);
116: }
117:
118: public static String getURLContent(String uri) {
119:
120: try {
121: StringBuffer content = new StringBuffer();
122: getURLContent(uri, content);
123: return content.toString();
124: } catch (Exception e) {
125: // TODO: handle this error
126: Logger
127: .error(
128: "org.pentaho.util.HttpUtil", Messages.getErrorString("HttpUtil.ERROR_0001_URL_ERROR", e.getMessage()), e); //$NON-NLS-1$ //$NON-NLS-2$
129: return null;
130: }
131: }
132:
133: public static InputStream getURLInputStream(String uri) {
134:
135: try {
136: URL url = new URL(uri);
137: HttpURLConnection connection = (HttpURLConnection) url
138: .openConnection();
139: connection.connect();
140: InputStream in = connection.getInputStream();
141: return in;
142: } catch (Exception e) {
143: // TODO: handle this error
144: Logger
145: .error(
146: "org.pentaho.util.HttpUtil", Messages.getErrorString("HttpUtil.ERROR_0001_URL_ERROR", e.getMessage()), e); //$NON-NLS-1$ //$NON-NLS-2$
147: return null;
148: }
149:
150: }
151:
152: public static Reader getURLReader(String uri) {
153:
154: try {
155: URL url = new URL(uri);
156: HttpURLConnection connection = (HttpURLConnection) url
157: .openConnection();
158: connection.connect();
159: InputStream in = connection.getInputStream();
160: return new InputStreamReader(in);
161: } catch (Exception e) {
162: // TODO: handle this error
163: Logger
164: .error(
165: HttpUtil.class.getName(),
166: Messages
167: .getErrorString(
168: "HttpUtil.ERROR_0001_URL_ERROR", e.getMessage()), e); //$NON-NLS-1$
169: return null;
170: }
171:
172: }
173:
174: //
175: // The code in the next two methods is based on the code in HttpUtils.java
176: // from
177: // javax.servlet.http. HttpUtils is deprecated - so I updated the methods to
178: // be a bit smarter
179: // and use Map instead of Hashtable
180: //
181: public static Map parseQueryString(String s) {
182: String valArray[] = null;
183: if (s == null)
184: throw new IllegalArgumentException();
185: Map rtn = new HashMap();
186: StringBuffer sb = new StringBuffer();
187: String key;
188: for (StringTokenizer st = new StringTokenizer(s, "&"); st.hasMoreTokens(); rtn.put(key, valArray)) { //$NON-NLS-1$
189: String pair = st.nextToken();
190: int pos = pair.indexOf('=');
191: if (pos == -1) {
192: throw new IllegalArgumentException();
193: }
194: key = parseName(pair.substring(0, pos), sb);
195: String val = parseName(pair.substring(pos + 1, pair
196: .length()), sb);
197: if (rtn.containsKey(key)) {
198: String oldVals[] = (String[]) rtn.get(key);
199: valArray = new String[oldVals.length + 1];
200: System.arraycopy(oldVals, 0, valArray, 0,
201: oldVals.length);
202: valArray[oldVals.length] = val;
203: } else {
204: valArray = new String[1];
205: valArray[0] = val;
206: }
207: }
208: return rtn;
209: }
210:
211: private static String parseName(String s, StringBuffer sb) {
212: sb.setLength(0);
213: char c;
214: for (int i = 0; i < s.length(); i++) {
215: c = s.charAt(i);
216: switch (c) {
217: case 43: { // '+'
218: sb.append(' ');
219: break;
220: }
221: case 37: { // '%'
222: try {
223: sb.append((char) Integer.parseInt(s.substring(
224: i + 1, i + 3), 16));
225: i += 2;
226: break;
227: } catch (NumberFormatException numberformatexception) {
228: throw new IllegalArgumentException();
229: } catch (StringIndexOutOfBoundsException oob) {
230: String rest = s.substring(i);
231: sb.append(rest);
232: if (rest.length() == 2) {
233: i++;
234: }
235: }
236: break;
237: }
238: default: {
239: sb.append(c);
240: break;
241: }
242: }
243: }
244: return sb.toString();
245: }
246:
247: }
|