javax.ws.rs.ext.ReaderInterceptorContext Java Examples

The following examples show how to use javax.ws.rs.ext.ReaderInterceptorContext. 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: ReaderInterceptorMBR.java    From cxf with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({
    "unchecked", "rawtypes"
})
@Override
public Object aroundReadFrom(ReaderInterceptorContext c) throws IOException, WebApplicationException {
    Class entityCls = c.getType();
    Type entityType = c.getGenericType();
    MediaType entityMt = c.getMediaType();
    Annotation[] entityAnns = c.getAnnotations();

    if ((reader == null || m.get(ProviderFactory.PROVIDER_SELECTION_PROPERTY_CHANGED) == Boolean.TRUE
        && !reader.isReadable(entityCls, entityType, entityAnns, entityMt))
        && entityStreamAvailable(c.getInputStream())) {
        reader = ProviderFactory.getInstance(m)
            .createMessageBodyReader(entityCls, entityType, entityAnns, entityMt, m);
    }
    if (reader == null) {
        String errorMessage = JAXRSUtils.logMessageHandlerProblem("NO_MSG_READER", entityCls, entityMt);
        throw new ProcessingException(errorMessage);
    }


    return reader.readFrom(entityCls, entityType, entityAnns, entityMt,
                           new HttpHeadersImpl(m).getRequestHeaders(),
                           c.getInputStream());
}
 
Example #2
Source File: WebActionBase64ReadInterceptorTest.java    From jrestless with Apache License 2.0 6 votes vote down vote up
public void testWrapsInputStream(String contentType) throws WebApplicationException, IOException {
	ReaderInterceptorContext context = mockContext(contentType);
	InputStream is = mock(InputStream.class);
	when(context.getInputStream()).thenReturn(is);

	readInterceptor.aroundReadFrom(context);

	verifyZeroInteractions(is);

	ArgumentCaptor<InputStream> updatedIsCapture = ArgumentCaptor.forClass(InputStream.class);
	verify(context).setInputStream(updatedIsCapture.capture());
	verify(context).getMediaType();
	verify(context).getInputStream();
	verify(context).proceed();
	verifyNoMoreInteractions(context);

	InputStream updatedIs = updatedIsCapture.getValue();

	// just make sure we have some wrapper
	assertNotSame(is, updatedIs);
	updatedIs.close();
	verify(is).close();
}
 
Example #3
Source File: ConditionalBase64ReadInterceptorTest.java    From jrestless with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrapsInputStreamAlways() throws WebApplicationException, IOException {
	ReaderInterceptorContext context = mock(ReaderInterceptorContext.class);
	InputStream is = mock(InputStream.class);
	when(context.getInputStream()).thenReturn(is);

	alwaysBase64ReadInterceptor.aroundReadFrom(context);

	verifyZeroInteractions(is);

	ArgumentCaptor<InputStream> updatedIsCapture = ArgumentCaptor.forClass(InputStream.class);
	verify(context).setInputStream(updatedIsCapture.capture());
	verify(context).proceed();
	verify(context).getInputStream();
	verifyNoMoreInteractions(context);

	InputStream updatedIs = updatedIsCapture.getValue();

	verify(alwaysBase64ReadInterceptor).isBase64(context);

	// just make sure we have some wrapper
	assertNotSame(is, updatedIs);
	updatedIs.close();
	verify(is).close();
}
 
Example #4
Source File: TracingInterceptor.java    From java-jaxrs with Apache License 2.0 6 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context)
    throws IOException, WebApplicationException {
    Span span = buildSpan(context, "deserialize");
    try (Scope scope = tracer.activateSpan(span)) {
        decorateRead(context, span);
        try {
            return context.proceed();
        } catch (Exception e) {
            //TODO add exception logs in case they are not added by the filter.
            Tags.ERROR.set(span, true);
            throw e;
        }
    } finally {
        span.finish();
    }
}
 
