public BuiltResponse invoke(HttpRequest request, HttpResponse response, Object target) { request.setAttribute(ResourceMethod.class.getName(), this); incrementMethodCount(request.getHttpMethod()); ResteasyUriInfo uriInfo = (ResteasyUriInfo) request.getUri(); uriInfo.pushCurrentResource(target); try { BuiltResponse jaxrsResponse = invokeOnTarget(request, response, target); if (jaxrsResponse != null && jaxrsResponse.getEntity() != null) { // if the content type isn't set, then set it to be either most desired type from the Accept // header // or the first media type in the @Produces annotation // See RESTEASY-144 Object type = jaxrsResponse.getMetadata().getFirst(HttpHeaderNames.CONTENT_TYPE); if (type == null) jaxrsResponse .getMetadata() .putSingle( HttpHeaderNames.CONTENT_TYPE, resolveContentType(request, jaxrsResponse.getEntity())); } return jaxrsResponse; } finally { uriInfo.popCurrentResource(); } }
public void handleException(HttpRequest request, HttpResponse response, Throwable e) { // See if there is an ExceptionMapper for the exact class of the exception instance being thrown if (executeExactExceptionMapper(request, response, e)) return; // These are wrapper exceptions so they need to be processed first as they map e.getCause() if (e instanceof ApplicationException) { handleApplicationException(request, response, (ApplicationException) e); return; } else if (e instanceof WriterException) { handleWriterException(request, response, (WriterException) e); return; } else if (e instanceof ReaderException) { handleReaderException(request, response, (ReaderException) e); return; } // First try and handle it with a mapper if (executeExceptionMapper(request, response, e)) { return; } // Otherwise do specific things else if (e instanceof WebApplicationException) { handleWebApplicationException(request, response, (WebApplicationException) e); } else if (e instanceof Failure) { handleFailure(request, response, (Failure) e); } else { logger.error( "Unknown exception while executing " + request.getHttpMethod() + " " + request.getUri().getPath(), e); throw new UnhandledException(e); } }
public static MockHttpRequest deepCopy(HttpRequest request) throws IOException { MockHttpRequest mock = new MockHttpRequest(); mock.uri = request.getUri(); mock.httpHeaders = (ResteasyHttpHeaders) request.getHttpHeaders(); mock.httpMethod = request.getHttpMethod(); byte[] bytes = ReadFromStream.readFromStream(1024, request.getInputStream()); mock.inputStream = new ByteArrayInputStream(bytes); return mock; }
public BuiltResponse invoke(HttpRequest request, HttpResponse response, Object target) { request.setAttribute(ResourceMethodInvoker.class.getName(), this); incrementMethodCount(request.getHttpMethod()); ResteasyUriInfo uriInfo = (ResteasyUriInfo) request.getUri(); if (method.getPath() != null) { uriInfo.pushMatchedURI(uriInfo.getMatchingPath()); } uriInfo.pushCurrentResource(target); BuiltResponse rtn = invokeOnTarget(request, response, target); return rtn; }
protected void handlePreflightRequest() { if (request.getHttpMethod().equalsIgnoreCase("OPTIONS")) { logger.debug("Cors admin pre-flight"); Response response = Cors.add(request, Response.ok()) .preflight() .allowedMethods("GET", "PUT", "POST", "DELETE") .auth() .build(); throw new NoLogWebApplicationException(response); } }
protected void handleFailure(HttpRequest request, HttpResponse response, Failure failure) { if (failure.isLoggable()) logger.error( "Failed executing " + request.getHttpMethod() + " " + request.getUri().getPath(), failure); else logger.debug( "Failed executing " + request.getHttpMethod() + " " + request.getUri().getPath(), failure); if (failure.getResponse() != null) { writeFailure(request, response, failure.getResponse()); } else { try { if (failure.getMessage() != null) { response.sendError(failure.getErrorCode(), failure.getMessage()); } else { response.sendError(failure.getErrorCode()); } } catch (IOException e1) { throw new UnhandledException(e1); } } }