View Javadoc

1   /*
2    * Copyright (C) 2012 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  package com.atlassian.jira.rest.client.internal.async;
17  
18  import com.atlassian.jira.rest.client.api.ProjectRestClient;
19  import com.atlassian.jira.rest.client.api.domain.BasicProject;
20  import com.atlassian.httpclient.api.HttpClient;
21  import com.atlassian.jira.rest.client.api.domain.Project;
22  import com.atlassian.jira.rest.client.internal.json.BasicProjectsJsonParser;
23  import com.atlassian.jira.rest.client.internal.json.ProjectJsonParser;
24  import com.atlassian.util.concurrent.Promise;
25  
26  import javax.ws.rs.core.UriBuilder;
27  import java.net.URI;
28  
29  /**
30   * Asynchronous implementation of ProjectRestClient.
31   *
32   * @since v2.0
33   */
34  public class AsynchronousProjectRestClient extends AbstractAsynchronousRestClient implements ProjectRestClient {
35  
36  	private static final String PROJECT_URI_PREFIX = "project";
37  	private final ProjectJsonParser projectJsonParser = new ProjectJsonParser();
38  	private final BasicProjectsJsonParser basicProjectsJsonParser = new BasicProjectsJsonParser();
39  
40  	private final URI baseUri;
41  
42  	public AsynchronousProjectRestClient(final URI baseUri, final HttpClient client) {
43  		super(client);
44  		this.baseUri = baseUri;
45  	}
46  
47  	@Override
48  	public Promise<Project> getProject(final String key) {
49  		final URI uri = UriBuilder.fromUri(baseUri).path(PROJECT_URI_PREFIX).path(key).build();
50  		return getAndParse(uri, projectJsonParser);
51  	}
52  
53  	@Override
54  	public Promise<Project> getProject(final URI projectUri) {
55  		return getAndParse(projectUri, projectJsonParser);
56  	}
57  
58  	@Override
59  	public Promise<Iterable<BasicProject>> getAllProjects() {
60  		final URI uri = UriBuilder.fromUri(baseUri).path(PROJECT_URI_PREFIX).build();
61  		return getAndParse(uri, basicProjectsJsonParser);
62  	}
63  }