Search in sources :

Example 16 with MessagePartInfo

use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.

the class XMLStreamDataWriterTest method testWriteRPCLit2.

@Test
public void testWriteRPCLit2() throws Exception {
    JAXBDataBinding db = getTestWriterFactory(MyComplexStruct.class);
    DataWriter<XMLStreamWriter> dw = db.createWriter(XMLStreamWriter.class);
    assertNotNull(dw);
    MyComplexStruct val = new MyComplexStruct();
    val.setElem1("This is element 1");
    val.setElem2("This is element 2");
    val.setElem3(1);
    QName elName = new QName("http://apache.org/hello_world_rpclit/types", "in");
    MessagePartInfo part = new MessagePartInfo(elName, null);
    part.setElement(true);
    part.setElementQName(elName);
    dw.write(val, part, streamWriter);
    streamWriter.flush();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    XMLStreamReader xr = inFactory.createXMLStreamReader(bais);
    DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
    StaxUtils.toNextElement(reader);
    assertEquals(new QName("http://apache.org/hello_world_rpclit/types", "in"), reader.getName());
    StaxUtils.nextEvent(reader);
    StaxUtils.toNextElement(reader);
    assertEquals(new QName("http://apache.org/hello_world_rpclit/types", "elem1"), reader.getName());
    StaxUtils.nextEvent(reader);
    StaxUtils.toNextText(reader);
    assertEquals("This is element 1", reader.getText());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) QName(javax.xml.namespace.QName) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) MyComplexStruct(org.apache.hello_world_rpclit.types.MyComplexStruct) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Test(org.junit.Test)

Example 17 with MessagePartInfo

use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.

the class XMLStreamDataReaderTest method testReadBare.

@Test
public void testReadBare() throws Exception {
    JAXBDataBinding db = getDataBinding(TradePriceData.class);
    reader = getTestReader("../resources/sayHiDocLitBareReq.xml");
    assertNotNull(reader);
    DataReader<XMLStreamReader> dr = db.createReader(XMLStreamReader.class);
    assertNotNull(dr);
    QName elName = new QName("http://apache.org/hello_world_doc_lit_bare/types", "inout");
    MessagePartInfo part = new MessagePartInfo(elName, null);
    part.setElement(true);
    part.setElementQName(elName);
    part.setTypeClass(TradePriceData.class);
    Object val = dr.read(part, reader);
    assertNotNull(val);
    assertTrue(val instanceof TradePriceData);
    assertEquals("CXF", ((TradePriceData) val).getTickerSymbol());
    assertEquals(Float.valueOf(1.0f), new Float(((TradePriceData) val).getTickerPrice()));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) JAXBDataBinding(org.apache.cxf.jaxb.JAXBDataBinding) TradePriceData(org.apache.hello_world_doc_lit_bare.types.TradePriceData) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Test(org.junit.Test)

Example 18 with MessagePartInfo

use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.

the class HolderOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    MessageContentsList outObjects = MessageContentsList.getContentsList(message);
    Exchange exchange = message.getExchange();
    OperationInfo op = exchange.getBindingOperationInfo() == null ? null : exchange.getBindingOperationInfo().getOperationInfo();
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("op: " + op);
        if (null != op) {
            LOG.fine("op.hasOutput(): " + op.hasOutput());
            if (op.hasOutput()) {
                LOG.fine("op.getOutput().size(): " + op.getOutput().size());
            }
        }
    }
    if (op == null || !op.hasOutput() || op.getOutput().size() == 0) {
        LOG.fine("Returning.");
        return;
    }
    if (!Boolean.TRUE.equals(message.get(Message.REQUESTOR_ROLE))) {
        List<MessagePartInfo> parts = op.getOutput().getMessageParts();
        MessageContentsList inObjects = MessageContentsList.getContentsList(exchange.getInMessage());
        if (inObjects != null) {
            if (inObjects != outObjects) {
                for (int x = 0; x < inObjects.size(); x++) {
                    Object o = inObjects.get(x);
                    if (o instanceof Holder) {
                        outObjects.set(x + 1, o);
                    }
                }
            } else {
                LOG.severe("CANNOT_SET_HOLDER_OBJECTS");
                throw new Fault(new org.apache.cxf.common.i18n.Message("CANNOT_SET_HOLDER_OBJECTS", LOG));
            }
        }
        for (MessagePartInfo part : parts) {
            if (part.getIndex() > 0 && part.getTypeClass() != null) {
                Holder<?> holder = (Holder<?>) outObjects.get(part);
                outObjects.put(part, holder.value);
            }
        }
    } else {
        List<Object> holders = new ArrayList<>(outObjects);
        for (int x = 0; x < outObjects.size(); x++) {
            Object o = outObjects.get(x);
            if (o instanceof Holder) {
                outObjects.set(x, ((Holder<?>) o).value);
            } else {
                holders.set(x, null);
            }
        }
        message.put(HolderInInterceptor.CLIENT_HOLDERS, holders);
    }
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) Holder(javax.xml.ws.Holder) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Exchange(org.apache.cxf.message.Exchange)

