Package javax.ws.rs.ext

Examples of javax.ws.rs.ext.MessageBodyWriter


     * @throws IOException
     */
    protected void writeBody(OutputStream os, Providers providers) throws IOException {

        if (getBody() != null) {
            MessageBodyWriter writer =
                providers.getMessageBodyWriter(getBody().getClass(), null, null, MediaType
                    .valueOf(getContentType()));
            if (writer == null) {
                logger
                    .error(Messages.getMessage("couldNotFindWriter", getBody().getClass(), //$NON-NLS-1$
                           getContentType()));
                throw new WebApplicationException(500);
            }
            writer.writeTo(getBody(), getBody().getClass(), null, null, MediaType
                .valueOf(getContentType()), getHeaders(), os);
        }
    }
View Full Code Here


                                                       (TypeVariable)genericType);
        }
       
        Annotation[] annotations = invoked != null ? invoked.getAnnotations() : new Annotation[]{};
       
        MessageBodyWriter writer = null;
        MediaType responseType = null;
        for (MediaType type : availableContentTypes) {
            writer = ProviderFactory.getInstance(message)
                .createMessageBodyWriter(targetType, genericType, annotations, type, message);
           
            if (writer != null) {
                responseType = type;
                break;
            }
        }
   
        OutputStream outOriginal = message.getContent(OutputStream.class);
        if (writer == null) {
            message.put(Message.CONTENT_TYPE, "text/plain");
            message.put(Message.RESPONSE_CODE, 500);
            writeResponseErrorMessage(outOriginal, "NO_MSG_WRITER", targetType.getSimpleName());
            return;
        }
        boolean enabled = checkBufferingMode(message, writer, firstTry);
        Object entity = getEntity(responseObj);
        try {
            responseType = checkFinalContentType(responseType);
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("Response content type is: " + responseType.toString());
            }
            message.put(Message.CONTENT_TYPE, responseType.toString());
           
            long size = writer.getSize(entity, targetType, genericType, annotations, responseType);
            if (size > 0) {
                LOG.fine("Setting ContentLength to " + size + " as requested by "
                         + writer.getClass().getName());
                responseHeaders.putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(size));
            }
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("Response EntityProvider is: " + writer.getClass().getName());
            }
            try {
                writer.writeTo(entity, targetType, genericType,
                               annotations,
                               responseType,
                               responseHeaders,
                               message.getContent(OutputStream.class));
               
View Full Code Here

            return;
        }
       
        MediaType contentType = MediaType.valueOf(headers.getFirst("Content-Type"));
       
        MessageBodyWriter mbw = ProviderFactory.getInstance(outMessage).createMessageBodyWriter(
            cls, type, anns, contentType, outMessage);
        if (mbw != null) {
            try {
                mbw.writeTo(o, cls, type, anns, contentType, headers, os);
                if (os != null) {
                    os.flush();
                }
            } catch (Exception ex) {
                reportMessageHandlerProblem("MSG_WRITER_PROBLEM", cls, contentType, ex, null);
View Full Code Here

    }

    @SuppressWarnings("unchecked")
    @Test
    public void testWriteBoolean() throws Exception {
        MessageBodyWriter p = new PrimitiveTextProvider();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        p.writeTo(Boolean.TRUE, null, null, null, MediaType.TEXT_PLAIN_TYPE, null, os);
        assertTrue(Arrays.equals(new String("true").getBytes(), os.toByteArray()));
       
        os = new ByteArrayOutputStream();
       
        final boolean value = true;
        p.writeTo(value, null, null, null, MediaType.TEXT_PLAIN_TYPE, null, os);
        assertTrue(Arrays.equals(new String("true").getBytes(), os.toByteArray()));
    }
View Full Code Here

   
   
    @SuppressWarnings("unchecked")
    @Test
    public void testWriteStringISO() throws Exception {
        MessageBodyWriter p = new PrimitiveTextProvider();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        MultivaluedMap<String, Object> headers = new MetadataMap<String, Object>();
       
        String eWithAcute = "\u00E9";
        String helloStringUTF16 = "Hello, my name is F" + eWithAcute + "lix Agn" + eWithAcute + "s";
       
        p.writeTo(helloStringUTF16,
                  String.class, String.class, null, MediaType.valueOf("text/plain;charset=ISO-8859-1"),
                  headers, os);
       
        byte[] iso88591bytes = helloStringUTF16.getBytes("ISO-8859-1");
        String helloStringISO88591 = new String(iso88591bytes, "ISO-8859-1");
View Full Code Here

    private static class MessageBodyWriterComparator
        implements Comparator<ProviderInfo<MessageBodyWriter>> {
       
        public int compare(ProviderInfo<MessageBodyWriter> p1,
                           ProviderInfo<MessageBodyWriter> p2) {
            MessageBodyWriter e1 = p1.getProvider();
            MessageBodyWriter e2 = p2.getProvider();
           
            List<MediaType> types1 =
                JAXRSUtils.sortMediaTypes(JAXRSUtils.getProviderProduceTypes(e1));
            List<MediaType> types2 =
                JAXRSUtils.sortMediaTypes(JAXRSUtils.getProviderProduceTypes(e2));
View Full Code Here

    }

    @SuppressWarnings("unchecked")
    @Test
    public void testWriteBoolean() throws Exception {
        MessageBodyWriter p = new PrimitiveTextProvider();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        p.writeTo(Boolean.TRUE, null, null, null, MediaType.TEXT_PLAIN_TYPE, null, os);
        assertTrue(Arrays.equals(new String("true").getBytes(), os.toByteArray()));
       
        os = new ByteArrayOutputStream();
       
        final boolean value = true;
        p.writeTo(value, null, null, null, MediaType.TEXT_PLAIN_TYPE, null, os);
        assertTrue(Arrays.equals(new String("true").getBytes(), os.toByteArray()));
    }
View Full Code Here

   
   
    @SuppressWarnings("unchecked")
    @Test
    public void testWriteStringISO() throws Exception {
        MessageBodyWriter p = new PrimitiveTextProvider();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        MultivaluedMap<String, Object> headers = new MetadataMap<String, Object>();
       
        String eWithAcute = "\u00E9";
        String helloStringUTF16 = "Hello, my name is F" + eWithAcute + "lix Agn" + eWithAcute + "s";
       
        p.writeTo(helloStringUTF16,
                  String.class, String.class, null, MediaType.valueOf("text/plain;charset=ISO-8859-1"),
                  headers, os);
       
        byte[] iso88591bytes = helloStringUTF16.getBytes("ISO-8859-1");
        String helloStringISO88591 = new String(iso88591bytes, "ISO-8859-1");
View Full Code Here

       
        MessageBodyReader customJaxbReader2 = pf.createMessageBodyReader((Class<?>)Book.class, null, null,
                                                              MediaType.TEXT_XML_TYPE, message);
        assertSame(customJaxbReader, customJaxbReader2);
        
        MessageBodyWriter customJaxbWriter = pf.createMessageBodyWriter((Class<?>)Book.class, null, null,
                                                              MediaType.TEXT_XML_TYPE, message);
        assertSame(customJaxbReader, customJaxbWriter);
       
        MessageBodyReader jaxbReader = ProviderFactory.getSharedInstance().createMessageBodyReader(
            (Class<?>)Book.class, null, null, MediaType.TEXT_XML_TYPE, message);
View Full Code Here

        pf.registerUserProvider(provider);
        MessageBodyReader customJaxbReader = pf.createMessageBodyReader((Class<?>)Book.class, null, null,
                                                              MediaType.TEXT_XML_TYPE, new MessageImpl());
        assertSame(customJaxbReader, provider);
       
        MessageBodyWriter customJaxbWriter = pf.createMessageBodyWriter((Class<?>)Book.class, null, null,
                                                              MediaType.TEXT_XML_TYPE, new MessageImpl());
        assertSame(customJaxbWriter, provider);
    }
View Full Code Here

TOP

Related Classes of javax.ws.rs.ext.MessageBodyWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.