KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > exchange > httptransport > WebDavHttpTransport


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.exchange.httptransport;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import java.net.Socket JavaDoc;
27
28 import java.util.logging.Logger JavaDoc;
29 import java.util.logging.Level JavaDoc;
30
31 import sync4j.exchange.util.StringTools;
32
33 import sync4j.framework.logging.Sync4jLogger;
34
35 /**
36  * This class provide methods
37  * to access exchange server datastore
38  * by socket connection
39  *
40  * @author Fabio Maggi @ Funambol
41  * @version $Id: WebDavHttpTransport.java,v 1.10 2005/06/24 15:50:50 fabius Exp $
42  **/

43 public class WebDavHttpTransport {
44
45     //---------------------------------------------------------------- Constants
46

47     private static final int SIZE_INPUT_BUFFER = 4096;
48     private static final String JavaDoc NEW_LINE = "\r\n" ;
49
50     //---------------------------------------------------------------- Private data
51

52     private String JavaDoc host = null ;
53     private int port = 0 ;
54
55     protected Logger JavaDoc log = null ;
56
57     //---------------------------------------------------------------- Costructors
58

59     public WebDavHttpTransport(String JavaDoc host,
60                                int port) {
61
62         log = Sync4jLogger.getLogger("source");
63
64         this.host = host ;
65         this.port = port ;
66     }
67
68     //---------------------------------------------------------------- Public methods
69

70     /**
71      * Send request to exchange server
72      * and get response.
73      *
74      * @param webDavCommand the first row of the header
75      * @param credentials the credential
76      * @param request the request
77      * @param encoding the encoding to use to encode the request and the response
78      * @return response
79      *
80      * @throws IOException
81      **/

82     public String JavaDoc sendRequest (String JavaDoc webDavCommand,
83                                String JavaDoc credentials,
84                                String JavaDoc request,
85                                String JavaDoc encoding)
86
87         throws IOException JavaDoc {
88
89         Socket JavaDoc s = null ;
90         OutputStream JavaDoc out = null ;
91         InputStream JavaDoc in = null ;
92
93         String JavaDoc fromExchange = null;
94         String JavaDoc toExchange = null;
95
96         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(request);
97         temp.append(NEW_LINE).append(NEW_LINE);
98         request = temp.toString();
99
100         StringBuffer JavaDoc requestToSend = new StringBuffer JavaDoc(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 JavaDoc(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 JavaDoc(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     /**
155      * Reads the content from the inputstream.
156      *
157      * @param in the request InputStream
158      *
159      * @throws IOException in case of errors
160      */

161     protected byte[] readContent(final InputStream JavaDoc in) throws IOException JavaDoc {
162         ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
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