Example 19 with MessagePartInfo

use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.

the class SwAOutInterceptor method processAttachments.

protected void processAttachments(SoapMessage message, SoapBodyInfo sbi) {
    Collection<Attachment> atts = setupAttachmentOutput(message);
    List<Object> outObjects = CastUtils.cast(message.getContent(List.class));
    for (MessagePartInfo mpi : sbi.getAttachments()) {
        String partName = mpi.getConcreteName().getLocalPart();
        String ct = (String) mpi.getProperty(Message.CONTENT_TYPE);
        String id = new StringBuilder().append(partName).append('=').append(UUID.randomUUID()).append("@apache.org").toString();
        // this assumes things are in order...
        int idx = mpi.getIndex();
        Object o = outObjects.get(idx);
        if (o == null) {
            continue;
        }
        outObjects.set(idx, null);
        DataHandler dh;
        // This code could probably be refactored out somewhere...
        if (o instanceof Source) {
            dh = new DataHandler(createDataSource((Source) o, ct));
        } else if (o instanceof Image) {
            final Image img = (Image) o;
            final String contentType = ct;
            dh = new DataHandler(o, ct) {

                @Override
                public InputStream getInputStream() throws IOException {
                    LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream();
                    writeTo(bout);
                    return bout.createInputStream();
                }

                @Override
                public void writeTo(OutputStream out) throws IOException {
                    ImageWriter writer = null;
                    Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType(contentType);
                    if (writers.hasNext()) {
                        writer = writers.next();
                    }
                    if (writer != null) {
                        BufferedImage bimg = convertToBufferedImage(img);
                        ImageOutputStream iout = ImageIO.createImageOutputStream(out);
                        writer.setOutput(iout);
                        writer.write(bimg);
                        writer.dispose();
                        iout.flush();
                        out.flush();
                    }
                }
            };
        } else if (o instanceof DataHandler) {
            dh = (DataHandler) o;
            ct = dh.getContentType();
            try {
                if ("text/xml".equals(ct) && dh.getContent() instanceof Source) {
                    dh = new DataHandler(createDataSource((Source) dh.getContent(), ct));
                }
            } catch (IOException e) {
            // ignore, use same dh
            }
        } else if (o instanceof byte[]) {
            if (ct == null) {
                ct = "application/octet-stream";
            }
            dh = new DataHandler(new ByteDataSource((byte[]) o, ct));
        } else if (o instanceof String) {
            if (ct == null) {
                ct = "text/plain; charset=\'UTF-8\'";
            }
            dh = new DataHandler(new ByteDataSource(((String) o).getBytes(StandardCharsets.UTF_8), ct));
        } else {
            throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_NOT_SUPPORTED", LOG, o.getClass()));
        }
        AttachmentImpl att = new AttachmentImpl(id);
        att.setDataHandler(dh);
        att.setHeader("Content-Type", ct);
        att.setHeader("Content-ID", "<" + id + ">");
        atts.add(att);
    }
}
Also used : LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ImageWriter(javax.imageio.ImageWriter) Attachment(org.apache.cxf.message.Attachment) Fault(org.apache.cxf.interceptor.Fault) DataHandler(javax.activation.DataHandler) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) BufferedImage(java.awt.image.BufferedImage) ByteDataSource(org.apache.cxf.attachment.ByteDataSource) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) List(java.util.List) ArrayList(java.util.ArrayList) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IOException(java.io.IOException) AttachmentImpl(org.apache.cxf.attachment.AttachmentImpl)

