View Javadoc

1   /**
2    * Copyright (C) 2008 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  /*
18   * Created by IntelliJ IDEA.
19   * User: amrk
20   * Date: 13/03/2004
21   * Time: 23:19:19
22   */
23  package com.atlassian.theplugin.jira.api;
24  
25  import com.atlassian.theplugin.commons.remoteapi.RemoteApiMalformedUrlException;
26  import com.atlassian.theplugin.commons.remoteapi.RemoteApiSessionExpiredException;
27  import com.atlassian.theplugin.commons.remoteapi.rest.AbstractHttpSession;
28  import com.intellij.openapi.diagnostic.Logger;
29  import org.apache.commons.httpclient.HttpMethod;
30  import org.jdom.Document;
31  import org.jdom.Element;
32  import org.jdom.JDOMException;
33  
34  import java.io.IOException;
35  import java.net.URLEncoder;
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.Iterator;
39  import java.util.List;
40  
41  public class JIRARssClient extends AbstractHttpSession {
42      private static final Logger LOGGER = Logger.getInstance(JIRARssClient.class.getName());
43  
44      public JIRARssClient(String url) throws RemoteApiMalformedUrlException {
45  		super(url);
46      }
47  
48  	protected void adjustHttpHeader(HttpMethod method) {
49  	}
50  
51  	protected void preprocessResult(Document doc) throws JDOMException, RemoteApiSessionExpiredException {
52  	}
53  
54  	public JIRARssClient(String url, String userName, String password) throws RemoteApiMalformedUrlException {
55  		super(url);
56          this.userName = userName;
57          this.password = password;
58      }
59  
60      public List getIssues(List<JIRAQueryFragment> fragments,
61  						  String sortBy,
62  						  String sortOrder, int start, int max) throws JIRAException {
63  
64          StringBuffer url = new StringBuffer(baseUrl + "/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?");
65  
66          for (JIRAQueryFragment fragment : fragments) {
67              if (fragment.getQueryStringFragment() != null) {
68                  url.append("&").append(fragment.getQueryStringFragment());
69              }
70          }
71  
72          url.append("&sorter/field=" + sortBy);
73  		url.append("&sorter/order=" + sortOrder);
74  		url.append("&pager/start=" + start);
75  		url.append("&tempMax=" + max);
76          url.append(appendAuthentication());
77  
78  		try {
79              Document doc = retrieveGetResponse(url.toString());
80              Element root = doc.getRootElement();
81              Element channel = root.getChild("channel");
82              if (channel != null && !channel.getChildren("item").isEmpty()) {
83                  return makeIssues(channel.getChildren("item"));
84              }
85              return Collections.EMPTY_LIST;
86          } catch (IOException e) {
87              throw new JIRAException(e.getMessage(), e);
88          } catch (JDOMException e) {
89              throw new JIRAException(e.getMessage(), e);
90          } catch (RemoteApiSessionExpiredException e) {
91  			throw new JIRAException(e.getMessage(), e);
92  		}
93  
94  	}
95  
96      public List getAssignedIssues(String assignee) throws JIRAException {
97          String url = baseUrl + "/sr/jira.issueviews:searchrequest-xml"
98                  + "/temp/SearchRequest.xml?resolution=-1&assignee=" + URLEncoder.encode(assignee)
99                  + "&sorter/field=updated&sorter/order=DESC&tempMax=100" + appendAuthentication();
100 
101         try {
102             Document doc = retrieveGetResponse(url);
103             Element root = doc.getRootElement();
104             Element channel = root.getChild("channel");
105             if (channel != null && !channel.getChildren("item").isEmpty()) {
106                 return makeIssues(channel.getChildren("item"));
107             }
108 
109             return Collections.EMPTY_LIST;
110         } catch (IOException e) {
111             throw new JIRAException(e.getMessage(), e);
112         } catch (JDOMException e) {
113             throw new JIRAException(e.getMessage(), e);
114         } catch (RemoteApiSessionExpiredException e) {
115             throw new JIRAException(e.getMessage(), e);
116 		}
117 	}
118 
119 	public List getSavedFilterIssues(JIRAQueryFragment fragment,
120 									 String sortBy,
121 									 String sortOrder,
122 									 int start, 
123 									 int max) throws JIRAException {
124 
125 		StringBuffer url = new StringBuffer(baseUrl + "/sr/jira.issueviews:searchrequest-xml/");
126 
127 		if (fragment.getQueryStringFragment() != null) {
128 			url.append(fragment.getQueryStringFragment())
129 					.append("/SearchRequest-")
130 					.append(fragment.getQueryStringFragment())
131 					.append(".xml");
132 		}
133 
134 		url.append("?sorter/field=" + sortBy);
135 		url.append("&sorter/order=" + sortOrder);
136 		url.append("&pager/start=" + start);
137 		url.append("&tempMax=" + max);
138 			
139 		url.append(appendAuthentication());
140 
141 		try {
142 			Document doc = retrieveGetResponse(url.toString());
143 			Element root = doc.getRootElement();
144 			Element channel = root.getChild("channel");
145 			if (channel != null && !channel.getChildren("item").isEmpty()) {
146 				return makeIssues(channel.getChildren("item"));
147 			}
148 			return Collections.EMPTY_LIST;
149 		} catch (IOException e) {
150 			throw new JIRAException(e.getMessage(), e);
151 		} catch (JDOMException e) {
152 			throw new JIRAException(e.getMessage(), e);
153 		} catch (RemoteApiSessionExpiredException e) {
154 			throw new JIRAException(e.getMessage(), e);
155 		}
156 
157 	}
158 
159 	private List makeIssues(List issueElements) {
160         List<JIRAIssue> result = new ArrayList<JIRAIssue>(issueElements.size());
161         for (Iterator iterator = issueElements.iterator(); iterator.hasNext();) {
162             result.add(new JIRAIssueBean(baseUrl, (Element) iterator.next()));
163         }
164         return result;
165     }
166 
167     private String appendAuthentication() {
168         if (userName != null) {
169             return "&os_username=" + URLEncoder.encode(userName)
170                     + "&os_password=" + URLEncoder.encode(password);
171         }
172         return "";
173     }
174 }