public static io.vertx.ext.stomp.StompServerHandler onAck(io.vertx.ext.stomp.StompServerHandler j_receiver, io.vertx.ext.stomp.StompServerConnection connection, java.util.Map<String, Object> subscribe, java.util.List<java.util.Map<String, Object>> messages) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.onAck(connection, subscribe != null ? new io.vertx.ext.stomp.Frame(io.vertx.core.impl.ConversionHelper.toJsonObject(subscribe)) : null, messages != null ? messages.stream().map(elt -> elt != null ? new io.vertx.ext.stomp.Frame(io.vertx.core.impl.ConversionHelper.toJsonObject(elt)) : null).collect(java.util.stream.Collectors.toList()) : null)); return j_receiver; } public static io.vertx.ext.stomp.StompServerHandler onNack(io.vertx.ext.stomp.StompServerHandler j_receiver, io.vertx.ext.stomp.StompServerConnection connection, java.util.Map<String, Object> subscribe, java.util.List<java.util.Map<String, Object>> messages) {
public static void toJson(Frame obj, java.util.Map<String, Object> json) { if (obj.getAck() != null) { json.put("ack", obj.getAck()); if (obj.getBody() != null) { json.put("body", java.util.Base64.getEncoder().encodeToString(obj.getBody().getBytes())); if (obj.getBodyAsString() != null) { json.put("bodyAsString", obj.getBodyAsString()); if (obj.getCommand() != null) { json.put("command", obj.getCommand().name()); if (obj.getDestination() != null) { json.put("destination", obj.getDestination()); if (obj.getHeaders() != null) { JsonObject map = new JsonObject(); obj.getHeaders().forEach((key, value) -> map.put(key, value)); json.put("headers", map); if (obj.getId() != null) { json.put("id", obj.getId()); if (obj.getReceipt() != null) { json.put("receipt", obj.getReceipt()); if (obj.getTransaction() != null) { json.put("transaction", obj.getTransaction());
/** * Creates a buffer for the current frame. This buffer may contain an empty line if the {@code trailingLine} is set * to {@code true} * * @param trailingLine whether or not a trailing line should be added to the buffer * @return a {@link Buffer} containing the STOMP frame. It follows strictly the STOMP specification (including * header encoding). */ public Buffer toBuffer(boolean trailingLine) { Buffer buffer = toBuffer(); if (trailingLine) { buffer.appendString("\n"); } return buffer; }
protected Subscription(StompServerConnection connection, Frame frame) { this.connection = connection; this.ackMode = frame.getAck() != null ? frame.getAck() : "auto"; this.id = frame.getId(); this.destination = frame.getDestination(); } }
/** * Gets the body of the frames as a String encoded in the frame encoding. * * @return the body, {@code null} if none */ public String getBodyAsString() { return getBodyAsString(encoding()); }
private Frame transform(Message<Object> msg, Subscription subscription) { String messageId = UUID.randomUUID().toString(); Frame frame = new Frame(); frame.setCommand(Frame.Command.MESSAGE); final Headers headers = Headers.create(frame.getHeaders()) frame.setHeaders(headers); Object body = msg.body(); if (body != null) { if (body instanceof String) { frame.setBody(Buffer.buffer((String) body)); } else if (body instanceof Buffer) { frame.setBody((Buffer) body); } else if (body instanceof JsonObject) { frame.setBody(Buffer.buffer(((JsonObject) body).encode())); } else { throw new IllegalStateException("Illegal body - unsupported body type: " + body.getClass().getName()); if (body != null && frame.getHeader(Frame.CONTENT_LENGTH) == null) { frame.addHeader(Frame.CONTENT_LENGTH, Integer.toString(frame.getBody().length()));
@Override public StompClientConnection beginTX(String id, Map<String, String> headers, Handler<Frame> receiptHandler) { Objects.requireNonNull(id); Objects.requireNonNull(headers); return send(new Frame().setCommand(Frame.Command.BEGIN).setTransaction(id), receiptHandler); }
private Buffer getConnectFrame(String host) { Headers headers = Headers.create(); String accepted = getAcceptedVersions(); if (accepted != null) { headers.put(Frame.ACCEPT_VERSION, accepted); } if (!options.isBypassHostHeader()) { headers.put(Frame.HOST, host); } if (options.getVirtualHost() != null) { headers.put(Frame.HOST, options.getVirtualHost()); } if (options.getLogin() != null) { headers.put(Frame.LOGIN, options.getLogin()); } if (options.getPasscode() != null) { headers.put(Frame.PASSCODE, options.getPasscode()); } headers.put(Frame.HEARTBEAT, Frame.Heartbeat.create(options.getHeartbeat()).toString()); Frame.Command cmd = options.isUseStompFrame() ? Frame.Command.STOMP : Frame.Command.CONNECT; final Frame frame = new Frame(cmd, headers, null); return frame.toBuffer(); }
public static Frame transform(Frame frame, Subscription subscription, String messageId) { final Headers headers = Headers.create(frame.getHeaders()) // Destination already set in the input headers. .add(Frame.SUBSCRIPTION, subscription.id) .add(Frame.MESSAGE_ID, messageId); if (!subscription.ackMode.equals("auto")) { // We reuse the message Id as ack Id headers.add(Frame.ACK, messageId); } return new Frame(Frame.Command.MESSAGE, headers, frame.getBody()); }
private void handleSubscribe(ServerFrame serverFrame) { // if ("MySubId2".equals(serverFrame.frame().getId())) { Map<String, String> receiptHeaders = new HashMap<>(); receiptHeaders.put(Frame.RECEIPT_ID, serverFrame.frame().getReceipt()); serverFrame.connection().write(new Frame(Frame.Command.RECEIPT, receiptHeaders, null)); System.out.println("serverFrame.frame().getId()" + serverFrame.frame().getId()); subscriptions.put(serverFrame.frame().getId(), new ServerSubscription(serverFrame.connection())); recordFrame(serverFrame); // } }
private void frameHandler(Frame frame, Subscription sub) { String bodyAsString = frame.getBodyAsString(); if (logger.isTraceEnabled()) logger.trace("Got body=" + bodyAsString); StompEvent stompEvent = JSonMapper.fromJson(bodyAsString, StompEvent.class); SerializedEvent serializedEvent = makeSerializedEvent(stompEvent); handleEvent(sub, serializedEvent, frame.getAck()); }
public static io.vertx.ext.stomp.StompClientConnection send(io.vertx.ext.stomp.StompClientConnection j_receiver, java.util.Map<String, Object> frame, io.vertx.core.Handler<java.util.Map<String, Object>> receiptHandler) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.send(frame != null ? new io.vertx.ext.stomp.Frame(io.vertx.core.impl.ConversionHelper.toJsonObject(frame)) : null, receiptHandler != null ? event -> receiptHandler.handle(event != null ? io.vertx.core.impl.ConversionHelper.fromJsonObject(event.toJson()) : null) : null)); return j_receiver; } public static io.vertx.ext.stomp.StompClientConnection send(io.vertx.ext.stomp.StompClientConnection j_receiver, java.lang.String destination, java.util.Map<String, java.lang.String> headers, io.vertx.core.buffer.Buffer body, io.vertx.core.Handler<java.util.Map<String, Object>> receiptHandler) {
@Override public StompClientConnection disconnect(Handler<Frame> receiptHandler) { return disconnect(new Frame().setCommand(Frame.Command.DISCONNECT), receiptHandler); }
Frame frame = serverFrame.frame(); StompServerConnection connection = serverFrame.connection(); String id = frame.getId(); if (id == null) { connection.write(Frames.createErrorFrame( "Id header missing", Headers.create(frame.getHeaders()), "Invalid ACK frame - the " + "'id' must be set")); connection.close(); String txId = frame.getHeader(Frame.TRANSACTION); if (txId != null) { Transaction transaction = Transactions.instance().getTransaction(connection, txId);
@Override public synchronized StompClientConnection send(Frame frame, Handler<Frame> receiptHandler) { if (receiptHandler != null) { String receiptId = UUID.randomUUID().toString(); frame.addHeader(Frame.RECEIPT, receiptId); pendingReceipts.put(receiptId, f -> receiptHandler.handle(frame)); } if (writingHandler != null) { writingHandler.handle(frame); } socket.write(frame.toBuffer(client.options().isTrailingLine())); return this; }
@Override public void handle(Frame frame) { synchronized (this) { if (receivedFrameHandler != null) { receivedFrameHandler.handle(frame); } } switch (frame.getCommand()) { case CONNECTED: handleConnected(frame); break; case RECEIPT: handleReceipt(frame); break; case MESSAGE: String id = frame.getHeader(Frame.SUBSCRIPTION); subscriptions.stream() .filter(s -> s.id.equals(id)).forEach(s -> s.handler.handle(frame)); break; case ERROR: if (errorHandler != null) { errorHandler.handle(frame); } break; case PING: // Do nothing. break; } }
public void assertAcked(int count) { assertThat(frames.timeout(30, TimeUnit.SECONDS) .filter(frame -> frame.frame().getCommand().equals(Frame.Command.ACK)) .take(5, TimeUnit.SECONDS).toList().toBlocking().first().size(), is(greaterThanOrEqualTo(count))); }
/** * Handles a un-subscription request to the current {@link Destination}. * * @param connection the connection * @param frame the {@code UNSUBSCRIBE} frame * @return {@code true} if the un-subscription has been handled, {@code false} otherwise. */ @Override public synchronized boolean unsubscribe(StompServerConnection connection, Frame frame) { for (Subscription subscription : new ArrayList<>(subscriptions)) { if (subscription.connection.equals(connection) && subscription.id.equals(frame.getId())) { boolean r = subscriptions.remove(subscription); Optional<Subscription> any = subscriptions.stream().filter(s -> s.destination.equals(subscription.destination)).findAny(); // We unregister the event bus consumer if there are no subscription on this address anymore. if (!any.isPresent()) { MessageConsumer<?> consumer = registry.remove(subscription.destination); if (consumer != null) { consumer.unregister(); } } return r; } } return false; }