Example 20 with MessagePartInfo

use of org.apache.cxf.service.model.MessagePartInfo in project cxf by apache.

the class SwAInInterceptor method handleMessage.

public void handleMessage(SoapMessage message) throws Fault {
    BindingOperationInfo bop = message.getExchange().getBindingOperationInfo();
    if (bop == null) {
        return;
    }
    if (bop.isUnwrapped()) {
        bop = bop.getWrappedOperation();
    }
    boolean client = isRequestor(message);
    BindingMessageInfo bmi = client ? bop.getOutput() : bop.getInput();
    if (bmi == null) {
        return;
    }
    SoapBodyInfo sbi = bmi.getExtensor(SoapBodyInfo.class);
    if (sbi == null || sbi.getAttachments() == null || sbi.getAttachments().isEmpty()) {
        return;
    }
    Set<Integer> foundAtts = new HashSet<>();
    MessageContentsList inObjects = MessageContentsList.getContentsList(message);
    for (MessagePartInfo mpi : sbi.getAttachments()) {
        String partName = mpi.getConcreteName().getLocalPart();
        String start = partName + "=";
        boolean found = false;
        if (foundAtts.contains(mpi.getIndex())) {
            continue;
        }
        foundAtts.add(mpi.getIndex());
        for (Attachment a : message.getAttachments()) {
            if (a.getId().startsWith(start)) {
                DataHandler dh = a.getDataHandler();
                String ct = dh.getContentType();
                final Object o;
                Class<?> typeClass = mpi.getTypeClass();
                if (DataHandler.class.isAssignableFrom(typeClass)) {
                    o = dh;
                } else if (String.class.isAssignableFrom(typeClass)) {
                    try {
                        // o = IOUtils.readBytesFromStream(dh.getInputStream());
                        o = dh.getContent();
                    } catch (IOException e) {
                        throw new Fault(e);
                    }
                } else if (byte[].class.isAssignableFrom(typeClass)) {
                    try {
                        o = IOUtils.readBytesFromStream(dh.getInputStream());
                    } catch (IOException e) {
                        throw new Fault(e);
                    }
                } else if (ct.startsWith("image/")) {
                    try {
                        o = ImageIO.read(dh.getInputStream());
                    } catch (IOException e) {
                        throw new Fault(e);
                    }
                } else if (ct.startsWith("text/xml") || ct.startsWith("application/xml")) {
                    try {
                        o = new StreamSource(dh.getInputStream());
                    } catch (IOException e) {
                        throw new Fault(e);
                    }
                } else {
                    o = dh;
                }
                inObjects.put(mpi, o);
                found = true;
                break;
            }
        }
        if (!found) {
            inObjects.put(mpi, null);
        }
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) SoapBodyInfo(org.apache.cxf.binding.soap.model.SoapBodyInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) StreamSource(javax.xml.transform.stream.StreamSource) Attachment(org.apache.cxf.message.Attachment) Fault(org.apache.cxf.interceptor.Fault) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) HashSet(java.util.HashSet)

Aggregations

MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)163 QName (javax.xml.namespace.QName)99 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)60 OperationInfo (org.apache.cxf.service.model.OperationInfo)46 MessageInfo (org.apache.cxf.service.model.MessageInfo)38 Test (org.junit.Test)38 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)29 Fault (org.apache.cxf.interceptor.Fault)21 Service (org.apache.cxf.service.Service)21 BindingMessageInfo (org.apache.cxf.service.model.BindingMessageInfo)21 BindingInfo (org.apache.cxf.service.model.BindingInfo)20 ArrayList (java.util.ArrayList)19 Endpoint (org.apache.cxf.endpoint.Endpoint)18 MessageContentsList (org.apache.cxf.message.MessageContentsList)18 SoapOperationInfo (org.apache.cxf.binding.soap.model.SoapOperationInfo)16 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)15 XMLStreamReader (javax.xml.stream.XMLStreamReader)13 Exchange (org.apache.cxf.message.Exchange)13 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)13 Method (java.lang.reflect.Method)12