Search in sources :

Example 1 with ProvidersImpl

use of org.apache.cxf.jaxrs.impl.ProvidersImpl in project cxf by apache.

the class JAXRSUtils method createContextValue.

public static <T> T createContextValue(Message m, Type genericType, Class<T> clazz) {
    Message contextMessage = getContextMessage(m);
    Object o = null;
    if (UriInfo.class.isAssignableFrom(clazz)) {
        o = createUriInfo(contextMessage);
    } else if (HttpHeaders.class.isAssignableFrom(clazz) || ProtocolHeaders.class.isAssignableFrom(clazz)) {
        o = createHttpHeaders(contextMessage, clazz);
    } else if (SecurityContext.class.isAssignableFrom(clazz)) {
        SecurityContext customContext = contextMessage.get(SecurityContext.class);
        o = customContext == null ? new SecurityContextImpl(contextMessage) : customContext;
    } else if (MessageContext.class.isAssignableFrom(clazz)) {
        o = new MessageContextImpl(m);
    } else if (ResourceInfo.class.isAssignableFrom(clazz)) {
        o = new ResourceInfoImpl(contextMessage);
    } else if (ResourceContext.class.isAssignableFrom(clazz)) {
        o = new ResourceContextImpl(contextMessage, contextMessage.getExchange().get(OperationResourceInfo.class));
    } else if (Request.class.isAssignableFrom(clazz)) {
        o = new RequestImpl(contextMessage);
    } else if (Providers.class.isAssignableFrom(clazz)) {
        o = new ProvidersImpl(contextMessage);
    } else if (ContextResolver.class.isAssignableFrom(clazz)) {
        o = createContextResolver(genericType, contextMessage);
    } else if (Configuration.class.isAssignableFrom(clazz)) {
        o = ProviderFactory.getInstance(contextMessage).getConfiguration(contextMessage);
    } else if (Application.class.isAssignableFrom(clazz)) {
        ProviderInfo<?> providerInfo = (ProviderInfo<?>) contextMessage.getExchange().getEndpoint().get(Application.class.getName());
        o = providerInfo == null ? null : providerInfo.getProvider();
    } else if (contextMessage != null) {
        ContextProvider<?> provider = ProviderFactory.getInstance(contextMessage).createContextProvider(clazz, contextMessage);
        if (provider != null) {
            o = provider.createContext(contextMessage);
        }
    }
    if (o == null && contextMessage != null && !MessageUtils.isRequestor(contextMessage)) {
        o = HttpUtils.createServletResourceValue(contextMessage, clazz);
    }
    return clazz.cast(o);
}
Also used : SecurityContextImpl(org.apache.cxf.jaxrs.impl.SecurityContextImpl) ResourceContext(javax.ws.rs.container.ResourceContext) Message(org.apache.cxf.message.Message) Configuration(javax.ws.rs.core.Configuration) ResourceInfoImpl(org.apache.cxf.jaxrs.impl.ResourceInfoImpl) ContextProvider(org.apache.cxf.jaxrs.ext.ContextProvider) Providers(javax.ws.rs.ext.Providers) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) SecurityContext(javax.ws.rs.core.SecurityContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) ResourceContextImpl(org.apache.cxf.jaxrs.impl.ResourceContextImpl) RequestImpl(org.apache.cxf.jaxrs.impl.RequestImpl) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Example 2 with ProvidersImpl

use of org.apache.cxf.jaxrs.impl.ProvidersImpl in project cxf by apache.

the class DOM4JProviderTest method testWriteJSONDropRoot.

@Test
public void testWriteJSONDropRoot() throws Exception {
    org.dom4j.Document dom = readXML(MediaType.APPLICATION_XML_TYPE, "<root><a/></root>");
    DOM4JProvider p = new DOM4JProvider();
    p.setProviders(new ProvidersImpl(createMessageWithJSONProvider()));
    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\":\"\"}", str);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) Test(org.junit.Test)

Example 3 with ProvidersImpl

use of org.apache.cxf.jaxrs.impl.ProvidersImpl in project cxf by apache.

the class DOM4JProviderTest method readXMLBom.

private org.dom4j.Document readXMLBom() throws Exception {
    byte[] bom = new byte[] { (byte) 239, (byte) 187, (byte) 191 };
    assertEquals("efbbbf", StringUtils.toHexString(bom));
    byte[] strBytes = "<a/>".getBytes(StandardCharsets.UTF_8);
    InputStream is = new SequenceInputStream(new ByteArrayInputStream(bom), new ByteArrayInputStream(strBytes));
    DOM4JProvider p = new DOM4JProvider();
    p.setProviders(new ProvidersImpl(createMessage(false)));
    return p.readFrom(org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, MediaType.valueOf("text/xml;a=b"), new MetadataMap<String, String>(), is);
}
Also used : SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl)

Example 4 with ProvidersImpl

use of org.apache.cxf.jaxrs.impl.ProvidersImpl in project cxf by apache.

the class DOM4JProviderTest method testReadJSONConvertToXML.

@Test
public void testReadJSONConvertToXML() throws Exception {
    final String xml = "{\"a\":{\"b\":2}}";
    DOM4JProvider p = new DOM4JProvider();
    p.setProviders(new ProvidersImpl(createMessage(false)));
    org.dom4j.Document dom = p.readFrom(org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
    String str = dom.asXML();
    // starts with the xml PI
    assertTrue(str.contains("<a><b>2</b></a>"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) Test(org.junit.Test)

Example 5 with ProvidersImpl

use of org.apache.cxf.jaxrs.impl.ProvidersImpl in project cxf by apache.

the class DOM4JProviderTest method testWriteXMLSuppressDeclaration.

@Test
public void testWriteXMLSuppressDeclaration() throws Exception {
    org.dom4j.Document dom = readXML(MediaType.APPLICATION_XML_TYPE, "<a/>");
    final Message message = createMessage(true);
    Providers providers = new ProvidersImpl(message);
    DOM4JProvider p = new DOM4JProvider() {

        protected Message getCurrentMessage() {
            return message;
        }
    };
    p.setProviders(providers);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, MediaType.APPLICATION_XML_TYPE, new MetadataMap<String, Object>(), bos);
    String str = bos.toString();
    assertFalse(str.startsWith("<?xml"));
    assertTrue(str.contains("<a/>") || str.contains("<a></a>"));
}
Also used : Message(org.apache.cxf.message.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Providers(javax.ws.rs.ext.Providers) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) Test(org.junit.Test)

Aggregations

ProvidersImpl (org.apache.cxf.jaxrs.impl.ProvidersImpl)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Test (org.junit.Test)5 Message (org.apache.cxf.message.Message)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Providers (javax.ws.rs.ext.Providers)3 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ResourceContext (javax.ws.rs.container.ResourceContext)1 Configuration (javax.ws.rs.core.Configuration)1 MediaType (javax.ws.rs.core.MediaType)1 SecurityContext (javax.ws.rs.core.SecurityContext)1 ContextProvider (org.apache.cxf.jaxrs.ext.ContextProvider)1 MessageContext (org.apache.cxf.jaxrs.ext.MessageContext)1 MessageContextImpl (org.apache.cxf.jaxrs.ext.MessageContextImpl)1 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)1