1 18 19 package sync4j.syncclient.spds; 20 21 import java.io.DataInputStream ; 22 import java.io.DataOutputStream ; 23 import java.io.IOException ; 24 25 import javax.microedition.io.HttpConnection; 26 import javax.microedition.io.Connector; 27 28 35 public final class SyncMLClientImpl { 36 37 39 private static final String PROP_MICROEDITION_CONFIGURATION = "microedition.configuration" ; 40 private static final String PROP_CONTENT_LANGUAGE = "Content-Language" ; 41 private static final String PROP_CONTENT_LENGTH = "Content-Length" ; 42 private static final String PROP_CONTENT_TYPE = "Content-Type" ; 43 private static final String PROP_MICROEDITION_LOCALE = "microedition.locale" ; 44 private static final String PROP_MICROEDITION_PROFILES = "microedition.profiles" ; 45 private static final String PROP_USER_AGENT = "User-Agent" ; 46 47 49 private final String requestURL; 50 51 57 public SyncMLClientImpl(final String requestURL) throws IOException 58 { 59 if (requestURL == null) 60 { 61 throw new NullPointerException ("request URL parameter is null"); 62 } 63 64 this.requestURL = requestURL; 65 } 66 67 68 70 75 public String sendMessage(final String request) 76 throws IOException 77 { 78 HttpConnection httpCon = null ; 79 DataInputStream inputStream = null ; 80 DataOutputStream outputStream = null ; 81 82 StringBuffer b = null; 83 byte[] data = null; 84 85 synchronized(this) { 86 87 try { 88 89 byte[] dataToSend = request.getBytes(); 90 91 httpCon = (HttpConnection)Connector.open(requestURL); 92 93 httpCon.setRequestMethod (HttpConnection.POST ); 94 httpCon.setRequestProperty (PROP_USER_AGENT , 95 "BlackBerry/3.6.1" ); 96 httpCon.setRequestProperty (PROP_CONTENT_LENGTH , 97 String.valueOf(dataToSend.length) ); 98 httpCon.setRequestProperty (PROP_CONTENT_TYPE , 99 "application/vnd.syncml+xml" ); 100 101 String locale = System.getProperty(PROP_MICROEDITION_LOCALE ); 102 103 if (locale != null) { 104 httpCon.setRequestProperty(PROP_CONTENT_LANGUAGE, locale ); 105 } 106 107 outputStream = httpCon.openDataOutputStream(); 108 outputStream.write(dataToSend); 109 outputStream.flush(); 110 111 inputStream = httpCon.openDataInputStream(); 112 113 int size = (int) httpCon.getLength(); 114 115 b = new StringBuffer (size >= 0 ? (int)size : 4096); 117 if (size != -1) { 118 int ch = 0; 120 for (int i = 0; i < size; i++) { 121 if ((ch = inputStream.read()) != -1) { 122 if (ch <= ' ') { 123 ch = ' '; 124 } 125 b.append((char) ch); 126 } 127 } 128 } else { 129 int ch = 0; 132 size = 0; 133 while ((ch = inputStream.read()) != -1) { 134 if (ch <= ' ') { 135 ch = ' '; 136 } 137 b.append((char)ch); 138 } 139 } 140 141 data = b.toString().getBytes(); 142 143 } finally { 144 if (inputStream != null) { 145 inputStream.close(); 146 inputStream = null ; 147 } 148 if (outputStream != null) { 149 outputStream.close(); 150 outputStream = null ; 151 } 152 if (httpCon != null) { 153 httpCon.close(); 154 httpCon = null ; 155 } 156 157 } 158 } 159 160 return new String (data); 161 } 162 163 165 } 166 | Popular Tags |