com.sun.jersey.api.ParamException Java Examples

The following examples show how to use com.sun.jersey.api.ParamException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TitusExceptionMapper.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private String toStandardHttpErrorMessage(int status, Throwable cause) {
    // Do not use message from Jersey exceptions, as we can do better
    if (cause instanceof ParamException) {
        ParamException pe = (ParamException) cause;
        return "invalid parameter " + pe.getParameterName() + "=" + pe.getDefaultStringValue() + " of type " + pe.getParameterType();
    }
    if (cause instanceof NotFoundException) {
        NotFoundException nfe = (NotFoundException) cause;
        return "resource not found: " + nfe.getNotFoundUri();
    }
    if (cause.getMessage() != null) {
        return cause.getMessage();
    }
    try {
        return Status.fromStatusCode(status).getReasonPhrase();
    } catch (Exception e) {
        return "HTTP error " + status;
    }
}
 
Example #2
Source File: Jersey1WebApplicationExceptionHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@DataProvider
public static Object[][] dataProviderForShouldHandleException() {
    return new Object[][] {
        { new NotFoundException(), testProjectApiErrors.getNotFoundApiError() },
        { mock(ParamException.URIParamException.class), testProjectApiErrors.getNotFoundApiError() },
        { mock(ParamException.class), testProjectApiErrors.getMalformedRequestApiError() },
        { new WebApplicationException(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), testProjectApiErrors.getUnsupportedMediaTypeApiError() },
        { new WebApplicationException(HttpServletResponse.SC_METHOD_NOT_ALLOWED), testProjectApiErrors.getMethodNotAllowedApiError() },
        { new WebApplicationException(HttpServletResponse.SC_UNAUTHORIZED), testProjectApiErrors.getUnauthorizedApiError() },
        { new WebApplicationException(HttpServletResponse.SC_NOT_ACCEPTABLE), testProjectApiErrors.getNoAcceptableRepresentationApiError() },
        { mock(JsonProcessingException.class), testProjectApiErrors.getMalformedRequestApiError() }
    };
}
 
Example #3
Source File: ExceptionHandler.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public Response toResponse(Exception e) {
  if (LOG.isTraceEnabled()) {
    LOG.trace("GOT EXCEPITION", e);
  }

  //clear content type
  response.setContentType(null);

  //Convert exception
  if (e instanceof ParamException) {
    final ParamException paramexception = (ParamException)e;
    e = new IllegalArgumentException("Invalid value for webhdfs parameter \""
        + paramexception.getParameterName() + "\": "
        + e.getCause().getMessage(), e);
  }
  if (e instanceof ContainerException) {
    e = toCause(e);
  }
  if (e instanceof RemoteException) {
    e = ((RemoteException)e).unwrapRemoteException();
  }

  if (e instanceof SecurityException) {
    e = toCause(e);
  }
  
  //Map response status
  final Response.Status s;
  if (e instanceof SecurityException) {
    s = Response.Status.FORBIDDEN;
  } else if (e instanceof AuthorizationException) {
    s = Response.Status.FORBIDDEN;
  } else if (e instanceof FileNotFoundException) {
    s = Response.Status.NOT_FOUND;
  } else if (e instanceof IOException) {
    s = Response.Status.FORBIDDEN;
  } else if (e instanceof UnsupportedOperationException) {
    s = Response.Status.BAD_REQUEST;
  } else if (e instanceof IllegalArgumentException) {
    s = Response.Status.BAD_REQUEST;
  } else {
    LOG.warn("INTERNAL_SERVER_ERROR", e);
    s = Response.Status.INTERNAL_SERVER_ERROR;
  }
 
  final String js = JsonUtil.toJsonString(e);
  return Response.status(s).type(MediaType.APPLICATION_JSON).entity(js).build();
}
 
