Commits

Steve Lancashire committed 15cd219

CONFDEV-10902 fix javascript error when the hipchat proxy returns something other than a user status

The hipchat proxy is a little more RESTful now, propagating the 404 when a user is not found.
Also a successful response is only returned if the request to hipchat was successful, otherwise return a 500 error
to the client.

Comments (0)

Files changed (4)

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

 import com.atlassian.labs.hipchat.components.ConfigurationManager;
 import com.atlassian.labs.hipchat.components.HipChatProxyClient;
 import com.atlassian.labs.hipchat.utils.InvalidAuthTokenException;
+import com.atlassian.sal.api.net.ResponseException;
 import com.opensymphony.xwork.Action;
+import com.sun.jersey.api.NotFoundException;
+
 import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class ViewSpaceConfigurationAction extends AbstractSpaceAdminAction
 {
+    private static final Logger log = LoggerFactory.getLogger(ViewSpaceConfigurationAction.class);
     private final HipChatProxyClient hipChatProxyClient;
     private final ConfigurationManager configurationManager;
 
                 setRoomJson(hipChatProxyClient.getRooms().toString());
             } catch (InvalidAuthTokenException e) {
                 return Action.ERROR;
+            } catch(NotFoundException e) {
+                log.debug("Room not found with key "+key, e);
+                return Action.ERROR;
+            } catch(ResponseException e) {
+                log.error("Could not get hipchat room with key {} with message {}", key, e);
+                log.error("More details: ", e);
+                return Action.ERROR;
             }
+
             return Action.SUCCESS;
         }
     }

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

 import com.atlassian.sal.api.net.Response;
 import com.atlassian.sal.api.net.ResponseException;
 import com.atlassian.sal.api.net.ReturningResponseHandler;
+import com.sun.jersey.api.NotFoundException;
+
 import org.apache.axis.utils.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
      *
      * @param userId ID or email address of the user.
      * @return JSON String
+     * @throws ResponseException
+     * @throws NotFoundException
+     * @throws InvalidAuthTokenException
      */
-    public JSONString getUser(final String userId) {
+    public JSONString getUser(final String userId) throws ResponseException, NotFoundException, InvalidAuthTokenException {
         final String url = API_BASE_URI + "/v1/users/show?user_id=" + userId;
         return jsonGet(url);
     }
      *
      * @return JSON String containing a list of rooms
      * @throws InvalidAuthTokenException
+     * @throws NotFoundException
+     * @throws ResponseException
      */
-    public JSONString getRooms() throws InvalidAuthTokenException {
+    public JSONString getRooms() throws InvalidAuthTokenException, NotFoundException, ResponseException {
         final String url = API_BASE_URI + "/v1/rooms/list";
         return jsonGet(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.
      */
-    private JSONString jsonGet(String url) {
+    private JSONString jsonGet(String url) throws ResponseException {
         String authToken = configurationManager.getHipChatAuthToken();
         if (StringUtils.isEmpty(authToken) || StringUtils.isEmpty(url)) {
             return new JSONString("");
         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 {
-            return new JSONString(request.executeAndReturn(new ResponseBodyReturningHandler()));
-        }
-        catch (ResponseException e) {
-            log.error("Failed to retrieve user", e);
-        }
-        return new JSONString("");
+        return new JSONString(request.executeAndReturn(new ResponseBodyReturningHandler()));
     }
 
     private class ResponseBodyReturningHandler implements ReturningResponseHandler<Response, String> {
             if(response.getStatusCode() == 401) {
                 throw new InvalidAuthTokenException();
             }
+            else if(response.getStatusCode() == 404) {
+                throw new NotFoundException(response.getResponseBodyAsString());
+            }
+            else if(!response.isSuccessful()) {
+                throw new ResponseException(response.getResponseBodyAsString());
+            }
+
             return response.getResponseBodyAsString();
         }
     }

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

 
 
 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)
     @Produces({MediaType.APPLICATION_JSON})
     public Response getUser(@QueryParam("user_id") String userId)
     {
-        return Response.ok(client.getUser(userId).toString()).build();
+        try
+        {
+            return Response.ok(client.getUser(userId).toString()).build();
+        }
+        catch(ResponseException ex)
+        {
+            log.error("Could not get hipchat user with id: {} with Msg: {} ", userId, ex.getMessage());
+            log.warn("More details: ", ex);
+            return Response.serverError().build();
+        }
     }
 
 }

src/main/resources/js/status.js

                     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');
                 }
             });
-
         }
     });