Search in sources :

Example 51 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class MediatorInInterceptor method selectEndpoint.

@Override
protected Endpoint selectEndpoint(Message message, Set<Endpoint> eps) {
    InputStream is = message.getContent(InputStream.class);
    if (is == null) {
        return null;
    }
    // cache the input stream
    CachedOutputStream bos = new CachedOutputStream(4096);
    try {
        IOUtils.copy(is, bos);
        is.close();
        message.setContent(InputStream.class, bos.getInputStream());
        String encoding = (String) message.get(Message.ENCODING);
        XMLStreamReader xsr;
        XMLInputFactory factory = StaxInInterceptor.getXMLInputFactory(message);
        if (factory == null) {
            xsr = StaxUtils.createXMLStreamReader(bos.getInputStream(), encoding);
        } else {
            synchronized (factory) {
                xsr = factory.createXMLStreamReader(bos.getInputStream(), encoding);
            }
        }
        // move to the soap body
        while (true) {
            xsr.nextTag();
            if ("Body".equals(xsr.getName().getLocalPart())) {
                break;
            }
        }
        xsr.nextTag();
        if (!xsr.isStartElement()) {
            return null;
        }
        String methodName = xsr.getName().getLocalPart();
        // to the new version of service on endpoint "local://localhost:9027/SoapContext/version2/SoapPort"
        for (Endpoint ep : eps) {
            if (methodName.indexOf("sayHi") != -1) {
                if ("2".equals(ep.get("version"))) {
                    return ep;
                }
            } else if ("1".equals(ep.get("version"))) {
                return ep;
            }
        }
        bos.close();
    } catch (Exception e) {
        throw new Fault(e);
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) Endpoint(org.apache.cxf.endpoint.Endpoint) InputStream(java.io.InputStream) Fault(org.apache.cxf.interceptor.Fault) XMLInputFactory(javax.xml.stream.XMLInputFactory) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 52 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class LocalConduit method dispatchDirect.

private void dispatchDirect(Message message) throws IOException {
    if (destination.getMessageObserver() == null) {
        throw new IllegalStateException("Local destination does not have a MessageObserver on address " + destination.getAddress().getAddress().getValue());
    }
    MessageImpl copy = new MessageImpl();
    copy.put(IN_CONDUIT, this);
    copy.setDestination(destination);
    transportFactory.copy(message, copy);
    MessageImpl.copyContent(message, copy);
    OutputStream out = message.getContent(OutputStream.class);
    out.flush();
    out.close();
    CachedOutputStream stream = message.get(CachedOutputStream.class);
    copy.setContent(InputStream.class, stream.getInputStream());
    copy.removeContent(CachedOutputStream.class);
    stream.releaseTempFileHold();
    // Create a new incoming exchange and store the original exchange for the response
    ExchangeImpl ex = new ExchangeImpl();
    ex.setInMessage(copy);
    ex.put(IN_EXCHANGE, message.getExchange());
    ex.put(LocalConduit.DIRECT_DISPATCH, true);
    ex.setDestination(destination);
    destination.getMessageObserver().onMessage(copy);
}
Also used : OutputStream(java.io.OutputStream) PipedOutputStream(java.io.PipedOutputStream) AbstractWrappedOutputStream(org.apache.cxf.io.AbstractWrappedOutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 53 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class Destination method processingComplete.

void processingComplete(Message message) {
    SequenceType sequenceType = RMContextUtils.retrieveRMProperties(message, false).getSequence();
    if (null == sequenceType) {
        return;
    }
    DestinationSequence seq = getSequence(sequenceType.getIdentifier());
    if (null != seq) {
        long mn = sequenceType.getMessageNumber().longValue();
        seq.processingComplete(mn);
        seq.purgeAcknowledged(mn);
        // remove acknowledged undelivered message
        seq.removeDeliveringMessageNumber(mn);
        if (seq.isTerminated() && seq.allAcknowledgedMessagesDelivered()) {
            removeSequence(seq);
        }
    }
    CachedOutputStream saved = (CachedOutputStream) message.remove(RMMessageConstants.SAVED_CONTENT);
    if (saved != null) {
        saved.releaseTempFileHold();
        try {
            saved.close();
        } catch (IOException e) {
        // ignore
        }
    }
}
Also used : SequenceType(org.apache.cxf.ws.rm.v200702.SequenceType) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 54 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class PersistenceUtils method decodeRMContent.

public static void decodeRMContent(RMMessage rmmsg, Message msg) throws IOException {
    String contentType = rmmsg.getContentType();
    final CachedOutputStream cos = rmmsg.getContent();
    if ((null != contentType) && contentType.startsWith("multipart/related")) {
        final InputStream is = cos.getInputStream();
        msg.put(Message.CONTENT_TYPE, contentType);
        msg.setContent(InputStream.class, is);
        AttachmentDeserializer ad = new AttachmentDeserializer(msg);
        ad.initializeAttachments();
        // create new cos with soap envelope only
        CachedOutputStream cosSoap = new CachedOutputStream();
        IOUtils.copy(msg.getContent(InputStream.class), cosSoap);
        cosSoap.flush();
        msg.put(RMMessageConstants.SAVED_CONTENT, cosSoap);
        // REVISIT -- At the moment references must be hold for retransmission
        // and the final cleanup of the CachedOutputStream.
        msg.put(RMMessageConstants.ATTACHMENTS_CLOSEABLE, new Closeable() {

            @Override
            public void close() throws IOException {
                try {
                    is.close();
                } catch (IOException e) {
                // Ignore
                }
                try {
                    cos.close();
                } catch (IOException e) {
                // Ignore
                }
            }
        });
    } else {
        msg.put(RMMessageConstants.SAVED_CONTENT, cos);
    }
}
Also used : AttachmentDeserializer(org.apache.cxf.attachment.AttachmentDeserializer) InputStream(java.io.InputStream) Closeable(java.io.Closeable) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 55 with CachedOutputStream

use of org.apache.cxf.io.CachedOutputStream in project cxf by apache.

the class RMTxStore method getMessages.

public Collection<RMMessage> getMessages(Identifier sid, boolean outbound) {
    Connection con = verifyConnection();
    PreparedStatement stmt = null;
    SQLException conex = null;
    Collection<RMMessage> msgs = new ArrayList<>();
    ResultSet res = null;
    try {
        stmt = getStatement(con, outbound ? SELECT_OUTBOUND_MESSAGES_STMT_STR : SELECT_INBOUND_MESSAGES_STMT_STR);
        stmt.setString(1, sid.getValue());
        res = stmt.executeQuery();
        while (res.next()) {
            long mn = res.getLong(1);
            String to = res.getString(2);
            long ct = res.getLong(3);
            Blob blob = res.getBlob(4);
            String contentType = res.getString(5);
            RMMessage msg = new RMMessage();
            msg.setMessageNumber(mn);
            msg.setTo(to);
            msg.setCreatedTime(ct);
            CachedOutputStream cos = new CachedOutputStream();
            IOUtils.copyAndCloseInput(blob.getBinaryStream(), cos);
            cos.flush();
            msg.setContent(cos);
            msg.setContentType(contentType);
            msgs.add(msg);
        }
    } catch (SQLException ex) {
        conex = ex;
        LOG.log(Level.WARNING, new Message(outbound ? "SELECT_OUTBOUND_MSGS_FAILED_MSG" : "SELECT_INBOUND_MSGS_FAILED_MSG", LOG).toString(), ex);
    } catch (IOException e) {
        abort(con);
        throw new RMStoreException(e);
    } finally {
        releaseResources(stmt, res);
        updateConnectionState(con, conex);
    }
    return msgs;
}
Also used : Blob(java.sql.Blob) Message(org.apache.cxf.common.i18n.Message) RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) SQLException(java.sql.SQLException) RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) RMStoreException(org.apache.cxf.ws.rm.persistence.RMStoreException) ResultSet(java.sql.ResultSet)

Aggregations

CachedOutputStream (org.apache.cxf.io.CachedOutputStream)105 InputStream (java.io.InputStream)38 IOException (java.io.IOException)35 Test (org.junit.Test)24 Message (org.apache.cxf.message.Message)22 OutputStream (java.io.OutputStream)18 Fault (org.apache.cxf.interceptor.Fault)18 MessageImpl (org.apache.cxf.message.MessageImpl)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 XMLStreamException (javax.xml.stream.XMLStreamException)10 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)10 PrintWriter (java.io.PrintWriter)8 XMLStreamReader (javax.xml.stream.XMLStreamReader)6 StreamSource (javax.xml.transform.stream.StreamSource)6 Endpoint (org.apache.cxf.endpoint.Endpoint)6 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)6 RMMessage (org.apache.cxf.ws.rm.persistence.RMMessage)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Source (javax.xml.transform.Source)5 ArrayList (java.util.ArrayList)4