1. Rich Manalang
  2. Hipchat for Confluence

Commits

Steve Lancashire [Atlassian]  committed e77dd47 Merge
  • Participants
  • Parent commits 9ac3e7b, c180191
  • Branches master

Comments (0)

Files changed (5)

File src/main/java/com/atlassian/labs/hipchat/actions/ViewSpaceConfigurationAction.java

View file
  • Ignore whitespace
 import com.atlassian.confluence.spaces.actions.AbstractSpaceAdminAction;
 import com.atlassian.labs.hipchat.components.ConfigurationManager;
 import com.atlassian.labs.hipchat.components.HipChatProxyClient;
-import com.atlassian.labs.hipchat.utils.InvalidAuthTokenException;
 import com.opensymphony.xwork.Action;
+
 import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.WebApplicationException;
 
 public class ViewSpaceConfigurationAction extends AbstractSpaceAdminAction
 {
+    private static final Logger log = LoggerFactory.getLogger(ViewSpaceConfigurationAction.class);
     private final HipChatProxyClient hipChatProxyClient;
     private final ConfigurationManager configurationManager;
 
         } else {
             try {
                 setRoomJson(hipChatProxyClient.getRooms().toString());
-            } catch (InvalidAuthTokenException e) {
+            } catch (WebApplicationException e) {
+                
+                if(e.getResponse().getStatus() >= 500) {
+                    log.error("Could not get hipchat room with key {} with message {}", key, e);
+                    log.warn("More details: ", e);
+                }
                 return Action.ERROR;
             }
+
             return Action.SUCCESS;
         }
     }

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

View file
  • Ignore whitespace
 package com.atlassian.labs.hipchat.components;
 
-import com.atlassian.labs.hipchat.utils.InvalidAuthTokenException;
 import com.atlassian.sal.api.executor.ThreadLocalDelegateExecutorFactory;
 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 javax.ws.rs.WebApplicationException;
+
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
      *
      * @param userId ID or email address of the user.
      * @return JSON String
+     * @throws WebApplicationException if the response from hipchat is not successful
      */
-    public JSONString getUser(final String userId) {
+    public JSONString getUser(final String userId) throws WebApplicationException {
         final String url = API_BASE_URI + "/v1/users/show?user_id=" + userId;
         return jsonGet(url);
     }
      * </pre>
      *
      * @return JSON String containing a list of rooms
-     * @throws InvalidAuthTokenException
+     * @throws WebApplicationException if the response from hipchat is not successful
      */
-    public JSONString getRooms() throws InvalidAuthTokenException {
+    public JSONString getRooms() {
         final String url = API_BASE_URI + "/v1/rooms/list";
         return jsonGet(url);
     }
      * @param url
      * @return String containing JSON if successful, empty String otherwise. Never returns null.
      *         This will always return an empty string if the Hip-Chat authentication token is not set.
+     * @throws WebApplicationException if the response from hipchat is not successful
      */
     private JSONString jsonGet(String url) {
         String authToken = configurationManager.getHipChatAuthToken();
         final String urlWithAuthToken = url.contains("?") ? url + "&auth_token=" + authToken : url + "?auth_token=" + authToken;
         final Request<?, Response> request = requestFactory.createRequest(Request.MethodType.GET, urlWithAuthToken);
         request.setConnectionTimeout(CONNECTION_TIMEOUT_MILLIS);
-        try {
+        try
+        {
             return new JSONString(request.executeAndReturn(new ResponseBodyReturningHandler()));
         }
-        catch (ResponseException e) {
-            log.error("Failed to retrieve user", e);
+        catch(ResponseException ex)
+        {
+            throw new WebApplicationException(ex, 500);
         }
-        return new JSONString("");
     }
 
     private class ResponseBodyReturningHandler implements ReturningResponseHandler<Response, String> {
         @Override public String handle(Response response) throws ResponseException {
-            if(response.getStatusCode() == 401) {
-                throw new InvalidAuthTokenException();
+            if(!response.isSuccessful()) {
+                log.debug("Unsuccessful response from hipchat : {}", response.getResponseBodyAsString());
+                throw new WebApplicationException(response.getStatusCode());
             }
             return response.getResponseBodyAsString();
         }

File src/main/java/com/atlassian/labs/hipchat/rest/HipChatUserProxy.java

View file
  • Ignore whitespace
 
 
 import com.atlassian.labs.hipchat.components.HipChatProxyClient;
+import com.atlassian.sal.api.net.ResponseException;
+
+import org.jfree.util.Log;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 @Path("/user/show")
 public class HipChatUserProxy
 {
+    private static Logger log = LoggerFactory.getLogger(HipChatUserProxy.class);
     private HipChatProxyClient client;
 
     public HipChatUserProxy(HipChatProxyClient client)
     {
         return Response.ok(client.getUser(userId).toString()).build();
     }
-
 }

File src/main/java/com/atlassian/labs/hipchat/utils/InvalidAuthTokenException.java

  • Ignore whitespace
-package com.atlassian.labs.hipchat.utils;
-
-public class InvalidAuthTokenException extends RuntimeException {
-}

File src/main/resources/js/status.js

View file
  • Ignore whitespace
                     email = $('a.email', vcard).html();
                 if (!vcard.hasClass('hc-status-applied')) {
                     $.get(AJS.Data.get("context-path") + '/rest/hipchatproxy/1/user/show?user_id=' + encodeURIComponent(email), function (d) {
+
+                        if(!(d && d.user && typeof d.user.status == 'string')) {
+                            return;
+                        }
+
                         vcard.addClass('hc-status-applied');
                         $('div.values', vcard)
                             .append('<a class="hc-status ' + d.user.status.toLowerCase() + '" title="'
                     }, 'json');
                 }
             });
-
         }
     });