KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > spds > SyncMLClientImpl


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.syncclient.spds;
20
21 import java.io.DataInputStream JavaDoc;
22 import java.io.DataOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import javax.microedition.io.HttpConnection;
26 import javax.microedition.io.Connector;
27
28 /**
29  * Rapresents a HTTP client implementation
30  *
31  * @author Fabio Maggi @ Funambol
32  * @version $Id: SyncMLClientImpl.java,v 1.4 2005/02/22 16:48:13 fabius Exp $
33  *
34  **/

35 public final class SyncMLClientImpl {
36
37     // ----------------------------------------------------------------------------------------- Constants
38

39     private static final String JavaDoc PROP_MICROEDITION_CONFIGURATION = "microedition.configuration" ;
40     private static final String JavaDoc PROP_CONTENT_LANGUAGE = "Content-Language" ;
41     private static final String JavaDoc PROP_CONTENT_LENGTH = "Content-Length" ;
42     private static final String JavaDoc PROP_CONTENT_TYPE = "Content-Type" ;
43     private static final String JavaDoc PROP_MICROEDITION_LOCALE = "microedition.locale" ;
44     private static final String JavaDoc PROP_MICROEDITION_PROFILES = "microedition.profiles" ;
45     private static final String JavaDoc PROP_USER_AGENT = "User-Agent" ;
46
47     // ----------------------------------------------------------------------------------------- Private Data
48

49     private final String JavaDoc requestURL;
50
51     // ------------------------------------------------------------ Constructors
52
/**
53      *
54      * @param requestURL must be non-null
55      *
56      */

57     public SyncMLClientImpl(final String JavaDoc requestURL) throws IOException JavaDoc
58     {
59         if (requestURL == null)
60         {
61             throw new NullPointerException JavaDoc("request URL parameter is null");
62         }
63
64         this.requestURL = requestURL;
65     }
66
67
68     // ---------------------------------------------------------- Public methods
69

70     /**
71      *
72      * @param request HTTP request
73      * @return HTTP response
74      */

75     public String JavaDoc sendMessage(final String JavaDoc request)
76     throws IOException JavaDoc
77     {
78         HttpConnection httpCon = null ;
79         DataInputStream JavaDoc inputStream = null ;
80         DataOutputStream JavaDoc outputStream = null ;
81     
82         StringBuffer JavaDoc 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 JavaDoc 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             //len = c.getLength();
116
b = new StringBuffer JavaDoc(size >= 0 ? (int)size : 4096);
117             if (size != -1) {
118                 // Read content-Length bytes, or until max is reached.
119
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                 // Read til the connection is closed, or til max is reached.
130
// (Typical HTTP/1.0 script generated output)
131
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 JavaDoc(data);
161     }
162
163     // ---------------------------------------------------------- Private metho
164

165 }
166
Popular Tags