Example #4
Source File: ExceptionHandler.java    From hadoop with Apache License 2.0 4 votes vote down vote up
static DefaultFullHttpResponse exceptionCaught(Throwable cause) {
  Exception e = cause instanceof Exception ? (Exception) cause : new Exception(cause);

  if (LOG.isTraceEnabled()) {
    LOG.trace("GOT EXCEPITION", e);
  }

  //Convert exception
  if (e instanceof ParamException) {
    final ParamException paramexception = (ParamException)e;
    e = new IllegalArgumentException("Invalid value for webhdfs parameter \""
                                       + paramexception.getParameterName() + "\": "
                                       + e.getCause().getMessage(), e);
  } else if (e instanceof ContainerException || e instanceof SecurityException) {
    e = toCause(e);
  } else if (e instanceof RemoteException) {
    e = ((RemoteException)e).unwrapRemoteException();
  }

  //Map response status
  final HttpResponseStatus s;
  if (e instanceof SecurityException) {
    s = FORBIDDEN;
  } else if (e instanceof AuthorizationException) {
    s = FORBIDDEN;
  } else if (e instanceof FileNotFoundException) {
    s = NOT_FOUND;
  } else if (e instanceof IOException) {
    s = FORBIDDEN;
  } else if (e instanceof UnsupportedOperationException) {
    s = BAD_REQUEST;
  } else if (e instanceof IllegalArgumentException) {
    s = BAD_REQUEST;
  } else {
    LOG.warn("INTERNAL_SERVER_ERROR", e);
    s = INTERNAL_SERVER_ERROR;
  }

  final byte[] js = JsonUtil.toJsonString(e).getBytes(Charsets.UTF_8);
  DefaultFullHttpResponse resp =
    new DefaultFullHttpResponse(HTTP_1_1, s, Unpooled.wrappedBuffer(js));

  resp.headers().set(CONTENT_TYPE, APPLICATION_JSON_UTF8);
  resp.headers().set(CONTENT_LENGTH, js.length);
  return resp;
}
 
Example #5
Source File: ExceptionHandler.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public Response toResponse(Exception e) {
  if (LOG.isTraceEnabled()) {
    LOG.trace("GOT EXCEPITION", e);
  }

  //clear content type
  response.setContentType(null);

  //Convert exception
  if (e instanceof ParamException) {
    final ParamException paramexception = (ParamException)e;
    e = new IllegalArgumentException("Invalid value for webhdfs parameter \""
        + paramexception.getParameterName() + "\": "
        + e.getCause().getMessage(), e);
  }
  if (e instanceof ContainerException) {
    e = toCause(e);
  }
  if (e instanceof RemoteException) {
    e = ((RemoteException)e).unwrapRemoteException();
  }

  if (e instanceof SecurityException) {
    e = toCause(e);
  }
  
  //Map response status
  final Response.Status s;
  if (e instanceof SecurityException) {
    s = Response.Status.FORBIDDEN;
  } else if (e instanceof AuthorizationException) {
    s = Response.Status.FORBIDDEN;
  } else if (e instanceof FileNotFoundException) {
    s = Response.Status.NOT_FOUND;
  } else if (e instanceof IOException) {
    s = Response.Status.FORBIDDEN;
  } else if (e instanceof UnsupportedOperationException) {
    s = Response.Status.BAD_REQUEST;
  } else if (e instanceof IllegalArgumentException) {
    s = Response.Status.BAD_REQUEST;
  } else {
    LOG.warn("INTERNAL_SERVER_ERROR", e);
    s = Response.Status.INTERNAL_SERVER_ERROR;
  }
 
  final String js = JsonUtil.toJsonString(e);
  return Response.status(s).type(MediaType.APPLICATION_JSON).entity(js).build();
}
 
