View Javadoc

1   package com.atlassian.sal.core.net;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.util.Map;
6   import java.util.HashMap;
7   
8   import org.apache.commons.httpclient.HttpMethod;
9   import org.apache.commons.httpclient.Header;
10  import com.atlassian.sal.api.net.ResponseException;
11  import com.atlassian.sal.api.net.Response;
12  
13  public class HttpClientResponse implements Response
14  {
15  
16      private final HttpMethod method;
17  
18      public HttpClientResponse(HttpMethod method)
19      {
20          this.method = method;
21      }
22  
23      public String getResponseBodyAsString() throws ResponseException
24      {
25          try
26          {
27              return method.getResponseBodyAsString();
28          } catch (IOException e)
29          {
30              throw new ResponseException(e.getMessage(), e);
31          }
32      }
33  
34      public InputStream getResponseBodyAsStream() throws ResponseException
35      {
36          try
37          {
38              return method.getResponseBodyAsStream();
39          } catch (IOException e)
40          {
41              throw new ResponseException(e.getMessage(), e);
42          }
43      }
44  
45      public <T> T getEntity(Class<T> entityClass) throws ResponseException
46      {
47          throw new UnsupportedOperationException("This SAL request does not support object marshalling. Use the RequestFactory component instead.");
48      }
49  
50      public int getStatusCode()
51      {
52          return method.getStatusCode();
53      }
54  
55      public String getStatusText()
56      {
57          return method.getStatusText();
58      }
59  
60      public boolean isSuccessful()
61      {
62          int codeOrder = method.getStatusCode() / 100;
63          return codeOrder == 2 || codeOrder ==  3;
64      }
65  
66      public String getHeader(String name)
67      {
68          final Header header = method.getResponseHeader(name);
69          return header == null ? null : header.getValue();
70      }
71  
72      public Map<String, String> getHeaders()
73      {
74          Map<String, String> map = new HashMap<String, String>();
75          for (Header header : method.getResponseHeaders())
76          {
77              map.put(header.getName(), header.getValue());
78          }
79          return map;
80      }
81  }