Java Code Examples for javax.ws.rs.ext.WriterInterceptorContext#getHeaders()

The following examples show how to use javax.ws.rs.ext.WriterInterceptorContext#getHeaders() . 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: GZIPWriterInterceptor.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException {


    MultivaluedMap<String, Object> responseHeaders = context.getHeaders();
    Object rangeHeader = responseHeaders.getFirst("Content-Range");

    // Use a custom header here
    // Some clients needs to know the content length in response headers in order to display a loading state
    // Browsers don't let programmers to change the default "Accept-Encoding" header, then we use a custom one.
    String acceptEncoding = requestHeaders.getHeaderString("x-accept-encoding");

    GZIPOutputStream gzipOutputStream = null;

    if (acceptEncoding != null && acceptEncoding.equals("identity")) {
        responseHeaders.add("Content-Encoding", "identity");
    } else if (rangeHeader == null) {
        responseHeaders.add("Content-Encoding", "gzip");
        responseHeaders.remove("Content-Length");
        gzipOutputStream = new GZIPOutputStream(context.getOutputStream(), DEFAULT_BUFFER_SIZE);
        context.setOutputStream(gzipOutputStream);
    }

    try {
        context.proceed();
    } finally {
        if (gzipOutputStream != null) {
            gzipOutputStream.finish();
        }
    }
}
 
Example 2
Source File: GZIPWriterInterceptor.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
    MultivaluedMap<String,Object> headers = context.getHeaders();
    headers.add("Content-Encoding", "gzip");

    final OutputStream outputStream = context.getOutputStream();
    context.setOutputStream(new GZIPOutputStream(outputStream));
    context.proceed();
}
 
Example 3
Source File: HttpHeaderStatisticInjector.java    From porcupine with Apache License 2.0 5 votes vote down vote up
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
    List<Statistics> pipelines = statistics.get();
    MultivaluedMap<String, Object> headers = context.getHeaders();
    pipelines.forEach(s -> headers.add("x-porcupine-statistics-" + s.getPipelineName(), serializeStatistics(s)));
    context.proceed();
}
 
Example 4
Source File: GZIPWriterInterceptor.java    From demo-rest-jersey-spring with MIT License 5 votes vote down vote up
@Override
public void aroundWriteTo(WriterInterceptorContext context)
                throws IOException, WebApplicationException {
	
	MultivaluedMap<String,Object> headers = context.getHeaders();
	headers.add("Content-Encoding", "gzip");
	
    final OutputStream outputStream = context.getOutputStream();
    context.setOutputStream(new GZIPOutputStream(outputStream));
    context.proceed();
}
 
Example 5
Source File: StreamingWriterInterceptor.java    From ameba with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
    if (isWritable(context)) {
        MultivaluedMap<String, Object> respHeaders = context.getHeaders();
        ContainerRequestContext requestContext = requestProvider.get();
        MultivaluedMap<String, String> reqHeaders = requestContext.getHeaders();
        if (reqHeaders.containsKey(MediaStreaming.RANGE)) {
            if (reqHeaders.containsKey(IF_RANGE)) {
                String ifRangeHeader = reqHeaders.getFirst(IF_RANGE);
                if (StringUtils.isBlank(ifRangeHeader)) {
                    return;
                }
                if (respHeaders.containsKey(HttpHeaders.ETAG)) {
                    if (MessageHelper.getHeaderString(respHeaders, HttpHeaders.ETAG)
                            .equals(ifRangeHeader)) {
                        applyStreaming(requestContext, context);
                        return;
                    }
                }
                if (respHeaders.containsKey(HttpHeaders.LAST_MODIFIED)) {
                    if (MessageHelper.getHeaderString(respHeaders, HttpHeaders.LAST_MODIFIED)
                            .equals(ifRangeHeader)) {
                        applyStreaming(requestContext, context);
                    }
                }
            } else {
                applyStreaming(requestContext, context);
            }
        }
    }
    context.proceed();
}
 
Example 6
Source File: WriterInterceptorMBW.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void aroundWriteTo(WriterInterceptorContext c) throws IOException, WebApplicationException {

    MultivaluedMap<String, Object> headers = c.getHeaders();
    Object mtObject = headers.getFirst(HttpHeaders.CONTENT_TYPE);
    MediaType entityMt = mtObject == null ? c.getMediaType() : JAXRSUtils.toMediaType(mtObject.toString());
    m.put(Message.CONTENT_TYPE, entityMt.toString());

    Class<?> entityCls = c.getType();
    Type entityType = c.getGenericType();
    Annotation[] entityAnns = c.getAnnotations();

    if (writer == null
        || m.get(ProviderFactory.PROVIDER_SELECTION_PROPERTY_CHANGED) == Boolean.TRUE
        && !writer.isWriteable(entityCls, entityType, entityAnns, entityMt)) {

        writer = (MessageBodyWriter<Object>)ProviderFactory.getInstance(m)
            .createMessageBodyWriter(entityCls, entityType, entityAnns, entityMt, m);
    }

    if (writer == null) {
        String errorMessage = JAXRSUtils.logMessageHandlerProblem("NO_MSG_WRITER", entityCls, entityMt);
        throw new ProcessingException(errorMessage);
    }
    
    HttpUtils.convertHeaderValuesToString(headers, true);

    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Response EntityProvider is: " + writer.getClass().getName());
    }
    
    writer.writeTo(c.getEntity(),
                   c.getType(),
                   c.getGenericType(),
                   c.getAnnotations(),
                   entityMt,
                   headers,
                   c.getOutputStream());
}
 
Example 7
Source File: GZIPWriterInterceptor.java    From wings with Apache License 2.0 5 votes vote down vote up
@Override
public void aroundWriteTo(WriterInterceptorContext context)
    throws IOException, WebApplicationException {

  boolean compress = false;
  String ae = request.getHeader("accept-encoding");
  if (ae != null && ae.indexOf("gzip") >= 0) {
    compress = true;
  }
  if(compress) {
    MultivaluedMap<String, Object> headers = context.getHeaders(); 
    for(Object type : headers.get("content-type")) {
      String ctype = type.toString();
      if(ctype.contains("zip") || 
          ctype.contains("compress") ||
          ctype.contains("image")) {
        compress = false;
        break;
      }
    }
    if(compress) {
      headers.add("Content-Encoding", "gzip");
      final OutputStream outputStream = context.getOutputStream();
      context.setOutputStream(new GZIPOutputStream(outputStream));
    }      
  }
  context.proceed();
}