Search in sources :

Example 1 with MessageBodyWriter

use of javax.ws.rs.ext.MessageBodyWriter in project che by eclipse.

the class CommitMessageWriter 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(Revision revision, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    Writer writer = new OutputStreamWriter(entityStream);
    writer.write('[');
    writer.write(revision.getBranch());
    writer.write(' ');
    writer.write(revision.getId());
    writer.write(']');
    writer.write(' ');
    writer.write(revision.getMessage());
    writer.write('\n');
    writer.flush();
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with MessageBodyWriter

use of javax.ws.rs.ext.MessageBodyWriter in project che by eclipse.

the class RemoteListWriter 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<Remote> remotes, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    Writer writer = new OutputStreamWriter(entityStream);
    for (Remote remote : remotes) {
        writer.write(remote.getName());
        String url = remote.getUrl();
        if (url != null) {
            writer.write(' ');
            writer.write(' ');
            writer.write(url);
        }
        writer.write('\n');
    }
    writer.flush();
}
Also used : Remote(org.eclipse.che.api.git.shared.Remote) OutputStreamWriter(java.io.OutputStreamWriter) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 3 with MessageBodyWriter

use of javax.ws.rs.ext.MessageBodyWriter in project che by eclipse.

the class TagListWriter 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<Tag> tags, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    Writer writer = new OutputStreamWriter(entityStream);
    for (Tag tag : tags) {
        writer.write(tag.getName());
        writer.write('\n');
    }
    writer.flush();
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) Tag(org.eclipse.che.api.git.shared.Tag) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 4 with MessageBodyWriter

use of javax.ws.rs.ext.MessageBodyWriter in project jersey by jersey.

the class JaxbStringReaderProviderTest method stringReaderDoesNotReadExternalDtds.

