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.SearchRestClient;
26  import com.atlassian.jira.rest.client.SessionRestClient;
27  import com.atlassian.jira.rest.client.UserRestClient;
28  import com.atlassian.jira.rest.client.VersionRestClient;
29  import com.sun.jersey.api.client.AsyncViewResource;
30  import com.sun.jersey.api.client.AsyncWebResource;
31  import com.sun.jersey.api.client.ViewResource;
32  import com.sun.jersey.api.client.WebResource;
33  import com.sun.jersey.client.apache.ApacheHttpClient;
34  import com.sun.jersey.client.apache.ApacheHttpClientHandler;
35  import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;
36  import org.apache.commons.httpclient.HttpClient;
37  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
38  
39  import javax.ws.rs.core.UriBuilder;
40  import java.net.URI;
41  
42  /**
43   * Jersey-based implementation of JIRA REST client.
44   *
45   * @since v0.1
46   */
47  public class JerseyJiraRestClient implements JiraRestClient {
48  
49      private final URI baseUri;
50      private final IssueRestClient issueRestClient;
51      private final SessionRestClient sessionRestClient;
52  	private final UserRestClient userRestClient;
53  	private final ProjectRestClient projectRestClient;
54  	private final ComponentRestClient componentRestClient;
55  	private final MetadataRestClient metadataRestClient;
56  	private final SearchRestClient searchRestClient;
57  	private final VersionRestClient versionRestClient;
58  
59  
60  	public JerseyJiraRestClient(final URI serverUri, final AuthenticationHandler authenticationHandler) {
61          this.baseUri = UriBuilder.fromUri(serverUri).path("/rest/api/latest").build();
62          DefaultApacheHttpClientConfig config = new DefaultApacheHttpClientConfig();
63          authenticationHandler.configure(config);
64          final ApacheHttpClient client = new ApacheHttpClient(createDefaultClientHander(config)) {
65              @Override
66              public WebResource resource(URI u) {
67                  final WebResource resource = super.resource(u);
68                  authenticationHandler.configure(resource, this);
69                  return resource;
70              }
71  
72              @Override
73              public AsyncWebResource asyncResource(URI u) {
74                  final AsyncWebResource resource = super.asyncResource(u);
75                  authenticationHandler.configure(resource, this);
76                  return resource;
77              }
78  
79              @Override
80              public ViewResource viewResource(URI u) {
81                  final ViewResource resource = super.viewResource(u);
82                  authenticationHandler.configure(resource, this);
83                  return resource;
84              }
85  
86              @Override
87              public AsyncViewResource asyncViewResource(URI u) {
88                  final AsyncViewResource resource = super.asyncViewResource(u);
89                  authenticationHandler.configure(resource, this);
90                  return resource;
91              }
92          };
93  		metadataRestClient = new JerseyMetadataRestClient(baseUri, client);
94          sessionRestClient = new JerseySessionRestClient(client, serverUri);
95  		issueRestClient = new JerseyIssueRestClient(baseUri, client, sessionRestClient, metadataRestClient);
96  		userRestClient = new JerseyUserRestClient(baseUri, client);
97  		projectRestClient = new JerseyProjectRestClient(baseUri, client);
98  		componentRestClient = new JerseyComponentRestClient(baseUri, client);
99  		searchRestClient = new JerseySearchRestClient(baseUri, client);
100 		versionRestClient = new JerseyVersionRestClient(baseUri, client);
101     }
102 
103     @Override
104     public IssueRestClient getIssueClient() {
105         return issueRestClient;
106     }
107 
108     @Override
109     public SessionRestClient getSessionClient() {
110         return sessionRestClient;
111     }
112 
113 	@Override
114 	public UserRestClient getUserClient() {
115 		return userRestClient;
116 	}
117 
118 	@Override
119 	public ProjectRestClient getProjectClient() {
120 		return projectRestClient;
121 	}
122 
123 	@Override
124 	public ComponentRestClient getComponentClient() {
125 		return componentRestClient;
126 	}
127 
128 	@Override
129 	public MetadataRestClient getMetadataClient() {
130 		return metadataRestClient;
131 	}
132 
133 	@Override
134 	public SearchRestClient getSearchClient() {
135 		return searchRestClient;
136 	}
137 
138 	@Override
139 	public VersionRestClient getVersionRestClient() {
140 		return versionRestClient;
141 	}
142 
143 	private static ApacheHttpClientHandler createDefaultClientHander(DefaultApacheHttpClientConfig config) {
144         final HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
145         return new ApacheHttpClientHandler(client, config);
146     }
147 
148 }
149