Example #5
Source File: EmptyPayloadInterceptor.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
    Object object = context.proceed();
    if (context.getProperty(EMPTY_PAYLOAD) != TRUE) {
        return object;
    }
    if (object instanceof Collection) {
        Collection<?> collection = (Collection<?>) object;
        if (collection.isEmpty()) {
            throw new EmptyPayloadException();
        }
    } else if (object instanceof Map) {
        Map<?, ?> map = (Map<?, ?>) object;
        if (map.isEmpty()) {
            throw new EmptyPayloadException();
        }
    } else if (object == null) {
        throw new EmptyPayloadException();
    }
    return object;
}
 
Example #6
Source File: TestSnappyReaderInterceptor.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testSnappyReaderInterceptor() throws IOException {

  SnappyReaderInterceptor readerInterceptor = new SnappyReaderInterceptor();

  MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
  headers.put(SnappyReaderInterceptor.CONTENT_ENCODING, Arrays.asList(SnappyReaderInterceptor.SNAPPY));

  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  SnappyFramedOutputStream snappyFramedOutputStream = new SnappyFramedOutputStream(byteArrayOutputStream);
  snappyFramedOutputStream.write("Hello".getBytes());
  ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

  ReaderInterceptorContext mockInterceptorContext = Mockito.mock(ReaderInterceptorContext.class);
  Mockito.when(mockInterceptorContext.getHeaders()).thenReturn(headers);
  Mockito.when(mockInterceptorContext.getInputStream()).thenReturn(inputStream);
  Mockito.doNothing().when(mockInterceptorContext).setInputStream(Mockito.any(InputStream.class));

  // call aroundReadFrom on mock
  readerInterceptor.aroundReadFrom(mockInterceptorContext);

  // verify that setInputStream method was called once with argument which is an instance of SnappyFramedInputStream
  Mockito.verify(mockInterceptorContext, Mockito.times(1))
    .setInputStream(Mockito.any(SnappyFramedInputStream.class));

}
 
Example #7
Source File: GZIPReaderInterceptor.java    From wings with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context)
                throws IOException, WebApplicationException {
  List<String> ce = context.getHeaders().get("content-encoding");
  if (ce != null && ce.contains("gzip")) {
    final InputStream originalInputStream = context.getInputStream();
    context.setInputStream(new GZIPInputStream(originalInputStream));
  }
  return context.proceed();
}
 
Example #8
Source File: AbstractFilterInterceptorTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(final ReaderInterceptorContext readerInterceptorCtx) throws IOException {
    final InputStream old = readerInterceptorCtx.getInputStream();
    readerInterceptorCtx.setInputStream(new UpperCaseInputStream(old));
    try {
        return readerInterceptorCtx.proceed();
    } finally {
        readerInterceptorCtx.setInputStream(old);
    }
}
 
Example #9
Source File: GZipInterceptor.java    From Alpine with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
    final List<String> header = context.getHeaders().get(HttpHeaders.CONTENT_ENCODING);
    if (header != null && header.contains("gzip")) {
        // DO NOT CLOSE STREAMS
        final InputStream contentInputSteam = context.getInputStream();
        final GZIPInputStream gzipInputStream = new GZIPInputStream(contentInputSteam);
        context.setInputStream(gzipInputStream);
    }
    return context.proceed();
}
 
Example #10
Source File: DecompressInterceptor.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context)
                             throws IOException {
    // NOTE: Currently we just support GZIP
    String encoding = context.getHeaders().getFirst("Content-Encoding");
    if (!GZIP.equalsIgnoreCase(encoding)) {
        return context.proceed();
    }
    context.setInputStream(new GZIPInputStream(context.getInputStream()));
    return context.proceed();
}
 
Example #11
Source File: LoggingFilter.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
    byte[] buffer = IOUtils.toByteArray(context.getInputStream());
    logger.info("The contents of request body is: \n" + new String(buffer, "UTF-8") + "\n");
    context.setInputStream(new ByteArrayInputStream(buffer));
    return context.proceed();
}
 
Example #12
Source File: WebActionBase64ReadInterceptorTest.java    From jrestless with Apache License 2.0 5 votes vote down vote up
private static ReaderInterceptorContext mockContext(String contentType) {
	ReaderInterceptorContext context = mock(ReaderInterceptorContext.class);
	if (contentType != null) {
		when(context.getMediaType()).thenReturn(MediaType.valueOf(contentType));
	}
	return context;
}
 
