View Javadoc

1   package com.atlassian.rest.jersey.client;
2   
3   import com.sun.jersey.api.client.Client;
4   import com.sun.jersey.api.client.WebResource;
5   
6   import java.net.URI;
7   import javax.ws.rs.core.UriBuilder;
8   
9   /**
10   * Gets {@link WebResource web resources} for testing.
11   */
12  public final class WebResourceFactory
13  {
14      public static final String LATEST = "latest";
15      public static final String REST_VERSION = System.getProperty("refimpl.rest.version", "1");
16      public static final String REST_VERSION_2 = System.getProperty("refimpl2.rest.version", "2");
17  
18      private static final String OS_USERNAME_QUERY_PARAM = "os_username";
19      private static final String OS_PASSWORD_QUERY_PARAM = "os_password";
20  
21      private static final String ADMIN_USERNAME = "admin";
22      private static final String ADMIN_PASSWORD = "admin";
23      private static final String PORT = System.getProperty("http.port", "5990");
24      private static final String CONTEXT = System.getProperty("context.path", "/refapp");
25  
26      static
27      {
28          System.out.println("Base URI (latest) is <" + getWebResourceUri(LATEST) + ">");
29          System.out.println("Base URI (version 1) is <" + getWebResourceUri(REST_VERSION) + ">");
30          System.out.println("Base URI (version 2) is <" + getWebResourceUri(REST_VERSION_2) + ">");
31      }
32  
33      public static WebResource authenticated(String version)
34      {
35          return authenticate(anonymous(version), ADMIN_USERNAME, ADMIN_PASSWORD);
36      }
37  
38      public static WebResource authenticated()
39      {
40          return authenticated(REST_VERSION);
41      }
42  
43      public static WebResource anonymous(String version)
44      {
45          return Client.create().resource(getWebResourceUri(version));
46      }
47  
48      public static WebResource anonymous()
49      {
50          return anonymous(REST_VERSION);
51      }
52  
53      public static UriBuilder getUriBuilder()
54      {
55          return UriBuilder.fromUri("http://localhost/").port(Integer.parseInt(PORT)).path(CONTEXT);
56      }
57  
58      private static URI getWebResourceUri(String version)
59      {
60          return getUriBuilder().path("rest").path("refimpl").path(version).build();
61      }
62  
63      public static WebResource authenticate(WebResource webResource, String username, String password)
64      {
65          return webResource.queryParam(OS_USERNAME_QUERY_PARAM, username).queryParam(OS_PASSWORD_QUERY_PARAM, password);
66      }
67  }