本文整理汇总了Java中javax.ws.rs.ext.WriterInterceptorContext类的典型用法代码示例。如果您正苦于以下问题:Java WriterInterceptorContext类的具体用法?Java WriterInterceptorContext怎么用?Java WriterInterceptorContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WriterInterceptorContext类属于javax.ws.rs.ext包,在下文中一共展示了WriterInterceptorContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: aroundWriteTo
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
MessageDigest digest = null;
try {
digest = getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e);
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DigestOutputStream digestStream = new DigestOutputStream(buffer, digest);
OutputStream old = context.getOutputStream();
context.setOutputStream(digestStream);
try {
context.proceed();
byte[] hash = digest.digest();
String encodedHash = getEncoder().encodeToString(hash);
context.getHeaders().putSingle(CONTENT_MD5_STRING, encodedHash);
byte[] content = buffer.toByteArray();
old.write(content);
} finally {
context.setOutputStream(old);
}
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:27,代码来源:ContentMD5Writer.java
示例2: testWrapsInputStream
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
public void testWrapsInputStream(String contentType) throws WebApplicationException, IOException {
WriterInterceptorContext context = mockContext(contentType);
OutputStream os = mock(OutputStream.class);
when(context.getOutputStream()).thenReturn(os);
writeInterceptor.aroundWriteTo(context);
verifyZeroInteractions(os);
ArgumentCaptor<OutputStream> updatedOsCapture = ArgumentCaptor.forClass(OutputStream.class);
verify(context).setOutputStream(updatedOsCapture.capture());
verify(context).getMediaType();
verify(context).getOutputStream();
verify(context).proceed();
verifyNoMoreInteractions(context);
OutputStream updatedOs = updatedOsCapture.getValue();
// just make sure we have some wrapper
assertNotSame(os, updatedOs);
updatedOs.close();
verify(os).close();
}
示例3: testWrapsOutputStreamAlways
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
@Test
public void testWrapsOutputStreamAlways() throws IOException {
WriterInterceptorContext context = mock(WriterInterceptorContext.class);
OutputStream os = mock(OutputStream.class);
when(context.getOutputStream()).thenReturn(os);
ArgumentCaptor<OutputStream> updatedOsCapture = ArgumentCaptor.forClass(OutputStream.class);
alwaysBase64WriteInterceptor.aroundWriteTo(context);
verify(alwaysBase64WriteInterceptor).isBase64(context);
verifyZeroInteractions(os);
verify(context).setOutputStream(updatedOsCapture.capture());
verify(context).proceed();
verify(context).getOutputStream();
verifyNoMoreInteractions(context);
OutputStream updatedOs = updatedOsCapture.getValue();
// just make sure we have some wrapper
assertNotSame(os, updatedOs);
updatedOs.close();
verify(os).close();
}
示例4: testBase64Encoding
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
private void testBase64Encoding(byte[] bytes, String expectedBase64) throws IOException {
WriterInterceptorContext context = mock(WriterInterceptorContext.class);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
when(context.getOutputStream()).thenReturn(baos);
ArgumentCaptor<OutputStream> updatesOsCapture = ArgumentCaptor.forClass(OutputStream.class);
alwaysBase64WriteInterceptor.aroundWriteTo(context);
verify(context).setOutputStream(updatesOsCapture.capture());
OutputStream updatedOs = updatesOsCapture.getValue();
updatedOs.write(bytes);
updatedOs.close();
assertEquals(expectedBase64, baos.toString());
}
示例5: aroundWriteTo
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
if (!isVersioningSupported(context)) {
context.proceed();
return;
}
String targetVersion = Version.get(context);
Object source = context.getEntity();
if (source instanceof Collection) {
context.setEntity(convertCollectionToLowerVersion(targetVersion, (Collection<?>)source));
} else {
mapper.map(source);
context.setEntity(converter.convertToLowerVersion(targetVersion, source));
}
Type targetType = getVersionType(context.getGenericType(), targetVersion);
context.setType(toClass(targetType));
context.setGenericType(targetType);
context.proceed();
Version.unset(context);
}
示例6: simpleWriterInterceptors
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
@Test
public void simpleWriterInterceptors() {
Response response =
client
.target(mockServer.url("/writerInterceptor").uri())
.register(
new WriterInterceptor() {
@Override
public void aroundWriteTo(WriterInterceptorContext context)
throws IOException, WebApplicationException {
context.getHeaders().putSingle(HEADER_NAME, HEADER_VALUE);
context.proceed();
}
})
.request()
.post(Entity.json(PAYLOAD));
assertThat(response.getStatusInfo()).isEqualTo(Response.Status.OK);
assertThat(response.readEntity(String.class)).isEqualTo(HEADER_VALUE);
}
示例7: testSnappyWriterInterceptor
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
@Test
public void testSnappyWriterInterceptor() throws IOException {
SnappyWriterInterceptor writerInterceptor = new SnappyWriterInterceptor();
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
WriterInterceptorContext mockInterceptorContext = Mockito.mock(WriterInterceptorContext.class);
Mockito.when(mockInterceptorContext.getHeaders()).thenReturn(headers);
Mockito.when(mockInterceptorContext.getOutputStream()).thenReturn(new ByteArrayOutputStream());
Mockito.doNothing().when(mockInterceptorContext).setOutputStream(Mockito.any(OutputStream.class));
// call aroundWriteTo on mock
writerInterceptor.aroundWriteTo(mockInterceptorContext);
// verify that setOutputStream method was called once with argument which is an instance of SnappyFramedOutputStream
Mockito.verify(mockInterceptorContext, Mockito.times(1))
.setOutputStream(Mockito.any(SnappyFramedOutputStream.class));
}
示例8: aroundWriteTo
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
@Override
public void aroundWriteTo(WriterInterceptorContext wic) throws IOException, WebApplicationException {
System.out.println("MyServerWriterInterceptor");
wic.setOutputStream(new FilterOutputStream(wic.getOutputStream()) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
@Override
public void write(int b) throws IOException {
baos.write(b);
super.write(b);
}
@Override
public void close() throws IOException {
System.out.println("MyServerWriterInterceptor --> " + baos.toString());
super.close();
}
});
wic.proceed();
}
示例9: applyStreaming
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
/**
* <p>applyStreaming.</p>
*
* @param requestContext a {@link javax.ws.rs.container.ContainerRequestContext} object.
* @param context a {@link javax.ws.rs.ext.WriterInterceptorContext} object.
* @throws java.io.IOException if any.
*/
protected void applyStreaming(ContainerRequestContext requestContext, WriterInterceptorContext context)
throws IOException {
Object entity = context.getEntity();
StreamingProcess<Object> process = MessageHelper.getStreamingProcess(context.getEntity(), manager);
if (process != null) {
ContainerResponseContext responseContext =
(ContainerResponseContext) requestContext.getProperty(RESP_PROP_N);
responseContext.setStatusInfo(Response.Status.PARTIAL_CONTENT);
context.getHeaders().putSingle(ACCEPT_RANGES, BYTES_RANGE);
context.setType(StreamingOutput.class);
context.setEntity(new MediaStreaming(
entity,
requestContext.getHeaderString(MediaStreaming.RANGE),
process,
context.getMediaType(),
context.getHeaders()
)
);
}
}
示例10: aroundWriteTo
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
if (!context.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)) {
Object entity = context.getEntity();
StreamingProcess<Object> process = MessageHelper.getStreamingProcess(entity, manager);
if (process != null) {
long length = process.length(entity);
if (length != -1)
context.getHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, length);
}
}
context.proceed();
}
示例11: aroundWriteTo
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void aroundWriteTo(final WriterInterceptorContext writerInterceptorContext)
throws IOException, WebApplicationException {
final LoggingStream stream = (LoggingStream) writerInterceptorContext.getProperty(ENTITY_LOGGER_PROPERTY);
writerInterceptorContext.proceed();
final Object requestId = Requests.getProperty(LOGGING_ID_PROPERTY);
final long id = requestId != null ? (Long) requestId : _id.incrementAndGet();
StringBuilder b = (StringBuilder) writerInterceptorContext.getProperty(LOGGER_BUFFER_PROPERTY);
if (b == null) {
b = new StringBuilder();
writerInterceptorContext.setProperty(LOGGER_BUFFER_PROPERTY, b);
}
printPrefixedHeaders(b, id, RESPONSE_PREFIX, HeaderUtils.asStringHeaders(writerInterceptorContext.getHeaders()));
if (stream != null) {
log(stream.getStringBuilder(MessageUtils.getCharset(writerInterceptorContext.getMediaType())));
} else {
log(b);
}
}
示例12: aroundWriteTo
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
@Override
public void aroundWriteTo(final WriterInterceptorContext context) throws IOException, WebApplicationException {
final Object entity = context.getEntity();
if (entity instanceof Viewable) {
User user = (User) securityContext.getUserPrincipal();
if ( ((Viewable) entity).getModel() instanceof ViewData) {
ViewData model = ((ViewData) ((Viewable) entity).getModel());
model.set("authUser", user);
String templateName = ((Viewable) entity).getTemplateName();
context.setEntity(new Viewable(templateName, model.getData()));
}
}
context.proceed();
}
示例13: aroundWriteTo
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
final List<String> requestHeader = httpHeaders.getRequestHeader(HttpHeaders.ACCEPT_ENCODING);
if (requestHeader != null && requestHeader.contains("gzip")) {
context.setOutputStream(new GZIPOutputStream(context.getOutputStream()));
context.getHeaders().add(HttpHeaders.CONTENT_ENCODING, "gzip");
}
context.proceed();
}
示例14: aroundWriteTo
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
final LoggingStream stream = (LoggingStream) context.getProperty("client.LoggingStream");
context.proceed();
if (stream != null) {
System.out.printf("Body: %s\n", stream.getString(StandardCharsets.UTF_8));
}
System.out.printf("-----------\n");
}
示例15: aroundWriteTo
import javax.ws.rs.ext.WriterInterceptorContext; //导入依赖的package包/类
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
context.proceed();
if (logger.isTraceEnabled()) {
logRequestBody(context);
}
}