1 18 19 package sync4j.exchange.httptransport; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 26 import java.net.Socket ; 27 28 import java.util.logging.Logger ; 29 import java.util.logging.Level ; 30 31 import sync4j.exchange.util.StringTools; 32 33 import sync4j.framework.logging.Sync4jLogger; 34 35 43 public class WebDavHttpTransport { 44 45 47 private static final int SIZE_INPUT_BUFFER = 4096; 48 private static final String NEW_LINE = "\r\n" ; 49 50 52 private String host = null ; 53 private int port = 0 ; 54 55 protected Logger log = null ; 56 57 59 public WebDavHttpTransport(String host, 60 int port) { 61 62 log = Sync4jLogger.getLogger("source"); 63 64 this.host = host ; 65 this.port = port ; 66 } 67 68 70 82 public String sendRequest (String webDavCommand, 83 String credentials, 84 String request, 85 String encoding) 86 87 throws IOException { 88 89 Socket s = null ; 90 OutputStream out = null ; 91 InputStream in = null ; 92 93 String fromExchange = null; 94 String toExchange = null; 95 96 StringBuffer temp = new StringBuffer (request); 97 temp.append(NEW_LINE).append(NEW_LINE); 98 request = temp.toString(); 99 100 StringBuffer requestToSend = new StringBuffer (webDavCommand); 101 requestToSend.append("Host: ").append(host).append(NEW_LINE); 102 requestToSend.append("Content-Type: text/xml; charset=\"").append(encoding).append("\"").append(NEW_LINE); 103 requestToSend.append("Content-Length: ").append(request.getBytes(encoding).length).append(NEW_LINE); 104 requestToSend.append("Authorization: Basic ").append(credentials).append(NEW_LINE); 105 requestToSend.append(NEW_LINE); 106 107 requestToSend.append(request); 108 109 toExchange = requestToSend.toString(); 110 111 if (log.isLoggable(Level.FINEST)) { 112 log.finest("Message to Exchange Server:" + toExchange); 113 } 114 115 try { 116 117 byte[] bRequest = toExchange.getBytes(encoding); 118 119 s = new Socket (this.host, 120 this.port) ; 121 122 in = s.getInputStream () ; 123 out = s.getOutputStream() ; 124 125 out.write(bRequest); 126 out.flush(); 127 128 byte[] bResponse = readContent(in); 129 in.close(); 130 131 fromExchange = new String (bResponse, encoding); 132 133 if (log.isLoggable(Level.FINEST)) { 134 log.finest("Message from Exchange Server:" + fromExchange); 135 } 136 137 return StringTools.unescapeXml(fromExchange); 138 139 } finally { 140 141 if (out != null) { 142 out.close () ; 143 } 144 if (in != null) { 145 in.close () ; 146 } 147 if (s != null) { 148 s.close () ; 149 } 150 } 151 152 } 153 154 161 protected byte[] readContent(final InputStream in) throws IOException { 162 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 163 byte[] buf = new byte[SIZE_INPUT_BUFFER]; 164 165 int c = 0; 166 int b = 0; 167 while ((c < buf.length) && (b = in.read(buf, c, buf.length-c)) >= 0) { 168 c+=b; 169 if (c == SIZE_INPUT_BUFFER) { 170 bout.write(buf); 171 buf = new byte[SIZE_INPUT_BUFFER]; 172 c = 0; 173 } 174 } 175 if (c != 0) { 176 bout.write(buf, 0, c); 177 } 178 return bout.toByteArray(); 179 } 180 181 } | Popular Tags |