1
0
Fork 0

Added another way to detect encoding in RestClient (current one doesn't always work). Added support for getProjects(), getProject(String) and getIssueTypes() in JiraClient.

master
Paulo Casanova 2014-04-16 20:13:34 +01:00
parent 96f014ec04
commit 3e85089f20
2 changed files with 82 additions and 3 deletions

View File

@ -337,6 +337,66 @@ public class JiraClient {
return username;
}
/**
* Obtains the list of all projects in Jira.
* @return all projects; not all data is returned for each project; to get
* the extra data use {@link #getProject(String)}
* @throws JiraException failed to obtain the project list.
*/
public List<Project> getProjects() throws JiraException {
try {
URI uri = restclient.buildURI(Resource.getBaseUri() + "project");
JSON response = restclient.get(uri);
JSONArray projectsArray = JSONArray.fromObject(response);
List<Project> projects = new ArrayList<Project>(projectsArray.size());
for (int i = 0; i < projectsArray.size(); i++) {
JSONObject p = projectsArray.getJSONObject(i);
projects.add(new Project(restclient, p));
}
return projects;
} catch (Exception ex) {
throw new JiraException(ex.getMessage(), ex);
}
}
/**
* Obtains information about a project, given its project key.
* @param key the project key
* @return the project
* @throws JiraException failed to obtain the project
*/
public Project getProject(String key) throws JiraException {
try {
URI uri = restclient.buildURI(Resource.getBaseUri() + "project/" + key);
JSON response = restclient.get(uri);
return new Project(restclient, (JSONObject) response);
} catch (Exception ex) {
throw new JiraException(ex.getMessage(), ex);
}
}
/**
* Obtains the list of all issue types in Jira.
* @return all issue types
* @throws JiraException failed to obtain the issue type list.
*/
public List<IssueType> getIssueTypes() throws JiraException {
try {
URI uri = restclient.buildURI(Resource.getBaseUri() + "issuetype");
JSON response = restclient.get(uri);
JSONArray issueTypeArray = JSONArray.fromObject(response);
List<IssueType> issueTypes = new ArrayList<IssueType>(issueTypeArray.size());
for (int i = 0; i < issueTypeArray.size(); i++) {
JSONObject it = issueTypeArray.getJSONObject(i);
issueTypes.add(new IssueType(restclient, it));
}
return issueTypes;
} catch (Exception ex) {
throw new JiraException(ex.getMessage(), ex);
}
}
}

View File

@ -32,8 +32,11 @@ import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
@ -125,8 +128,24 @@ public class RestClient {
StringBuilder result = new StringBuilder();
if (ent != null) {
InputStreamReader isr = ent.getContentEncoding() != null ?
new InputStreamReader(ent.getContent(), ent.getContentEncoding().getValue()) :
String encoding = null;
if (ent.getContentEncoding() != null) {
encoding = ent.getContentEncoding().getValue();
}
if (encoding == null) {
Header contentTypeHeader = resp.getFirstHeader("Content-Type");
HeaderElement[] contentTypeElements = contentTypeHeader.getElements();
for (HeaderElement he : contentTypeElements) {
NameValuePair nvp = he.getParameterByName("charset");
if (nvp != null) {
encoding = nvp.getValue();
}
}
}
InputStreamReader isr = encoding != null ?
new InputStreamReader(ent.getContent(), encoding) :
new InputStreamReader(ent.getContent());
BufferedReader br = new BufferedReader(isr);
String line = "";