Code formating and whitespace cleanup
[jpim.git] / src / de / j32 / httplib / HttpRequest.java
1 package de.j32.httplib;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.UnsupportedEncodingException;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.net.URLEncoder;
10
11 public abstract class HttpRequest
12 {
13     StringBuffer url_;
14     StringBuffer query_ = new StringBuffer();
15     String method_;
16     ByteArrayOutputStream body_ = new ByteArrayOutputStream();
17     String contentType_;
18
19     public HttpRequest(String url, String method)
20     {
21         url_ = new StringBuffer(url);
22         method_ = method;
23     }
24
25     protected StringBuffer query()
26     {
27         return query_;
28     }
29
30     protected ByteArrayOutputStream body()
31     {
32         return body_;
33     }
34
35     protected void setContentType(String c)
36     {
37         contentType_ = c;
38     }
39
40     protected static void appendParameter(Appendable buffer, boolean first,
41             String name, byte[] value)
42     {
43         try {
44             if (!first) buffer.append("&");
45             buffer.append(URLEncoder.encode(name, "utf-8"));
46             buffer.append("=");
47             // We really would need a URLEncoder for byte[] (pre-encoded or raw
48             // date)
49             buffer.append(URLEncoder
50                     .encode(new String(value, "ascii"), "ascii"));
51         }
52         catch (UnsupportedEncodingException e) {
53             throw new AssertionError("Missing encoding");
54         }
55         catch (IOException e) {
56             throw new AssertionError("IOException on buffer-based Appendable");
57         }
58     }
59
60     public HttpResponse execute() throws MalformedURLException, IOException
61     {
62         if (query_.length() > 0) url_.append("?");
63         url_.append(query_);
64         HttpURLConnection connection = (HttpURLConnection) new URL(new String(
65                 url_)).openConnection();
66         connection.setRequestMethod(method_);
67         if (contentType_ != null) {
68             connection.setRequestProperty("Content-Type", contentType_);
69             connection.setDoOutput(true);
70             connection.setFixedLengthStreamingMode(body_.size());
71             connection.getOutputStream().write(body_.toByteArray());
72         }
73         if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) throw new IOException(
74                 "HTTP request failed: " + connection.getResponseCode() + " "
75                         + connection.getResponseMessage());
76         return new HttpResponse(connection);
77     }
78
79     public HttpRequest addParameter(String name, String value)
80             throws UnsupportedEncodingException
81     {
82         return addParameter(name, value, "utf-8");
83     }
84
85     public HttpRequest addParameter(String name, String value, String encoding)
86             throws UnsupportedEncodingException
87     {
88         return addParameter(name, value.getBytes(encoding), encoding);
89     }
90
91     abstract public HttpRequest addParameter(String name, byte[] value,
92             String encoding);
93 }