@Test
public void stringReaderDoesNotReadExternalDtds() {
    Provider<SAXParserFactory> saxParserFactoryProvider = new Provider<SAXParserFactory>() {

        final SaxParserFactoryInjectionProvider spf = new SaxParserFactoryInjectionProvider(new CommonConfig(RuntimeType.SERVER, ComponentBag.INCLUDE_ALL));

        @Override
        public SAXParserFactory get() {
            return spf.get();
        }
    };
    JaxbStringReaderProvider.RootElementProvider provider = new JaxbStringReaderProvider.RootElementProvider(saxParserFactoryProvider, new Providers() {

        @Override
        public <T> MessageBodyReader<T> getMessageBodyReader(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return null;
        }

        @Override
        public <T> MessageBodyWriter<T> getMessageBodyWriter(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
            return null;
        }

        @Override
        public <T extends Throwable> ExceptionMapper<T> getExceptionMapper(Class<T> type) {
            return null;
        }

        @Override
        public <T> ContextResolver<T> getContextResolver(Class<T> contextType, MediaType mediaType) {
            return null;
        }
    });
    String content = "<!DOCTYPE x SYSTEM 'file:///no-such-file'> <rootObject/>";
    provider.getConverter(RootObject.class, null, null).fromString(content);
}
Also used : CommonConfig(org.glassfish.jersey.model.internal.CommonConfig) Providers(javax.ws.rs.ext.Providers) Annotation(java.lang.annotation.Annotation) Provider(javax.inject.Provider) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) RuntimeType(javax.ws.rs.RuntimeType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MediaType(javax.ws.rs.core.MediaType) ContextResolver(javax.ws.rs.ext.ContextResolver) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Test(org.junit.Test)

Example 5 with MessageBodyWriter

use of javax.ws.rs.ext.MessageBodyWriter in project jersey by jersey.

the class MultiPartWriter method writeTo.

/**
     * Write the entire list of body parts to the output stream, using the
     * appropriate provider implementation to serialize each body part's entity.
     *
     * @param entity      the {@link MultiPart} instance to write.
     * @param type        the class of the object to be written (i.e. {@link MultiPart}.class).
     * @param genericType the type of object to be written.
     * @param annotations annotations on the resource method that returned this object.
     * @param mediaType   media type ({@code multipart/*}) of this entity.
     * @param headers     mutable map of HTTP headers for the entire response.
     * @param stream      output stream to which the entity should be written.
     * @throws java.io.IOException if an I/O error occurs.
     * @throws javax.ws.rs.WebApplicationException
     *                             if an HTTP error response
     *                             needs to be produced (only effective if the response is not committed yet).
     */
@Override
public void writeTo(final MultiPart entity, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> headers, final OutputStream stream) throws IOException, WebApplicationException {
    // Verify that there is at least one body part.
    if ((entity.getBodyParts() == null) || (entity.getBodyParts().size() < 1)) {
        throw new IllegalArgumentException(LocalizationMessages.MUST_SPECIFY_BODY_PART());
    }
    // If our entity is not nested, make sure the MIME-Version header is set.
    if (entity.getParent() == null) {
        final Object value = headers.getFirst("MIME-Version");
        if (value == null) {
            headers.putSingle("MIME-Version", "1.0");
        }
    }
    // Initialize local variables we need.
    final Writer writer = new BufferedWriter(new OutputStreamWriter(stream, MessageUtils.getCharset(mediaType)));
    // Determine the boundary string to be used, creating one if needed.
    final MediaType boundaryMediaType = Boundary.addBoundary(mediaType);
    if (boundaryMediaType != mediaType) {
        headers.putSingle(HttpHeaders.CONTENT_TYPE, boundaryMediaType.toString());
    }
    final String boundaryString = boundaryMediaType.getParameters().get("boundary");
    // Iterate through the body parts for this message.
    boolean isFirst = true;
    for (final BodyPart bodyPart : entity.getBodyParts()) {
        // Write the leading boundary string
        if (isFirst) {
            isFirst = false;
            writer.write("--");
        } else {
            writer.write("\r\n--");
        }
        writer.write(boundaryString);
        writer.write("\r\n");
        // Write the headers for this body part
        final MediaType bodyMediaType = bodyPart.getMediaType();
        if (bodyMediaType == null) {
            throw new IllegalArgumentException(LocalizationMessages.MISSING_MEDIA_TYPE_OF_BODY_PART());
        }
        final MultivaluedMap<String, String> bodyHeaders = bodyPart.getHeaders();
        bodyHeaders.putSingle("Content-Type", bodyMediaType.toString());
        if (bodyHeaders.getFirst("Content-Disposition") == null && bodyPart.getContentDisposition() != null) {
            bodyHeaders.putSingle("Content-Disposition", bodyPart.getContentDisposition().toString());
        }
        // Iterate for the nested body parts
        for (final Map.Entry<String, List<String>> entry : bodyHeaders.entrySet()) {
            // Write this header and its value(s)
            writer.write(entry.getKey());
            writer.write(':');
            boolean first = true;
            for (final String value : entry.getValue()) {
                if (first) {
                    writer.write(' ');
                    first = false;
                } else {
                    writer.write(',');
                }
                writer.write(value);
            }
            writer.write("\r\n");
        }
        // Mark the end of the headers for this body part
        writer.write("\r\n");
        writer.flush();
        // Write the entity for this body part
        Object bodyEntity = bodyPart.getEntity();
        if (bodyEntity == null) {
            throw new IllegalArgumentException(LocalizationMessages.MISSING_ENTITY_OF_BODY_PART(bodyMediaType));
        }
        Class bodyClass = bodyEntity.getClass();
        if (bodyEntity instanceof BodyPartEntity) {
            bodyClass = InputStream.class;
            bodyEntity = ((BodyPartEntity) bodyEntity).getInputStream();
        }
        final MessageBodyWriter bodyWriter = providers.getMessageBodyWriter(bodyClass, bodyClass, EMPTY_ANNOTATIONS, bodyMediaType);
        if (bodyWriter == null) {
            throw new IllegalArgumentException(LocalizationMessages.NO_AVAILABLE_MBW(bodyClass, mediaType));
        }
        bodyWriter.writeTo(bodyEntity, bodyClass, bodyClass, EMPTY_ANNOTATIONS, bodyMediaType, bodyHeaders, stream);
    }
    // Write the final boundary string
    writer.write("\r\n--");
    writer.write(boundaryString);
    writer.write("--\r\n");
    writer.flush();
}
Also used : BodyPart(org.glassfish.jersey.media.multipart.BodyPart) BufferedWriter(java.io.BufferedWriter) BodyPartEntity(org.glassfish.jersey.media.multipart.BodyPartEntity) MediaType(javax.ws.rs.core.MediaType) OutputStreamWriter(java.io.OutputStreamWriter) List(java.util.List) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

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