use of javax.ws.rs.ext.MessageBodyWriter in project jersey by jersey.
the class OutboundEventWriter method writeTo.
@Override
@SuppressWarnings("unchecked")
public void writeTo(final OutboundEvent outboundEvent, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException {
final Charset charset = MessageUtils.getCharset(mediaType);
if (outboundEvent.getComment() != null) {
for (final String comment : outboundEvent.getComment().split("\n")) {
entityStream.write(COMMENT_LEAD);
entityStream.write(comment.getBytes(charset));
entityStream.write(EOL);
}
}
if (outboundEvent.getType() != null) {
if (outboundEvent.getName() != null) {
entityStream.write(NAME_LEAD);
entityStream.write(outboundEvent.getName().getBytes(charset));
entityStream.write(EOL);
}
if (outboundEvent.getId() != null) {
entityStream.write(ID_LEAD);
entityStream.write(outboundEvent.getId().getBytes(charset));
entityStream.write(EOL);
}
if (outboundEvent.getReconnectDelay() > SseFeature.RECONNECT_NOT_SET) {
entityStream.write(RETRY_LEAD);
entityStream.write(Long.toString(outboundEvent.getReconnectDelay()).getBytes(charset));
entityStream.write(EOL);
}
final MediaType eventMediaType = outboundEvent.getMediaType() == null ? MediaType.TEXT_PLAIN_TYPE : outboundEvent.getMediaType();
final MessageBodyWriter messageBodyWriter = workersProvider.get().getMessageBodyWriter(outboundEvent.getType(), outboundEvent.getGenericType(), annotations, eventMediaType);
messageBodyWriter.writeTo(outboundEvent.getData(), outboundEvent.getType(), outboundEvent.getGenericType(), annotations, eventMediaType, httpHeaders, new OutputStream() {
private boolean start = true;
@Override
public void write(final int i) throws IOException {
if (start) {
entityStream.write(DATA_LEAD);
start = false;
}
entityStream.write(i);
if (i == '\n') {
entityStream.write(DATA_LEAD);
}
}
});
entityStream.write(EOL);
}
}
use of javax.ws.rs.ext.MessageBodyWriter in project che by eclipse.
the class MergeResultWriter method writeTo.
/**
* @see MessageBodyWriter#writeTo(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType,
* javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)
*/
@Override
public void writeTo(MergeResult mergeResult, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
Writer writer = new OutputStreamWriter(entityStream);
MergeResult.MergeStatus status = mergeResult.getMergeStatus();
switch(mergeResult.getMergeStatus()) {
case FAST_FORWARD:
case ALREADY_UP_TO_DATE:
case MERGED:
writer.write(status.toString());
writer.write('\n');
break;
case FAILED:
writer.write("error: Failed to merge:");
for (String failed : mergeResult.getFailed()) {
writer.write(" ");
writer.write(failed);
writer.write('\n');
}
break;
case CONFLICTING:
for (String conflict : mergeResult.getConflicts()) {
writer.write("CONFLICT(content): Merge conflict in: " + conflict);
writer.write('\n');
}
writer.write("Automatic merge failed; fix conflicts and then commit the result");
writer.write('\n');
break;
case NOT_SUPPORTED:
writer.write("Operation not supported");
writer.write('\n');
break;
default:
break;
}
writer.flush();
}
use of javax.ws.rs.ext.MessageBodyWriter in project che by eclipse.
the class BranchListWriter method writeTo.
/**
* @see MessageBodyWriter#writeTo(Object, Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType,
* javax.ws.rs.core.MultivaluedMap, java.io.OutputStream)
*/
@Override
public void writeTo(Iterable<Branch> branches, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
Writer writer = new OutputStreamWriter(entityStream);
for (Branch branch : branches) {
if (branch.isActive()) {
writer.write('*');
} else {
writer.write(' ');
}
writer.write(' ');
if (branch.getName().startsWith("refs/remotes")) {
// trim leading 'refs/'
writer.write(branch.getName().substring(5));
} else {
writer.write(branch.getDisplayName());
}
writer.write('\n');
}
writer.flush();
}
use of javax.ws.rs.ext.MessageBodyWriter in project cxf by apache.
the class ProviderFactoryTest method testRegisterMbrMbwProviderAsMbwOnly.
@Test
public void testRegisterMbrMbwProviderAsMbwOnly() {
ServerProviderFactory pf = ServerProviderFactory.getInstance();
JAXBElementProvider<Book> customProvider = new JAXBElementProvider<Book>();
pf.registerUserProvider((Feature) context -> {
context.register(customProvider, MessageBodyWriter.class);
return true;
});
MessageBodyWriter<Book> writer = pf.createMessageBodyWriter(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
assertSame(writer, customProvider);
MessageBodyReader<Book> reader = pf.createMessageBodyReader(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
assertTrue(reader instanceof JAXBElementProvider);
assertNotSame(reader, customProvider);
}
use of javax.ws.rs.ext.MessageBodyWriter in project minijax by minijax.
the class WriterTest method testWriteMap.
@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());
}
Aggregations