| 1 17 18 package org.pentaho.util; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.PrintWriter ; 24 import java.io.Reader ; 25 import java.io.StringWriter ; 26 import java.net.HttpURLConnection ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 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 ; 39 import java.util.HashMap ; 40 import java.util.StringTokenizer ; 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 url, StringBuffer content ) throws MalformedURLException , IOException { 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 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 e) { 82 } 84 } 85 } catch (Throwable e) { 86 StringWriter writer = new StringWriter (); 87 PrintWriter writer2 = new PrintWriter ( 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 uri, StringBuffer content) throws MalformedURLException , IOException { 96 97 URL url = new URL (uri); 98 HttpURLConnection connection = (HttpURLConnection ) url.openConnection(); 99 connection.connect(); 100 InputStream 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 getURLContent(String uri) { 113 114 try { 115 StringBuffer content = new StringBuffer (); 116 getURLContent( uri, content ); 117 return content.toString(); 118 } catch (Exception e) { 119 Logger.error("org.pentaho.util.HttpUtil", Messages.getErrorString("HttpUtil.ERROR_0001_URL_ERROR", e.getMessage()), e); return null; 122 } 123 } 124 125 public static InputStream getURLInputStream(String uri) { 126 127 try { 128 URL url = new URL (uri); 129 HttpURLConnection connection = (HttpURLConnection ) url.openConnection(); 130 connection.connect(); 131 InputStream in = connection.getInputStream(); 132 return in; 133 } catch (Exception e) { 134 Logger.error("org.pentaho.util.HttpUtil", Messages.getErrorString("HttpUtil.ERROR_0001_URL_ERROR", e.getMessage()), e); return null; 137 } 138 139 } 140 141 public static Reader getURLReader(String uri) { 142 143 try { 144 URL url = new URL (uri); 145 HttpURLConnection connection = (HttpURLConnection ) url.openConnection(); 146 connection.connect(); 147 InputStream in = connection.getInputStream(); 148 return new InputStreamReader (in); 149 } catch (Exception e) { 150 Logger.error(HttpUtil.class.getName(), Messages.getErrorString("HttpUtil.ERROR_0001_URL_ERROR", e.getMessage()), e); return null; 153 } 154 155 } 156 157 public static Map parseQueryString(String s) { 165 String valArray[] = null; 166 if (s == null) 167 throw new IllegalArgumentException (); 168 Map rtn = new HashMap (); 169 StringBuffer sb = new StringBuffer (); 170 String key; 171 for (StringTokenizer st = new StringTokenizer (s, "&"); st.hasMoreTokens(); rtn.put(key, valArray)) { String pair = st.nextToken(); 173 int pos = pair.indexOf('='); 174 if (pos == -1) { 175 throw new IllegalArgumentException (); 176 } 177 key = parseName(pair.substring(0, pos), sb); 178 String val = parseName(pair.substring(pos + 1, pair.length()), sb); 179 if (rtn.containsKey(key)) { 180 String oldVals[] = (String []) rtn.get(key); 181 valArray = new String [oldVals.length + 1]; 182 System.arraycopy(oldVals, 0, valArray, 0, oldVals.length); 183 valArray[oldVals.length] = val; 184 } else { 185 valArray = new String [1]; 186 valArray[0] = val; 187 } 188 } 189 return rtn; 190 } 191 192 private static String parseName(String s, StringBuffer 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: { sb.append(' '); 200 break; 201 } 202 case 37: { try { 204 sb.append((char) Integer.parseInt(s.substring(i + 1, i + 3), 16)); 205 i += 2; 206 break; 207 } catch (NumberFormatException numberformatexception) { 208 throw new IllegalArgumentException (); 209 } catch (StringIndexOutOfBoundsException oob) { 210 String 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 |