Search in sources :

Example 11 with RMMessage

use of org.apache.cxf.ws.rm.persistence.RMMessage 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)

Example 12 with RMMessage

use of org.apache.cxf.ws.rm.persistence.RMMessage in project cxf by apache.

the class RMManager method recoverSourceSequence.

private void recoverSourceSequence(Endpoint endpoint, Conduit conduit, Source s, SourceSequence ss) {
    Collection<RMMessage> ms = store.getMessages(ss.getIdentifier(), true);
    if (null == ms || ms.isEmpty()) {
        store.removeSourceSequence(ss.getIdentifier());
        return;
    }
    LOG.log(Level.FINE, "Number of messages in sequence: {0}", ms.size());
    // only recover the sequence if there are pending messages
    s.addSequence(ss, false);
    // choosing an arbitrary valid source sequence as the current source sequence
    if (s.getAssociatedSequence(null) == null && !ss.isExpired() && !ss.isLastMessage()) {
        s.setCurrent(ss);
    }
    // make sure this is associated with the offering id
    s.setCurrent(ss.getOfferingSequenceIdentifier(), ss);
    for (RMMessage m : ms) {
        Message message = new MessageImpl();
        Exchange exchange = new ExchangeImpl();
        message.setExchange(exchange);
        exchange.setOutMessage(message);
        if (null != conduit) {
            exchange.setConduit(conduit);
            message.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
        }
        exchange.put(Endpoint.class, endpoint);
        exchange.put(Service.class, endpoint.getService());
        exchange.put(Binding.class, endpoint.getBinding());
        exchange.put(Bus.class, bus);
        SequenceType st = new SequenceType();
        st.setIdentifier(ss.getIdentifier());
        st.setMessageNumber(m.getMessageNumber());
        RMProperties rmps = new RMProperties();
        rmps.setSequence(st);
        rmps.setCreatedTime(m.getCreatedTime());
        rmps.exposeAs(ss.getProtocol().getWSRMNamespace());
        if (ss.isLastMessage() && ss.getCurrentMessageNr() == m.getMessageNumber()) {
            CloseSequenceType close = new CloseSequenceType();
            close.setIdentifier(ss.getIdentifier());
            rmps.setCloseSequence(close);
        }
        RMContextUtils.storeRMProperties(message, rmps, true);
        if (null == conduit) {
            String to = m.getTo();
            AddressingProperties maps = new AddressingProperties();
            maps.setTo(RMUtils.createReference(to));
            RMContextUtils.storeMAPs(maps, message, true, false);
        }
        try {
            // RMMessage is stored in a serialized way, therefore
            // RMMessage content must be splitted into soap root message
            // and attachments
            PersistenceUtils.decodeRMContent(m, message);
            RMContextUtils.setProtocolVariation(message, ss.getProtocol());
            retransmissionQueue.addUnacknowledged(message);
        } catch (IOException e) {
            LOG.log(Level.SEVERE, "Error reading persisted message data", e);
        }
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) Message(org.apache.cxf.message.Message) RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) CloseSequenceType(org.apache.cxf.ws.rm.v200702.CloseSequenceType) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) CloseSequenceType(org.apache.cxf.ws.rm.v200702.CloseSequenceType) SequenceType(org.apache.cxf.ws.rm.v200702.SequenceType) IOException(java.io.IOException) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 13 with RMMessage

use of org.apache.cxf.ws.rm.persistence.RMMessage in project cxf by apache.

the class RMTxStoreTestBase method testCreateDeleteMessages.

