Commits

Rich Manalang [Atlassian]  committed e353f32 Merge

Merged in ssaasen/hipchat-for-confluence (pull request #2)

  • Participants
  • Parent commits 5449e47, 59a1b57

Comments (0)

Files changed (3)

         <url>https://bitbucket.org/rmanalan/hipchat-for-confluence</url>
     </scm>
     <build>
+        <finalName>${artifactId}-hipchat-${version}</finalName>
         <plugins>
             <plugin>
                 <groupId>com.atlassian.maven.plugins</groupId>
                     <productDataVersion>${confluence.data.version}</productDataVersion>
                     <instructions>
                         <Import-Package>
-                            com.atlassian.sal.api.pluginsettings.*,
+                            com.atlassian.sal.api*;version="[2.6.0,3.0)",
                             *
                         </Import-Package>
                         <Export-Package/>
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>com.atlassian.sal</groupId>
+            <artifactId>sal-api</artifactId>
+            <version>2.6.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>
             <version>2.4</version>

File src/main/java/com/atlassian/labs/hipchat/components/HipChatProxyClient.java

 package com.atlassian.labs.hipchat.components;
 
+import com.atlassian.confluence.util.GeneralUtil;
 import com.atlassian.labs.hipchat.utils.InvalidAuthTokenException;
 import com.atlassian.plugin.webresource.UrlMode;
 import com.atlassian.plugin.webresource.WebResourceUrlProvider;
-import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpStatus;
-import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
-import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.params.HttpMethodParams;
+import com.atlassian.sal.api.net.Request;
+import com.atlassian.sal.api.net.RequestFactory;
+import com.atlassian.sal.api.net.Response;
+import com.atlassian.sal.api.net.ResponseException;
+import com.atlassian.sal.api.net.ReturningResponseHandler;
+import org.apache.axis.utils.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.DisposableBean;
 
-import java.io.IOException;
-
-public class HipChatProxyClient implements DisposableBean
-{
+public class HipChatProxyClient {
     private static final Logger log = LoggerFactory.getLogger("atlassian.plugin");
     private static final String API_BASE_URI = "https://api.hipchat.com";
     private static final String MSG = " - new blog post by ";
 
-    private final HttpClient httpClient;
     private final WebResourceUrlProvider webResourceUrlProvider;
     private final ConfigurationManager configurationManager;
-    private final MultiThreadedHttpConnectionManager connectionManager;
+    private final RequestFactory<Request<?, Response>> requestFactory;
 
-    public HipChatProxyClient(ConfigurationManager configurationManager, WebResourceUrlProvider webResourceUrlProvider) {
+    public HipChatProxyClient(ConfigurationManager configurationManager, WebResourceUrlProvider webResourceUrlProvider, RequestFactory<Request<?, Response>> requestFactory) {
         this.configurationManager = configurationManager;
-        this.connectionManager = new MultiThreadedHttpConnectionManager();
-        this.httpClient = new HttpClient(connectionManager);
         this.webResourceUrlProvider = webResourceUrlProvider;
+        this.requestFactory = requestFactory;
     }
 
-    public String getUser(String userId)
-    {
+    public String getUser(final String userId) {
         String authToken = configurationManager.getHipChatAuthToken();
-        if (authToken.equals("")) return "";
-        GetMethod get = new GetMethod(API_BASE_URI + "/v1/users/show?auth_token=" + authToken + "&user_id=" + userId);
-        get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
-
+        if (StringUtils.isEmpty(authToken)) {
+            return "";
+        }
+        final String url = API_BASE_URI + "/v1/users/show?auth_token=" + authToken + "&user_id=" + userId;
+        final Request<?, Response> request = requestFactory.createRequest(Request.MethodType.GET, url);
         try {
-            int statusCode = httpClient.executeMethod(get);
-            if (statusCode != HttpStatus.SC_OK) {
-                log.info("Method failed: " + get.getStatusLine());
-            }
-
-            byte[] responseBody = get.getResponseBody();
-
-            return new String(responseBody);
-        } catch (IOException e) {
+            return request.executeAndReturn(new ResponseBodyReturningHandler());
+        }
+        catch (ResponseException e) {
             log.error("Failed to retrieve user", e);
-        } finally {
-            get.releaseConnection();
         }
-
         return "";
     }
 
-    private String getIconUrl(){
-        return "<img src=\"" +
-                webResourceUrlProvider.getBaseUrl(UrlMode.ABSOLUTE) + "/images/icons/blogentry_16.gif" +
-                "\" width=16 height=16 />&nbsp;";
-    }
-
     public String getRooms() throws InvalidAuthTokenException {
         String authToken = configurationManager.getHipChatAuthToken();
-        if (authToken.equals("")) return "";
-        GetMethod get = new GetMethod(API_BASE_URI + "/v1/rooms/list?auth_token=" + authToken);
-        get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
+        if (StringUtils.isEmpty(authToken)) {
+            return "";
+        }
 
+        final String url = API_BASE_URI + "/v1/rooms/list?auth_token=" + authToken;
+        final Request<?, Response> request = requestFactory.createRequest(Request.MethodType.GET, url);
         try {
-            int statusCode = httpClient.executeMethod(get);
-            if (statusCode != HttpStatus.SC_OK) {
-                log.info("Method failed: " + get.getStatusLine());
-                throw new InvalidAuthTokenException();
-            }
-
-            byte[] responseBody = get.getResponseBody();
-
-            return new String(responseBody);
-        } catch (IOException e) {
+            return request.executeAndReturn(new ResponseBodyReturningHandler());
+        }
+        catch (ResponseException e) {
             log.error("Failed to retrieve rooms", e);
-        } finally {
-            get.releaseConnection();
         }
-
         return "";
     }
 
-    public void notifyRoom(String room, String postLink, String creator, String personalSpacePath)
-    {
+
+    public void notifyRoom(final String room, final String postLink, final String creator, final String personalSpacePath) {
         String authToken = configurationManager.getHipChatAuthToken();
-        if (authToken.equals("")) return;
-        PostMethod post = new PostMethod(API_BASE_URI + "/v1/rooms/message?auth_token=" + authToken);
-        post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
-        post.addParameter("room_id", room);
-        post.addParameter("from", "Confluence");
-        post.addParameter("message",  getIconUrl() + postLink + MSG + "<a href=\"" +
+        if (StringUtils.isEmpty(authToken)) {
+            return;
+        }
+        final String url = API_BASE_URI + "/v1/rooms/message?auth_token=" + authToken;
+        final Request<?, Response> request = requestFactory.createRequest(Request.MethodType.POST, url);
+        request.addRequestParameters(
+                "room_id", room,
+                "from", "Confluence",
+                "message", getIconUrl() + postLink + MSG + "<a href=\"" +
                 webResourceUrlProvider.getBaseUrl(UrlMode.ABSOLUTE) + personalSpacePath +
-                "\">" + creator + "</a>");
-        post.addParameter("format", "json");
-
+                "\">" + GeneralUtil.escapeXml(creator) + "</a>",
+                "format", "json");
         try {
-            int statusCode = httpClient.executeMethod(post);
-            if (statusCode != HttpStatus.SC_OK) {
-                log.info("Method failed: " + post.getStatusLine());
-            }
-        } catch (IOException e) {
+            request.executeAndReturn(new ResponseBodyReturningHandler());
+        }
+        catch (ResponseException e) {
             log.error("Failed to notify rooms", e);
-        } finally {
-            post.releaseConnection();
         }
     }
 
-    @Override public void destroy() throws Exception {
-        connectionManager.shutdown();
-        log.debug("Shutdown connection manager.");
+
+    private class ResponseBodyReturningHandler implements ReturningResponseHandler<Response, String> {
+        @Override public String handle(Response response) throws ResponseException {
+            return response.getResponseBodyAsString();
+        }
+    }
+
+    private String getIconUrl() {
+        return "<img src=\"" +
+                webResourceUrlProvider.getBaseUrl(UrlMode.ABSOLUTE) + "/images/icons/blogentry_16.gif" +
+                "\" width=16 height=16 />&nbsp;";
     }
 }

File src/main/resources/atlassian-plugin.xml

     <component name="Annotated Event Listener" key="annotated-listener"
                class="com.atlassian.labs.hipchat.components.AnnotatedListener"/>
 
+    <component-import name="Request Factory" key="request-factory" interface="com.atlassian.sal.api.net.RequestFactory" />
     <component name="HipChat Proxy Client" key="proxy-client"
                class="com.atlassian.labs.hipchat.components.HipChatProxyClient"/>