View Javadoc

1   package com.atlassian.plugins.rest.module.jersey;
2   
3   import com.atlassian.sal.api.net.Request;
4   import com.atlassian.sal.api.net.RequestFilePart;
5   import com.atlassian.sal.api.net.Response;
6   import com.atlassian.sal.api.net.ResponseException;
7   import com.atlassian.sal.api.net.ResponseHandler;
8   import com.atlassian.sal.api.net.ReturningResponseHandler;
9   import com.atlassian.sal.api.net.auth.Authenticator;
10  
11  import javax.ws.rs.core.HttpHeaders;
12  import javax.ws.rs.core.MediaType;
13  import java.io.IOException;
14  import java.nio.charset.Charset;
15  import java.util.List;
16  import java.util.Map;
17  
18  import static org.apache.commons.lang.Validate.notNull;
19  
20  public class JerseyRequest implements Request<JerseyRequest, JerseyResponse>
21  {
22      private final Request delegateRequest;
23      private final JerseyEntityHandler jerseyEntityHandler;
24  
25      private Object entity;
26  
27      public JerseyRequest(final Request delegateRequest, final JerseyEntityHandler jerseyEntityHandler)
28      {
29          this.delegateRequest = delegateRequest;
30          this.jerseyEntityHandler = jerseyEntityHandler;
31      }
32  
33      public JerseyRequest setEntity(final Object entity)
34      {
35          notNull(entity);
36          this.entity = entity;
37          return this;
38      }
39  
40      public JerseyRequest setConnectionTimeout(final int i)
41      {
42          delegateRequest.setConnectionTimeout(i);
43          return this;
44      }
45  
46      public JerseyRequest setSoTimeout(final int i)
47      {
48          delegateRequest.setSoTimeout(i);
49          return this;
50      }
51  
52      public JerseyRequest setUrl(final String s)
53      {
54          delegateRequest.setUrl(s);
55          return this;
56      }
57  
58      public JerseyRequest setRequestBody(final String s)
59      {
60          delegateRequest.setRequestBody(s);
61          return this;
62      }
63  
64      public JerseyRequest setFiles(List<RequestFilePart> files)
65      {
66          delegateRequest.setFiles(files);
67          return this;
68      }
69  
70      public JerseyRequest setRequestContentType(final String s)
71      {
72          delegateRequest.setRequestContentType(s);
73          return this;
74      }
75  
76      public JerseyRequest addRequestParameters(final String... strings)
77      {
78          delegateRequest.addRequestParameters(strings);
79          return this;
80      }
81  
82      public JerseyRequest addAuthentication(final Authenticator authenticator)
83      {
84          delegateRequest.addAuthentication(authenticator);
85          return this;
86      }
87  
88      public JerseyRequest addTrustedTokenAuthentication()
89      {
90          delegateRequest.addTrustedTokenAuthentication();
91          return this;
92      }
93  
94      public JerseyRequest addTrustedTokenAuthentication(final String s)
95      {
96          delegateRequest.addTrustedTokenAuthentication(s);
97          return this;
98      }
99  
100     public JerseyRequest addBasicAuthentication(final String s, final String s1)
101     {
102         delegateRequest.addBasicAuthentication(s, s1);
103         return this;
104     }
105 
106     public JerseyRequest addSeraphAuthentication(final String s, final String s1)
107     {
108         delegateRequest.addSeraphAuthentication(s, s1);
109         return this;
110     }
111 
112     public JerseyRequest addHeader(final String s, final String s1)
113     {
114         delegateRequest.addHeader(s, s1);
115         return this;
116     }
117 
118     public JerseyRequest setHeader(final String s, final String s1)
119     {
120         delegateRequest.setHeader(s, s1);
121         return this;
122     }
123 	
124 	public JerseyRequest setFollowRedirects(final boolean follow)
125     {
126         delegateRequest.setFollowRedirects(follow);
127         return this;
128     }
129 
130     public Map<String, List<String>> getHeaders()
131     {
132         return delegateRequest.getHeaders();
133     }
134 
135     public void execute(final ResponseHandler<JerseyResponse> responseHandler)
136             throws ResponseException
137     {
138         executeAndReturn(new ReturningResponseHandler<JerseyResponse, Void>()
139         {
140             public Void handle(JerseyResponse jerseyResponse) throws ResponseException
141             {
142                 responseHandler.handle(jerseyResponse);
143                 return null;
144             }
145         });
146     }
147 
148     public String execute()
149             throws ResponseException
150     {
151         marshallEntity();
152         return delegateRequest.execute();
153     }
154 
155     public <RET> RET executeAndReturn(final ReturningResponseHandler<JerseyResponse, RET> responseHandler) throws ResponseException
156     {
157         marshallEntity();
158         final Object result = delegateRequest.executeAndReturn(new ReturningResponseHandler<Response, RET>()
159         {
160             public RET handle(final Response response) throws ResponseException
161             {
162                 JerseyResponse res = new JerseyResponse(response, jerseyEntityHandler);
163                 return responseHandler.handle(res);
164             }
165         });
166 
167         return (RET)result;
168     }
169 
170     private void marshallEntity() throws EntityConversionException
171     {
172         if (entity != null)
173         {
174             String contentType = getOrSetSingleHeaderValue(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML);
175             String encoding = getOrSetSingleHeaderValue(HttpHeaders.CONTENT_ENCODING, "UTF-8");
176             getOrSetSingleHeaderValue(HttpHeaders.ACCEPT, contentType);
177             Charset charset = Charset.forName(encoding);
178             MediaType type;
179             try
180             {
181                 type = MediaType.valueOf(contentType);
182             }
183             catch (IllegalArgumentException e)
184             {
185                 throw new UnsupportedContentTypeException(e.getMessage(), e);
186             }
187 
188             try
189             {
190                 String body = jerseyEntityHandler.marshall(entity, type, charset);
191                 setRequestBody(body);
192                 setRequestContentType(contentType);
193             }
194             catch (IOException e)
195             {
196                 throw new EntityConversionException(e);
197             }
198         }
199     }
200 
201     /**
202      * Retrieve the specified header value, or set it to the supplied defaultValue if the header is currently unset.
203      */
204     private String getOrSetSingleHeaderValue(final String headerName, final String defaultValue)
205     {
206         String value = defaultValue;
207         List<String> headers = getHeaders().get(headerName);
208         if (headers != null && !headers.isEmpty())
209         {
210             if (headers.size() == 1)
211             {
212                 value = headers.get(0);
213             }
214         }
215         else
216         {
217             setHeader(headerName, defaultValue);
218         }
219         return value;
220     }
221 
222 }