PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 36ms app.codeStats 0ms

/signpost-jetty6/src/main/java/oauth/signpost/jetty/HttpRequestAdapter.java

http://oauth-signpost.googlecode.com/
Java | 61 lines | 44 code | 14 blank | 3 comment | 2 complexity | 77710ccb30b0af32e2af01b6f079a311 MD5 | raw file
 1package oauth.signpost.jetty;
 2
 3import java.io.ByteArrayInputStream;
 4import java.io.IOException;
 5import java.io.InputStream;
 6
 7import oauth.signpost.http.HttpRequest;
 8
 9import org.mortbay.jetty.HttpFields;
10import org.mortbay.jetty.client.HttpExchange;
11
12public class HttpRequestAdapter implements HttpRequest {
13
14    private HttpExchange request;
15
16    private String requestUrl;
17
18    public HttpRequestAdapter(HttpExchange request) {
19        this.request = request;
20        buildRequestUrl();
21    }
22
23    public String getContentType() {
24        HttpFields fields = request.getRequestFields();
25        return fields.getStringField("Content-Type");
26    }
27
28    public String getHeader(String name) {
29        HttpFields fields = request.getRequestFields();
30        return fields.getStringField(name);
31    }
32
33    public InputStream getMessagePayload() throws IOException {
34        return new ByteArrayInputStream(request.getRequestContent().array());
35    }
36
37    public String getMethod() {
38        return request.getMethod();
39    }
40
41    public String getRequestUrl() {
42        return requestUrl;
43    }
44
45    public void setHeader(String name, String value) {
46        request.setRequestHeader(name, value);
47    }
48
49    // Jetty has some very weird mechanism for handling URLs... we have to
50    // reconstruct it here.
51    private void buildRequestUrl() {
52        StringBuilder sb = new StringBuilder();
53        sb.append(request.getScheme() + "://");
54        sb.append(request.getAddress().toString().replaceAll(":\\d+", ""));
55        if (request.getURI() != null) {
56            // the "URI" in Jetty is actually the path... WTF?!
57            sb.append(request.getURI());
58        }
59        this.requestUrl = sb.toString();
60    }
61}