Commits

Steve Lancashire committed 89e9c70

CONFDEV-10902 simplified exception handling and removed dependency on sun NotFoundException in response to code review

  • Participants
  • Parent commits 15cd219

Comments (0)

Files changed (4)

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

 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);
         } else {
             try {
                 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);
+            } 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;
             }
 

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

 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 com.sun.jersey.api.NotFoundException;
 
 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 ResponseException
-     * @throws NotFoundException
-     * @throws InvalidAuthTokenException
+     * @throws WebApplicationException if the response from hipchat is not successful
      */
-    public JSONString getUser(final String userId) throws ResponseException, NotFoundException, InvalidAuthTokenException {
+    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 NotFoundException
-     * @throws ResponseException
+     * @throws WebApplicationException if the response from hipchat is not successful
      */
-    public JSONString getRooms() throws InvalidAuthTokenException, NotFoundException, ResponseException {
+    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) throws ResponseException {
+    private JSONString jsonGet(String url) {
         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);
-        return new JSONString(request.executeAndReturn(new ResponseBodyReturningHandler()));
+        try
+        {
+            return new JSONString(request.executeAndReturn(new ResponseBodyReturningHandler()));
+        }
+        catch(ResponseException ex)
+        {
+            throw new WebApplicationException(ex, 500);
+        }
     }
 
     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());
             }
-            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

     @Produces({MediaType.APPLICATION_JSON})
     public Response getUser(@QueryParam("user_id") String userId)
     {
-        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();
-        }
+        return Response.ok(client.getUser(userId).toString()).build();
     }
-
 }

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

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