View Javadoc

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