@Test
public void testCreateDeleteMessages() throws IOException, SQLException {
    RMMessage msg1 = control.createMock(RMMessage.class);
    RMMessage msg2 = control.createMock(RMMessage.class);
    Identifier sid1 = new Identifier();
    sid1.setValue("sequence1");
    EasyMock.expect(msg1.getMessageNumber()).andReturn(ONE).anyTimes();
    EasyMock.expect(msg2.getMessageNumber()).andReturn(ONE).anyTimes();
    byte[] bytes = new byte[89];
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    CachedOutputStream cos = new CachedOutputStream();
    IOUtils.copy(bais, cos);
    cos.flush();
    bais.close();
    EasyMock.expect(msg1.getContent()).andReturn(cos).anyTimes();
    EasyMock.expect(msg2.getContent()).andReturn(cos).anyTimes();
    EasyMock.expect(msg1.getContentType()).andReturn("text/xml").times(1);
    control.replay();
    Connection con = getConnection();
    try {
        store.beginTransaction();
        store.storeMessage(con, sid1, msg1, true);
        store.storeMessage(con, sid1, msg2, false);
        store.commit(con);
    } finally {
        releaseConnection(con);
    }
    control.verify();
    control.reset();
    EasyMock.expect(msg1.getMessageNumber()).andReturn(ONE);
    EasyMock.expect(msg1.getContent()).andReturn(cos);
    control.replay();
    con = getConnection();
    try {
        store.beginTransaction();
        store.storeMessage(con, sid1, msg1, true);
    } catch (SQLException ex) {
        assertEquals("23505", ex.getSQLState());
        store.abort(con);
    } finally {
        releaseConnection(con);
    }
    control.verify();
    control.reset();
    EasyMock.expect(msg1.getMessageNumber()).andReturn(TEN).anyTimes();
    EasyMock.expect(msg2.getMessageNumber()).andReturn(TEN).anyTimes();
    EasyMock.expect(msg1.getContent()).andReturn(cos).anyTimes();
    EasyMock.expect(msg2.getContent()).andReturn(cos).anyTimes();
    control.replay();
    con = getConnection();
    try {
        store.beginTransaction();
        store.storeMessage(con, sid1, msg1, true);
        store.storeMessage(con, sid1, msg2, false);
        store.commit(con);
    } finally {
        releaseConnection(con);
    }
    control.verify();
    Collection<Long> messageNrs = Arrays.asList(ZERO, TEN, ONE, TEN);
    store.removeMessages(sid1, messageNrs, true);
    store.removeMessages(sid1, messageNrs, false);
    Identifier sid2 = new Identifier();
    sid1.setValue("sequence2");
    store.removeMessages(sid2, messageNrs, true);
}
Also used : Identifier(org.apache.cxf.ws.rm.v200702.Identifier) RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) Test(org.junit.Test)

Example 14 with RMMessage

use of org.apache.cxf.ws.rm.persistence.RMMessage in project cxf by apache.

the class RMTxStoreTestBase method setupInboundMessage.

private void setupInboundMessage(DestinationSequence seq, long mn, String to) throws IOException, SQLException {
    RMMessage msg = createRMMessage(ONE, to);
    control.replay();
    store.persistIncoming(seq, msg);
    control.reset();
}
Also used : RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage)

Example 15 with RMMessage

use of org.apache.cxf.ws.rm.persistence.RMMessage in project cxf by apache.

the class RMTxStoreTestBase method checkRecoveredMessages.

private void checkRecoveredMessages(Collection<RMMessage> msgs) {
    for (RMMessage msg : msgs) {
        long mn = msg.getMessageNumber();
        assertTrue(mn == 1 || mn == 10);
        if (mn == 10) {
            assertEquals(NON_ANON_ACKS_TO, msg.getTo());
        } else {
            assertNull(msg.getTo());
        }
        assertEquals(TIME, msg.getCreatedTime());
        try {
            InputStream actual = msg.getContent().getInputStream();
            assertEquals(new String("Message " + mn), IOUtils.readStringFromStream(actual));
        } catch (IOException e) {
            fail("failed to get the input stream");
        }
    }
}
Also used : RMMessage(org.apache.cxf.ws.rm.persistence.RMMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

RMMessage (org.apache.cxf.ws.rm.persistence.RMMessage)18 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)6 Identifier (org.apache.cxf.ws.rm.v200702.Identifier)6 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)4 Message (org.apache.cxf.message.Message)4 RMStore (org.apache.cxf.ws.rm.persistence.RMStore)4 Connection (java.sql.Connection)3 ArrayList (java.util.ArrayList)3 SequenceType (org.apache.cxf.ws.rm.v200702.SequenceType)3 Method (java.lang.reflect.Method)2 SQLException (java.sql.SQLException)2 Binding (org.apache.cxf.binding.Binding)2 SoapBinding (org.apache.cxf.binding.soap.SoapBinding)2 Endpoint (org.apache.cxf.endpoint.Endpoint)2 Exchange (org.apache.cxf.message.Exchange)2 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)2 MessageImpl (org.apache.cxf.message.MessageImpl)2