From 3e85089f206fcc06e6dd1f948443419cbabf9feb Mon Sep 17 00:00:00 2001 From: Paulo Casanova Date: Wed, 16 Apr 2014 20:13:34 +0100 Subject: [PATCH] Added another way to detect encoding in RestClient (current one doesn't always work). Added support for getProjects(), getProject(String) and getIssueTypes() in JiraClient. --- .../java/net/rcarz/jiraclient/JiraClient.java | 62 ++++++++++++++++++- .../java/net/rcarz/jiraclient/RestClient.java | 23 ++++++- 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/rcarz/jiraclient/JiraClient.java b/src/main/java/net/rcarz/jiraclient/JiraClient.java index 6511ba3..6c8beb0 100644 --- a/src/main/java/net/rcarz/jiraclient/JiraClient.java +++ b/src/main/java/net/rcarz/jiraclient/JiraClient.java @@ -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 getProjects() throws JiraException { + try { + URI uri = restclient.buildURI(Resource.getBaseUri() + "project"); + JSON response = restclient.get(uri); + JSONArray projectsArray = JSONArray.fromObject(response); + List projects = new ArrayList(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 getIssueTypes() throws JiraException { + try { + URI uri = restclient.buildURI(Resource.getBaseUri() + "issuetype"); + JSON response = restclient.get(uri); + JSONArray issueTypeArray = JSONArray.fromObject(response); + + List issueTypes = new ArrayList(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); + } + } } - diff --git a/src/main/java/net/rcarz/jiraclient/RestClient.java b/src/main/java/net/rcarz/jiraclient/RestClient.java index 1bbc1b9..acd230c 100644 --- a/src/main/java/net/rcarz/jiraclient/RestClient.java +++ b/src/main/java/net/rcarz/jiraclient/RestClient.java @@ -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 = "";