Search in sources :

Example 6 with ProvidersImpl

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

the class DOM4JProviderTest method readXML.

private org.dom4j.Document readXML(MediaType ct, final String xml) throws Exception {
    DOM4JProvider p = new DOM4JProvider();
    p.setProviders(new ProvidersImpl(createMessage(false)));
    return p.readFrom(org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, ct, new MetadataMap<String, String>(), new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl)

Example 7 with ProvidersImpl

use of org.apache.cxf.jaxrs.impl.ProvidersImpl 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)

Example 8 with ProvidersImpl

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

the class DOM4JProviderTest method doTestWriteXML.

private void doTestWriteXML(MediaType ct, boolean convert) throws Exception {
    org.dom4j.Document dom = readXML(ct, "<a/>");
    final Message message = createMessage(false);
    Providers providers = new ProvidersImpl(message);
    DOM4JProvider p = new DOM4JProvider() {

        protected Message getCurrentMessage() {
            return message;
        }
    };
    p.setProviders(providers);
    p.convertToDOMAlways(convert);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, ct, new MetadataMap<String, Object>(), bos);
    String str = bos.toString();
    if (convert) {
        assertFalse(str.startsWith("<?xml"));
    } else {
        assertTrue(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)

Example 9 with ProvidersImpl

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

the class DOM4JProviderTest method testWriteJSON.

@Test
public void testWriteJSON() throws Exception {
    org.dom4j.Document dom = readXML();
    DOM4JProvider p = new DOM4JProvider();
    p.setProviders(new ProvidersImpl(createMessage(false)));
    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 10 with ProvidersImpl

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

the class MessageContextImpl method createAttachments.

private MultipartBody createAttachments(String propertyName) {
    Message inMessage = m.getExchange().getInMessage();
    boolean embeddedAttachment = inMessage.get("org.apache.cxf.multipart.embedded") != null;
    Object o = inMessage.get(propertyName);
    if (o != null) {
        return (MultipartBody) o;
    }
    if (embeddedAttachment) {
        inMessage = new MessageImpl();
        inMessage.setExchange(new ExchangeImpl());
        inMessage.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_DIRECTORY));
        inMessage.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD));
        inMessage.put(AttachmentDeserializer.ATTACHMENT_MAX_SIZE, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MAX_SIZE));
        inMessage.put(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE, m.getExchange().getInMessage().get(AttachmentDeserializer.ATTACHMENT_MAX_HEADER_SIZE));
        inMessage.setContent(InputStream.class, m.getExchange().getInMessage().get("org.apache.cxf.multipart.embedded.input"));
        inMessage.put(Message.CONTENT_TYPE, m.getExchange().getInMessage().get("org.apache.cxf.multipart.embedded.ctype").toString());
    }
    new AttachmentInputInterceptor().handleMessage(inMessage);
    List<Attachment> newAttachments = new LinkedList<Attachment>();
    try {
        Map<String, List<String>> headers = CastUtils.cast((Map<?, ?>) inMessage.get(AttachmentDeserializer.ATTACHMENT_PART_HEADERS));
        Attachment first = new Attachment(AttachmentUtil.createAttachment(inMessage.getContent(InputStream.class), headers), new ProvidersImpl(inMessage));
        newAttachments.add(first);
    } catch (IOException ex) {
        throw ExceptionUtils.toInternalServerErrorException(ex, null);
    }
    Collection<org.apache.cxf.message.Attachment> childAttachments = inMessage.getAttachments();
    if (childAttachments == null) {
        childAttachments = Collections.emptyList();
    }
    childAttachments.size();
    for (org.apache.cxf.message.Attachment a : childAttachments) {
        newAttachments.add(new Attachment(a, new ProvidersImpl(inMessage)));
    }
    MediaType mt = embeddedAttachment ? (MediaType) inMessage.get("org.apache.cxf.multipart.embedded.ctype") : getHttpHeaders().getMediaType();
    MultipartBody body = new MultipartBody(newAttachments, mt, false);
    inMessage.put(propertyName, body);
    return body;
}
Also used : Message(org.apache.cxf.message.Message) AttachmentInputInterceptor(org.apache.cxf.jaxrs.interceptor.AttachmentInputInterceptor) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) MediaType(javax.ws.rs.core.MediaType) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

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