Debian Codegraph @

signpost-commonshttp4/src/main/java/oauth/signpost/commonshttp/HttpRequestAdapter.java

raw
1 package oauth.signpost.commonshttp;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.apache.http.Header;
9 import org.apache.http.HttpEntity;
10 import org.apache.http.HttpEntityEnclosingRequest;
11 import org.apache.http.client.methods.HttpUriRequest;
12
13 public class HttpRequestAdapter implements oauth.signpost.http.HttpRequest {
14
15 private HttpUriRequest request;
16
17 private HttpEntity entity;
18
19 public HttpRequestAdapter(org.apache.http.client.methods.HttpUriRequest request) {
20 this.request = request;
21 if (request instanceof HttpEntityEnclosingRequest) {
22 entity = ((HttpEntityEnclosingRequest) request).getEntity();
23 }
24 }
25
26 public String getMethod() {
27 return request.getRequestLine().getMethod();
28 }
29
30 public String getRequestUrl() {
31 return request.getURI().toString();
32 }
33
34 public void setRequestUrl(String url) {
35 throw new RuntimeException(new UnsupportedOperationException());
36 }
37
38 public String getHeader(String name) {
39 Header header = request.getFirstHeader(name);
40 if (header == null) {
41 return null;
42 }
43 return header.getValue();
44 }
45
46 public void setHeader(String name, String value) {
47 request.setHeader(name, value);
48 }
49
50 public Map<String, String> getAllHeaders() {
51 Header[] origHeaders = request.getAllHeaders();
52 HashMap<String, String> headers = new HashMap<String, String>();
53 for (Header h : origHeaders) {
54 headers.put(h.getName(), h.getValue());
55 }
56 return headers;
57 }
58
59 public String getContentType() {
60 if (entity == null) {
61 return null;
62 }
63 Header header = entity.getContentType();
64 if (header == null) {
65 return null;
66 }
67 return header.getValue();
68 }
69
70 public InputStream getMessagePayload() throws IOException {
71 if (entity == null) {
72 return null;
73 }
74 return entity.getContent();
75 }
76
77 public Object unwrap() {
78 return request;
79 }
80 }