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);
}
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");
}
}
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;
}
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);
}
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);
}
Aggregations