CMSConnection.java (8578B)
1 /* 2 OpenRat Java-Client 3 Copyright (C) 2009 Jan Dankert 4 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Library General Public 7 License as published by the Free Software Foundation; either 8 version 2 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Library General Public License for more details. 14 15 You should have received a copy of the GNU Library General Public 16 License along with this library; if not, write to the 17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 18 Boston, MA 02110-1301, USA. 19 20 */ 21 package de.openrat.client.util; 22 23 import java.io.PrintWriter; 24 import java.net.Socket; 25 import java.util.Locale; 26 27 /** 28 * API-Request to the OpenRat Content Management System. <br> 29 * <br> 30 * The call to the CMS server is done via a (non-SSL) HTTP connection.<br> 31 * <br> 32 * Before a call you are able to set some key/value-pairs as parameters. After 33 * calling the CMS a DOM-document is returned, which contains the server 34 * response.<br> 35 * Example <br> 36 * 37 * <pre> 38 * CMSRequest request = new CMSRequest("your.openrat.example.com"); 39 * // prints tracing information to stdout. 40 * request.trace = true; 41 * try 42 * { 43 * request.parameter.put("action", "index"); 44 * request.parameter.put("subaction", "showlogin"); // login page 45 * request.parameter.put("...", "..."); 46 * Document response = request.call(); 47 * // now traverse through the dom tree and get your information. 48 * } 49 * catch (IOException e) 50 * { 51 * // your error handling. 52 * } 53 * </pre> 54 * 55 * @author Jan Dankert 56 */ 57 public class CMSConnection 58 { 59 60 private String serverPath; 61 private String serverHost; 62 private int serverPort; 63 private boolean secure; 64 65 private String proxyHostname; 66 private int proxyPort; 67 68 private String proxyUser; 69 private String proxyPassword; 70 71 private PrintWriter logWriter; 72 73 private CookieStoreMap cookieStore = new CookieStoreMap(); 74 private String token = ""; 75 76 private String paramActionName = "action"; 77 private String paramMethodName = "subaction"; 78 79 private Locale locale = Locale.getDefault(); 80 private int timeout = 5000; 81 private boolean keepAlive; 82 private Socket socket; 83 84 /** 85 * Inhalt des Feldes <code>socket</code>. 86 * 87 * @return socket 88 */ 89 public Socket getSocket() 90 { 91 return socket; 92 } 93 94 /** 95 * Setzt das Feld <code>socket</code>. 96 * 97 * @param socket 98 * socket 99 */ 100 public void setSocket(Socket socket) 101 { 102 this.socket = socket; 103 } 104 105 /** 106 * Inhalt des Feldes <code>timeout</code>. 107 * 108 * @return timeout 109 */ 110 public int getTimeout() 111 { 112 return timeout; 113 } 114 115 /** 116 * Constructs a CMS-Request to the specified server/path/port. 117 * 118 * @param host 119 * hostname 120 * @param port 121 * port-number 122 * @param path 123 * path 124 */ 125 public CMSConnection(String host, int port, String path) 126 { 127 128 super(); 129 this.serverHost = host; 130 this.serverPath = path; 131 this.serverPort = port; 132 133 if (this.serverHost.startsWith("https://")) 134 { 135 secure = true; 136 if (port == 80) 137 { 138 this.serverPort = 443; 139 } 140 this.serverHost = this.serverHost.substring(8); 141 } 142 else if (this.serverHost.startsWith("http://")) 143 { 144 this.serverHost = this.serverHost.substring(7); 145 } 146 else if (port == 443) 147 { 148 secure = true; 149 } 150 } 151 152 /** 153 * Inhalt des Feldes <code>serverPath</code>. 154 * 155 * @return serverPath 156 */ 157 public String getServerPath() 158 { 159 return serverPath; 160 } 161 162 /** 163 * Setzt das Feld <code>serverPath</code>. 164 * 165 * @param serverPath 166 * serverPath 167 */ 168 public void setServerPath(String serverPath) 169 { 170 this.serverPath = serverPath; 171 } 172 173 /** 174 * Inhalt des Feldes <code>serverHost</code>. 175 * 176 * @return serverHost 177 */ 178 public String getServerHost() 179 { 180 return serverHost; 181 } 182 183 /** 184 * Setzt das Feld <code>serverHost</code>. 185 * 186 * @param serverHost 187 * serverHost 188 */ 189 public void setServerHost(String serverHost) 190 { 191 this.serverHost = serverHost; 192 } 193 194 /** 195 * Inhalt des Feldes <code>serverPort</code>. 196 * 197 * @return serverPort 198 */ 199 public int getServerPort() 200 { 201 return serverPort; 202 } 203 204 /** 205 * Setzt das Feld <code>serverPort</code>. 206 * 207 * @param serverPort 208 * serverPort 209 */ 210 public void setServerPort(int serverPort) 211 { 212 this.serverPort = serverPort; 213 } 214 215 /** 216 * Inhalt des Feldes <code>proxyHostname</code>. 217 * 218 * @return proxyHostname 219 */ 220 public String getProxyHostname() 221 { 222 return proxyHostname; 223 } 224 225 /** 226 * Setzt das Feld <code>proxyHostname</code>. 227 * 228 * @param proxyHostname 229 * proxyHostname 230 */ 231 public void setProxyHostname(String proxyHostname) 232 { 233 this.proxyHostname = proxyHostname; 234 } 235 236 /** 237 * Inhalt des Feldes <code>proxyPort</code>. 238 * 239 * @return proxyPort 240 */ 241 public int getProxyPort() 242 { 243 return proxyPort; 244 } 245 246 /** 247 * Setzt das Feld <code>proxyPort</code>. 248 * 249 * @param proxyPort 250 * proxyPort 251 */ 252 public void setProxyPort(int proxyPort) 253 { 254 this.proxyPort = proxyPort; 255 } 256 257 /** 258 * Setzt das Feld <code>logWriter</code>. 259 * 260 * @param logWriter 261 * logWriter 262 */ 263 public void setLogWriter(PrintWriter logWriter) 264 { 265 this.logWriter = logWriter; 266 } 267 268 /** 269 * Setzt das Feld <code>secure</code>. 270 * 271 * @param secure 272 * secure 273 */ 274 public void setSecure(boolean secure) 275 { 276 this.secure = secure; 277 } 278 279 /** 280 * Setzt das Feld <code>proxyUser</code>. 281 * 282 * @param proxyUser 283 * proxyUser 284 */ 285 public void setProxyUser(String proxyUser) 286 { 287 this.proxyUser = proxyUser; 288 } 289 290 /** 291 * Setzt das Feld <code>proxyPassword</code>. 292 * 293 * @param proxyPassword 294 * proxyPassword 295 */ 296 public void setProxyPassword(String proxyPassword) 297 { 298 this.proxyPassword = proxyPassword; 299 } 300 301 /** 302 * Inhalt des Feldes <code>proxyUser</code>. 303 * 304 * @return proxyUser 305 */ 306 public String getProxyUser() 307 { 308 return proxyUser; 309 } 310 311 /** 312 * Inhalt des Feldes <code>proxyPassword</code>. 313 * 314 * @return proxyPassword 315 */ 316 public String getProxyPassword() 317 { 318 return proxyPassword; 319 } 320 321 public PrintWriter getLogWriter() 322 { 323 return logWriter; 324 } 325 326 /** 327 * Inhalt des Feldes <code>cookieStore</code>. 328 * 329 * @return cookieStore 330 */ 331 public CookieStoreMap getCookieStore() 332 { 333 return cookieStore; 334 } 335 336 /** 337 * Inhalt des Feldes <code>token</code>. 338 * 339 * @return token 340 */ 341 public String getToken() 342 { 343 return token; 344 } 345 346 /** 347 * Setzt das Feld <code>token</code>. 348 * 349 * @param token 350 * token 351 */ 352 public void setToken(String token) 353 { 354 this.token = token; 355 } 356 357 /** 358 * Inhalt des Feldes <code>secure</code>. 359 * 360 * @return secure 361 */ 362 public boolean isSecure() 363 { 364 return secure; 365 } 366 367 /** 368 * Inhalt des Feldes <code>paramActionName</code>. 369 * 370 * @return paramActionName 371 */ 372 public String getParamActionName() 373 { 374 return paramActionName; 375 } 376 377 /** 378 * Setzt das Feld <code>paramActionName</code>. 379 * 380 * @param paramActionName 381 * paramActionName 382 */ 383 public void setParamActionName(String paramActionName) 384 { 385 this.paramActionName = paramActionName; 386 } 387 388 /** 389 * Inhalt des Feldes <code>paramMethodName</code>. 390 * 391 * @return paramMethodName 392 */ 393 public String getParamMethodName() 394 { 395 return paramMethodName; 396 } 397 398 /** 399 * Setzt das Feld <code>paramMethodName</code>. 400 * 401 * @param paramMethodName 402 * paramMethodName 403 */ 404 public void setParamMethodName(String paramMethodName) 405 { 406 this.paramMethodName = paramMethodName; 407 } 408 409 /** 410 * Inhalt des Feldes <code>locale</code>. 411 * 412 * @return locale 413 */ 414 public Locale getLocale() 415 { 416 return this.locale; 417 } 418 419 /** 420 * Setzt das Feld <code>locale</code>. 421 * 422 * @param locale 423 * locale 424 */ 425 public void setLocale(Locale locale) 426 { 427 this.locale = locale; 428 } 429 430 /** 431 * Setzt das Feld <code>timeout</code>. 432 * 433 * @param timeout 434 * timeout 435 */ 436 public void setTimeout(int timeout) 437 { 438 this.timeout = timeout; 439 } 440 441 /** 442 * Using Keep-alive-sockets and HTTP-persistent-connections. we do NOT 443 * support this! DO NOT set this to true! 444 * 445 * @param keepAlive 446 * keepAlive 447 */ 448 public void setKeepAlive(boolean keepAlive) 449 { 450 this.keepAlive = keepAlive; 451 } 452 453 public boolean isKeepAlive() 454 { 455 return keepAlive; 456 } 457 458 @Override 459 public String toString() 460 { 461 return super.toString() + ": " + serverHost + ":" + serverPort + serverPath; 462 463 } 464 }