1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
37
38
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 }