本文整理汇总了Java中javax.ws.rs.ext.MessageBodyWriter.writeTo方法的典型用法代码示例。如果您正苦于以下问题:Java MessageBodyWriter.writeTo方法的具体用法?Java MessageBodyWriter.writeTo怎么用?Java MessageBodyWriter.writeTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.ws.rs.ext.MessageBodyWriter
的用法示例。
在下文中一共展示了MessageBodyWriter.writeTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeTo
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
@Override
public void writeTo(final JsonWebKeySet jwks,
final Class<?> type,
final Type genericType,
final Annotation[] annotations,
final MediaType mediaType,
final MultivaluedMap<String, Object> httpHeaders,
final OutputStream os) throws IOException,
WebApplicationException {
final MessageBodyWriter<JsonWebKey> writer = providers.getMessageBodyWriter(JsonWebKey.class, JsonWebKey.class, annotations, mediaType);
final JsonArrayBuilder keysArray = Json.createArrayBuilder();
for (final JsonWebKey key : jwks.getKeys()) {
final ByteArrayOutputStream keyStream = new ByteArrayOutputStream();
writer.writeTo(key, JsonWebKey.class, null, annotations, mediaType, null, keyStream);
keyStream.close();
keysArray.add(Json.createReader(new ByteArrayInputStream(keyStream.toByteArray()))
.readObject());
}
final JsonObject jwksObject = Json.createObjectBuilder()
.add("keys", keysArray)
.build();
final JsonWriter w = Json.createWriter(os);
w.write(jwksObject);
w.close();
}
示例2: writeTo
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public void writeTo(Value<?> entity,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream)
throws IOException {
final Object entityValue = entity.getOrElseThrow(() -> EmptyValueException.INSTANCE);
final Class<?> entityClass = entityValue.getClass();
final Type actualGenericTypeArgument;
if (genericType instanceof ParameterizedType) {
actualGenericTypeArgument = ((ParameterizedType) genericType).getActualTypeArguments()[0];
} else {
actualGenericTypeArgument = entityClass;
}
final MessageBodyWriter writer = mbw.get().getMessageBodyWriter(entityClass, actualGenericTypeArgument, annotations, mediaType);
writer.writeTo(entityValue, entityClass, actualGenericTypeArgument, annotations, mediaType, httpHeaders, entityStream);
}
示例3: debugWrite
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
private void debugWrite(Object root, MediaType format) {
MessageBodyWriter messageBodyWriter =
workers.getMessageBodyWriter(root.getClass(), root.getClass(),
new Annotation[]{}, format);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
// use the MBW to serialize myBean into baos
messageBodyWriter.writeTo(root,
root.getClass(), root.getClass(), new Annotation[]{},
format, new MultivaluedHashMap<String, Object>(),
baos);
} catch (Throwable e) {
logger.error(String.format("could not serialize %s to format %s", root, format), e);
throw propagate(e);
}
logger.info("{}", baos);
}
示例4: writeTo
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
@Override
public void writeTo(MultipartUploadRepresentation o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
Class<BucketObjectUpdateRepresentation> writerType = BucketObjectUpdateRepresentation.class;
Type writerGenericType = writerType;
Annotation[] writerAnnotations = new Annotation[0];
MessageBodyWriter<BucketObjectUpdateRepresentation> writer = providers.getMessageBodyWriter(writerType, writerGenericType, writerAnnotations, MediaType.WILDCARD_TYPE);
String boundary = UUID.randomUUID().toString();
httpHeaders.putSingle(BlazeStorageHeaders.QUIET, Boolean.toString(o.isQuiet()));
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, mediaType.toString() + "; boundary=" + boundary);
byte[] boundaryBytes = ("--" + boundary).getBytes();
for (Map.Entry<String, BucketObjectUpdateRepresentation> entry : o.getUploads().entrySet()) {
BucketObjectUpdateRepresentation value = entry.getValue();
MediaType writerMediaType = MediaType.valueOf(value.getContentType());
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<String, Object>();
headers.add(BlazeStorageHeaders.MULTIPART_KEY, entry.getKey());
entityStream.write(boundaryBytes);
entityStream.write("\r\n".getBytes());
writer.writeTo(entry.getValue(), writerType, writerGenericType, writerAnnotations, writerMediaType, headers, new HeaderFlushedOutputStream(headers, entityStream));
entityStream.write("\r\n".getBytes());
}
entityStream.write(boundaryBytes);
entityStream.write("--".getBytes());
}
示例5: writeViewable
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
/**
* <p>writeViewable.</p>
*
* @param viewable a {@link org.glassfish.jersey.server.mvc.Viewable} object.
* @param mediaType a {@link javax.ws.rs.core.MediaType} object.
* @param httpHeaders a {@link javax.ws.rs.core.MultivaluedMap} object.
* @param entityStream a {@link java.io.OutputStream} object.
* @throws java.io.IOException if any.
*/
protected void writeViewable(Viewable viewable,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException {
MessageBodyWriter<Viewable> writer = workers.get().getMessageBodyWriter(Viewable.class, null,
new Annotation[]{}, null);
if (writer != null) {
writer.writeTo(viewable,
Viewable.class,
Viewable.class,
new Annotation[0],
mediaType,
httpHeaders,
entityStream);
}
}
示例6: writeTo
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void writeTo(Optional<?> entity,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream)
throws IOException {
if (!entity.isPresent()) {
throw new NotFoundException();
}
final Type innerGenericType = (genericType instanceof ParameterizedType) ?
((ParameterizedType) genericType).getActualTypeArguments()[0] : entity.get().getClass();
MessageBodyWriter writer = mbw.get().getMessageBodyWriter(entity.get().getClass(),
innerGenericType, annotations, mediaType);
writer.writeTo(entity.get(), entity.get().getClass(),
innerGenericType, annotations, mediaType, httpHeaders, entityStream);
}
示例7: createBodyPart
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static MimeBodyPart createBodyPart(Providers providers, SMIMEOutput out) throws IOException, MessagingException {
ByteArrayOutputStream bodyOs = new ByteArrayOutputStream();
MessageBodyWriter writer = providers.getMessageBodyWriter(out.getType(), out.getGenericType(), null, out.getMediaType());
if (writer == null) {
throw new RuntimeException("Failed to find writer for type: " + out.getType().getName());
}
MultivaluedMap<String, String> bodyHeaders = new MultivaluedHashMap<String, String>();
bodyHeaders.add("Content-Type", out.getMediaType().toString());
writer.writeTo(out.getEntity(), out.getType(), out.getGenericType(), null, out.getMediaType(), bodyHeaders, bodyOs);
InternetHeaders ih = new InternetHeaders();
for (Map.Entry<String, List<String>> entry : bodyHeaders.entrySet()) {
for (String value : entry.getValue()) {
ih.addHeader(entry.getKey(), value);
}
}
return new MimeBodyPart(ih, bodyOs.toByteArray());
}
示例8: testOccurrenceXML
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
@Test
public void testOccurrenceXML() throws IOException {
MessageBodyWriter<Occurrence> occurrenceDwcXMLBodyWriter = new OccurrenceDwcXMLBodyWriter();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Occurrence occ = new Occurrence();
occ.setCountry(Country.MADAGASCAR);
occ.setVerbatimField(DwcTerm.verbatimLocality, "mad");
occ.setReferences(URI.create("http://www.gbif.org"));
Term customTerm = TermFactory.instance().findTerm("MyTerm");
occ.setVerbatimField(customTerm, "MyTerm value");
occurrenceDwcXMLBodyWriter.writeTo(occ, null, null, null, null, null, baos);
String expectedContent = IOUtils.toString(new FileInputStream(FileUtils.getClasspathFile("dwc_xml/occurrence.xml")));
assertEquals(CharMatcher.WHITESPACE.removeFrom(expectedContent), CharMatcher.WHITESPACE.removeFrom(baos.toString()));
}
示例9: writeTo
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public void writeTo(Either<?, ?> entity,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream)
throws IOException {
final Object entityValue;
final int typeArgumentIndex;
if (entity.isLeft()) {
entityValue = entity.getLeft();
typeArgumentIndex = 0;
} else {
entityValue = entity.get();
typeArgumentIndex = 1;
}
final Class<?> entityClass = entityValue.getClass();
final Type actualGenericTypeArgument;
if (genericType instanceof ParameterizedType) {
actualGenericTypeArgument = ((ParameterizedType) genericType).getActualTypeArguments()[typeArgumentIndex];
} else {
actualGenericTypeArgument = entityClass;
}
final MessageBodyWriter writer = mbw.get().getMessageBodyWriter(entityClass,
actualGenericTypeArgument, annotations, mediaType);
writer.writeTo(entityValue, entityClass,
actualGenericTypeArgument,
annotations, mediaType, httpHeaders, entityStream);
}
示例10: testWriteMap
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
@Test
@SuppressWarnings("unchecked")
public void testWriteMap() throws IOException {
final Map<String, String> map = new HashMap<>();
map.put("key", "value");
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final MessageBodyWriter<Map<String, String>> mapWriter = (MessageBodyWriter<Map<String, String>>) writer;
mapWriter.writeTo(map, Map.class, null, null, MediaType.APPLICATION_JSON_TYPE, null, outputStream);
assertEquals("{\"key\":\"value\"}", outputStream.toString());
}
示例11: testWriteWidget
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
@Test
@SuppressWarnings("unchecked")
public void testWriteWidget() throws IOException {
final Widget widget = new Widget();
widget.id = "456";
widget.value = "foobar";
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final MessageBodyWriter<Widget> widgetWriter = (MessageBodyWriter<Widget>) writer;
widgetWriter.writeTo(widget, Widget.class, null, null, MediaType.APPLICATION_JSON_TYPE, null, outputStream);
assertEquals("{\"id\":\"456\",\"value\":\"foobar\"}", outputStream.toString());
}
示例12: write
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
@SuppressWarnings({ "rawtypes", "unchecked" })
public void write(
final MinijaxRequestContext context,
final Response response,
final HttpServletResponse servletResponse)
throws IOException {
servletResponse.setStatus(response.getStatus());
for (final Entry<String, List<Object>> entry : response.getHeaders().entrySet()) {
final String name = entry.getKey();
for (final Object value : entry.getValue()) {
servletResponse.addHeader(name, value.toString());
}
}
if (context.getMethod().equals("OPTIONS")) {
return;
}
final MediaType mediaType = response.getMediaType();
if (mediaType != null) {
servletResponse.setContentType(mediaType.toString());
}
final Object obj = response.getEntity();
if (obj == null) {
return;
}
final MessageBodyWriter writer = providers.getMessageBodyWriter(obj.getClass(), null, null, mediaType);
if (writer != null) {
writer.writeTo(obj, obj.getClass(), null, null, mediaType, null, servletResponse.getOutputStream());
return;
}
// What to do
servletResponse.getWriter().println(obj.toString());
}
示例13: handle
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
public void handle(Object result, RequestContext requestCtx) {
MessageBodyWriter writer = providers.getMessageBodyWriter(result.getClass(), result.getClass(), result.getClass().getAnnotations(), MediaType.valueOf(requestCtx.contentType()));
try {
writer.writeTo(result, result.getClass(), null, result.getClass().getAnnotations(), MediaType.valueOf(requestCtx.contentType()), null, requestCtx.getResponse().getOutputStream());
} catch (WebApplicationException | IllegalArgumentException | IOException e) {
e.printStackTrace();
}
}
示例14: writeTo
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public void writeTo(Object entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
final Type actualTypeArgument = actual(genericType);
final MessageBodyWriter writer = workers.get().getMessageBodyWriter(entity.getClass(), actualTypeArgument, annotations, mediaType);
writer.writeTo(entity, entity.getClass(), actualTypeArgument, annotations, mediaType, httpHeaders, entityStream);
}
示例15: aroundReadFrom
import javax.ws.rs.ext.MessageBodyWriter; //导入方法依赖的package包/类
@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");
}
}