Example #6
Source File: ExceptionHandler.java    From big-c with Apache License 2.0 4 votes vote down vote up
static DefaultFullHttpResponse exceptionCaught(Throwable cause) {
  Exception e = cause instanceof Exception ? (Exception) cause : new Exception(cause);

  if (LOG.isTraceEnabled()) {
    LOG.trace("GOT EXCEPITION", e);
  }

  //Convert exception
  if (e instanceof ParamException) {
    final ParamException paramexception = (ParamException)e;
    e = new IllegalArgumentException("Invalid value for webhdfs parameter \""
                                       + paramexception.getParameterName() + "\": "
                                       + e.getCause().getMessage(), e);
  } else if (e instanceof ContainerException || e instanceof SecurityException) {
    e = toCause(e);
  } else if (e instanceof RemoteException) {
    e = ((RemoteException)e).unwrapRemoteException();
  }

  //Map response status
  final HttpResponseStatus s;
  if (e instanceof SecurityException) {
    s = FORBIDDEN;
  } else if (e instanceof AuthorizationException) {
    s = FORBIDDEN;
  } else if (e instanceof FileNotFoundException) {
    s = NOT_FOUND;
  } else if (e instanceof IOException) {
    s = FORBIDDEN;
  } else if (e instanceof UnsupportedOperationException) {
    s = BAD_REQUEST;
  } else if (e instanceof IllegalArgumentException) {
    s = BAD_REQUEST;
  } else {
    LOG.warn("INTERNAL_SERVER_ERROR", e);
    s = INTERNAL_SERVER_ERROR;
  }

  final byte[] js = JsonUtil.toJsonString(e).getBytes(Charsets.UTF_8);
  DefaultFullHttpResponse resp =
    new DefaultFullHttpResponse(HTTP_1_1, s, Unpooled.wrappedBuffer(js));

  resp.headers().set(CONTENT_TYPE, APPLICATION_JSON_UTF8);
  resp.headers().set(CONTENT_LENGTH, js.length);
  return resp;
}
 
Example #7
Source File: Jersey1WebApplicationExceptionHandlerListener.java    From backstopper with Apache License 2.0 4 votes vote down vote up
@Override
public ApiExceptionHandlerListenerResult shouldHandleException(Throwable ex) {

    ApiExceptionHandlerListenerResult result;
    SortedApiErrorSet handledErrors = null;
    List<Pair<String, String>> extraDetailsForLogging = new ArrayList<>();

    if (ex instanceof NotFoundException) {
        handledErrors = singletonSortedSetOf(projectApiErrors.getNotFoundApiError());
    }
    else if (ex instanceof ParamException.URIParamException) {
        utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging);
        // Returning a 404 is intentional here.
        //      The Jersey contract for URIParamException states it should map to a 404.
        handledErrors = singletonSortedSetOf(projectApiErrors.getNotFoundApiError());
    }
    else if (ex instanceof ParamException) {
        utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging);
        handledErrors = singletonSortedSetOf(projectApiErrors.getMalformedRequestApiError());
    }
    else if (ex instanceof WebApplicationException) {
        utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging);
        WebApplicationException webex = (WebApplicationException) ex;
        Response webExResponse = webex.getResponse();
        if (webExResponse != null) {
            int webExStatusCode = webExResponse.getStatus();
            if (webExStatusCode == HttpServletResponse.SC_NOT_ACCEPTABLE) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getNoAcceptableRepresentationApiError());
            }
            else if (webExStatusCode == HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getUnsupportedMediaTypeApiError());
            }
            else if (webExStatusCode == HttpServletResponse.SC_METHOD_NOT_ALLOWED) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getMethodNotAllowedApiError());
            }
            else if (webExStatusCode == HttpServletResponse.SC_UNAUTHORIZED) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getUnauthorizedApiError());
            }
        }
    }
    else if (ex instanceof JsonProcessingException) {
        utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging);
        handledErrors = singletonSortedSetOf(projectApiErrors.getMalformedRequestApiError());
    }

    // Return an indication that we will handle this exception if handledErrors got set
    if (handledErrors != null) {
        result = ApiExceptionHandlerListenerResult.handleResponse(handledErrors, extraDetailsForLogging);
    }
    else {
        result = ApiExceptionHandlerListenerResult.ignoreResponse();
    }

    return result;
}