View Javadoc

1   /*
2    * Copyright (C) 2010 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.atlassian.jira.rest.client.internal.jersey;
18  
19  import com.atlassian.jira.rest.client.AuthenticationHandler;
20  import com.atlassian.jira.rest.client.ComponentRestClient;
21  import com.atlassian.jira.rest.client.IssueRestClient;
22  import com.atlassian.jira.rest.client.JiraRestClient;
23  import com.atlassian.jira.rest.client.MetadataRestClient;
24  import com.atlassian.jira.rest.client.ProjectRestClient;
25  import com.atlassian.jira.rest.client.ProjectRolesRestClient;
26  import com.atlassian.jira.rest.client.SearchRestClient;
27  import com.atlassian.jira.rest.client.SessionRestClient;
28  import com.atlassian.jira.rest.client.UserRestClient;
29  import com.atlassian.jira.rest.client.VersionRestClient;
30  import com.sun.jersey.api.client.AsyncViewResource;
31  import com.sun.jersey.api.client.AsyncWebResource;
32  import com.sun.jersey.api.client.ViewResource;
33  import com.sun.jersey.api.client.WebResource;
34  import com.sun.jersey.client.apache.ApacheHttpClient;
35  import com.sun.jersey.client.apache.ApacheHttpClientHandler;
36  import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;
37  import org.apache.commons.httpclient.HttpClient;
38  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
39  
40  import javax.ws.rs.core.UriBuilder;
41  import java.net.URI;
42  
43  /**
44   * Jersey-based implementation of JIRA REST client.
45   *
46   * @since v0.1
47   */
48  public class JerseyJiraRestClient implements JiraRestClient {
49  
50      private final URI baseUri;
51      private final IssueRestClient issueRestClient;
52      private final SessionRestClient sessionRestClient;
53  	private final UserRestClient userRestClient;
54  	private final ProjectRestClient projectRestClient;
55  	private final ComponentRestClient componentRestClient;
56  	private final MetadataRestClient metadataRestClient;
57  	private final SearchRestClient searchRestClient;
58  	private final VersionRestClient versionRestClient;
59  	private final ProjectRolesRestClient projectRolesRestClient;
60  
61  
62  	public JerseyJiraRestClient(final URI serverUri, final AuthenticationHandler authenticationHandler) {
63          this.baseUri = UriBuilder.fromUri(serverUri).path("/rest/api/latest").build();
64          DefaultApacheHttpClientConfig config = new DefaultApacheHttpClientConfig();
65          authenticationHandler.configure(config);
66          final ApacheHttpClient client = new ApacheHttpClient(createDefaultClientHander(config)) {
67              @Override
68              public WebResource resource(URI u) {
69                  final WebResource resource = super.resource(u);
70                  authenticationHandler.configure(resource, this);
71                  return resource;
72              }
73  
74              @Override
75              public AsyncWebResource asyncResource(URI u) {
76                  final AsyncWebResource resource = super.asyncResource(u);
77                  authenticationHandler.configure(resource, this);
78                  return resource;
79              }
80  
81              @Override
82              public ViewResource viewResource(URI u) {
83                  final ViewResource resource = super.viewResource(u);
84                  authenticationHandler.configure(resource, this);
85                  return resource;
86              }
87  
88              @Override
89              public AsyncViewResource asyncViewResource(URI u) {
90                  final AsyncViewResource resource = super.asyncViewResource(u);
91                  authenticationHandler.configure(resource, this);
92                  return resource;
93              }
94          };
95  		metadataRestClient = new JerseyMetadataRestClient(baseUri, client);
96          sessionRestClient = new JerseySessionRestClient(client, serverUri);
97  		issueRestClient = new JerseyIssueRestClient(baseUri, client, sessionRestClient, metadataRestClient);
98  		userRestClient = new JerseyUserRestClient(baseUri, client);
99  		projectRestClient = new JerseyProjectRestClient(baseUri, client);
100 		componentRestClient = new JerseyComponentRestClient(baseUri, client);
101 		searchRestClient = new JerseySearchRestClient(baseUri, client);
102 		versionRestClient = new JerseyVersionRestClient(baseUri, client);
103 		projectRolesRestClient = new JerseyProjectRolesRestClient(baseUri, client, serverUri);
104     }
105 
106     @Override
107     public IssueRestClient getIssueClient() {
108         return issueRestClient;
109     }
110 
111     @Override
112     public SessionRestClient getSessionClient() {
113         return sessionRestClient;
114     }
115 
116 	@Override
117 	public UserRestClient getUserClient() {
118 		return userRestClient;
119 	}
120 
121 	@Override
122 	public ProjectRestClient getProjectClient() {
123 		return projectRestClient;
124 	}
125 
126 	@Override
127 	public ComponentRestClient getComponentClient() {
128 		return componentRestClient;
129 	}
130 
131 	@Override
132 	public MetadataRestClient getMetadataClient() {
133 		return metadataRestClient;
134 	}
135 
136 	@Override
137 	public SearchRestClient getSearchClient() {
138 		return searchRestClient;
139 	}
140 
141 	@Override
142 	public VersionRestClient getVersionRestClient() {
143 		return versionRestClient;
144 	}
145 
146 	@Override
147 	public ProjectRolesRestClient getProjectRolesRestClient() {
148 		return projectRolesRestClient;
149 	}
150 
151 	private static ApacheHttpClientHandler createDefaultClientHander(DefaultApacheHttpClientConfig config) {
152         final HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
153         return new ApacheHttpClientHandler(client, config);
154     }
155 
156 }
157