View Javadoc

1   package com.atlassian.plugins.rest.sample.entities;
2   
3   import com.atlassian.sal.api.ApplicationProperties;
4   import com.atlassian.sal.api.net.*;
5   
6   import javax.servlet.ServletException;
7   import javax.servlet.http.HttpServlet;
8   import javax.servlet.http.HttpServletRequest;
9   import javax.servlet.http.HttpServletResponse;
10  import javax.ws.rs.core.HttpHeaders;
11  import java.io.IOException;
12  import java.io.PrintWriter;
13  
14  public class EntityClientServlet extends HttpServlet {
15      private final RequestFactory restRequestFactory;
16      private final ApplicationProperties applicationProperties;
17  
18      public static final String P_ORANGE = "orange";
19      public static final String P_CONTENT_TYPE = "contentType";
20      public static final String P_ACCEPT = "accept";
21  
22      public EntityClientServlet(RequestFactory restRequestFactory, ApplicationProperties applicationProperties) {
23          this.restRequestFactory = restRequestFactory;
24          this.applicationProperties = applicationProperties;
25      }
26  
27      @Override
28      protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
29          resp.setContentType("text/plain");
30          final PrintWriter out = resp.getWriter();
31          try {
32  
33              String name = req.getParameter(P_ORANGE);
34              if (name == null) {
35                  out.write("name is a required parameter");
36                  return;
37              }
38  
39              Request restRequest = restRequestFactory.createRequest(Request.MethodType.POST, applicationProperties.getBaseUrl() + "/rest/entity/1/fruit");
40  
41              // leave Content-Type & Accept null if not otherwise specified
42              String contentType = req.getParameter(P_CONTENT_TYPE);
43              if (contentType != null) {
44                  restRequest.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
45              }
46              String[] accepts = req.getParameterValues(P_ACCEPT);
47              if (accepts != null) {
48                  for (String accept : accepts) {
49                      restRequest.addHeader(HttpHeaders.ACCEPT, accept);
50                  }
51              }
52  
53              restRequest.setEntity(new Orange(name));
54  
55              restRequest.execute(new ResponseHandler<Response>() {
56                  public void handle(final Response restResponse) throws ResponseException {
57                      Apple apple = restResponse.getEntity(Apple.class);
58                      out.write(apple.getName() + "\n");
59                      out.write("Content-Type=" + apple.getReqContentType() + "\n");
60                      out.write("Accept=" + apple.getReqAccept());
61                  }
62              });
63              return;
64          } catch (ResponseException e) {
65              out.write(e.toString());
66          } catch (RuntimeException e) {
67              out.write(e.toString());
68              throw e;
69          } finally {
70              out.close();
71          }
72      }
73  
74  }