/** * Get an OutboundJaxrsResponse instance for a given JAX-RS response. * * @param response response instance to from. * @return corresponding {@code OutboundJaxrsResponse} instance. */ public static OutboundJaxrsResponse from(javax.ws.rs.core.Response response) { if (response instanceof OutboundJaxrsResponse) { return (OutboundJaxrsResponse) response; } else { final StatusType status = response.getStatusInfo(); final OutboundMessageContext context = new OutboundMessageContext(); context.getHeaders().putAll(response.getMetadata()); context.setEntity(response.getEntity()); return new OutboundJaxrsResponse(status, context); } }
@Override public OutputStream getEntityStream() { return messageContext.getEntityStream(); }
@Override public MultivaluedMap<String, String> getStringHeaders() { return context.getStringHeaders(); }
@Override public javax.ws.rs.core.Response build() { StatusType st = status; if (st == null) { st = context.hasEntity() ? Status.OK : Status.NO_CONTENT; } return new OutboundJaxrsResponse(st, new OutboundMessageContext(context)); }
@Override public void close() throws ProcessingException { closed = true; context.close(); if (buffered) { // release buffer context.setEntity(null); } else if (context.hasEntity() && InputStream.class.isAssignableFrom(context.getEntityClass())) { try { InputStream.class.cast(context.getEntity()).close(); } catch (IOException ex) { throw new ProcessingException(ex); } } }
/** * Get the allowed HTTP methods from the Allow HTTP header. * * @return the allowed HTTP methods, all methods will returned as upper case * strings. */ public Set<String> getAllowedMethods() { final String allowed = getHeaderString(HttpHeaders.ALLOW); if (allowed == null || allowed.isEmpty()) { return Collections.emptySet(); } try { return new HashSet<String>(HttpHeaderReader.readStringList(allowed)); } catch (java.text.ParseException e) { throw exception(HttpHeaders.ALLOW, allowed, e); } }
/** * Closes the context. Flushes and closes the entity stream. */ public void close() { if (hasEntity()) { try { final OutputStream es = getEntityStream(); es.flush(); es.close(); } catch (IOException e) { // Happens when the client closed connection before receiving the full response. // This is OK and not interesting in vast majority of the cases // hence the log level set to FINE to make sure it does not flood the log unnecessarily // (especially for clients disconnecting from SSE listening, which is very common). Logger.getLogger(OutboundMessageContext.class.getName()).log(Level.FINE, e.getMessage(), e); } finally { // In case some of the output stream wrapper does not delegate close() call we // close the root stream manually to make sure it commits the data. if (!committingOutputStream.isClosed()) { try { committingOutputStream.close(); } catch (IOException e) { // Just log the exception Logger.getLogger(OutboundMessageContext.class.getName()).log(Level.FINE, e.getMessage(), e); } } } } } }
@Override public Set<String> getAllowedMethods() { return messageContext.getAllowedMethods(); }
@Override public Date getDate() { return messageContext.getDate(); }
@Override public EntityTag getEntityTag() { return context.getEntityTag(); }
@Override public String getHeaderString(String name) { return context.getHeaderString(name); }
@Override public ResponseBuilder createResponseBuilder() { return new OutboundJaxrsResponse.Builder(new OutboundMessageContext()); }
/** * Closes the response. Flushes and closes the entity stream, frees up container resources associated with * the corresponding request. */ public void close() { if (!closed) { closed = true; messageContext.close(); requestContext.getResponseWriter().commit(); } }
@Override public Object getEntity() { return messageContext.getEntity(); }
@Override public Class<?> getEntityClass() { return messageContext.getEntityClass(); }
@Override public void close() throws ProcessingException { closed = true; context.close(); if (buffered) { // release buffer context.setEntity(null); } else if (context.hasEntity() && InputStream.class.isAssignableFrom(context.getEntityClass())) { try { InputStream.class.cast(context.getEntity()).close(); } catch (IOException ex) { throw new ProcessingException(ex); } } }
@Override public javax.ws.rs.core.Response build() { StatusType st = status; if (st == null) { st = context.hasEntity() ? Status.OK : Status.NO_CONTENT; } return new OutboundJaxrsResponse(st, new OutboundMessageContext(context)); }
/** * Closes the context. Flushes and closes the entity stream. */ public void close() { if (hasEntity()) { try { final OutputStream es = getEntityStream(); es.flush(); es.close(); } catch (IOException e) { // Happens when the client closed connection before receiving the full response. // This is OK and not interesting in vast majority of the cases // hence the log level set to FINE to make sure it does not flood the log unnecessarily // (especially for clients disconnecting from SSE listening, which is very common). Logger.getLogger(OutboundMessageContext.class.getName()).log(Level.FINE, e.getMessage(), e); } finally { // In case some of the output stream wrapper does not delegate close() call we // close the root stream manually to make sure it commits the data. if (!committingOutputStream.isClosed()) { try { committingOutputStream.close(); } catch (IOException e) { // Just log the exception Logger.getLogger(OutboundMessageContext.class.getName()).log(Level.FINE, e.getMessage(), e); } } } } } }
@Override public Set<String> getAllowedMethods() { return context.getAllowedMethods(); }
@Override public Date getDate() { return messageContext.getDate(); }