Search in sources :

Example 21 with MessageBodyWriter

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);
    }
}
Also used : OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) MediaType(javax.ws.rs.core.MediaType) IOException(java.io.IOException) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter)

Example 22 with MessageBodyWriter

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();
}
Also used : MergeResult(org.eclipse.che.api.git.shared.MergeResult) OutputStreamWriter(java.io.OutputStreamWriter) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 23 with MessageBodyWriter

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();
}
Also used : Branch(org.eclipse.che.api.git.shared.Branch) OutputStreamWriter(java.io.OutputStreamWriter) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 24 with MessageBodyWriter

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);
}
Also used : Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) Priorities(javax.ws.rs.Priorities) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) ParamConverter(javax.ws.rs.ext.ParamConverter) ContextResolver(javax.ws.rs.ext.ContextResolver) JAXBContextProvider2(org.apache.cxf.jaxrs.JAXBContextProvider2) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) JAXBContextProvider(org.apache.cxf.jaxrs.JAXBContextProvider) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Collection(java.util.Collection) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Customer(org.apache.cxf.jaxrs.Customer) Priority(javax.annotation.Priority) List(java.util.List) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) Response(javax.ws.rs.core.Response) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) Bus(org.apache.cxf.Bus) MessageImpl(org.apache.cxf.message.MessageImpl) Feature(javax.ws.rs.core.Feature) DataHandler(javax.activation.DataHandler) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) Status(javax.ws.rs.core.Response.Status) JAXBContext(javax.xml.bind.JAXBContext) WebApplicationExceptionMapper(org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper) Before(org.junit.Before) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) CustomerParameterHandler(org.apache.cxf.jaxrs.CustomerParameterHandler) Message(org.apache.cxf.message.Message) IOUtils(org.apache.cxf.helpers.IOUtils) IOException(java.io.IOException) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Book(org.apache.cxf.jaxrs.resources.Book) Exchange(org.apache.cxf.message.Exchange) File(java.io.File) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) AbstractResourceInfo(org.apache.cxf.jaxrs.model.AbstractResourceInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) BusFactory(org.apache.cxf.BusFactory) DataSource(javax.activation.DataSource) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 25 with MessageBodyWriter

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());
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Map(java.util.Map) HashMap(java.util.HashMap) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Aggregations

MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)25 MediaType (javax.ws.rs.core.MediaType)10 Test (org.junit.Test)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 OutputStreamWriter (java.io.OutputStreamWriter)6 Writer (java.io.Writer)6 Type (java.lang.reflect.Type)6 IOException (java.io.IOException)5 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)5 List (java.util.List)4 Map (java.util.Map)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 OutputStream (java.io.OutputStream)3 Annotation (java.lang.annotation.Annotation)3 ArrayList (java.util.ArrayList)3 Produces (javax.ws.rs.Produces)3 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)3 File (java.io.File)2