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
.HttpURLConnection
;
16 import java
.net
.MalformedURLException
;
18 import java
.net
.URLConnection
;
19 import java
.net
.URLEncoder
;
20 import java
.util
.HashMap
;
22 import java
.util
.Map
.Entry
;
24 public class SimpleHTTP
{
26 private URL url
= null;
27 private BufferedInputStream inBS
= null;
28 private BufferedOutputStream outBS
= null;
29 // private ConsoleHandler hand = new ConsoleHandler();
30 private URLConnection conn
= null;
31 private int responseCode
= 0;
33 final static int BUFMAX
= 1024;
35 // private static Logger logger =
36 // Logger.getLogger(SimpleHTTP.class.getName());
42 // logger.addHandler(hand);
43 // hand.setFormatter(new ReallySimpleFormatter());
45 // if (new Boolean(System.getProperty("debugging"))) {
46 // hand.setLevel(Level.FINE);
47 // logger.setLevel(Level.FINE);
49 // hand.setLevel(Level.INFO);
50 // logger.setLevel(Level.INFO);
58 public SimpleHTTP(URL inURL
) {
65 * @throws MalformedURLException
67 public SimpleHTTP(String strURL
) throws MalformedURLException
{
68 init(new URL(strURL
));
75 private void init(URL initURL
) {
81 * the responseCode to set
83 protected void setResponseCode(int responseCode
) {
84 this.responseCode
= responseCode
;
88 * @return the responseCode
90 public int getResponseCode() {
106 * @throws IOException
108 private URLConnection
getConnection(Map
<String
, String
> headers
)
110 if (headers
== null) {
111 headers
= new HashMap
<String
, String
>();
114 conn
= url
.openConnection();
115 for (Entry
<String
, String
> e
: headers
.entrySet()) {
116 conn
.addRequestProperty(e
.getKey(), e
.getValue());
123 * Provides {@link BufferedOutputStream} for writing (via POST method) to
127 * @return BufferedOutputStream for current URL
128 * @throws IOException
129 * @see getResponseCode
131 * When the stream is later closed, {@literal getResponseCode} returns
132 * response code of the executed HTTP request (if our current URL is
135 public BufferedOutputStream
getWriter(Map
<String
, String
> headers
)
138 conn
= getConnection(headers
);
139 conn
.setDoOutput(true);
140 outBS
= new BufferedOutputStream(conn
.getOutputStream());
141 } catch (UnsupportedEncodingException e
) {
142 // Cannot happen, UTF-8 is correct.
143 } catch (IOException e
) {
144 throw new IOException("Cannot read from the HTTP command results.",
151 * Provides {@link BufferedInputStream} pointing to the URL.
154 * @return BufferedInputStream for reading the remote content from URL
155 * @throws IOException
156 * @see getResponseCode
158 * When the stream is later closed, {@literal getResponseCode} returns
159 * response code of the executed HTTP request (if our current URL is
162 public BufferedInputStream
getInputStream(Map
<String
, String
> headers
)
165 conn
= getConnection(headers
);
166 inBS
= new BufferedInputStream(conn
.getInputStream());
167 } catch (IOException e
) {
168 System
.err
.println("Cannot create BufferedInputStream for URL "
177 * reads whole stream into string
180 * incoming byte stream
181 * @return String in UTF-8 encoding
182 * @throws IOException
184 String
suck(BufferedInputStream in
) throws IOException
{
185 StringBuffer inBuf
= new StringBuffer();
186 InputStreamReader inR
= null;
190 inR
= new InputStreamReader(in
, "UTF-8");
191 } catch (UnsupportedEncodingException e
) {
192 // Cannot happen, UTF-8 is always supported
194 char[] cbuf
= new char[BUFMAX
];
196 while ((count
= inR
.read(cbuf
)) != -1) {
197 inBuf
.append(cbuf
, 0, count
);
201 if (conn
instanceof HttpURLConnection
) {
202 setResponseCode(((HttpURLConnection
) this.conn
).getResponseCode());
205 return inBuf
.toString();
209 * Convenience method for getting {@link String} from the URL with
210 * possibility to set request headers.
213 * Map<String,String> with the name, value pairs of HTTP request
215 * @return String with the remote content
216 * @throws IOException
218 public String
get(Map
<String
, String
> headers
) throws IOException
{
219 BufferedInputStream inCh
;
221 if (headers
== null) {
222 headers
= new HashMap
<String
, String
>();
226 inCh
= getInputStream(headers
);
227 } catch (IOException e
) {
228 throw new IOException("Cannot create Reader for URL "
229 + url
.toString(), e
);
236 * The most simple convenience method for getting {@link String} from the
239 * @return String with the remote content
240 * @throws IOException
242 public String
get() throws IOException
{
251 * String with MIME type of data in {@literal inStream}
252 * @return BufferedInputStream with the data returned from the server
253 * @throws IOException
255 @SuppressWarnings("unused")
256 public BufferedInputStream
post(BufferedInputStream inStream
,
257 Map
<String
, String
> headers
, String mime
) throws IOException
{
259 BufferedOutputStream outCh
;
260 BufferedInputStream inCh
;
263 byte[] inBuf
= new byte[BUFMAX
];
264 String mimeType
= "";
267 mimeType
= "application/octet-stream";
272 if (headers
== null) {
273 headers
= new HashMap
<String
, String
>();
277 headers
.put("Content-Type", mimeType
);
278 headers
.put("Content-Length", Long
.toString(inLen
));
282 outCh
= getWriter(headers
);
283 while ((c
= inStream
.read(inBuf
)) != -1) {
288 } catch (IOException e
) {
289 throw new IOException("Cannot POST to URL " + url
.toString(), e
);
292 if (conn
instanceof HttpURLConnection
) {
293 setResponseCode(((HttpURLConnection
) this.conn
).getResponseCode());
297 inCh
= getInputStream(null);
298 } catch (IOException e
) {
299 throw new IOException("Cannot create Reader for URL "
300 + url
.toString(), e
);
307 * The main POST method for the object, expected to be used most. Both
308 * accepts data as String and returnes back String as a result.
311 * String with data to be sent in the body of POST
313 * Map with pairs of String ... name of the header and the value
315 * @return String with the result of the calling POST
316 * @throws IOException
318 public String
post(String data
, Map
<String
, String
> headers
)
321 return suck(post(new BufferedInputStream(new ByteArrayInputStream(data
322 .getBytes("UTF-8"))), headers
,
323 "application/x-www-form-urlencoded"));
331 * @throws IOException
333 public String
post(Map
<String
, String
> inDict
, Map
<String
, String
> headers
)
337 for (Entry
<String
, String
> e
: inDict
.entrySet()) {
339 strData
+= URLEncoder
.encode(e
.getKey(), "UTF-8") + "="
340 + URLEncoder
.encode(e
.getValue(), "UTF-8") + "&";
341 } catch (UnsupportedEncodingException e1
) {
342 // Never happens, UTF-8 is always supported
345 if (strData
.charAt(strData
.length() - 1) == '&') {
346 strData
= strData
.substring(0, strData
.length() - 1);
348 return post(strData
, headers
);
355 * @throws IOException
357 public String
post(String inData
) throws IOException
{
358 return post(inData
, null);
365 * @throws IOException
367 public String
post(Map
<String
, String
> inDict
) throws IOException
{
368 return post(inDict
, null);