View Javadoc

1   package com.atlassian.asap.it;
2   
3   import com.atlassian.asap.api.Jwt;
4   import com.atlassian.asap.core.server.AuthenticationContext;
5   import com.atlassian.asap.core.server.jersey.Asap;
6   import com.atlassian.asap.core.server.jersey.AuthenticationRequestFilter;
7   import com.atlassian.asap.core.server.jersey.AuthorizationRequestFilter;
8   import com.atlassian.asap.core.server.jersey.JwtParam;
9   import com.atlassian.asap.core.server.jersey.JwtParamBinder;
10  import com.sun.net.httpserver.HttpHandler;
11  import com.sun.net.httpserver.HttpServer;
12  import org.junit.AfterClass;
13  import org.junit.BeforeClass;
14  
15  import javax.ws.rs.Consumes;
16  import javax.ws.rs.GET;
17  import javax.ws.rs.Path;
18  import javax.ws.rs.Produces;
19  import javax.ws.rs.core.Application;
20  import javax.ws.rs.core.MediaType;
21  import javax.ws.rs.ext.RuntimeDelegate;
22  import java.net.InetSocketAddress;
23  import java.net.URI;
24  import java.util.Set;
25  
26  import static com.google.common.collect.Sets.newHashSet;
27  
28  public class JerseyIntegrationTest extends BaseIntegrationTest {
29      private static final String BASE_URL = "http://localhost:8080/";
30      private static HttpServer httpServer;
31  
32      @Override
33      protected URI getUrlForResourceName(String resourceName) {
34          return URI.create(BASE_URL).resolve(resourceName);
35      }
36  
37      @BeforeClass
38      public static void startHttpServer() throws Exception {
39          Application jerseyApp = new Application() {
40              private final Set<Class<?>> resources = newHashSet(Controller.class);
41              private final Set<Object> singletons = newHashSet(
42                      AuthenticationRequestFilter.newInstance(new AuthenticationContext(AUDIENCE, PUBLIC_KEY_PROVIDER)),
43                      AuthorizationRequestFilter.newInstance(),
44                      new JwtParamBinder());
45  
46              @Override
47              public Set<Class<?>> getClasses() {
48                  return resources;
49              }
50  
51              @Override
52              public Set<Object> getSingletons() {
53                  return singletons;
54              }
55          };
56          // create a new server listening at port 8080
57          httpServer = HttpServer.create(new InetSocketAddress(URI.create(BASE_URL).getPort()), 0);
58  
59          // create a handler wrapping the JAX-RS application
60          HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(jerseyApp, HttpHandler.class);
61  
62          // map JAX-RS handler to the server root
63          httpServer.createContext(URI.create(BASE_URL).getPath(), handler);
64  
65          // start the server
66          httpServer.start();
67      }
68  
69      @AfterClass
70      public static void stopHttpServer() throws Exception {
71          if (httpServer != null) {
72              httpServer.stop(1);
73              httpServer = null;
74          }
75      }
76  
77      @Path("/")
78      @Asap(authorizedSubjects = "issuer1")
79      @Produces({MediaType.APPLICATION_JSON})
80      @Consumes({MediaType.APPLICATION_JSON})
81      public static class Controller {
82  
83          @GET
84          @Path(RESOURCE)
85          public String resourceIssuer1(
86                  @JwtParam Jwt jwt
87          ) {
88              return jwt.getClaims().getIssuer();
89          }
90      }
91  }