Example #13
Source File: ConditionalBase64ReadInterceptor.java    From jrestless with Apache License 2.0 5 votes vote down vote up
@Override
public final Object aroundReadFrom(ReaderInterceptorContext context) throws IOException {
	if (isBase64(context)) {
		context.setInputStream(Base64.getDecoder().wrap(context.getInputStream()));
	}
	return context.proceed();
}
 
Example #14
Source File: ConditionalBase64ReadInterceptorTest.java    From jrestless with Apache License 2.0 5 votes vote down vote up
@Test
public void testWrapsInputStreamNever() throws WebApplicationException, IOException {
	ReaderInterceptorContext context = mock(ReaderInterceptorContext.class);
	InputStream is = mock(InputStream.class);
	when(context.getInputStream()).thenReturn(is);

	neverBase64ReadInterceptor.aroundReadFrom(context);

	verify(neverBase64ReadInterceptor).isBase64(context);
	verify(context).proceed();
	verifyNoMoreInteractions(context);
}
 
Example #15
Source File: TestHttpTarget.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context)  throws IOException, WebApplicationException {
  if (context.getHeaders().containsKey("Content-Encoding") &&
    context.getHeaders().get("Content-Encoding").contains("snappy")) {
    InputStream originalInputStream = context.getInputStream();
    context.setInputStream(new SnappyFramedInputStream(originalInputStream, true));
  }
  return context.proceed();
}
 
Example #16
Source File: SnappyReaderInterceptor.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context)  throws IOException, WebApplicationException {
  if (context.getHeaders().containsKey(CONTENT_ENCODING) &&
    context.getHeaders().get(CONTENT_ENCODING).contains(SNAPPY)) {
    InputStream originalInputStream = context.getInputStream();
    context.setInputStream(new SnappyFramedInputStream(originalInputStream, true));
  }
  return context.proceed();
}
 
Example #17
Source File: ReaderTestInterceptor.java    From java-client with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext) throws IOException, WebApplicationException {
    logger.info("Coolio we received something!");
    Object returnedObject = readerInterceptorContext.proceed();
    logger.info("Received:" + returnedObject);
    receivedEvents.add(returnedObject);
    return returnedObject;
}
 
Example #18
Source File: GZIPReaderInterceptor.java    From divide with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context)
        throws IOException, WebApplicationException {
    final InputStream originalInputStream = context.getInputStream();
    context.setInputStream(new GZIPInputStream(originalInputStream));
    return context.proceed();
}
 
Example #19
Source File: FormReaderInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext ctx) throws IOException, WebApplicationException {
    BufferedReader br = new BufferedReader(new InputStreamReader(ctx.getInputStream()));
    String line;
    while ((line = br.readLine()) != null) {
        LOG.info("readLine: " + line);
    }

    ByteArrayInputStream bais = new ByteArrayInputStream("value=MODIFIED".getBytes());
    LOG.info("set value=MODIFIED");
    ctx.setInputStream(bais);
    return ctx.proceed();
}
 
Example #20
Source File: BookServer20.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException,
    WebApplicationException {
    if (ri.getResourceClass() == BookStore.class) {
        context.getHeaders().add("ServerReaderInterceptor", "serverRead");
    }
    return context.proceed();

}
 
Example #21
Source File: BookServer20.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException,
    WebApplicationException {
    if (ri.getResourceClass() == BookStore.class) {
        String serverRead = context.getHeaders().getFirst("ServerReaderInterceptor");
        if (serverRead == null || !"serverRead".equals(serverRead)) {
            throw new RuntimeException();
        }
        if (ui.getPath().endsWith("/async")) {
            context.getHeaders().putSingle("ServerReaderInterceptor", "serverReadAsync");
        }
    }
    return context.proceed();

}
 
Example #22
Source File: JAXRS20ClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException,
    WebApplicationException {
    if (context.getInputStream() != null) {
        context.getHeaders().add("ClientReaderInterceptor", "clientRead");
    }
    return context.proceed();
}
 
