View Javadoc

1   package com.atlassian.plugins.rest.module.jersey;
2   
3   import java.io.IOException;
4   import java.nio.charset.Charset;
5   import java.nio.charset.StandardCharsets;
6   import java.util.List;
7   import java.util.Map;
8   
9   import javax.ws.rs.core.HttpHeaders;
10  import javax.ws.rs.core.MediaType;
11  
12  import com.atlassian.plugin.Plugin;
13  import com.atlassian.sal.api.net.Request;
14  import com.atlassian.sal.api.net.RequestFilePart;
15  import com.atlassian.sal.api.net.Response;
16  import com.atlassian.sal.api.net.ResponseException;
17  import com.atlassian.sal.api.net.ResponseHandler;
18  import com.atlassian.sal.api.net.ReturningResponseHandler;
19  import com.google.common.base.Preconditions;
20  
21  public class JerseyRequest implements Request<JerseyRequest, JerseyResponse> {
22      private final Request delegateRequest;
23      private final JerseyEntityHandler jerseyEntityHandler;
24      private final Plugin plugin;
25  
26      private Object entity;
27  
28      public JerseyRequest(final Request delegateRequest, final JerseyEntityHandler jerseyEntityHandler, Plugin plugin) {
29          this.delegateRequest = delegateRequest;
30          this.jerseyEntityHandler = jerseyEntityHandler;
31          this.plugin = plugin;
32      }
33  
34  
35      public JerseyRequest addBasicAuthentication(String hostname, String username, String password) {
36          delegateRequest.addBasicAuthentication(hostname, username, password);
37          return this;
38      }
39  
40  
41      @Override
42      public JerseyRequest setEntity(final Object entity) {
43          this.entity = Preconditions.checkNotNull(entity);
44          return this;
45      }
46  
47      @Override
48      public JerseyRequest setConnectionTimeout(final int i) {
49          delegateRequest.setConnectionTimeout(i);
50          return this;
51      }
52  
53      @Override
54      public JerseyRequest setSoTimeout(final int i) {
55          delegateRequest.setSoTimeout(i);
56          return this;
57      }
58  
59      @Override
60      public JerseyRequest setUrl(final String s) {
61          delegateRequest.setUrl(s);
62          return this;
63      }
64  
65      @Override
66      public JerseyRequest setRequestBody(final String s) {
67          delegateRequest.setRequestBody(s);
68          return this;
69      }
70  
71      public JerseyRequest setRequestBody(String requestBody, String contentType) {
72          delegateRequest.setRequestBody(requestBody, contentType);
73          return this;
74      }
75  
76      @Override
77      public JerseyRequest setFiles(List<RequestFilePart> files) {
78          delegateRequest.setFiles(files);
79          return this;
80      }
81  
82      @Override
83      public JerseyRequest addRequestParameters(final String... strings) {
84          delegateRequest.addRequestParameters(strings);
85          return this;
86      }
87  
88      @Override
89      public JerseyRequest addHeader(final String s, final String s1) {
90          delegateRequest.addHeader(s, s1);
91          return this;
92      }
93  
94      @Override
95      public JerseyRequest setHeader(final String s, final String s1) {
96          delegateRequest.setHeader(s, s1);
97          return this;
98      }
99  
100     @Override
101     public JerseyRequest setFollowRedirects(final boolean follow) {
102         delegateRequest.setFollowRedirects(follow);
103         return this;
104     }
105 
106     @Override
107     public Map<String, List<String>> getHeaders() {
108         return delegateRequest.getHeaders();
109     }
110 
111     @Override
112     public void execute(final ResponseHandler<? super JerseyResponse> responseHandler) throws ResponseException {
113         executeAndReturn(new ReturningResponseHandler<JerseyResponse, Void>() {
114             public Void handle(JerseyResponse jerseyResponse) throws ResponseException {
115                 responseHandler.handle(jerseyResponse);
116                 return null;
117             }
118         });
119     }
120 
121     @Override
122     public String execute()
123             throws ResponseException {
124         marshallEntity();
125         return delegateRequest.execute();
126     }
127 
128     @Override
129     public <RET> RET executeAndReturn(final ReturningResponseHandler<? super JerseyResponse, RET> responseHandler) throws ResponseException {
130         marshallEntity();
131         final Object result = delegateRequest.executeAndReturn(new ReturningResponseHandler<Response, RET>() {
132             public RET handle(final Response response) throws ResponseException {
133                 JerseyResponse res = new JerseyResponse(response, jerseyEntityHandler, plugin);
134                 return responseHandler.handle(res);
135             }
136         });
137 
138         return (RET) result;
139     }
140 
141     private void marshallEntity() throws EntityConversionException {
142         if (entity != null) {
143             String contentType = getOrSetSingleHeaderValue(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML);
144             getOrSetSingleHeaderValue(HttpHeaders.ACCEPT, contentType);
145             Charset charset = StandardCharsets.UTF_8;
146             MediaType type;
147             try {
148                 type = MediaType.valueOf(contentType);
149             } catch (IllegalArgumentException e) {
150                 throw new UnsupportedContentTypeException(e.getMessage(), e);
151             }
152 
153             try {
154                 String body = jerseyEntityHandler.marshall(entity, type, charset);
155                 setRequestBody(body, contentType);
156             } catch (IOException e) {
157                 throw new EntityConversionException(e);
158             }
159         }
160     }
161 
162     /**
163      * Retrieve the specified header value, or set it to the supplied defaultValue if the header is currently unset.
164      */
165     private String getOrSetSingleHeaderValue(final String headerName, final String defaultValue) {
166         String value = defaultValue;
167         List<String> headers = getHeaders().get(headerName);
168         if (headers != null && !headers.isEmpty()) {
169             if (headers.size() == 1) {
170                 value = headers.get(0);
171             }
172         } else {
173             setHeader(headerName, defaultValue);
174         }
175         return value;
176     }
177 
178 }