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.ComponentRestClient;
19  import com.atlassian.jira.rest.client.api.domain.Component;
20  import com.atlassian.jira.rest.client.api.domain.input.ComponentInput;
21  import com.atlassian.jira.rest.client.internal.domain.input.ComponentInputWithProjectKey;
22  import com.atlassian.jira.rest.client.internal.json.ComponentJsonParser;
23  import com.atlassian.jira.rest.client.internal.json.JsonObjectParser;
24  import com.atlassian.jira.rest.client.internal.json.gen.ComponentInputWithProjectKeyJsonGenerator;
25  import com.atlassian.httpclient.api.HttpClient;
26  import com.atlassian.util.concurrent.Promise;
27  import org.codehaus.jettison.json.JSONException;
28  import org.codehaus.jettison.json.JSONObject;
29  
30  import javax.annotation.Nullable;
31  import javax.ws.rs.core.UriBuilder;
32  import java.net.URI;
33  
34  /**
35   * Asynchronous implementation of ComponentRestClient.
36   *
37   * @since v2.0
38   */
39  public class AsynchronousComponentRestClient extends AbstractAsynchronousRestClient implements ComponentRestClient {
40  
41  	private final ComponentJsonParser componentJsonParser = new ComponentJsonParser();
42  	private final URI componentUri;
43  
44  	public AsynchronousComponentRestClient(final URI baseUri, final HttpClient client) {
45  		super(client);
46  		componentUri = UriBuilder.fromUri(baseUri).path("component").build();
47  	}
48  
49  	@Override
50  	public Promise<Component> getComponent(final URI componentUri) {
51  		return getAndParse(componentUri, componentJsonParser);
52  	}
53  
54  	@Override
55  	public Promise<Component> createComponent(final String projectKey, final ComponentInput componentInput) {
56  		final ComponentInputWithProjectKey helper = new ComponentInputWithProjectKey(projectKey, componentInput);
57  		return postAndParse(componentUri, helper, new ComponentInputWithProjectKeyJsonGenerator(), componentJsonParser);
58  	}
59  
60  	@Override
61  	public Promise<Component> updateComponent(URI componentUri, ComponentInput componentInput) {
62  		final ComponentInputWithProjectKey helper = new ComponentInputWithProjectKey(null, componentInput);
63  		return putAndParse(componentUri, helper, new ComponentInputWithProjectKeyJsonGenerator(), componentJsonParser);
64  	}
65  
66  	@Override
67  	public Promise<Void> removeComponent(URI componentUri, @Nullable URI moveIssueToComponentUri) {
68  		final UriBuilder uriBuilder = UriBuilder.fromUri(componentUri);
69  		if (moveIssueToComponentUri != null) {
70  			uriBuilder.queryParam("moveIssuesTo", moveIssueToComponentUri);
71  		}
72  		return delete(uriBuilder.build());
73  	}
74  
75  	@Override
76  	public Promise<Integer> getComponentRelatedIssuesCount(URI componentUri) {
77  		final URI relatedIssueCountsUri = UriBuilder.fromUri(componentUri).path("relatedIssueCounts").build();
78  		return getAndParse(relatedIssueCountsUri, new JsonObjectParser<Integer>() {
79  			@Override
80  			public Integer parse(JSONObject json) throws JSONException {
81  				return json.getInt("issueCount");
82  			}
83  		});
84  	}
85  }