Search in sources :

Example 6 with ProviderFactory

use of org.apache.cxf.jaxrs.provider.ProviderFactory in project tomee by apache.

the class JAXRSUtils method readFromMessageBody.

private static Object readFromMessageBody(Class<?> targetTypeClass, Type parameterType, Annotation[] parameterAnnotations, InputStream is, MediaType contentType, OperationResourceInfo ori, Message m) throws IOException, WebApplicationException {
    List<MediaType> types = JAXRSUtils.intersectMimeTypes(ori.getConsumeTypes(), contentType);
    final ProviderFactory pf = ServerProviderFactory.getInstance(m);
    for (MediaType type : types) {
        List<ReaderInterceptor> readers = pf.createMessageBodyReaderInterceptor(targetTypeClass, parameterType, parameterAnnotations, type, m, true, ori.getNameBindings());
        if (readers != null) {
            try {
                return readFromMessageBodyReader(readers, targetTypeClass, parameterType, parameterAnnotations, is, type, m);
            } catch (IOException e) {
                if (e.getClass().getName().equals(NO_CONTENT_EXCEPTION)) {
                    throw ExceptionUtils.toBadRequestException(e, null);
                }
                throw e;
            } catch (WebApplicationException ex) {
                throw ex;
            } catch (Exception ex) {
                throw new Fault(ex);
            }
        }
    }
    logMessageHandlerProblem("NO_MSG_READER", targetTypeClass, contentType);
    throw new WebApplicationException(Response.Status.UNSUPPORTED_MEDIA_TYPE);
}
Also used : ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) WebApplicationException(javax.ws.rs.WebApplicationException) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) MediaType(javax.ws.rs.core.MediaType) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) ClientErrorException(javax.ws.rs.ClientErrorException) NoContentException(javax.ws.rs.core.NoContentException)

Example 7 with ProviderFactory

use of org.apache.cxf.jaxrs.provider.ProviderFactory in project tomee by apache.

the class ResponseImpl method doReadEntity.

public <T> T doReadEntity(Class<T> cls, Type t, Annotation[] anns) throws ProcessingException, IllegalStateException {
    checkEntityIsClosed();
    // according to javadoc, should close when is not buffered.
    boolean shouldClose = !entityBufferred && !JAXRSUtils.isStreamingOutType(cls);
    if (lastEntity != null && cls.isAssignableFrom(lastEntity.getClass()) && !(lastEntity instanceof InputStream)) {
        return cls.cast(lastEntity);
    }
    MediaType mediaType = getMediaType();
    if (mediaType == null) {
        mediaType = MediaType.WILDCARD_TYPE;
    }
    // the stream is available if entity is IS or
    // message contains XMLStreamReader or Reader
    boolean entityStreamAvailable = entityStreamAvailable();
    InputStream entityStream = null;
    if (!entityStreamAvailable) {
        // try create a stream if the entity is String or Number
        entityStream = convertEntityToStreamIfPossible();
        entityStreamAvailable = entityStream != null;
    } else if (entity instanceof InputStream) {
        entityStream = InputStream.class.cast(entity);
    } else {
        Message inMessage = getResponseMessage();
        Reader reader = inMessage.getContent(Reader.class);
        if (reader != null) {
            entityStream = InputStream.class.cast(new ReaderInputStream(reader));
        }
    }
    // we need to check for readers even if no IS is set - the readers may still do it
    List<ReaderInterceptor> readers = outMessage == null ? null : ProviderFactory.getInstance(outMessage).createMessageBodyReaderInterceptor(cls, t, anns, mediaType, outMessage, entityStreamAvailable, null);
    if (readers != null) {
        try {
            if (entityBufferred) {
                InputStream.class.cast(entity).reset();
            }
            Message responseMessage = getResponseMessage();
            responseMessage.put(Message.PROTOCOL_HEADERS, getHeaders());
            lastEntity = JAXRSUtils.readFromMessageBodyReader(readers, cls, t, anns, entityStream, mediaType, responseMessage);
            // close the entity after readEntity is called.
            T tCastLastEntity = castLastEntity();
            shouldClose = shouldClose && !(tCastLastEntity instanceof AutoCloseable) && !(tCastLastEntity instanceof Source);
            if (shouldClose) {
                close();
            }
            return tCastLastEntity;
        } catch (NoContentException ex) {
            // check if basic type. Basic type throw exception, else return null.
            if (isBasicType(cls)) {
                autoClose(cls, true);
                reportMessageHandlerProblem("MSG_READER_PROBLEM", cls, mediaType, ex);
            } else {
                if (shouldClose) {
                    close();
                }
                return null;
            }
        } catch (Exception ex) {
            autoClose(cls, true);
            reportMessageHandlerProblem("MSG_READER_PROBLEM", cls, mediaType, ex);
        } finally {
            ProviderFactory pf = ProviderFactory.getInstance(outMessage);
            if (pf != null) {
                pf.clearThreadLocalProxies();
            }
        }
    } else if (entity != null && cls.isAssignableFrom(entity.getClass())) {
        lastEntity = entity;
        return castLastEntity();
    } else if (entityStreamAvailable) {
        reportMessageHandlerProblem("NO_MSG_READER", cls, mediaType, null);
    }
    throw new IllegalStateException("The entity is not backed by an input stream, entity class is : " + (entity != null ? entity.getClass().getName() : cls.getName()));
}
Also used : ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) Message(org.apache.cxf.message.Message) PushbackInputStream(java.io.PushbackInputStream) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) NoContentException(javax.ws.rs.core.NoContentException) Source(javax.xml.transform.Source) NoContentException(javax.ws.rs.core.NoContentException) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) MediaType(javax.ws.rs.core.MediaType)