Example #23
Source File: JAXRSUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static Object readFromMessageBodyReader(List<ReaderInterceptor> readers,
                                               Class<?> targetTypeClass,
                                               Type parameterType,
                                               Annotation[] parameterAnnotations,
                                               InputStream is,
                                               MediaType mediaType,
                                               Message m) throws IOException, WebApplicationException {

    // Verbose but avoids an extra context instantiation for the typical path
    if (readers.size() > 1) {
        ReaderInterceptor first = readers.remove(0);
        ReaderInterceptorContext context = new ReaderInterceptorContextImpl(targetTypeClass,
                                                                        parameterType,
                                                                        parameterAnnotations,
                                                                        is,
                                                                        m,
                                                                        readers);

        return first.aroundReadFrom(context);
    }
    MessageBodyReader<?> provider = ((ReaderInterceptorMBR)readers.get(0)).getMBR();
    @SuppressWarnings("rawtypes")
    Class cls = targetTypeClass;
    return provider.readFrom(
              cls, parameterType, parameterAnnotations, mediaType,
              new HttpHeadersImpl(m).getRequestHeaders(), is);
}
 
Example #24
Source File: XmlSecInInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext ctx) throws IOException, WebApplicationException {
    Message message = ((ReaderInterceptorContextImpl)ctx).getMessage();

    if (!canDocumentBeRead(message)) {
        return ctx.proceed();
    }
    prepareMessage(message);
    Object object = ctx.proceed();
    new StaxActionInInterceptor(requireSignature,
                                requireEncryption).handleMessage(message);
    return object;

}
 
Example #25
Source File: CacheControlClientReaderInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected boolean isCacheControlValid(final ReaderInterceptorContext context,
                                      final CacheControl responseControl) {

    boolean valid =
        responseControl != null && !responseControl.isNoCache() && !responseControl.isNoStore();
    if (valid) {
        String clientHeader =
            (String)context.getProperty(CacheControlClientRequestFilter.CLIENT_CACHE_CONTROL);
        CacheControl clientControl = clientHeader == null ? null : CacheControl.valueOf(clientHeader);
        if (clientControl != null && clientControl.isPrivate() != responseControl.isPrivate()) {
            valid = false;
        }
    }
    return valid;
}
 
Example #26
Source File: RequestServerReaderInterceptor.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
    LOG.info("Request reader interceptor in the server side");

    InputStream is = context.getInputStream();
    String body = new BufferedReader(new InputStreamReader(is)).lines()
        .collect(Collectors.joining("\n"));

    context.setInputStream(new ByteArrayInputStream((body + " message added in server reader interceptor").getBytes()));

    return context.proceed();
}
 
Example #27
Source File: WebActionBase64ReadInterceptorTest.java    From jrestless with Apache License 2.0 5 votes vote down vote up
public void testDoesNotWrapInputStream(String contentType) throws WebApplicationException, IOException {
	ReaderInterceptorContext context = mockContext(contentType);
	InputStream is = mock(InputStream.class);
	when(context.getInputStream()).thenReturn(is);

	readInterceptor.aroundReadFrom(context);

	verifyZeroInteractions(is);

	verify(context).getMediaType();
	verify(context).proceed();
	verifyNoMoreInteractions(context);
}
 
Example #28
Source File: TraceInterceptor.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext) throws IOException, WebApplicationException {
    System.out.println("Reader interceptor invoked");
    return readerInterceptorContext.proceed();
}
 
Example #29
Source File: TraceInterceptor.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext) throws IOException, WebApplicationException {
    System.out.println("Reader interceptor invoked");
    return readerInterceptorContext.proceed();
}
 
Example #30
Source File: LoggingFilter.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
    byte[] buffer = IOUtils.toByteArray(context.getInputStream());
    logger.info("The contents of request body is: \n" + new String(buffer, "UTF-8") + "\n");
    context.setInputStream(new ByteArrayInputStream(buffer));
    return context.proceed();
}