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();
}
java类javax.ws.rs.ext.ReaderInterceptorContext的实例源码
WebActionBase64ReadInterceptorTest.java 文件源码
项目:jrestless
阅读 24
收藏 0
点赞 0
评论 0
ConditionalBase64ReadInterceptorTest.java 文件源码
项目:jrestless
阅读 23
收藏 0
点赞 0
评论 0
@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();
}
MessageBodyConverter.java 文件源码
项目:jaxrs-versioning
阅读 23
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
if (!isVersioningSupported(context)) {
return context.proceed();
}
String sourceVersion = Version.get(context);
Type targetType = context.getGenericType();
Type sourceType = getVersionType(targetType, sourceVersion);
context.setType(toClass(sourceType));
context.setGenericType(sourceType);
Object sourceObject = context.proceed();
Object target;
if (sourceObject instanceof Collection) {
target = convertCollectionToHigherVersion(targetType, (Collection<?>)sourceObject, sourceVersion);
} else {
target = converter.convertToHigherVersion(toClass(targetType), sourceObject, sourceVersion);
}
context.setType(toClass(targetType));
context.setGenericType(targetType);
return target;
}
TestSnappyReaderInterceptor.java 文件源码
项目:datacollector
阅读 20
收藏 0
点赞 0
评论 0
@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));
}
EmptyPayloadInterceptor.java 文件源码
项目:hawkular-metrics
阅读 19
收藏 0
点赞 0
评论 0
@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;
}
MyClientReaderInterceptor.java 文件源码
项目:JavaIncrementalParser
阅读 22
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext ric) throws IOException, WebApplicationException {
System.out.println("MyClientReaderInterceptor");
final InputStream old = ric.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
while ((c = old.read()) != -1) {
baos.write(c);
}
System.out.println("MyClientReaderInterceptor --> " + baos.toString());
ric.setInputStream(new ByteArrayInputStream(baos.toByteArray()));
return ric.proceed();
}
ValidCharacterInterceptor.java 文件源码
项目:oracle-samples
阅读 21
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext) throws IOException, WebApplicationException {
final InputStream originalInputStream = readerInterceptorContext.getInputStream();
readerInterceptorContext.setInputStream(new InputStream() {
@Override
public int read() throws IOException {
boolean isOk;
int b;
do {
b = originalInputStream.read();
isOk = b == -1 || Character.isLetterOrDigit(b) || Character.isWhitespace(b) || b == ((int) '.');
} while (!isOk);
return b;
}
});
try {
return readerInterceptorContext.proceed();
} finally {
readerInterceptorContext.setInputStream(originalInputStream);
}
}
GZIPReaderInterceptor.java 文件源码
项目:cloudstore
阅读 20
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
InputStream originalInputStream = context.getInputStream();
if (!originalInputStream.markSupported())
originalInputStream = new BufferedInputStream(originalInputStream);
// Test, if it contains data. We only try to unzip, if it is not empty.
originalInputStream.mark(5);
int read = originalInputStream.read();
originalInputStream.reset();
if (read > -1)
context.setInputStream(new GZIPInputStream(originalInputStream));
else {
context.setInputStream(originalInputStream); // We might have wrapped it with our BufferedInputStream!
logger.debug("aroundReadFrom: originalInputStream is empty! Skipping GZIP.");
}
return context.proceed();
}
ServerReaderInterceptor.java 文件源码
项目:Mastering-Java-EE-Development-with-WildFly
阅读 20
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext interceptorContext)
throws IOException, WebApplicationException {
logger.info("ServerReaderInterceptor invoked");
InputStream inputStream = interceptorContext.getInputStream();
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String requestContent = new String(bytes);
requestContent = requestContent + ".Request changed in ServerReaderInterceptor.";
interceptorContext.setInputStream(new ByteArrayInputStream(requestContent.getBytes()));
return interceptorContext.proceed();
}
ClientSecondReaderInterceptor.java 文件源码
项目:Mastering-Java-EE-Development-with-WildFly
阅读 21
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext interceptorContext)
throws IOException, WebApplicationException {
logger.info("ClientSecondReaderInterceptor invoked.");
InputStream inputStream = interceptorContext.getInputStream();
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String requestContent = new String(bytes);
requestContent = requestContent + " Request changed in ClientSecondReaderInterceptor.";
interceptorContext.setInputStream(new ByteArrayInputStream(requestContent.getBytes()));
return interceptorContext.proceed();
}
ClientFirstReaderInterceptor.java 文件源码
项目:Mastering-Java-EE-Development-with-WildFly
阅读 21
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext interceptorContext)
throws IOException, WebApplicationException {
logger.info("ClientFirstReaderInterceptor invoked");
InputStream inputStream = interceptorContext.getInputStream();
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String requestContent = new String(bytes);
requestContent = requestContent + ".Request changed in ClientFirstReaderInterceptor.";
interceptorContext.setInputStream(new ByteArrayInputStream(requestContent.getBytes()));
return interceptorContext.proceed();
}
GZipInterceptor.java 文件源码
项目:Alpine
阅读 24
收藏 0
点赞 0
评论 0
@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")) {
context.setInputStream(new GZIPInputStream(context.getInputStream()));
}
return context.proceed();
}
WebActionBase64ReadInterceptorTest.java 文件源码
项目:jrestless
阅读 24
收藏 0
点赞 0
评论 0
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);
}
WebActionBase64ReadInterceptorTest.java 文件源码
项目:jrestless
阅读 22
收藏 0
点赞 0
评论 0
private static ReaderInterceptorContext mockContext(String contentType) {
ReaderInterceptorContext context = mock(ReaderInterceptorContext.class);
if (contentType != null) {
when(context.getMediaType()).thenReturn(MediaType.valueOf(contentType));
}
return context;
}
ConditionalBase64ReadInterceptor.java 文件源码
项目:jrestless
阅读 20
收藏 0
点赞 0
评论 0
@Override
public final Object aroundReadFrom(ReaderInterceptorContext context) throws IOException {
if (isBase64(context)) {
context.setInputStream(Base64.getDecoder().wrap(context.getInputStream()));
}
return context.proceed();
}
ConditionalBase64ReadInterceptorTest.java 文件源码
项目:jrestless
阅读 72
收藏 0
点赞 0
评论 0
@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);
}
TracingInterceptor.java 文件源码
项目:java-jaxrs
阅读 24
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext context)
throws IOException, WebApplicationException {
try (ActiveSpan activeSpan = decorateRead(context, buildSpan(context, "deserialize"))) {
try {
return context.proceed();
} catch (Exception e) {
//TODO add exception logs in case they are not added by the filter.
Tags.ERROR.set(activeSpan, true);
throw e;
}
}
}
TraceMessageBodyInterceptor.java 文件源码
项目:cloud-trace-java-instrumentation
阅读 18
收藏 0
点赞 0
评论 0
public Object aroundReadFrom(ReaderInterceptorContext context)
throws IOException, WebApplicationException {
TraceContext traceContext = tracer.startSpan("MessageBodyReader#readFrom");
Object result;
try {
result = context.proceed();
} finally {
tracer.endSpan(traceContext);
}
return result;
}
CsrfGuardian.java 文件源码
项目:sealion
阅读 22
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext context)
throws IOException, WebApplicationException {
Object entity = context.proceed();
MediaType mediaType = context.getMediaType();
if (mediaType != null
&& mediaType.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
Form form = (Form) entity;
String csrfToken = form.asMap().getFirst("csrfToken");
if (userProvider.validateCsrfToken(csrfToken) == false) {
throw new BadRequestException();
}
}
return entity;
}
Resource.java 文件源码
项目:Krill
阅读 31
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom (ReaderInterceptorContext context)
throws IOException, WebApplicationException {
final InputStream originalInputStream = context.getInputStream();
context.setInputStream(new GZIPInputStream(originalInputStream));
return context.proceed();
}
PutJobConfigInterceptor.java 文件源码
项目:omakase
阅读 26
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException {
String json = JsonStrings.inputStreamToString(context.getInputStream());
JsonStrings.isNotNullOrEmpty(json);
context.setInputStream(new ByteArrayInputStream(json.getBytes(Charset.forName("UTF-8"))));
String jobId = uriInfo.getPathParameters().getFirst("jobId");
Job job = jobManager.getJob(jobId).orElseThrow(NotFoundException::new);
Class<? extends JobConfigurationModel> configurationModelType = getJobConfigurationModelClass(job.getJobConfiguration());
context.setType(configurationModelType);
context.setGenericType(configurationModelType);
return context.proceed();
}
PutRepositoryConfigInterceptor.java 文件源码
项目:omakase
阅读 24
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException {
String json = JsonStrings.inputStreamToString(context.getInputStream());
JsonStrings.isNotNullOrEmpty(json);
context.setInputStream(new ByteArrayInputStream(json.getBytes(Charset.forName("UTF-8"))));
String repositoryId = uriInfo.getPathParameters().getFirst("repositoryId");
Class<? extends RepositoryConfiguration> configurationType = repositoryManager.getRepositoryConfigurationType(repositoryId);
context.setType(configurationType);
context.setGenericType(configurationType);
return context.proceed();
}
JsonPatchInterceptor.java 文件源码
项目:omakase
阅读 24
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException {
Optional<Object> optionalBean = getCurrentObject();
if (optionalBean.isPresent()) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// Currently assume the bean is of type BuiltResponse, this may need to change in the future
Object bean = ((BuiltResponse) optionalBean.get()).getEntity();
MessageBodyWriter<? super Object> bodyWriter = providers.getMessageBodyWriter(Object.class, bean.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE);
bodyWriter.writeTo(bean, bean.getClass(), bean.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MultivaluedHashMap<>(), outputStream);
// Use the Jackson 2.x classes to convert both the incoming patch and the current state of the object into a JsonNode / JsonPatch
ObjectMapper mapper = new ObjectMapper();
JsonNode serverState = mapper.readValue(outputStream.toByteArray(), JsonNode.class);
JsonNode patchAsNode = mapper.readValue(context.getInputStream(), JsonNode.class);
JsonPatch patch = JsonPatch.fromJson(patchAsNode);
try {
JsonNode result = patch.apply(serverState);
// Stream the result & modify the stream on the readerInterceptor
ByteArrayOutputStream resultAsByteArray = new ByteArrayOutputStream();
mapper.writeValue(resultAsByteArray, result);
context.setInputStream(new ByteArrayInputStream(resultAsByteArray.toByteArray()));
return context.proceed();
} catch (JsonPatchException | JsonMappingException e) {
throw new WebApplicationException(e.getMessage(), e, Response.status(Response.Status.BAD_REQUEST).build());
}
} else {
throw new IllegalArgumentException("No matching GET method on resource");
}
}
PutLocationConfigInterceptor.java 文件源码
项目:omakase
阅读 21
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException {
String json = JsonStrings.inputStreamToString(context.getInputStream());
JsonStrings.isNotNullOrEmpty(json);
context.setInputStream(new ByteArrayInputStream(json.getBytes(Charset.forName("UTF-8"))));
String locationId = uriInfo.getPathParameters().getFirst("locationId");
Class<? extends LocationConfiguration> configurationType = locationManager.getLocationConfigurationType(locationId);
context.setType(configurationType);
context.setGenericType(configurationType);
return context.proceed();
}
TestHttpTarget.java 文件源码
项目:datacollector
阅读 21
收藏 0
点赞 0
评论 0
@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();
}
SnappyReaderInterceptor.java 文件源码
项目:datacollector
阅读 21
收藏 0
点赞 0
评论 0
@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();
}
RestSkolReaderInterceptor.java 文件源码
项目:restskol
阅读 23
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext readerInterceptorContext)
throws IOException, WebApplicationException {
logger.info("Enters RestSkolReaderInterceptor.aroundReadFrom()");
final InputStream inputStream = readerInterceptorContext.getInputStream();
readerInterceptorContext.setInputStream(new GZIPInputStream(inputStream));
return readerInterceptorContext.proceed();
}
MyClientReaderInterceptor.java 文件源码
项目:JavaIncrementalParser
阅读 26
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext ric) throws IOException, WebApplicationException {
System.out.println("MyClientReaderInterceptor");
// ric.setInputStream(new FilterInputStream(ric.getInputStream()) {
//
// final ByteArrayOutputStream baos = new ByteArrayOutputStream();
//
// @Override
// public int read(byte[] b, int off, int len) throws IOException {
// baos.write(b, off, len);
//// System.out.println("@@@@@@ " + b);
// return super.read(b, off, len);
// }
//
// @Override
// public void close() throws IOException {
// System.out.println("### " + baos.toString());
// super.close();
// }
// });
final InputStream old = ric.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
while ((c = old.read()) != -1) {
baos.write(c);
}
System.out.println("MyClientReaderInterceptor --> " + baos.toString());
ric.setInputStream(new ByteArrayInputStream(baos.toByteArray()));
return ric.proceed();
}
MyServerReaderInterceptor.java 文件源码
项目:JavaIncrementalParser
阅读 19
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext ric) throws IOException, WebApplicationException {
System.out.println("MyServerReaderInterceptor");
final InputStream old = ric.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
while ((c = old.read()) != -1) {
baos.write(c);
}
System.out.println("MyClientReaderInterceptor --> " + baos.toString());
ric.setInputStream(new ByteArrayInputStream(baos.toByteArray()));
return ric.proceed();
}
MyServerReaderInterceptor.java 文件源码
项目:JavaIncrementalParser
阅读 21
收藏 0
点赞 0
评论 0
@Override
public Object aroundReadFrom(ReaderInterceptorContext ric) throws IOException, WebApplicationException {
System.out.println("MyServerReaderInterceptor");
final InputStream old = ric.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
while ((c = old.read()) != -1) {
baos.write(c);
}
System.out.println("MyClientReaderInterceptor --> " + baos.toString());
ric.setInputStream(new ByteArrayInputStream(baos.toByteArray()));
return ric.proceed();
}