Example 8 with ProviderFactory

use of org.apache.cxf.jaxrs.provider.ProviderFactory in project cxf by apache.

the class ResponseImpl method doReadEntity.

public <T> T doReadEntity(Class<T> cls, Type t, Annotation[] anns) throws ProcessingException, IllegalStateException {
    checkEntityIsClosed();
    if (lastEntity != null && cls.isAssignableFrom(lastEntity.getClass()) && !(lastEntity instanceof InputStream)) {
        return cls.cast(lastEntity);
    }
    MediaType mediaType = getMediaType();
    if (mediaType == null) {
        mediaType = MediaType.WILDCARD_TYPE;
    }
    // the stream is available if entity is IS or
    // message contains XMLStreamReader or Reader
    boolean entityStreamAvailable = entityStreamAvailable();
    InputStream entityStream = null;
    if (!entityStreamAvailable) {
        // try create a stream if the entity is String or Number
        entityStream = convertEntityToStreamIfPossible();
        entityStreamAvailable = entityStream != null;
    } else if (entity instanceof InputStream) {
        entityStream = InputStream.class.cast(entity);
    } else {
        Message inMessage = getResponseMessage();
        Reader reader = inMessage.getContent(Reader.class);
        if (reader != null) {
            entityStream = InputStream.class.cast(new ReaderInputStream(reader));
        }
    }
    // we need to check for readers even if no IS is set - the readers may still do it
    List<ReaderInterceptor> readers = outMessage == null ? null : ProviderFactory.getInstance(outMessage).createMessageBodyReaderInterceptor(cls, t, anns, mediaType, outMessage, entityStreamAvailable, null);
    if (readers != null) {
        try {
            if (entityBufferred) {
                InputStream.class.cast(entity).reset();
            }
            Message responseMessage = getResponseMessage();
            responseMessage.put(Message.PROTOCOL_HEADERS, getHeaders());
            lastEntity = JAXRSUtils.readFromMessageBodyReader(readers, cls, t, anns, entityStream, mediaType, responseMessage);
            autoClose(cls, false);
            return castLastEntity();
        } catch (Exception ex) {
            autoClose(cls, true);
            reportMessageHandlerProblem("MSG_READER_PROBLEM", cls, mediaType, ex);
        } finally {
            ProviderFactory pf = ProviderFactory.getInstance(outMessage);
            if (pf != null) {
                pf.clearThreadLocalProxies();
            }
        }
    } else if (entity != null && cls.isAssignableFrom(entity.getClass())) {
        lastEntity = entity;
        return castLastEntity();
    } else if (entityStreamAvailable) {
        reportMessageHandlerProblem("NO_MSG_READER", cls, mediaType, null);
    }
    throw new IllegalStateException("The entity is not backed by an input stream, entity class is : " + (entity != null ? entity.getClass().getName() : cls.getName()));
}
Also used : ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) Message(org.apache.cxf.message.Message) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) MediaType(javax.ws.rs.core.MediaType) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException)

