1 18 19 package sync4j.syncclient.spds; 20 21 import java.io.InputStream ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 28 import java.util.Properties ; 29 import java.util.Vector ; 30 31 import sync4j.syncclient.common.logging.Logger; 32 import sync4j.syncclient.spds.SyncMLClient; 33 34 import sync4j.syncclient.spds.event.SyncTransportEvent; 35 36 import sync4j.syncclient.spds.event.SyncTransportListener; 37 38 39 52 abstract class BaseSyncMLClient implements SyncMLClient { 53 54 56 private static final String API_CHARSET = "UTF-8" ; 57 private static final String PROP_CHARSET = "spds.charset" ; 58 private static final String KEY_DEFAULT_CHARSET = "DEFAULT" ; 59 60 private static final String PROP_CONTENT_TYPE = "Content-Type" ; 61 private static final String PROP_CONTENT_LENGTH = "Content-Length" ; 62 63 private static final String PROP_PROXY_HOST = "http.proxyHost" ; 64 private static final String PROP_PROXY_PORT = "http.proxyPort" ; 65 66 68 private URL requestURL = null ; 69 private Logger logger = new Logger() ; 70 private Vector syncTransportListeners = null ; 71 private SyncTransportEvent syncTransportEvent = null ; 72 73 75 78 private String charSet = null; 79 80 84 public String getCharSet() { 85 return charSet; 86 } 87 88 92 public void setCharSet(String charSet) { 93 this.charSet = charSet; 94 } 95 96 99 private String contentType = null; 100 101 102 107 public void setContentType(String contentType) { 108 this.contentType = contentType; 109 } 110 111 115 private int timeout = 60000; 116 117 121 public int getTimeout() { 122 return timeout; 123 } 124 125 130 public void setTimeout(int timeout) { 131 this.timeout = timeout; 132 } 133 134 137 private String proxyHost = null; 138 139 142 public java.lang.String getProxyHost() { 143 return proxyHost; 144 } 145 146 149 public void setProxyHost(java.lang.String proxyHost) { 150 this.proxyHost = proxyHost; 151 } 152 153 157 private int proxyPort = 8080; 158 159 162 public int getProxyPort() { 163 return proxyPort; 164 } 165 166 169 public void setProxyPort(int proxyPort) { 170 this.proxyPort = proxyPort; 171 } 172 173 177 private boolean useProxy = false; 178 179 182 public boolean getUseProxy() { 183 return useProxy; 184 } 185 186 189 public void setUseProxy(boolean useProxy) { 190 this.useProxy = useProxy; 191 } 192 193 196 public void setUrl(String url) 197 throws IOException { 198 199 if (url == null) { 200 throw new NullPointerException 201 ("requestURL parameter is null"); 202 } 203 204 try { 205 this.requestURL = new URL (url); 206 } catch (Exception e) { 207 throw new IOException (e.getMessage()); 208 209 } 210 } 211 212 214 219 public BaseSyncMLClient() { 220 221 syncTransportListeners = new Vector (); 222 223 } 224 225 227 232 public void addSyncTransportListener (SyncTransportListener listener) { 233 syncTransportListeners.addElement(listener); 234 } 235 236 241 public void removeSyncTransportListener (SyncTransportListener listener) { 242 syncTransportListeners.removeElement(listener); 243 } 244 245 public String toString() { 246 return requestURL.toString(); 247 } 248 249 251 protected String sendMessage(byte[] bytes) 252 throws IOException { 253 254 URLConnection conn = null ; 255 OutputStream os = null ; 256 InputStream is = null ; 257 258 Properties prop = null ; 259 260 setCharSet(); 261 262 prop = System.getProperties(); 263 264 try { 265 266 if (this.useProxy) { 267 prop.put(PROP_PROXY_HOST, proxyHost ) ; 268 prop.put(PROP_PROXY_PORT, String.valueOf(proxyPort) ) ; 269 } 270 271 conn = requestURL.openConnection(); 272 conn.setDoInput (true) ; 273 conn.setDoOutput(true) ; 274 conn.setRequestProperty (PROP_CONTENT_TYPE, 275 contentType ); 276 conn.setRequestProperty (PROP_CONTENT_LENGTH, 277 String.valueOf(bytes.length) ); 278 279 if (logger.isLoggable(Logger.DEBUG)) { 280 logger.debug("Connected to " + requestURL); 281 } 282 283 os = conn.getOutputStream(); 284 285 syncTransportEvent = 286 new SyncTransportEvent(SyncTransportEvent.SEND_DATA_BEGIN , 287 bytes.length ); 288 fireSyncTransportEvent(syncTransportEvent); 289 290 os.write(bytes); os.flush(); 291 292 syncTransportEvent = 293 new SyncTransportEvent(SyncTransportEvent.SEND_DATA_END , 294 bytes.length ); 295 fireSyncTransportEvent(syncTransportEvent); 296 297 if (logger.isLoggable(Logger.DEBUG)) { 298 logger.debug("Message sent" ) ; 299 logger.debug("Reading response..." ) ; 300 } 301 302 is = conn.getInputStream(); 303 304 int contentLength = conn.getContentLength () ; 305 String contentType = conn.getContentType () ; 306 307 syncTransportEvent = 308 new SyncTransportEvent(SyncTransportEvent.RECEIVE_DATA_BEGIN , 309 contentLength ); 310 fireSyncTransportEvent(syncTransportEvent); 311 312 if (logger.isLoggable(Logger.DEBUG)) { 313 logger.debug(PROP_CONTENT_TYPE + ": " + contentType ) ; 314 logger.debug(PROP_CONTENT_LENGTH + ": " + contentLength ) ; 315 } 316 317 if (contentType == null) { 318 throw new IOException ("Content type is null"); 319 } 320 321 byte[] buf = new byte[contentLength]; 322 323 int c = 0 ; 324 int b = 0 ; 325 326 while ((c < buf.length) && (b = is.read(buf, c, buf.length-c)) >= 0) { 327 c+=b; 328 syncTransportEvent = 329 new SyncTransportEvent(SyncTransportEvent.DATA_RECEIVED , 330 b ); 331 fireSyncTransportEvent(syncTransportEvent); 332 } 333 334 if (logger.isLoggable(Logger.DEBUG)) { 335 logger.debug("Response read") ; 336 } 337 338 syncTransportEvent = 339 new SyncTransportEvent(SyncTransportEvent.RECEIVE_DATA_END , 340 contentLength ); 341 fireSyncTransportEvent(syncTransportEvent); 342 343 if (!KEY_DEFAULT_CHARSET.equals(charSet)) { 344 return new String (buf, charSet); 345 } else { 346 return new String (buf); 347 } 348 349 } catch (IOException e) { 350 e.printStackTrace(); 351 throw e; 352 } finally { 353 354 358 if (is != null) { 359 is.close(); 360 is = null ; 361 } 362 if (os != null) { 363 os.close(); 364 os = null ; 365 } 366 if (conn != null) { 367 conn = null ; 368 } 369 } 370 371 } 372 373 375 378 private void setCharSet() { 379 this.charSet = System.getProperty(PROP_CHARSET, API_CHARSET); 380 } 381 382 388 private void fireSyncTransportEvent(SyncTransportEvent syncTransportEvent) { 389 390 for (int i = 0, l = syncTransportListeners.size(); i < l; i++) { 391 392 if (syncTransportEvent.getType() == 393 SyncTransportEvent.SEND_DATA_BEGIN) { 394 ((SyncTransportListener) 395 syncTransportListeners.elementAt(i)). 396 sendDataBegin(syncTransportEvent); 397 } else if (syncTransportEvent.getType() == 398 SyncTransportEvent.SEND_DATA_END) { 399 ((SyncTransportListener) 400 syncTransportListeners.elementAt(i)). 401 sendDataEnd(syncTransportEvent); 402 } else if (syncTransportEvent.getType() == 403 SyncTransportEvent.RECEIVE_DATA_BEGIN) { 404 ((SyncTransportListener) 405 syncTransportListeners.elementAt(i)). 406 receiveDataBegin(syncTransportEvent); 407 } else if (syncTransportEvent.getType() == 408 SyncTransportEvent.DATA_RECEIVED) { 409 ((SyncTransportListener) 410 syncTransportListeners.elementAt(i)). 411 dataReceived(syncTransportEvent); 412 } else if (syncTransportEvent.getType() == 413 SyncTransportEvent.RECEIVE_DATA_END) { 414 ((SyncTransportListener) 415 syncTransportListeners.elementAt(i)). 416 receiveDataEnd(syncTransportEvent); 417 } 418 419 } 420 421 } 422 423 } | Popular Tags |