com.sun.jersey.api.container.ContainerException Java Examples

The following examples show how to use com.sun.jersey.api.container.ContainerException. 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: ConsoleEndPointServer.java    From db with GNU Affero General Public License v3.0 6 votes vote down vote up
@PostConstruct
private void init() {
    final Map<String, List<String>> portConfigData = ConfigUtil.getConfigData(Constants.APP_CONFIG_FILE, "/config/ports/port");
    final int port = Integer.parseInt(portConfigData.get(this.getClass().getName()).get(0));
    final Map<String, List<String>> packageConfigData = ConfigUtil.getConfigData(Constants.APP_CONFIG_FILE, "/config/resources/package");
    final List<String> packageList = packageConfigData.get(this.getClass().getName());

    try {
        final ResourceConfig resourceConfig = new PackagesResourceConfig(packageList.toArray(new String[packageList.size()]));
        final String serverAddress = "localhost";

        logger.debug("Attempting to start CLI listener at address {} with port {}", serverAddress, port);
        new Thread(new ConsoleEndPointServerSocket(port)).start();
        logger.info("CLI listener has started. CLI end point for database is ready."); // this is obviously non complete code
    } catch (ContainerException ex) {
        logger.error("CLI specified by {} couldn't be started because no root resource classes could be found in the listed packages ({}). Confirm if the packages have been refactored.", this.getClass(), packageList);
    }
}
 
Example #2
Source File: HttpFSExceptionProvider.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Maps different exceptions thrown by HttpFSServer to HTTP status codes.
 * <ul>
 * <li>SecurityException : HTTP UNAUTHORIZED</li>
 * <li>FileNotFoundException : HTTP NOT_FOUND</li>
 * <li>IOException : INTERNAL_HTTP SERVER_ERROR</li>
 * <li>UnsupporteOperationException : HTTP BAD_REQUEST</li>
 * <li>all other exceptions : HTTP INTERNAL_SERVER_ERROR </li>
 * </ul>
 *
 * @param throwable exception thrown.
 *
 * @return mapped HTTP status code
 */
@Override
public Response toResponse(Throwable throwable) {
  Response.Status status;
  if (throwable instanceof FileSystemAccessException) {
    throwable = throwable.getCause();
  }
  if (throwable instanceof ContainerException) {
    throwable = throwable.getCause();
  }
  if (throwable instanceof SecurityException) {
    status = Response.Status.UNAUTHORIZED;
  } else if (throwable instanceof FileNotFoundException) {
    status = Response.Status.NOT_FOUND;
  } else if (throwable instanceof IOException) {
    status = Response.Status.INTERNAL_SERVER_ERROR;
  } else if (throwable instanceof UnsupportedOperationException) {
    status = Response.Status.BAD_REQUEST;
  } else if (throwable instanceof IllegalArgumentException) {
    status = Response.Status.BAD_REQUEST;
  } else {
    status = Response.Status.INTERNAL_SERVER_ERROR;
  }
  return createResponse(status, throwable);
}
 
Example #3
Source File: HttpFSExceptionProvider.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Maps different exceptions thrown by HttpFSServer to HTTP status codes.
 * <ul>
 * <li>SecurityException : HTTP UNAUTHORIZED</li>
 * <li>FileNotFoundException : HTTP NOT_FOUND</li>
 * <li>IOException : INTERNAL_HTTP SERVER_ERROR</li>
 * <li>UnsupporteOperationException : HTTP BAD_REQUEST</li>
 * <li>all other exceptions : HTTP INTERNAL_SERVER_ERROR </li>
 * </ul>
 *
 * @param throwable exception thrown.
 *
 * @return mapped HTTP status code
 */