Example 9 with ProviderFactory

use of org.apache.cxf.jaxrs.provider.ProviderFactory in project cxf by apache.

the class AbstractClient method convertParamValue.

protected String convertParamValue(Object pValue, Class<?> pClass, Annotation[] anns) {
    if (pValue == null && pClass == null) {
        return null;
    }
    ProviderFactory pf = ClientProviderFactory.getInstance(cfg.getEndpoint());
    if (pf != null) {
        Message m = null;
        if (pf.isParamConverterContextsAvailable()) {
            m = new MessageImpl();
            m.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
            m.setExchange(new ExchangeImpl());
            m.getExchange().setOutMessage(m);
            m.getExchange().put(Endpoint.class, cfg.getEndpoint());
        }
        @SuppressWarnings("unchecked") ParamConverter<Object> prov = (ParamConverter<Object>) pf.createParameterHandler(pClass, pClass, anns, m);
        if (prov != null) {
            try {
                return prov.toString(pValue);
            } finally {
                if (m != null) {
                    pf.clearThreadLocalProxies();
                }
            }
        }
    }
    final String v = pValue == null ? null : pValue.toString();
    if (anns != null && StringUtils.isEmpty(v)) {
        final PathParam pp = AnnotationUtils.getAnnotation(anns, PathParam.class);
        if (null != pp) {
            Object allowEmptyProp = getConfiguration().getBus().getProperty(ALLOW_EMPTY_PATH_VALUES);
            if (!PropertyUtils.isTrue(allowEmptyProp)) {
                throw new IllegalArgumentException("Value for " + pp.value() + " is not specified");
            }
        }
    }
    return v;
}
Also used : Message(org.apache.cxf.message.Message) ParamConverter(javax.ws.rs.ext.ParamConverter) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) PathParam(javax.ws.rs.PathParam) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 10 with ProviderFactory

use of org.apache.cxf.jaxrs.provider.ProviderFactory in project cxf by apache.

the class DOM4JProviderTest method testWriteJSONAsArray.

@Test
public void testWriteJSONAsArray() throws Exception {
    org.dom4j.Document dom = readXML(MediaType.APPLICATION_XML_TYPE, "<root><a>1</a></root>");
    DOM4JProvider p = new DOM4JProvider();
    ProviderFactory factory = ServerProviderFactory.getInstance();
    JSONProvider<Object> provider = new JSONProvider<Object>();
    provider.setSerializeAsArray(true);
    provider.setDropRootElement(true);
    provider.setDropElementsInXmlStream(false);
    provider.setIgnoreNamespaces(true);
    factory.registerUserProvider(provider);
    p.setProviders(new ProvidersImpl(createMessage(factory)));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), bos);
    String str = bos.toString();
    assertEquals("[{\"a\":1}]", str);
}
Also used : ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) JSONProvider(org.apache.cxf.jaxrs.provider.json.JSONProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) Test(org.junit.Test)

Aggregations

ProviderFactory (org.apache.cxf.jaxrs.provider.ProviderFactory)17 ServerProviderFactory (org.apache.cxf.jaxrs.provider.ServerProviderFactory)13 Message (org.apache.cxf.message.Message)12 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 MessageImpl (org.apache.cxf.message.MessageImpl)10 Endpoint (org.apache.cxf.endpoint.Endpoint)9 Exchange (org.apache.cxf.message.Exchange)9 Application (javax.ws.rs.core.Application)6 IOException (java.io.IOException)4 MediaType (javax.ws.rs.core.MediaType)4 ReaderInterceptor (javax.ws.rs.ext.ReaderInterceptor)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Reader (java.io.Reader)2 ClientErrorException (javax.ws.rs.ClientErrorException)2 ProcessingException (javax.ws.rs.ProcessingException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)2 NoContentException (javax.ws.rs.core.NoContentException)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2