1 23 24 package org.infoglue.deliver.util; 25 26 import java.io.BufferedReader ; 27 import java.io.BufferedWriter ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.io.OutputStreamWriter ; 32 import java.io.PrintWriter ; 33 import java.net.URL ; 34 import java.net.URLConnection ; 35 import java.net.URLEncoder ; 36 import java.util.Enumeration ; 37 import java.util.Hashtable ; 38 import java.util.Iterator ; 39 import java.util.Map ; 40 41 import javax.servlet.http.Cookie ; 42 import javax.servlet.http.HttpServletRequest ; 43 44 import org.apache.log4j.Logger; 45 46 47 48 53 54 public class HttpHelper 55 { 56 private final static Logger logger = Logger.getLogger(HttpHelper.class.getName()); 57 58 public String postToUrl(String urlAddress, HttpServletRequest request, boolean includeRequest) throws Exception 59 { 60 if(includeRequest) 61 return postToUrl(urlAddress, requestToHashtable(request), "UTF-8"); 62 else 63 return postToUrl(urlAddress, new Hashtable (), "UTF-8"); 64 } 65 66 public String postToUrl(String urlAddress, HttpServletRequest request, boolean includeRequest, String encoding) throws Exception 67 { 68 if(includeRequest) 69 return postToUrl(urlAddress, requestToHashtable(request), encoding); 70 else 71 return postToUrl(urlAddress, new Hashtable (), encoding); 72 } 73 74 84 85 public String postToUrl(String urlAddress, Hashtable inHash, String encoding) throws Exception 86 { 87 URL url = new URL (urlAddress); 88 URLConnection urlConn = url.openConnection(); 89 urlConn.setAllowUserInteraction(false); 90 urlConn.setDoOutput (true); 91 urlConn.setDoInput (true); 92 urlConn.setUseCaches (false); 93 urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 94 PrintWriter printout = new PrintWriter (urlConn.getOutputStream(), true); 95 String argString = ""; 96 if(inHash != null) 97 { 98 argString = toEncodedString(inHash, encoding); 99 } 100 printout.print(argString); 101 printout.flush(); 102 printout.close (); 103 InputStream inStream = null; 104 inStream = urlConn.getInputStream(); 105 InputStreamReader inStreamReader = new InputStreamReader (inStream); 106 BufferedReader buffer = new BufferedReader (inStreamReader); 107 StringBuffer strbuf = new StringBuffer (); 108 String line; 109 while((line = buffer.readLine()) != null) 110 { 111 strbuf.append(line); 112 } 113 String readData = strbuf.toString(); 114 buffer.close(); 115 return readData; 116 } 117 118 119 129 130 public String postToUrl(String urlAddress, Hashtable inHash, String userName, String password, boolean shouldEncode) throws Exception 131 { 132 String encodedPassword = HTUU.encode(userName + ":" + password); 133 134 URL url = new URL (urlAddress); 135 URLConnection urlConn = url.openConnection(); 136 urlConn.setAllowUserInteraction(false); 137 urlConn.setDoOutput (true); 138 urlConn.setDoInput (true); 139 urlConn.setUseCaches (false); 140 urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 141 urlConn.setRequestProperty("Authorization", "Basic " + encodedPassword); 142 PrintWriter printout = new PrintWriter (urlConn.getOutputStream(), true); 143 String argString = ""; 144 if(inHash != null) 145 { 146 if (shouldEncode) 147 argString = toEncodedString(inHash, "UTF-8"); 148 else 149 argString = toString(inHash); 150 } 151 printout.print(argString); 152 printout.flush(); 153 printout.close (); 154 InputStream inStream = null; 155 inStream = urlConn.getInputStream(); 156 InputStreamReader inStreamReader = new InputStreamReader (inStream); 157 BufferedReader buffer = new BufferedReader (inStreamReader); 158 StringBuffer strbuf = new StringBuffer (); 159 String line; 160 while((line = buffer.readLine()) != null) 161 { 162 strbuf.append(line); 163 } 164 String readData = strbuf.toString(); 165 buffer.close(); 166 return readData; 167 } 168 169 170 public String getUrlContent(String urlAddress, HttpServletRequest request, boolean includeRequest) throws Exception 171 { 172 if(includeRequest) 173 return getUrlContent(urlAddress, requestToHashtable(request)); 174 else 175 return getUrlContent(urlAddress); 176 } 177 178 public String getUrlContent(String urlAddress, HttpServletRequest request, boolean includeRequest, String encoding) throws Exception 179 { 180 if(includeRequest) 181 return getUrlContent(urlAddress, requestToHashtable(request), encoding); 182 else 183 return getUrlContent(urlAddress, encoding); 184 } 185 186 public String getUrlContent(String urlAddress, Hashtable inHash) throws Exception 187 { 188 String argString = ""; 189 if(inHash != null) 190 { 191 if(urlAddress.indexOf("?") > -1) 192 argString = "&" + toEncodedString(inHash, "UTF-8"); 193 else 194 argString = "?" + toEncodedString(inHash, "UTF-8"); 195 } 196 197 logger.info("Getting content from url: " + urlAddress + argString); 198 199 URL url = new URL (urlAddress + argString); 200 URLConnection connection = url.openConnection(); 201 connection.setUseCaches(false); 202 InputStream inStream = null; 203 inStream = connection.getInputStream(); 204 InputStreamReader inStreamReader = new InputStreamReader (inStream, "ISO-8859-1"); 205 BufferedReader buffer = new BufferedReader (inStreamReader); 206 StringBuffer strbuf = new StringBuffer (); 207 String line; 208 while((line = buffer.readLine()) != null) 209 { 210 strbuf.append(line); 211 } 212 String readData = strbuf.toString(); 213 buffer.close(); 214 215 return readData; 216 } 217 218 219 public String getUrlContent(String urlAddress, Hashtable inHash, String encoding) throws Exception 220 { 221 String argString = ""; 222 if(inHash != null) 223 { 224 if(urlAddress.indexOf("?") > -1) 225 argString = "&" + toEncodedString(inHash, encoding); 226 else 227 argString = "?" + toEncodedString(inHash, encoding); 228 } 229 230 logger.info("Getting content from url: " + urlAddress + argString); 231 232 URL url = new URL (urlAddress + argString); 233 URLConnection connection = url.openConnection(); 234 connection.setUseCaches(false); 235 InputStream inStream = null; 236 inStream = connection.getInputStream(); 237 InputStreamReader inStreamReader = new InputStreamReader (inStream, encoding); 238 BufferedReader buffer = new BufferedReader (inStreamReader); 239 StringBuffer strbuf = new StringBuffer (); 240 String line; 241 while((line = buffer.readLine()) != null) 242 { 243 strbuf.append(line); 244 } 245 String readData = strbuf.toString(); 246 buffer.close(); 247 248 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 249 BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (baos, "UTF-8")); 250 writer.write(readData); 251 252 baos.flush(); 253 baos.close(); 254 writer.flush(); 255 writer.close(); 256 257 readData = new String (baos.toString(encoding)); 258 259 return readData; 260 } 261 262 263 public String getUrlContent(String urlAddress) throws Exception 264 { 265 URL url = new URL (urlAddress); 266 URLConnection connection = url.openConnection(); 267 connection.setUseCaches(false); 268 269 InputStream inStream = null; 270 inStream = connection.getInputStream(); 271 InputStreamReader inStreamReader = new InputStreamReader (inStream); 272 BufferedReader buffer = new BufferedReader (inStreamReader); 273 StringBuffer strbuf = new StringBuffer (); 274 String line; 275 while((line = buffer.readLine()) != null) 276 { 277 strbuf.append(line); 278 } 279 String readData = strbuf.toString(); 280 buffer.close(); 281 return readData; 282 } 283 284 public String getUrlContent(String urlAddress, Map requestParameters) throws Exception 285 { 286 URL url = new URL (urlAddress); 287 URLConnection connection = url.openConnection(); 288 connection.setUseCaches(false); 289 290 Iterator mapIterator = requestParameters.keySet().iterator(); 291 while(mapIterator.hasNext()) 292 { 293 String key = (String )mapIterator.next(); 294 String value = (String )requestParameters.get(key); 295 connection.setRequestProperty(key, value); 296 } 297 298 InputStream inStream = null; 299 inStream = connection.getInputStream(); 300 InputStreamReader inStreamReader = new InputStreamReader (inStream); 301 BufferedReader buffer = new BufferedReader (inStreamReader); 302 StringBuffer strbuf = new StringBuffer (); 303 String line; 304 while((line = buffer.readLine()) != null) 305 { 306 strbuf.append(line); 307 } 308 String readData = strbuf.toString(); 309 buffer.close(); 310 return readData; 311 } 312 313 public String getUrlContent(String urlAddress, String encoding) throws Exception 314 { 315 URL url = new URL (urlAddress); 316 URLConnection connection = url.openConnection(); 317 connection.setUseCaches(false); 318 InputStream inStream = null; 319 inStream = connection.getInputStream(); 320 InputStreamReader inStreamReader = new InputStreamReader (inStream, encoding); 321 BufferedReader buffer = new BufferedReader (inStreamReader); 322 StringBuffer strbuf = new StringBuffer (); 323 String line; 324 while((line = buffer.readLine()) != null) 325 { 326 strbuf.append(line); 327 } 328 String readData = strbuf.toString(); 329 buffer.close(); 330 331 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 332 BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (baos, "UTF-8")); 333 writer.write(readData); 334 335 baos.flush(); 336 baos.close(); 337 writer.flush(); 338 writer.close(); 339 340 readData = new String (baos.toString(encoding)); 341 342 return readData; 343 } 344 345 355 356 public String getUrlContent(String urlAddress, Hashtable inHash, String userName, String password, boolean shouldEncode) throws Exception 357 { 358 String encodedPassword = HTUU.encode(userName + ":" + password); 359 360 String argString = ""; 361 if(inHash != null) 362 { 363 if (shouldEncode) 364 argString = "?" + toEncodedString(inHash, "UTF-8"); 365 else 366 argString = "?" + toString(inHash); 367 } 368 URL url = new URL (urlAddress + argString); 369 URLConnection connection = url.openConnection(); 370 connection.setUseCaches(false); 371 connection.setRequestProperty("Authorization", "Basic " + encodedPassword); 372 InputStream inStream = null; 373 inStream = connection.getInputStream(); 374 InputStreamReader inStreamReader = new InputStreamReader (inStream); 375 BufferedReader buffer = new BufferedReader (inStreamReader); 376 StringBuffer strbuf = new StringBuffer (); 377 String line; 378 while((line = buffer.readLine()) != null) 379 { 380 strbuf.append(line); 381 } 382 String readData = strbuf.toString(); 383 buffer.close(); 384 return readData; 385 } 386 387 388 389 public String getUrlContent(String urlAddress, String data, String userName, String password) throws Exception 390 { 391 String encodedPassword = HTUU.encode(userName + ":" + password); 392 393 String argString = "?" + data; 394 395 URL url = new URL (urlAddress + argString); 396 URLConnection connection = url.openConnection(); 397 connection.setUseCaches(false); 398 connection.setRequestProperty("Authorization", "Basic " + encodedPassword); 399 InputStream inStream = null; 400 inStream = connection.getInputStream(); 401 InputStreamReader inStreamReader = new InputStreamReader (inStream); 402 BufferedReader buffer = new BufferedReader (inStreamReader); 403 StringBuffer strbuf = new StringBuffer (); 404 String line; 405 while((line = buffer.readLine()) != null) 406 { 407 strbuf.append(line); 408 } 409 String readData = strbuf.toString(); 410 buffer.close(); 411 return readData; 412 } 413 414 415 416 427 428 public InputStream getURLStream(String urlAddress, Hashtable inHash) throws Exception 429 { 430 String argString = ""; 431 if(inHash != null) 432 { 433 argString = "?" + toEncodedString(inHash, "UTF-8"); 434 } 435 URL url = new URL (urlAddress + argString); 436 URLConnection connection = url.openConnection(); 437 connection.setUseCaches(false); 438 InputStream inStream = null; 439 inStream = connection.getInputStream(); 440 return inStream; 441 } 442 443 448 449 public String getCookie(HttpServletRequest request, String cookieName) 450 { 451 if(request != null) 452 { 453 Cookie [] cookies = request.getCookies(); 454 if(cookies != null) 455 { 456 for(int i=0; i<cookies.length; i++) 457 { 458 Cookie cookie = cookies[i]; 459 if(cookie.getName().equals(cookieName)) 460 return cookie.getValue(); 461 } 462 } 463 } 464 465 return null; 466 } 467 468 469 472 473 public Hashtable requestToHashtable(HttpServletRequest request) 474 { 475 Hashtable parameters = new Hashtable (); 476 477 if(request != null) 478 { 479 for (Enumeration e = request.getParameterNames(); e.hasMoreElements() ;) 480 { 481 String name = (String )e.nextElement(); 482 String value = (String )request.getParameter(name); 483 parameters.put(name, value); 484 } 485 } 486 487 return parameters; 488 489 } 490 491 492 498 499 private String toEncodedString(Hashtable inHash, String encoding) throws Exception 500 { 501 StringBuffer buffer = new StringBuffer (); 502 Enumeration names = inHash.keys(); 503 while(names.hasMoreElements()) 504 { 505 String name = names.nextElement().toString(); 506 String value = inHash.get(name).toString(); 507 buffer.append(URLEncoder.encode(name, encoding) + "=" + URLEncoder.encode(value, encoding)); 508 if(names.hasMoreElements()) 509 { 510 buffer.append("&"); 511 } 512 } 513 return buffer.toString(); 514 } 515 516 private String toString(Hashtable inHash) 517 { 518 StringBuffer buffer = new StringBuffer (); 519 Enumeration names = inHash.keys(); 520 while(names.hasMoreElements()) 521 { 522 String name = names.nextElement().toString(); 523 String value = inHash.get(name).toString(); 524 buffer.append(name + "=" + value); 525 if(names.hasMoreElements()) 526 { 527 buffer.append("&"); 528 } 529 } 530 return buffer.toString(); 531 } 532 } 533 | Popular Tags |