1919 */
2020package org .xwiki .contrib .jira .macro .internal ;
2121
22- import java .io .IOException ;
2322import java .io .InputStream ;
2423import java .net .MalformedURLException ;
2524import java .net .URL ;
2625import java .nio .charset .StandardCharsets ;
27- import java .util .function .Function ;
2826
2927import javax .inject .Singleton ;
3028
3129import org .apache .commons .io .IOUtils ;
3230import org .apache .commons .lang3 .StringUtils ;
31+ import org .apache .commons .lang3 .function .FailableFunction ;
3332import org .apache .http .HttpHost ;
3433import org .apache .http .auth .AuthScope ;
3534import org .apache .http .auth .UsernamePasswordCredentials ;
4443import org .apache .http .impl .client .CloseableHttpClient ;
4544import org .apache .http .impl .client .HttpClientBuilder ;
4645import org .jdom2 .Document ;
47- import org .jdom2 .JDOMException ;
4846import org .jdom2 .input .SAXBuilder ;
4947import org .xwiki .component .annotation .Component ;
5048import org .xwiki .contrib .jira .config .JIRAServer ;
@@ -70,21 +68,18 @@ public class HTTPJIRAFetcher
7068 * @param jiraServer the jira server data containing optional credentials (used to setup preemptive basic
7169 * authentication
7270 * @return the {@link Document} object containing the XML data
73- * @throws Exception if an error happened during the fetch or if the passed URL is malformed
71+ * @throws JIRAConnectionException if an error happened during the fetch or if the passed URL is malformed
7472 */
75- public Document fetch (String urlString , JIRAServer jiraServer ) throws Exception
73+ public Document fetch (String urlString , JIRAServer jiraServer ) throws JIRAConnectionException
7674 {
77- return performRequest (urlString , jiraServer , is -> {
78- try {
79- Document document = createSAXBuilder ().build (is );
80- return document ;
81- } catch (JDOMException | IOException e ) {
82- // The XML has failed to be parsed, read it as plain text ans return it, for debugging purpose.
83- String message = String .format ("Failed to parse JIRA XML content [%s]" , getRawJIRAResponse (urlString ));
84- // FIXME: this is ugly
85- throw new RuntimeException (message , e );
86- }
87- });
75+ try {
76+ return performRequest (urlString , jiraServer , is -> createSAXBuilder ().build (is ));
77+ } catch (Exception e ) {
78+ // The XML has failed to be parsed, read it as plain text and return it, for debugging purpose.
79+ String message = String .format ("Failed to parse JIRA XML content [%s]" ,
80+ getRawJIRAResponse (urlString , jiraServer ));
81+ throw new JIRAConnectionException (message , e );
82+ }
8883 }
8984
9085 private ObjectMapper getObjectMapper ()
@@ -103,34 +98,33 @@ private ObjectMapper getObjectMapper()
10398 * @param type the actual type to obtain when parsing the JSON
10499 * @return an instance of the POJO corresponding to the JSON answer
105100 * @param <T> the expected type
106- * @throws Exception in case of problem when performing the request
101+ * @throws JIRAConnectionException in case of problem when performing the request
107102 */
108- public <T > T fetchJSON (String urlString , JIRAServer jiraServer , Class <T > type ) throws Exception
103+ public <T > T fetchJSON (String urlString , JIRAServer jiraServer , Class <T > type ) throws JIRAConnectionException
109104 {
110- return performRequest (urlString , jiraServer , is -> {
111- try {
112- return getObjectMapper ().readValue (is , type );
113- } catch (IOException e ) {
114- // The JSON has failed to be parsed, read it as plain text and return it, for debugging purpose.
115- String message = String .format ("Failed to parse JIRA JSON content [%s]" , getRawJIRAResponse (urlString ));
116- // FIXME: this is ugly
117- throw new RuntimeException (message , e );
118- }
119- });
105+ try {
106+ return performRequest (urlString , jiraServer , is -> getObjectMapper ().readValue (is , type ));
107+ } catch (Exception e ) {
108+ // The JSON has failed to be parsed, read it as plain text and return it, for debugging purpose.
109+ String message = String .format ("Failed to parse JIRA JSON content [%s]" ,
110+ getRawJIRAResponse (urlString , jiraServer ));
111+ throw new JIRAConnectionException (message , e );
112+ }
120113 }
121114
122- private String getRawJIRAResponse (String urlString )
115+ private String getRawJIRAResponse (String urlString , JIRAServer jiraServer )
123116 {
124117 String message ;
125118 try {
126- message = IOUtils .toString (new URL ( urlString ) , StandardCharsets .UTF_8 );
127- } catch (IOException ee ) {
119+ message = performRequest ( urlString , jiraServer , is -> IOUtils .toString (is , StandardCharsets .UTF_8 ) );
120+ } catch (Exception e ) {
128121 message = String .format ("Failed to get JIRA content for [%s]" , urlString );
129122 }
130123 return message ;
131124 }
132125
133- private <T > T performRequest (String url , JIRAServer jiraServer , Function <InputStream , T > callback ) throws Exception
126+ private <T > T performRequest (String url , JIRAServer jiraServer ,
127+ FailableFunction <InputStream , T , Exception > callback ) throws Exception
134128 {
135129 HttpGet httpGet = new HttpGet (url );
136130 CloseableHttpClient httpClient = createHttpClientBuilder ().build ();
@@ -145,7 +139,7 @@ private <T> T performRequest(String url, JIRAServer jiraServer, Function<InputSt
145139 } else {
146140 // The error message is in the HTML. We extract it to perform some good error-reporting, by extracting
147141 // it from the <h1> tag.
148- throw new Exception (String .format ("Error = [%s]. URL = [%s]" ,
142+ throw new JIRAConnectionException (String .format ("Error = [%s]. URL = [%s]" ,
149143 EXTRACTOR .extract (response .getEntity ().getContent ()), httpGet .getURI ().toString ()));
150144 }
151145 }
0 commit comments