View Javadoc

1   /*
2    * Copyright (C) 2011 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.ProgressMonitor;
20  import com.atlassian.jira.rest.client.RestClientException;
21  import com.atlassian.jira.rest.client.SearchRestClient;
22  import com.atlassian.jira.rest.client.domain.SearchResult;
23  import com.atlassian.jira.rest.client.internal.json.SearchResultJsonParser;
24  import com.sun.jersey.client.apache.ApacheHttpClient;
25  import org.codehaus.jettison.json.JSONException;
26  import org.codehaus.jettison.json.JSONObject;
27  
28  import javax.annotation.Nullable;
29  import javax.ws.rs.core.UriBuilder;
30  import java.net.URI;
31  
32  /**
33   * Jersey-based implementation of SearchRestClient
34   *
35   * @since v0.2
36   */
37  public class JerseySearchRestClient extends AbstractJerseyRestClient implements SearchRestClient{
38  	private static final String START_AT_ATTRIBUTE = "startAt";
39  	private static final String MAX_RESULTS_ATTRIBUTE = "maxResults";
40  	private static final int MAX_JQL_LENGTH_FOR_HTTP_GET = 500;
41  	private static final String JQL_ATTRIBUTE = "jql";
42  	private final SearchResultJsonParser searchResultJsonParser = new SearchResultJsonParser();
43  
44  	private static final String SEARCH_URI_PREFIX = "search";
45  	private final URI searchUri;
46  
47  	public JerseySearchRestClient(URI baseUri, ApacheHttpClient client) {
48  		super(baseUri, client);
49  		searchUri = UriBuilder.fromUri(baseUri).path(SEARCH_URI_PREFIX).build();
50  	}
51  
52  	@Override
53  	public SearchResult searchJql(@Nullable String jql, ProgressMonitor progressMonitor) {
54  		if (jql == null) {
55  			jql = "";
56  		}
57  		if (jql.length() > MAX_JQL_LENGTH_FOR_HTTP_GET) {
58  			final JSONObject postEntity = new JSONObject();
59  			try {
60  				postEntity.put(JQL_ATTRIBUTE, jql);
61  			} catch (JSONException e) {
62  				throw new RestClientException(e);
63  			}
64  			return postAndParse(searchUri, postEntity, searchResultJsonParser, progressMonitor);
65  		} else {
66  			final URI uri = UriBuilder.fromUri(searchUri).queryParam(JQL_ATTRIBUTE, jql).build();
67  			return getAndParse(uri, searchResultJsonParser, progressMonitor);
68  		}
69  	}
70  
71  	@Override
72  	public SearchResult searchJql(@Nullable String jql, int maxResults, int startAt, ProgressMonitor progressMonitor) {
73  		if (jql == null) {
74  			jql = "";
75  		}
76  		if (jql.length() > MAX_JQL_LENGTH_FOR_HTTP_GET) {
77  			final JSONObject postEntity = new JSONObject();
78  			try {
79  				postEntity.put(JQL_ATTRIBUTE, jql);
80  				postEntity.put(START_AT_ATTRIBUTE, startAt);
81  				postEntity.put(MAX_RESULTS_ATTRIBUTE, maxResults);
82  			} catch (JSONException e) {
83  				throw new RestClientException(e);
84  			}
85  			return postAndParse(searchUri, postEntity, searchResultJsonParser, progressMonitor);
86  		} else {
87  			final URI uri = UriBuilder.fromUri(searchUri).queryParam(JQL_ATTRIBUTE, jql).queryParam(MAX_RESULTS_ATTRIBUTE, maxResults)
88  					.queryParam(START_AT_ATTRIBUTE, startAt).build();
89  			return getAndParse(uri, searchResultJsonParser, progressMonitor);
90  		}
91  	}
92  }