Search in sources :

Example 1 with StompFrame

use of org.codehaus.stomp.StompFrame in project narayana by jbosstm.

the class ProtocolConverter method onStompConnect.

// Implemenation methods
// -------------------------------------------------------------------------
protected void onStompConnect(StompFrame command) throws IOException, JMSException {
    if (noneXaSession != null) {
        throw new ProtocolException("Already connected.");
    }
    Map<String, Object> headers = command.getHeaders();
    login = (String) headers.get(Stomp.Headers.Connect.LOGIN);
    passcode = (String) headers.get(Stomp.Headers.Connect.PASSCODE);
    clientId = (String) headers.get(Stomp.Headers.Connect.CLIENT_ID);
    Connection noneXaConnection;
    if (login != null) {
        noneXaConnection = noneXAConnectionFactory.createConnection(login, passcode);
    } else {
        noneXaConnection = noneXAConnectionFactory.createConnection();
    }
    if (clientId != null) {
        noneXaConnection.setClientID(clientId);
    }
    noneXaConnection.start();
    Session session = noneXaConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    if (log.isDebugEnabled()) {
        log.debug("Created session with ack mode: " + session.getAcknowledgeMode());
    }
    this.noneXaSession = new StompSession(initialContext, this, session, noneXaConnection);
    Map<String, Object> responseHeaders = new HashMap<String, Object>();
    responseHeaders.put(Stomp.Headers.Connected.SESSION, clientId);
    String requestId = (String) headers.get(Stomp.Headers.Connect.REQUEST_ID);
    if (requestId == null) {
        // TODO legacy
        requestId = (String) headers.get(Stomp.Headers.RECEIPT_REQUESTED);
    }
    if (requestId != null) {
        // TODO legacy
        responseHeaders.put(Stomp.Headers.Connected.RESPONSE_ID, requestId);
        responseHeaders.put(Stomp.Headers.Response.RECEIPT_ID, requestId);
    }
    StompFrame sc = new StompFrame();
    sc.setAction(Stomp.Responses.CONNECTED);
    sc.setHeaders(responseHeaders);
    sendToStomp(sc);
}
Also used : ProtocolException(org.codehaus.stomp.ProtocolException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StompFrame(org.codehaus.stomp.StompFrame) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) Session(javax.jms.Session)

Example 2 with StompFrame

use of org.codehaus.stomp.StompFrame in project narayana by jbosstm.

the class ProtocolConverter method sendResponse.

protected void sendResponse(StompFrame command) throws IOException {
    final String receiptId = (String) command.getHeaders().get(Stomp.Headers.RECEIPT_REQUESTED);
    // A response may not be needed.
    if (receiptId != null) {
        StompFrame sc = new StompFrame();
        sc.setAction(Stomp.Responses.RECEIPT);
        sc.setHeaders(new HashMap<String, Object>(1));
        sc.getHeaders().put(Stomp.Headers.Response.RECEIPT_ID, receiptId);
        sendToStomp(sc);
    } else {
        log.trace("No receipt required");
    }
}
Also used : StompFrame(org.codehaus.stomp.StompFrame)

Example 3 with StompFrame

use of org.codehaus.stomp.StompFrame in project narayana by jbosstm.

the class StompSession method convertMessage.

