1 23 24 package org.infoglue.deliver.util; 25 26 27 import java.io.BufferedReader ; 28 import java.io.BufferedWriter ; 29 import java.io.ByteArrayOutputStream ; 30 import java.io.InputStream ; 31 import java.io.InputStreamReader ; 32 import java.io.OutputStreamWriter ; 33 import java.io.PrintWriter ; 34 import java.net.URL ; 35 import java.net.URLConnection ; 36 import java.net.URLEncoder ; 37 import java.util.Enumeration ; 38 import java.util.Hashtable ; 39 40 import javax.servlet.http.HttpServletRequest ; 41 42 import org.apache.log4j.Logger; 43 44 45 46 54 55 56 public class HttpUtilities 57 { 58 private final static Logger logger = Logger.getLogger(HttpUtilities.class.getName()); 59 60 63 public HttpUtilities() 64 { 65 } 66 67 77 78 public static String postToUrl(String urlAddress, Hashtable inHash) throws Exception 79 { 80 URL url = new URL (urlAddress); 81 URLConnection urlConn = url.openConnection(); 82 urlConn.setAllowUserInteraction(false); 83 urlConn.setDoOutput (true); 84 urlConn.setDoInput (true); 85 urlConn.setUseCaches (false); 86 urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 87 PrintWriter printout = new PrintWriter (urlConn.getOutputStream(), true); 88 String argString = ""; 89 if(inHash != null) 90 { 91 argString = toEncodedString(inHash); 92 } 93 printout.print(argString); 94 printout.flush(); 95 printout.close (); 96 InputStream inStream = null; 97 inStream = urlConn.getInputStream(); 98 InputStreamReader inStreamReader = new InputStreamReader (inStream); 99 BufferedReader buffer = new BufferedReader (inStreamReader); 100 StringBuffer strbuf = new StringBuffer (); 101 String line; 102 while((line = buffer.readLine()) != null) 103 { 104 strbuf.append(line); 105 } 106 String readData = strbuf.toString(); 107 buffer.close(); 108 return readData; 109 } 110 111 112 122 123 public static String postToUrl(String urlAddress, Hashtable inHash, String userName, String password, boolean shouldEncode) throws Exception 124 { 125 String encodedPassword = HTUU.encode(userName + ":" + password); 126 127 URL url = new URL (urlAddress); 128 URLConnection urlConn = url.openConnection(); 129 urlConn.setAllowUserInteraction(false); 130 urlConn.setDoOutput (true); 131 urlConn.setDoInput (true); 132 urlConn.setUseCaches (false); 133 urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 134 urlConn.setRequestProperty("Authorization", "Basic " + encodedPassword); 135 PrintWriter printout = new PrintWriter (urlConn.getOutputStream(), true); 136 String argString = ""; 137 if(inHash != null) 138 { 139 if (shouldEncode) 140 argString = toEncodedString(inHash); 141 else 142 argString = toString(inHash); 143 } 144 printout.print(argString); 145 printout.flush(); 146 printout.close (); 147 InputStream inStream = null; 148 inStream = urlConn.getInputStream(); 149 InputStreamReader inStreamReader = new InputStreamReader (inStream); 150 BufferedReader buffer = new BufferedReader (inStreamReader); 151 StringBuffer strbuf = new StringBuffer (); 152 String line; 153 while((line = buffer.readLine()) != null) 154 { 155 strbuf.append(line); 156 } 157 String readData = strbuf.toString(); 158 buffer.close(); 159 return readData; 160 } 161 162 163 public static String getUrlContent(String urlAddress, HttpServletRequest request, boolean includeRequest) throws Exception 164 { 165 if(includeRequest) 166 return getUrlContent(urlAddress, requestToHashtable(request)); 167 else 168 return getUrlContent(urlAddress); 169 } 170 171 public static String getUrlContent(String urlAddress, HttpServletRequest request, boolean includeRequest, String encoding) throws Exception 172 { 173 if(includeRequest) 174 return getUrlContent(urlAddress, requestToHashtable(request), encoding); 175 else 176 return getUrlContent(urlAddress, encoding); 177 } 178 179 public static String getUrlContent(String urlAddress, Hashtable inHash) throws Exception 180 { 181 String argString = ""; 182 if(inHash != null) 183 { 184 if(urlAddress.indexOf("?") > -1) 185 argString = "&" + toEncodedString(inHash); 186 else 187 argString = "?" + toEncodedString(inHash); 188 } 189 190 logger.info("Getting content from url: " + urlAddress + argString); 191 192 URL url = new URL (urlAddress + argString); 193 URLConnection connection = url.openConnection(); 194 connection.setUseCaches(false); 195 InputStream inStream = null; 196 inStream = connection.getInputStream(); 197 InputStreamReader inStreamReader = new InputStreamReader (inStream, "ISO-8859-1"); 198 BufferedReader buffer = new BufferedReader (inStreamReader); 199 StringBuffer strbuf = new StringBuffer (); 200 String line; 201 while((line = buffer.readLine()) != null) 202 { 203 strbuf.append(line); 204 } 205 String readData = strbuf.toString(); 206 buffer.close(); 207 208 return readData; 209 } 210 211 212 public static String getUrlContent(String urlAddress, Hashtable inHash, String encoding) throws Exception 213 { 214 String argString = ""; 215 if(inHash != null) 216 { 217 if(urlAddress.indexOf("?") > -1) 218 argString = "&" + toEncodedString(inHash); 219 else 220 argString = "?" + toEncodedString(inHash); 221 } 222 223 logger.info("Getting content from url: " + urlAddress + argString); 224 225 URL url = new URL (urlAddress + argString); 226 URLConnection connection = url.openConnection(); 227 connection.setUseCaches(false); 228 InputStream inStream = null; 229 inStream = connection.getInputStream(); 230 InputStreamReader inStreamReader = new InputStreamReader (inStream, encoding); 231 BufferedReader buffer = new BufferedReader (inStreamReader); 232 StringBuffer strbuf = new StringBuffer (); 233 String line; 234 while((line = buffer.readLine()) != null) 235 { 236 strbuf.append(line); 237 } 238 String readData = strbuf.toString(); 239 buffer.close(); 240 241 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 242 BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (baos, "UTF-8")); 243 writer.write(readData); 244 245 baos.flush(); 246 baos.close(); 247 writer.flush(); 248 writer.close(); 249 250 readData = new String (baos.toString(encoding)); 251 252 return readData; 253 } 254 255 256 public static String getUrlContent(String urlAddress) throws Exception 257 { 258 URL url = new URL (urlAddress); 259 URLConnection connection = url.openConnection(); 260 connection.setUseCaches(false); 261 InputStream inStream = null; 262 inStream = connection.getInputStream(); 263 InputStreamReader inStreamReader = new InputStreamReader (inStream); 264 BufferedReader buffer = new BufferedReader (inStreamReader); 265 StringBuffer strbuf = new StringBuffer (); 266 String line; 267 while((line = buffer.readLine()) != null) 268 { 269 strbuf.append(line); 270 } 271 String readData = strbuf.toString(); 272 buffer.close(); 273 return readData; 274 } 275 276 277 public static String getUrlContent(String urlAddress, String encoding) throws Exception 278 { 279 URL url = new URL (urlAddress); 280 URLConnection connection = url.openConnection(); 281 connection.setUseCaches(false); 282 InputStream inStream = null; 283 inStream = connection.getInputStream(); 284 InputStreamReader inStreamReader = new InputStreamReader (inStream, encoding); 285 BufferedReader buffer = new BufferedReader (inStreamReader); 286 StringBuffer strbuf = new StringBuffer (); 287 String line; 288 while((line = buffer.readLine()) != null) 289 { 290 strbuf.append(line); 291 } 292 String readData = strbuf.toString(); 293 buffer.close(); 294 295 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 296 BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (baos, "UTF-8")); 297 writer.write(readData); 298 299 baos.flush(); 300 baos.close(); 301 writer.flush(); 302 writer.close(); 303 304 readData = new String (baos.toString(encoding)); 305 306 return readData; 307 } 308 309 319 320 public static String getUrlContent(String urlAddress, Hashtable inHash, String userName, String password, boolean shouldEncode) throws Exception 321 { 322 String encodedPassword = HTUU.encode(userName + ":" + password); 323 324 String argString = ""; 325 if(inHash != null) 326 { 327 if (shouldEncode) 328 argString = "?" + toEncodedString(inHash); 329 else 330 argString = "?" + toString(inHash); 331 } 332 URL url = new URL (urlAddress + argString); 333 URLConnection connection = url.openConnection(); 334 connection.setUseCaches(false); 335 connection.setRequestProperty("Authorization", "Basic " + encodedPassword); 336 InputStream inStream = null; 337 inStream = connection.getInputStream(); 338 InputStreamReader inStreamReader = new InputStreamReader (inStream); 339 BufferedReader buffer = new BufferedReader (inStreamReader); 340 StringBuffer strbuf = new StringBuffer (); 341 String line; 342 while((line = buffer.readLine()) != null) 343 { 344 strbuf.append(line); 345 } 346 String readData = strbuf.toString(); 347 buffer.close(); 348 return readData; 349 } 350 351 352 353 public static String getUrlContent(String urlAddress, String data, String userName, String password) throws Exception 354 { 355 String encodedPassword = HTUU.encode(userName + ":" + password); 356 357 String argString = "?" + data; 358 359 URL url = new URL (urlAddress + argString); 360 URLConnection connection = url.openConnection(); 361 connection.setUseCaches(false); 362 connection.setRequestProperty("Authorization", "Basic " + encodedPassword); 363 InputStream inStream = null; 364 inStream = connection.getInputStream(); 365 InputStreamReader inStreamReader = new InputStreamReader (inStream); 366 BufferedReader buffer = new BufferedReader (inStreamReader); 367 StringBuffer strbuf = new StringBuffer (); 368 String line; 369 while((line = buffer.readLine()) != null) 370 { 371 strbuf.append(line); 372 } 373 String readData = strbuf.toString(); 374 buffer.close(); 375 return readData; 376 } 377 378 379 380 391 392 public static InputStream getURLStream(String urlAddress, Hashtable inHash) throws Exception 393 { 394 String argString = ""; 395 if(inHash != null) 396 { 397 argString = "?" + toEncodedString(inHash); 398 } 399 URL url = new URL (urlAddress + argString); 400 URLConnection connection = url.openConnection(); 401 connection.setUseCaches(false); 402 InputStream inStream = null; 403 inStream = connection.getInputStream(); 404 return inStream; 405 } 406 407 408 409 412 413 public static Hashtable requestToHashtable(HttpServletRequest request) 414 { 415 Hashtable parameters = new Hashtable (); 416 417 if(request != null) 418 { 419 for (Enumeration e = request.getParameterNames(); e.hasMoreElements() ;) 420 { 421 String name = (String )e.nextElement(); 422 String value = (String )request.getParameter(name); 423 parameters.put(name, value); 424 } 425 } 426 427 return parameters; 428 429 } 430 431 432 438 439 private static String toEncodedString(Hashtable inHash) throws Exception 440 { 441 StringBuffer buffer = new StringBuffer (); 442 Enumeration names = inHash.keys(); 443 while(names.hasMoreElements()) 444 { 445 String name = names.nextElement().toString(); 446 String value = inHash.get(name).toString(); 447 buffer.append(URLEncoder.encode(name, "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8")); 448 if(names.hasMoreElements()) 449 { 450 buffer.append("&"); 451 } 452 } 453 return buffer.toString(); 454 } 455 456 private static String toString(Hashtable inHash) 457 { 458 StringBuffer buffer = new StringBuffer (); 459 Enumeration names = inHash.keys(); 460 while(names.hasMoreElements()) 461 { 462 String name = names.nextElement().toString(); 463 String value = inHash.get(name).toString(); 464 buffer.append(name + "=" + value); 465 if(names.hasMoreElements()) 466 { 467 buffer.append("&"); 468 } 469 } 470 return buffer.toString(); 471 } 472 473 } 474 | Popular Tags |