Refine search
private void onException(Throwable e, Response r, boolean mapped) { if (request.isTracingEnabled()) { Response.Status s = Response.Status.fromStatusCode(r.getStatus()); if (s != null) { request.trace(String.format("mapped exception to response: %s -> %d (%s)", ReflectionHelper.objectToString(e), r.getStatus(), s.getReasonPhrase())); } else { request.trace(String.format("mapped exception to response: %s -> %d", ReflectionHelper.objectToString(e), r.getStatus())); } } if (!mapped && r.getStatus() >= 500) { logException(e, r, Level.SEVERE); } else if (LOGGER.isLoggable(Level.FINE)) { logException(e, r, Level.FINE); } setResponse(r); this.mappedThrowable = e; if (getEntity() != null && getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE) == null) { Object m = request.getProperties().get(HttpMethodRule.CONTENT_TYPE_PROPERTY); if (m != null) { request.getProperties().remove(HttpMethodRule.CONTENT_TYPE_PROPERTY); getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE, m); } } }
public OutputStream writeStatusAndHeaders(long contentLength, ContainerResponse cResponse) throws IOException { Headers eh = exchange.getResponseHeaders(); for (Map.Entry<String, List<Object>> e : cResponse.getHttpHeaders().entrySet()) { List<String> values = new ArrayList<String>(); for (Object v : e.getValue()) values.add(ContainerResponse.getHeaderValue(v)); eh.put(e.getKey(), values); } if (cResponse.getStatus() == 204) { // Work around bug in LW HTTP server // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6886436 exchange.sendResponseHeaders(cResponse.getStatus(), -1); } else { exchange.sendResponseHeaders(cResponse.getStatus(), getResponseLength(contentLength)); } return exchange.getResponseBody(); }
@Override public Response getResponse() { return acr.getResponse(); }
public ContainerResponse filter(ContainerRequest request, ContainerResponse response) { response.getHttpHeaders().add(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); // add vary header String acceptEncoding = request.getRequestHeaders().getFirst(HttpHeaders.ACCEPT_ENCODING); String contentEncoding = (String) response.getHttpHeaders().getFirst(HttpHeaders.CONTENT_ENCODING); if (acceptEncoding != null && contentEncoding == null && acceptEncoding.contains("gzip")) { // Check EntityTag header if (response.getHttpHeaders().containsKey(HttpHeaders.ETAG)) { EntityTag entityTag = (EntityTag) response.getHttpHeaders().getFirst(HttpHeaders.ETAG); if (entityTag != null) { response.getHttpHeaders().putSingle(HttpHeaders.ETAG, new EntityTag(entityTag.getValue() + ENTITY_TAG_GZIP_SUFFIX_VALUE, entityTag.isWeak())); } } // wrap entity with gzip if (response.getEntity() != null) { response.getHttpHeaders().add(HttpHeaders.CONTENT_ENCODING, "gzip"); response.setContainerResponseWriter(new Adapter(response.getContainerResponseWriter())); } } return response; } }
if (cr.getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE) != null) { m = cr.getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE).toString(); cr.setResponse((Response) e.getMessage()); cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m); cr.write(); try { cr.getOutputStream().flush(); } catch (IOException ex) { logger.trace("", ex); cr.setResponse(Response.ok(msg).build()); cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m); cr.write(); cr.getOutputStream().flush(); } catch (IOException ex) { logger.trace("", ex); cr.setResponse(Response.ok(e.getMessage()).build()); cr.getHttpHeaders().add(HttpHeaders.CONTENT_TYPE, m); cr.write(); try { cr.getOutputStream().flush(); } catch (IOException ex) { logger.trace("", ex); cr.setEntity(null);
requestUri); cRequest.setSecurityContext(new SecurityContext() { public Principal getUserPrincipal() { return request.getUserPrincipal(); return w.cResponse.getStatus(); } catch (WebApplicationException ex) { final Response exResponse = ex.getResponse(); final String entity = exResponse.getEntity() != null ? exResponse.getEntity().toString() : null; response.sendError(exResponse.getStatus(), entity); return exResponse.getStatus();
if (request.isTracingEnabled()) { configureTrace(responseWriter); if (!getHttpHeaders().containsKey(HttpHeaders.VARY)) { final String varyHeader = (String)request.getProperties().get(ContainerRequest.VARY_HEADER); if (varyHeader != null) { getHttpHeaders().add(HttpHeaders.VARY, varyHeader); MediaType contentType = getMediaType(); if (contentType == null) { contentType = getMessageBodyWorkers().getMessageBodyWriterMediaType( entity.getClass(), entityType, annotations, request.getAcceptableMediaTypes()); if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) contentType = MediaType.APPLICATION_OCTET_STREAM_TYPE; getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE, contentType); final MessageBodyWriter writer = getMessageBodyWorkers().getMessageBodyWriter( entity.getClass(), entityType, annotations, contentType); Map<MediaType, List<MessageBodyWriter>> m = getMessageBodyWorkers(). getWriters(contentType); LOGGER.severe(message + "The registered message body writers compatible with the MIME media type are:\n" + getMessageBodyWorkers().writersToString(m));
MediaType contentType = response.getMediaType(); if (contentType == null && response.getEntity() != null) { LinkedList<MediaType> l = new LinkedList<MediaType>(); l.add(request.getAcceptableMediaType(new LinkedList<MediaType>())); contentType = response.getMessageBodyWorkers().getMessageBodyWriterMediaType( response.getEntity().getClass(), response.getEntityType(), response.getAnnotations(), l); Object entity = response.getEntity(); Response.ResponseBuilder b = Response.ok(); b = configureHeaders(b); response.setResponse(b.entity(entity).build()); response.write(); response.setEntity(null); r.suspend(timeout); } catch (IOException ex) {
@Override public OutputStream writeStatusAndHeaders(long contentLength, ContainerResponse response) { int responseStatus = response.getStatus(); serverResponse.setStatus(HttpResponseStatus.valueOf(responseStatus)); HttpResponseHeaders responseHeaders = serverResponse.getHeaders(); for(Map.Entry<String, List<Object>> header : response.getHttpHeaders().entrySet()){ responseHeaders.setHeader(header.getKey(), header.getValue()); } return new ByteBufOutputStream(contentBuffer); }
@Override public MediaType getMediaType() { final Object mediaTypeHeader = getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE); if (mediaTypeHeader instanceof MediaType) { return (MediaType)mediaTypeHeader; } else if (mediaTypeHeader != null) { return MediaType.valueOf(mediaTypeHeader.toString()); } return null; }
private boolean isJsonResponse(ContainerResponse response) { final MultivaluedMap<String, Object> httpHeaders = response.getHttpHeaders(); return httpHeaders.containsKey(CONTENT_TYPE) && httpHeaders.getFirst(CONTENT_TYPE).equals(MediaType.APPLICATION_JSON_TYPE); } }
public ContainerResponse filter(ContainerRequest request, ContainerResponse containerResponse) { if (!response.containsHeader(ANTI_SNIFFING_HEADER_NAME) && !containerResponse.getHttpHeaders().containsKey(ANTI_SNIFFING_HEADER_NAME)) { containerResponse.getHttpHeaders().add(ANTI_SNIFFING_HEADER_NAME, ANTI_SNIFFING_HEADER_VALUE); } return containerResponse; } }
public void onHeader(String name, String value) { c.getContainerResponse().getHttpHeaders().add(name, value); } });
void writeHeaders() { if (contentLength != -1 && contentLength < Integer.MAX_VALUE) response.setContentLength((int) contentLength); MultivaluedMap<String, Object> headers = cResponse.getHttpHeaders(); for (Map.Entry<String, List<Object>> e : headers.entrySet()) { for (Object v : e.getValue()) { response.addHeader(e.getKey(), ContainerResponse.getHeaderValue(v)); } } } }
if (response.getStatus() == 204) { response.setStatus(200); URI location = null; if (useResumeAnnotation && !sessionSupported && !resumeOnBroadcast && response.getHttpHeaders().getFirst("Location") == null) { String uuid = UUID.randomUUID().toString(); if (response.getEntity() == null) { response.setEntity(""); if (response.getEntity() instanceof Broadcastable) { Broadcastable b = (Broadcastable) response.getEntity(); bc = b.getBroadcaster(); response.setEntity(b.getResponseMessage());
@Override public ContainerResponse filter(ContainerRequest containerRequest, ContainerResponse containerResponse) { LOGGER.trace("CORS filter called for request: {}", containerRequest); Response.ResponseBuilder response = Response.fromResponse(containerResponse.getResponse()); if ("OPTIONS".equals(containerRequest.getMethod())) { response.status(Status.NO_CONTENT); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_ORIGIN))) response.header(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_ALLOW_METHODS))) response.header(ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_REQUEST_METHOD))) response.header(ACCESS_CONTROL_REQUEST_METHOD, "GET, POST, PUT, DELETE, OPTIONS"); if (Objects.isNull(containerResponse.getHttpHeaders().get(ACCESS_CONTROL_MAX_AGE))) response.header(ACCESS_CONTROL_MAX_AGE, deltaSeconds); if (Objects.isNull(containerResponse.getHttpHeaders().get(CONTENT_TYPE))) response.header(CONTENT_TYPE, APPLICATION_JSON); if (Objects.isNull(containerResponse.getHttpHeaders().get(X_APPLICATION_ID))) response.header(X_APPLICATION_ID, applicationName); response.entity(null); String requestHeader = containerRequest.getHeaderValue(ACCESS_CONTROL_REQUEST_HEADERS); if (requestHeader != null) { response.header(ACCESS_CONTROL_ALLOW_HEADERS, requestHeader); } } containerResponse.setResponse(response.build()); return containerResponse; }
@Override public ContainerResponse filter(ContainerRequest request, ContainerResponse response) { MultivaluedMap<String, Object> headers = response.getHttpHeaders(); if (!headers.containsKey("Cache-Control") && !headers.containsKey("Expires")) { log.trace("Response does not have caching headers, adding 'Cache-Control: no-cache, no-store'"); headers.putSingle("Cache-Control", "no-cache, no-store, no-transform"); } return response; } }
@Override public ContainerResponse filter(ContainerRequest cres, ContainerResponse response) { initLazily(servletRequest); String requestOrigin = cres.getHeaderValue("Origin"); if (requestOrigin != null) { // is this is a cors request (ajax request that targets a different domain than the one the page was loaded from) if (allowedOrigin != null && allowedOrigin.startsWith(requestOrigin)) { // no cors allowances make are applied if allowedOrigins == null // only return the origin the client informed response.getHttpHeaders().add("Access-Control-Allow-Origin", requestOrigin); response.getHttpHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization"); response.getHttpHeaders().add("Access-Control-Allow-Credentials", "true"); response.getHttpHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); response.getHttpHeaders().add("Access-Control-Max-Age", "1209600"); } } return response; }
/** * Handle HTTP HEAD method (headers, ..) during writing a response. * * @param size size of a response entity. * @throws IOException if an error occurred when writing out the status and headers or obtaining the output stream. */ private void writeHttpHead(final long size) throws IOException { if (size != -1) { getHttpHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(size)); } isCommitted = true; responseWriter.writeStatusAndHeaders(size, this); // Close the InputStream if the entity is an instance of this type. if (entity instanceof InputStream) { ((InputStream)entity).close(); } }