protected StompFrame convertMessage(Message message) throws JMSException, UnsupportedEncodingException {
    StompFrame command = new StompFrame();
    command.setAction(Stomp.Responses.MESSAGE);
    Map headers = new HashMap(25);
    command.setHeaders(headers);
    copyStandardHeadersFromMessageToFrame(message, command);
    if (message instanceof TextMessage) {
        TextMessage msg = (TextMessage) message;
        command.setContent(msg.getText().getBytes(StandardCharsets.UTF_8));
    } else if (message instanceof BytesMessage) {
        BytesMessage msg = (BytesMessage) message;
        byte[] data = new byte[(int) msg.getBodyLength()];
        msg.readBytes(data);
        headers.put(Stomp.Headers.CONTENT_LENGTH, "" + data.length);
        command.setContent(data);
    }
    return command;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StompFrame(org.codehaus.stomp.StompFrame) BytesMessage(javax.jms.BytesMessage) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TextMessage(javax.jms.TextMessage)

Example 4 with StompFrame

use of org.codehaus.stomp.StompFrame in project narayana by jbosstm.

the class StompSession method sendToStomp.

public void sendToStomp(Message message, String subscriptionID) throws JMSException, IOException {
    log.debug("Sending to stomp");
    StompFrame frame = convertMessage(message);
    frame.getHeaders().put(Stomp.Headers.Message.SUBSCRIPTION, subscriptionID);
    protocolConverter.sendToStomp(frame);
}
Also used : StompFrame(org.codehaus.stomp.StompFrame)

Example 5 with StompFrame

use of org.codehaus.stomp.StompFrame in project narayana by jbosstm.

the class ProtocolConverter method onStompReceive.

protected void onStompReceive(StompFrame command) throws IllegalStateException, SystemException, JMSException, NamingException, IOException {
    checkConnected();
    Map<String, Object> headers = command.getHeaders();
    String destinationName = (String) headers.remove(Stomp.Headers.Send.DESTINATION);
    String xid = (String) headers.get("messagexid");
    Message msg = null;
    StompSession session = null;
    if (xid != null) {
        if (xid.startsWith("IOR")) {
            log.error("OTS Transaction Not Supported");
        } else if (xid.startsWith("http")) {
            log.trace("RTS Transaction was propagated: " + xid);
            String enlistmentUrl = xid;
            final InboundBridge inboundBridge = InboundBridgeManager.getInstance().createInboundBridge(enlistmentUrl);
            log.trace("Start inboundBridge");
            inboundBridge.start();
            session = getXASession(inboundBridge.getXid());
            msg = session.receiveFromJms(destinationName, headers);
            inboundBridge.stop();
            log.trace("Stop inboundBridge");
        } else {
            log.error(xid + " is not OTS or RTS transaction");
        }
    } else {
        log.trace("WAS NULL XID");
        session = noneXaSession;
        msg = session.receiveFromJms(destinationName, headers);
        log.trace("Received from JMS");
    }
    StompFrame sf;
    if (msg == null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter stream = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
        stream.print("No messages available");
        stream.close();
        Map<String, Object> eheaders = new HashMap<String, Object>();
        eheaders.put(Stomp.Headers.Error.MESSAGE, "timeout");
        sf = new StompFrame(Stomp.Responses.ERROR, eheaders, baos.toByteArray());
    } else {
        // Don't use sendResponse since it uses Stomp.Responses.RECEIPT as the action
        // which only allows zero length message bodies, Stomp.Responses.MESSAGE is correct:
        sf = session.convertMessage(msg);
    }
    if (headers.containsKey(Stomp.Headers.RECEIPT_REQUESTED))
        sf.getHeaders().put(Stomp.Headers.Response.RECEIPT_ID, headers.get(Stomp.Headers.RECEIPT_REQUESTED));
    sendToStomp(sf);
}
Also used : Message(javax.jms.Message) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StompFrame(org.codehaus.stomp.StompFrame) InboundBridge(org.jboss.narayana.rest.bridge.inbound.InboundBridge) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PrintWriter(java.io.PrintWriter)

Aggregations

StompFrame (org.codehaus.stomp.StompFrame)6 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ProtocolException (org.codehaus.stomp.ProtocolException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URISyntaxException (java.net.URISyntaxException)1 Map (java.util.Map)1 BytesMessage (javax.jms.BytesMessage)1 Connection (javax.jms.Connection)1 JMSException (javax.jms.JMSException)1 Message (javax.jms.Message)1 Session (javax.jms.Session)1 TextMessage (javax.jms.TextMessage)1 XAConnection (javax.jms.XAConnection)1 InboundBridge (org.jboss.narayana.rest.bridge.inbound.InboundBridge)1