View Javadoc

1   package com.atlassian.plugins.rest.module.jersey;
2   
3   import com.atlassian.plugin.Plugin;
4   import com.atlassian.plugins.rest.module.ChainingClassLoader;
5   import com.atlassian.sal.api.net.Response;
6   import com.atlassian.sal.api.net.ResponseException;
7   
8   import java.io.IOException;
9   import java.io.InputStream;
10  import java.util.Collections;
11  import java.util.HashMap;
12  import java.util.List;
13  import java.util.Map;
14  import javax.ws.rs.core.HttpHeaders;
15  import javax.ws.rs.core.MediaType;
16  
17  public class JerseyResponse implements Response {
18      private final Response delegateResponse;
19      private final JerseyEntityHandler jerseyEntityHandler;
20      private final Plugin plugin;
21  
22      public JerseyResponse(Response delegateResponse, JerseyEntityHandler jerseyEntityHandler, Plugin plugin) {
23          this.delegateResponse = delegateResponse;
24          this.jerseyEntityHandler = jerseyEntityHandler;
25          this.plugin = plugin;
26      }
27  
28      public <T> T getEntity(final Class<T> entityClass) throws ResponseException {
29          Map<String, String> headers = getHeaders();
30          Map<String, List<String>> headerListMap = new HashMap<String, List<String>>();
31          for (final Map.Entry<String, String> entry : headers.entrySet()) {
32              headerListMap.put(entry.getKey(), Collections.singletonList(entry.getValue()));
33          }
34          String contentType = HeaderHelper.getSingleHeaderValue(headerListMap, HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML);
35          InputStream entityStream = getResponseBodyAsStream();
36  
37          ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
38          ChainingClassLoader chainingClassLoader = getChainingClassLoader(plugin);
39          try {
40              Thread.currentThread().setContextClassLoader(chainingClassLoader);
41              return jerseyEntityHandler.unmarshall(entityClass, MediaType.valueOf(contentType), entityStream, headerListMap);
42          } catch (IOException e) {
43              throw new EntityConversionException(e);
44          } finally {
45              Thread.currentThread().setContextClassLoader(oldClassLoader);
46          }
47      }
48  
49      public int getStatusCode() {
50          return delegateResponse.getStatusCode();
51      }
52  
53      public String getResponseBodyAsString()
54              throws ResponseException {
55          return delegateResponse.getResponseBodyAsString();
56      }
57  
58      public InputStream getResponseBodyAsStream()
59              throws ResponseException {
60          return delegateResponse.getResponseBodyAsStream();
61      }
62  
63      public String getStatusText() {
64          return delegateResponse.getStatusText();
65      }
66  
67      public boolean isSuccessful() {
68          return delegateResponse.isSuccessful();
69      }
70  
71      public String getHeader(final String s) {
72          return delegateResponse.getHeader(s);
73      }
74  
75      public Map<String, String> getHeaders() {
76          return delegateResponse.getHeaders();
77      }
78  
79      private ChainingClassLoader getChainingClassLoader(final Plugin plugin) {
80          return new ChainingClassLoader(getClass().getClassLoader(), plugin.getClassLoader());
81      }
82  }