@Override
public Response toResponse(Throwable throwable) {
  Response.Status status;
  if (throwable instanceof FileSystemAccessException) {
    throwable = throwable.getCause();
  }
  if (throwable instanceof ContainerException) {
    throwable = throwable.getCause();
  }
  if (throwable instanceof SecurityException) {
    status = Response.Status.UNAUTHORIZED;
  } else if (throwable instanceof FileNotFoundException) {
    status = Response.Status.NOT_FOUND;
  } else if (throwable instanceof IOException) {
    status = Response.Status.INTERNAL_SERVER_ERROR;
  } else if (throwable instanceof UnsupportedOperationException) {
    status = Response.Status.BAD_REQUEST;
  } else if (throwable instanceof IllegalArgumentException) {
    status = Response.Status.BAD_REQUEST;
  } else {
    status = Response.Status.INTERNAL_SERVER_ERROR;
  }
  return createResponse(status, throwable);
}
 
Example #4
Source File: RequestDecoder.java    From jersey-hmac-auth with Apache License 2.0 6 votes vote down vote up
/**
 * Under normal circumstances, the body of the request can only be read once, because it is
 * backed by an {@code InputStream}, and thus is not easily consumed multiple times. This
 * method gets the request content and resets it so it can be read again later if necessary.
 */
private byte[] safelyGetContent(HttpRequestContext request) {
    ContainerRequest containerRequest = (ContainerRequest) request;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream in = containerRequest.getEntityInputStream();

    try {
        ReaderWriter.writeTo(in, out);
        byte[] content = out.toByteArray();

        // Reset the input stream so that it can be read again by another filter or resource
        containerRequest.setEntityInputStream(new ByteArrayInputStream(content));
        return content;

    } catch (IOException ex) {
        throw new ContainerException(ex);
    }
}
 
Example #5
Source File: KMSExceptionsProvider.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Maps different exceptions thrown by KMS to HTTP status codes.
 */
@Override
public Response toResponse(Exception exception) {
  Response.Status status;
  boolean doAudit = true;
  Throwable throwable = exception;
  if (exception instanceof ContainerException) {
    throwable = exception.getCause();
  }
  if (throwable instanceof SecurityException) {
    status = Response.Status.FORBIDDEN;
  } else if (throwable instanceof AuthenticationException) {
    status = Response.Status.FORBIDDEN;
    // we don't audit here because we did it already when checking access
    doAudit = false;
  } else if (throwable instanceof AuthorizationException) {
    status = Response.Status.FORBIDDEN;
    // we don't audit here because we did it already when checking access
    doAudit = false;
  } else if (throwable instanceof AccessControlException) {
    status = Response.Status.FORBIDDEN;
  } else if (exception instanceof IOException) {
    status = Response.Status.INTERNAL_SERVER_ERROR;
  } else if (exception instanceof UnsupportedOperationException) {
    status = Response.Status.BAD_REQUEST;
  } else if (exception instanceof IllegalArgumentException) {
    status = Response.Status.BAD_REQUEST;
  } else {
    status = Response.Status.INTERNAL_SERVER_ERROR;
  }
  if (doAudit) {
    KMSWebApp.getKMSAudit().error(KMSMDCFilter.getUgi(),
        KMSMDCFilter.getMethod(),
        KMSMDCFilter.getURL(), getOneLineMessage(exception));
  }
  return createResponse(status, throwable);
}
 
Example #6
Source File: KMSExceptionsProvider.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Maps different exceptions thrown by KMS to HTTP status codes.
 */
