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