1 package eu
.cepl
.network
;
5 * - I have to figure out how to switch on logging for HTTP protocol
9 import java
.io
.BufferedInputStream
;
10 import java
.io
.BufferedOutputStream
;
11 import java
.io
.ByteArrayInputStream
;
12 import java
.io
.IOException
;
13 import java
.io
.InputStreamReader
;
14 import java
.io
.UnsupportedEncodingException
;
15 import java
.net
.MalformedURLException
;
17 import java
.net
.URLConnection
;
18 import java
.net
.URLEncoder
;
19 import java
.util
.HashMap
;
21 import java
.util
.Map
.Entry
;
23 public class SimpleHTTP
{
25 private URL url
= null;
26 private BufferedInputStream inBS
= null;
27 private BufferedOutputStream outBS
= null;
28 // private ConsoleHandler hand = new ConsoleHandler();
29 private URLConnection conn
= null;
30 private URLConnection connection
;
32 final static int BUFMAX
= 1024;
34 // private static Logger logger =
35 // Logger.getLogger(SimpleHTTP.class.getName());
41 // logger.addHandler(hand);
42 // hand.setFormatter(new ReallySimpleFormatter());
44 // if (new Boolean(System.getProperty("debugging"))) {
45 // hand.setLevel(Level.FINE);
46 // logger.setLevel(Level.FINE);
48 // hand.setLevel(Level.INFO);
49 // logger.setLevel(Level.INFO);
57 public SimpleHTTP(URL inURL
) {
64 * @throws MalformedURLException
66 public SimpleHTTP(String strURL
) throws MalformedURLException
{
67 init(new URL(strURL
));
74 private void init(URL initURL
) {
92 private URLConnection
getConnection(Map
<String
, String
> headers
)
94 if (headers
== null) {
95 headers
= new HashMap
<String
, String
>();
98 conn
= url
.openConnection();
99 for (Entry
<String
, String
> e
: headers
.entrySet()) {
100 conn
.addRequestProperty(e
.getKey(), e
.getValue());
110 * @throws IOException
112 public BufferedOutputStream
getWriter(Map
<String
, String
> headers
)
115 connection
= getConnection(headers
);
116 connection
.setDoOutput(true);
117 outBS
= new BufferedOutputStream(connection
.getOutputStream());
118 } catch (UnsupportedEncodingException e
) {
119 // Cannot happen, UTF-8 is correct.
120 } catch (IOException e
) {
121 throw new IOException("Cannot read from the HTTP command results.",
128 * Provides BufferedInputStream pointing to the URL.
132 * @throws IOException
134 public BufferedInputStream
getInputStream(Map
<String
, String
> headers
)
137 connection
= getConnection(headers
);
138 inBS
= new BufferedInputStream(connection
.getInputStream());
139 } catch (IOException e
) {
140 System
.err
.println("Cannot create BufferedInputStream for URL "
149 * reads whole stream into string
152 * incoming byte stream
153 * @return String in UTF-8 encoding
154 * @throws IOException
156 String
suck(BufferedInputStream in
) throws IOException
{
157 StringBuffer inBuf
= new StringBuffer();
158 InputStreamReader inR
= null;
162 inR
= new InputStreamReader(in
, "UTF-8");
163 } catch (UnsupportedEncodingException e
) {
164 // Cannot happen, UTF-8 is always supported
166 char[] cbuf
= new char[BUFMAX
];
168 while ((count
= inR
.read(cbuf
)) != -1) {
169 inBuf
.append(cbuf
, 0, count
);
172 return inBuf
.toString();
179 * @throws IOException
181 public String
get(Map
<String
, String
> headers
) throws IOException
{
182 BufferedInputStream inCh
;
184 if (headers
== null) {
185 headers
= new HashMap
<String
, String
>();
189 inCh
= getInputStream(headers
);
190 } catch (IOException e
) {
191 throw new IOException("Cannot create Reader for URL "
192 + url
.toString(), e
);
201 * @throws IOException
203 public String
get() throws IOException
{
211 * @param mime String with MIME type of data in {@literal inStream}
212 * @return BufferedInputStream with the data returned from the server
213 * @throws IOException
215 @SuppressWarnings("unused")
216 public BufferedInputStream
post(BufferedInputStream inStream
,
217 Map
<String
, String
> headers
, String mime
) throws IOException
{
219 BufferedOutputStream outCh
;
220 BufferedInputStream inCh
;
223 byte[] inBuf
= new byte[BUFMAX
];
224 String mimeType
= "";
227 mimeType
= "application/octet-stream";
232 if (headers
== null) {
233 headers
= new HashMap
<String
, String
>();
237 headers
.put("Content-Type", mimeType
);
238 headers
.put("Content-Length", Long
.toString(inLen
));
242 outCh
= getWriter(headers
);
243 while ((c
= inStream
.read(inBuf
)) != -1) {
248 } catch (IOException e
) {
249 throw new IOException("Cannot POST to URL " + url
.toString(), e
);
253 inCh
= getInputStream(null);
254 } catch (IOException e
) {
255 throw new IOException("Cannot create Reader for URL "
256 + url
.toString(), e
);
263 * The main POST method for the object, expected to be used most. Both
264 * accepts data as String and returnes back String as a result.
267 * String with data to be sent in the body of POST
269 * Map with pairs of String ... name of the header and the value
271 * @return String with the result of the calling POST
272 * @throws IOException
274 public String
post(String data
, Map
<String
, String
> headers
)
277 return suck(post(new BufferedInputStream(new ByteArrayInputStream(
278 data
.getBytes("UTF-8"))), headers
,
279 "application/x-www-form-urlencoded"));
287 * @throws IOException
289 public String
post(Map
<String
, String
> inDict
, Map
<String
, String
> headers
)
293 for (Entry
<String
, String
> e
: inDict
.entrySet()) {
295 strData
+= URLEncoder
.encode(e
.getKey(), "UTF-8") + "="
296 + URLEncoder
.encode(e
.getValue(), "UTF-8") + "&";
297 } catch (UnsupportedEncodingException e1
) {
298 // Never happens, UTF-8 is always supported
301 if (strData
.charAt(strData
.length() - 1) == '&') {
302 strData
= strData
.substring(0, strData
.length() - 1);
304 return post(strData
, headers
);
311 * @throws IOException
313 public String
post(String inData
) throws IOException
{
314 return post(inData
, null);
321 * @throws IOException
323 public String
post(Map
<String
, String
> inDict
) throws IOException
{
324 return post(inDict
, null);