@Override
public Response toResponse(Exception exception) {
  Response.Status status;
  boolean doAudit = true;
  Throwable throwable = exception;
  if (exception instanceof ContainerException) {
    throwable = exception.getCause();
  }
  if (throwable instanceof SecurityException) {
    status = Response.Status.FORBIDDEN;
  } else if (throwable instanceof AuthenticationException) {
    status = Response.Status.FORBIDDEN;
    // we don't audit here because we did it already when checking access
    doAudit = false;
  } else if (throwable instanceof AuthorizationException) {
    status = Response.Status.FORBIDDEN;
    // we don't audit here because we did it already when checking access
    doAudit = false;
  } else if (throwable instanceof AccessControlException) {
    status = Response.Status.FORBIDDEN;
  } else if (exception instanceof IOException) {
    status = Response.Status.INTERNAL_SERVER_ERROR;
  } else if (exception instanceof UnsupportedOperationException) {
    status = Response.Status.BAD_REQUEST;
  } else if (exception instanceof IllegalArgumentException) {
    status = Response.Status.BAD_REQUEST;
  } else {
    status = Response.Status.INTERNAL_SERVER_ERROR;
  }
  if (doAudit) {
    KMSWebApp.getKMSAudit().error(KMSMDCFilter.getUgi(),
        KMSMDCFilter.getMethod(),
        KMSMDCFilter.getURL(), getOneLineMessage(exception));
  }
  return createResponse(status, throwable);
}
 
Example #7
Source File: NettyContainerProvider.java    From karyon with Apache License 2.0 5 votes vote down vote up
@Override
public NettyContainer createContainer(Class<NettyContainer> type, ResourceConfig resourceConfig,
                                      WebApplication application) throws ContainerException {
    Preconditions.checkNotNull(type);
    Preconditions.checkNotNull(application);
    if (!type.equals(NettyContainer.class)) {
        logger.error(
                "Netty container provider can only create container of type {}. Invoked to create container of type {}",
                NettyContainer.class.getName(), type.getName());
    }
    return new NettyContainer(application);
}
 
Example #8
Source File: JerseyContainerProvider.java    From recipes-rss with Apache License 2.0 5 votes vote down vote up
public NettyHandlerContainer createContainer(Class<NettyHandlerContainer> clazz, ResourceConfig config,WebApplication webApp)
           throws ContainerException {
	if (clazz != NettyHandlerContainer.class) {
		return null;
	}
	return new NettyHandlerContainer(webApp, config);
}
 
Example #9
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 #10
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 #11
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 #12
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 #13
Source File: KMSExceptionsProvider.java    From ranger with Apache License 2.0 4 votes vote down vote up
/**
 * Maps different exceptions thrown by KMS to HTTP status codes.
 */
@Override
public Response toResponse(Exception exception) {
  Response.Status status;
  boolean doAudit = true;
  Throwable throwable = exception;
  if (exception instanceof ContainerException) {
    throwable = exception.getCause();
  }
  if (throwable instanceof SecurityException) {
    status = Response.Status.FORBIDDEN;
  } else if (throwable instanceof AuthenticationException) {
    status = Response.Status.FORBIDDEN;
    // we don't audit here because we did it already when checking access
    doAudit = false;
  } else if (throwable instanceof AuthorizationException) {
    status = Response.Status.FORBIDDEN;
    // we don't audit here because we did it already when checking access
    doAudit = false;
  } else if (throwable instanceof AccessControlException) {
    status = Response.Status.FORBIDDEN;
  } else if (exception instanceof IOException) {
    status = Response.Status.INTERNAL_SERVER_ERROR;
    log(status, throwable);
  } else if (exception instanceof UnsupportedOperationException) {
    status = Response.Status.BAD_REQUEST;
  } else if (exception instanceof IllegalArgumentException) {
    status = Response.Status.BAD_REQUEST;
  } else {
    status = Response.Status.INTERNAL_SERVER_ERROR;
    log(status, throwable);
  }
  if (doAudit) {
    KMSWebApp.getKMSAudit().error(KMSMDCFilter.getUgi(),
        KMSMDCFilter.getMethod(),
        KMSMDCFilter.getURL(), getOneLineMessage(exception));
  }
  EXCEPTION_LOG.warn("User {} request {} {} caused exception.",
    KMSMDCFilter.getUgi(), KMSMDCFilter.getMethod(),
    KMSMDCFilter.getURL(), exception);
  return createResponse(status, throwable);
}