1 /** 2 * EGroupware - Notifications Java Desktop App 3 * 4 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License 5 * @package notifications 6 * @subpackage jdesk 7 * @link http://www.egroupware.org 8 * @author Stefan Werfling <stefan.werfling@hw-softwareentwicklung.de> 9 * @author Maik Hüttner <maik.huettner@hw-softwareentwicklung.de> 10 */ 11 12 package egroupwaretray; 13 14 import java.io.*; 15 import java.net.HttpURLConnection; 16 import java.net.URL; 17 import java.util.List; 18 import java.util.Map; 19 import java.util.logging.Level; 20 import java.util.logging.Logger; 21 import javax.net.ssl.HttpsURLConnection; 22 import javax.net.ssl.SSLContext; 23 24 /** 25 * BaseHttp 26 * 27 * @author Stefan Werfling <stefan.werfling@hw-softwareentwicklung.de> 28 */ 29 public class BaseHttp 30 { 31 private HttpURLConnection _con = null; 32 private boolean _isSSL = false; 33 private boolean _isAjax = false; 34 private String _cookie = ""; 35 private int _sockettimeout = 10000; 36 37 static private BaseHttpsTrustManager _bhtm = null; 38 getTrustManager()39 static public BaseHttpsTrustManager getTrustManager() 40 { 41 return BaseHttp._bhtm; 42 } 43 44 /** 45 * setIsSSL 46 * 47 * @param enable boolean 48 */ setIsSSL(boolean enable)49 public void setIsSSL(boolean enable) 50 { 51 this._isSSL = enable; 52 } 53 54 /** 55 * setIsAjax 56 * setzt das Request als Ajax 57 * 58 * @param enable boolean 59 */ setIsAjax(boolean enable)60 public void setIsAjax(boolean enable) 61 { 62 this._isAjax = enable; 63 } 64 65 /** 66 * setSocketTimeOut 67 * set den Socket connection timeout 68 * 69 * @param timeout int 70 */ setSocketTimeOut(int timeout)71 public void setSocketTimeOut(int timeout) 72 { 73 this._sockettimeout = timeout; 74 } 75 76 /** 77 * openHttpSite 78 * open/load HTTP Site 79 * 80 * @param URL 81 * @return String 82 * @throws IOException 83 */ openHttpSite(String URL)84 public String openHttpSite(String URL) throws IOException 85 { 86 URL jtUrl = new URL(URL); 87 this._con = (HttpURLConnection) jtUrl.openConnection(); 88 this._con.setConnectTimeout(this._sockettimeout); 89 90 DataInputStream dis = new DataInputStream(this._con.getInputStream()); 91 String inputLine; 92 String buff = ""; 93 94 while ((inputLine = dis.readLine()) != null) 95 { 96 buff = buff + "\r\n" + inputLine; 97 } 98 99 dis.close(); 100 101 /** 102 * Schließen 103 */ 104 this._con.disconnect(); 105 106 return buff; 107 } 108 109 /** 110 * openHttpContentSitePost 111 * open/load Site content (POST) 112 * 113 * @param URL 114 * @param post String 115 * @return String 116 * @throws Exception 117 */ openHttpContentSitePost(String URL, String post)118 public String openHttpContentSitePost(String URL, String post) throws Exception 119 { 120 if( this._isSSL ) 121 { 122 URL = "https://" + URL; 123 } 124 else 125 { 126 URL = "http://" + URL; 127 } 128 129 HttpURLConnection.setFollowRedirects(false); 130 131 URL jtUrl = new URL(URL); 132 133 if( this._isSSL ) 134 { 135 SSLContext sslContext = SSLContext.getInstance("SSL"); 136 137 if( BaseHttp._bhtm == null ) 138 { 139 BaseHttp._bhtm = new BaseHttpsTrustManager(); 140 } 141 142 sslContext.init( 143 null, 144 new javax.net.ssl.TrustManager[] { BaseHttp._bhtm }, 145 new java.security.SecureRandom() 146 ); 147 148 HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); 149 } 150 151 this._con = (HttpURLConnection) jtUrl.openConnection(); 152 this._con.setConnectTimeout(this._sockettimeout); 153 154 if( !this._cookie.equals("") ) 155 { 156 this._con.addRequestProperty("Cookie", this._cookie); 157 } 158 159 if( this._isAjax ) 160 { 161 this._con.addRequestProperty("X-Requested-With", "XMLHttpRequest"); 162 } 163 164 this._con.setDoOutput(true); 165 166 OutputStreamWriter wr = new OutputStreamWriter(this._con.getOutputStream()); 167 wr.write(post); 168 wr.flush(); 169 wr.close(); 170 171 if( this._con != null ) 172 { 173 Map<String, List<String>> headers = this._con.getHeaderFields(); 174 List<String> values = headers.get("Set-Cookie"); 175 176 this._cookie = ""; 177 178 if( values != null ) 179 { 180 for(int i=0; i<values.size(); i++) 181 { 182 //this._con.setRequestProperty("Cookie", values.get(i)); 183 if( !this._cookie.equals("") ) 184 { 185 this._cookie += ";"; 186 } 187 188 this._cookie += values.get(i); 189 } 190 } 191 } 192 193 String buff = ""; 194 195 try 196 { 197 BufferedReader bufferedReader = new BufferedReader( 198 new InputStreamReader(this._con.getInputStream())); 199 200 String line = bufferedReader.readLine(); 201 202 while( line != null ) 203 { 204 buff = buff + line + "\r\n"; 205 206 line = bufferedReader.readLine(); 207 } 208 209 bufferedReader.close(); 210 211 /** 212 * close 213 */ 214 this._con.disconnect(); 215 } 216 catch( Exception exp ) 217 { 218 egwDebuging.log.log(Level.SEVERE, null, exp); 219 throw new Exception("NETERROR"); 220 } 221 222 return buff; 223 } 224 225 /** 226 * openHttpContentSite 227 * open load Site content (GET) 228 * 229 * @param URL 230 * @return 231 * @throws Exception 232 */ openHttpContentSite(String URL)233 public String openHttpContentSite(String URL) throws Exception 234 { 235 if( this._isSSL ) 236 { 237 URL = "https://" + URL; 238 } 239 else 240 { 241 URL = "http://" + URL; 242 } 243 244 HttpURLConnection.setFollowRedirects(false); 245 246 URL jtUrl = new URL(URL); 247 248 if( this._isSSL ) 249 { 250 SSLContext sslContext = SSLContext.getInstance("SSL"); 251 252 if( BaseHttp._bhtm == null ) 253 { 254 BaseHttp._bhtm = new BaseHttpsTrustManager(); 255 } 256 257 sslContext.init( 258 null, 259 new javax.net.ssl.TrustManager[] { BaseHttp._bhtm }, 260 new java.security.SecureRandom() 261 ); 262 263 HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); 264 } 265 266 this._con = (HttpURLConnection) jtUrl.openConnection(); 267 this._con.setConnectTimeout(this._sockettimeout); 268 269 if( !this._cookie.equals("") ) 270 { 271 this._con.addRequestProperty("Cookie", this._cookie); 272 } 273 274 if( this._isAjax ) 275 { 276 this._con.addRequestProperty("X-Requested-With", "XMLHttpRequest"); 277 } 278 279 if( this._con != null ) 280 { 281 Map<String, List<String>> headers = this._con.getHeaderFields(); 282 List<String> values = headers.get("Set-Cookie"); 283 284 this._cookie = ""; 285 286 if( values != null ) 287 { 288 for(int i=0; i<values.size(); i++) 289 { 290 //this._con.setRequestProperty("Cookie", values.get(i)); 291 if( !this._cookie.equals("") ) 292 { 293 this._cookie += ";"; 294 } 295 296 this._cookie += values.get(i); 297 } 298 } 299 } 300 301 String buff = ""; 302 303 try 304 { 305 BufferedReader bufferedReader = new BufferedReader( 306 new InputStreamReader(this._con.getInputStream())); 307 308 String line = bufferedReader.readLine(); 309 310 while( line != null ) 311 { 312 buff = buff + line + "\r\n"; 313 314 line = bufferedReader.readLine(); 315 } 316 317 bufferedReader.close(); 318 319 /** 320 * close 321 */ 322 this._con.disconnect(); 323 } 324 catch(Exception exp) 325 { 326 egwDebuging.log.log(Level.SEVERE, null, exp); 327 throw new Exception("NETERROR"); 328 } 329 330 331 return buff; 332 } 333 334 /** 335 * getHeaderField 336 * get HTTP Header by name 337 * 338 * @param name String 339 * @return String 340 */ getHeaderField(String name)341 public String getHeaderField(String name) 342 { 343 Map<String, List<String>> headers = this._con.getHeaderFields(); 344 List<String> values = headers.get("Set-Cookie"); 345 346 String v = ""; 347 348 for(int i=0; i<values.size(); i++) 349 { 350 v = v + values.get(i); 351 } 352 353 int pos = v.indexOf(name + "="); 354 int end = v.indexOf(";", pos); 355 356 String back = v.substring( pos + name.length()+1, end); 357 358 return back; 359 } 360 361 /** 362 * getLinkVariable 363 * 364 * 365 * @param content 366 * @param field 367 * @return 368 */ getLinkVariable(String content, String field)369 public String getLinkVariable(String content, String field) 370 { 371 String back = ""; 372 373 int pos = content.indexOf(field + "="); 374 375 if(pos > -1) 376 { 377 int end = content.indexOf("&", pos); 378 int end2 = content.indexOf("\r\n", pos); 379 380 if( (end == -1) || (end2 < end)) 381 { 382 end = end2; 383 } 384 385 back = content.substring( pos + field.length() +1, end); 386 } 387 388 return back; 389 } 390 391 /** 392 * getCookieVariable 393 * 394 * 395 * @param content 396 * @param field 397 * @return 398 */ getCookieVariable(String content, String field)399 public String getCookieVariable(String content, String field) 400 { 401 String back = ""; 402 403 int pos = content.indexOf(field + "="); 404 405 if(pos > -1) 406 { 407 int end = content.indexOf(";", pos); 408 int end2 = content.indexOf("\r\n", pos); 409 410 if( (end == -1) || ( ( end2 != -1 ) && (end2 < end) )) 411 { 412 end = end2; 413 } 414 415 if( end == -1 ) 416 { 417 end = content.length(); 418 } 419 420 back = content.substring( pos + field.length() +1, end); 421 } 422 423 return back; 424 } 425 426 /** 427 * getSocketHeaderField 428 * 429 * @param content 430 * @param field 431 * @return 432 */ getSocketHeaderField(String content, String field)433 public String getSocketHeaderField(String content, String field) 434 { 435 String back = ""; 436 437 int pos = content.indexOf(field + "="); 438 439 if(pos > -1) 440 { 441 int end = content.indexOf(";", pos); 442 //int end2 = content.indexOf("\r\n", pos); 443 int end2 = content.length(); 444 445 if( (end == -1) || (end2 < end)) 446 { 447 end = end2; 448 } 449 450 back = content.substring( pos + field.length() +1, end); 451 } 452 453 return back; 454 } 455 456 /** 457 * isHostMoved 458 * is server send host moved 459 * 460 * @param host 461 * @param path 462 * @return 463 * @throws Exception 464 */ isHostMoved(String host, String path)465 public String isHostMoved(String host, String path) throws Exception 466 { 467 if( path == null ) 468 { 469 path = "/"; 470 } 471 472 String back = ""; 473 474 String buffer = this.openHttpContentSite(host + path); 475 476 HttpURLConnection tmp = (HttpURLConnection) this._con; 477 int httpcode = tmp.getResponseCode(); 478 479 if( httpcode == HttpURLConnection.HTTP_MOVED_PERM ) 480 { 481 back = tmp.getHeaderField("Location"); 482 } 483 484 return back; 485 } 486 487 /** 488 * isNotFound 489 * server send site not found 490 * 491 * @return boolean 492 */ isNotFound()493 public boolean isNotFound() 494 { 495 try 496 { 497 if( this._con.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND ) 498 { 499 return true; 500 } 501 } 502 catch( IOException ex) 503 { 504 egwDebuging.log.log(Level.SEVERE, null, ex); 505 } 506 507 return false; 508 } 509 510 /** 511 * getLocation 512 * server send location (move) 513 * 514 * @return String 515 */ getLocation()516 public String getLocation() 517 { 518 return this._con.getHeaderField("Location"); 519 } 520 521 /** 522 * getCookie 523 * get cookie in String 524 * 525 * @return String 526 */ getCookie()527 public String getCookie() 528 { 529 return this._cookie; 530 } 531 532 /** 533 * setCookie 534 * set cookie 535 * 536 * @param cookie String 537 */ setCookie(String cookie)538 public void setCookie(String cookie) 539 { 540 this._cookie = cookie; 541 } 542 } 543