1 18 19 package sync4j.server.notification; 20 21 import java.io.BufferedOutputStream ; 22 import java.net.Socket ; 23 import java.net.URL ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 27 import sync4j.framework.logging.Sync4jLogger; 28 import sync4j.framework.notification.NotificationConstants; 29 import sync4j.framework.notification.NotificationException; 30 import sync4j.framework.notification.Sender; 31 import sync4j.framework.logging.Sync4jLoggerName; 32 33 34 48 public class HttpSender implements Sender { 49 50 private String deviceAddress = null; 53 54 private Logger log = null; 56 57 public HttpSender() { 59 log = Sync4jLogger.getLogger(Sync4jLoggerName.SERVER_DM_NOTIFICATION); 60 } 61 62 64 69 public String getDeviceAddress() { 70 return deviceAddress; 71 } 72 73 78 public void setDeviceAddress(String deviceAddress) { 79 this.deviceAddress = deviceAddress; 80 } 81 82 89 public void sendMessage(int messageType, String phoneNumber, byte[] message, String info) 90 throws NotificationException { 91 92 if (log.isLoggable(Level.INFO)) { 93 log.info("sendMessage: " + message + " (length: " + message.length + ")"); 94 } 95 96 97 if (messageType != NotificationConstants.MESSAGE_TYPE_NOTIFICATION_GENERIC) { 98 throw new NotificationException("This sender is usable only for notification message"); 99 } 100 101 byte[] nestedRequest = buildNestedRequest(message);; 102 byte[] request = buildRequest(nestedRequest); 103 104 try { 105 sendHttpRequest(request); 106 } catch (Exception e) { 107 throw new NotificationException("Error sending the message via http", e); 108 } 109 110 } 111 112 123 public void sendMessages(int messageType, 124 String contentType, 125 String [] digest, 126 int[] authMethods, 127 String [] phoneNumbers, 128 byte[][] messages, 129 String info) throws NotificationException { 130 131 if (log.isLoggable(Level.INFO)) { 132 log.info("sendMessage with empty implementation"); 133 } 134 } 135 136 144 public void sendMessages(int messageType, 145 String [] phoneNumbers, 146 byte[][] messages, 147 String info) throws NotificationException { 148 149 for (int i=0; i<phoneNumbers.length; i++) { 150 151 if (i != 0) { 155 System.out.println("Wait 20 s for test with SCTS"); 156 try { 157 Thread.sleep(20000); 158 } catch (InterruptedException ex) { 159 } 160 161 } 162 sendMessage(messageType, phoneNumbers[i], messages[i], info); 163 } 164 } 165 166 167 private void sendHttpRequest(byte[] message) throws Exception { 168 if (log.isLoggable(Level.INFO)) { 169 log.info("Send message to: " + deviceAddress); 170 } 171 URL url = new URL (deviceAddress); 172 String host = url.getHost(); 173 int port = url.getPort(); 174 175 Socket conn = new Socket (host, port); 176 177 BufferedOutputStream wr = new BufferedOutputStream (conn.getOutputStream()); 179 180 wr.write(message); 181 wr.flush(); 182 183 wr.close(); 184 } 185 186 private byte[] buildRequest(byte[] nestedRequest) { 187 StringBuffer sb = new StringBuffer ("POST /wappush HTTP/1.1\r\n"); 188 sb.append("Host: 127.0.0.1\r\n"); 189 sb.append("Content-Type: application/http\r\n"); 190 sb.append("Content-Length: ").append(nestedRequest.length).append("\r\n"); 191 sb.append("X-Wap-Push-OTA-Version: 1.0\r\n\r\n"); 192 193 sb.append(new String (nestedRequest)); 194 195 return sb.toString().getBytes(); 196 197 } 198 199 200 private byte[] buildNestedRequest(byte[] message) { 201 StringBuffer sb = new StringBuffer ("HTTP/1.1 200 OK\r\n"); 202 sb.append("Content-Language: en\r\n"); 203 sb.append("Content-Length: ").append(message.length).append("\r\n"); 204 sb.append("Content-Type: application/vnd.syncml.notification\r\n\r\n"); 205 206 byte[] header = sb.toString().getBytes(); 207 208 byte[] nestedRequest = new byte[header.length + message.length]; 209 210 System.arraycopy(header, 0, nestedRequest, 0, header.length); 211 System.arraycopy(message, 0, nestedRequest, header.length, message.length); 212 213 return nestedRequest; 214 215 216 } 217 218 } | Popular Tags |