Format code according to Java conventions

This commit is contained in:
Andrejs Mivreņiks 2016-10-08 17:15:00 +03:00
parent 1a61590480
commit 7106667793
6 changed files with 294 additions and 294 deletions

View File

@ -43,208 +43,208 @@ import com.neovisionaries.ws.client.WebSocketState;
* *
*/ */
public class StompClient { public class StompClient {
private static final String STOMP_VERSION = "1.2"; private static final String STOMP_VERSION = "1.2";
private final Map<Integer, StompSubscription> subscriptions = new HashMap<>(); private final Map<Integer, StompSubscription> subscriptions = new HashMap<>();
private WebSocket webSocket; private WebSocket webSocket;
private boolean stompConnected; private boolean stompConnected;
public StompClient(String url) throws IOException { public StompClient(String url) throws IOException {
this.webSocket = new WebSocketFactory().createSocket(url); this.webSocket = new WebSocketFactory().createSocket(url);
this.webSocket.addListener(webSocketListener); this.webSocket.addListener(webSocketListener);
} }
public void connect() throws WebSocketException, IOException { public void connect() throws WebSocketException, IOException {
if (webSocket.getState() != WebSocketState.CREATED) { if (webSocket.getState() != WebSocketState.CREATED) {
webSocket = webSocket.recreate(); webSocket = webSocket.recreate();
} }
webSocket.connect(); webSocket.connect();
connectStomp(); connectStomp();
} }
protected void connectStomp() { protected void connectStomp() {
if (stompConnected) if (stompConnected)
return; return;
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
headers.put(StompHeader.ACCEPT_VERSION.toString(), STOMP_VERSION); headers.put(StompHeader.ACCEPT_VERSION.toString(), STOMP_VERSION);
headers.put(StompHeader.HOST.toString(), webSocket.getURI().getHost()); headers.put(StompHeader.HOST.toString(), webSocket.getURI().getHost());
webSocket.sendText(new StompFrame(StompCommand.CONNECT, headers).toString()); webSocket.sendText(new StompFrame(StompCommand.CONNECT, headers).toString());
} }
public void disconnect() { public void disconnect() {
disconnectStomp(); disconnectStomp();
webSocket.disconnect(); webSocket.disconnect();
} }
protected void disconnectStomp() { protected void disconnectStomp() {
if (!stompConnected) if (!stompConnected)
return; return;
webSocket.sendText(new StompFrame(StompCommand.DISCONNECT).toString()); webSocket.sendText(new StompFrame(StompCommand.DISCONNECT).toString());
} }
public boolean isConnected() { public boolean isConnected() {
return stompConnected; return stompConnected;
} }
public void send(String destination, String message) { public void send(String destination, String message) {
send(destination, message, null); send(destination, message, null);
} }
public void send(String destination, String message, Map<String, String> headers) { public void send(String destination, String message, Map<String, String> headers) {
if (headers == null) if (headers == null)
headers = new HashMap<>(); headers = new HashMap<>();
headers.put(StompHeader.DESTINATION.toString(), destination); headers.put(StompHeader.DESTINATION.toString(), destination);
webSocket.sendText(new StompFrame(StompCommand.SEND, headers, message).toString()); webSocket.sendText(new StompFrame(StompCommand.SEND, headers, message).toString());
} }
public StompSubscription subscribe(String destination, StompMessageListener listener) { public StompSubscription subscribe(String destination, StompMessageListener listener) {
StompSubscription subscription = new StompSubscription(UUID.randomUUID().hashCode(), destination, listener); StompSubscription subscription = new StompSubscription(UUID.randomUUID().hashCode(), destination, listener);
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
headers.put(StompHeader.ID.toString(), String.valueOf(subscription.getId())); headers.put(StompHeader.ID.toString(), String.valueOf(subscription.getId()));
headers.put(StompHeader.DESTINATION.toString(), subscription.getDestination()); headers.put(StompHeader.DESTINATION.toString(), subscription.getDestination());
webSocket.sendText(new StompFrame(StompCommand.SUBSCRIBE, headers).toString()); webSocket.sendText(new StompFrame(StompCommand.SUBSCRIBE, headers).toString());
subscriptions.put(subscription.getId(), subscription); subscriptions.put(subscription.getId(), subscription);
return subscription; return subscription;
} }
public void unsubscribe(StompSubscription subscription) { public void unsubscribe(StompSubscription subscription) {
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
headers.put(StompHeader.ID.toString(), String.valueOf(subscription.getId())); headers.put(StompHeader.ID.toString(), String.valueOf(subscription.getId()));
webSocket.sendText(new StompFrame(StompCommand.UNSUBSCRIBE, headers).toString()); webSocket.sendText(new StompFrame(StompCommand.UNSUBSCRIBE, headers).toString());
subscriptions.remove(subscription.getId()); subscriptions.remove(subscription.getId());
} }
public void unsubscribeAll(String destination) { public void unsubscribeAll(String destination) {
for (StompSubscription s : subscriptions.values()) for (StompSubscription s : subscriptions.values())
if (s.getDestination().equals(destination)) if (s.getDestination().equals(destination))
unsubscribe(s); unsubscribe(s);
} }
private final WebSocketListener webSocketListener = new WebSocketListener() { private final WebSocketListener webSocketListener = new WebSocketListener() {
@Override @Override
public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception { public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception {
} }
@Override @Override
public void onBinaryFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onBinaryFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
} }
@Override @Override
public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception { public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception {
} }
@Override @Override
public void onCloseFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onCloseFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
} }
@Override @Override
public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception { public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
} }
@Override @Override
public void onConnectError(WebSocket websocket, WebSocketException cause) throws Exception { public void onConnectError(WebSocket websocket, WebSocketException cause) throws Exception {
} }
@Override @Override
public void onContinuationFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onContinuationFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
} }
@Override @Override
public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame,
WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception { WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
stompConnected = false; stompConnected = false;
} }
@Override @Override
public void onError(WebSocket websocket, WebSocketException cause) throws Exception { public void onError(WebSocket websocket, WebSocketException cause) throws Exception {
} }
@Override @Override
public void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
} }
@Override @Override
public void onFrameError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) public void onFrameError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame)
throws Exception { throws Exception {
} }
@Override @Override
public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onFrameSent(WebSocket websocket, WebSocketFrame frame) throws Exception {
} }
@Override @Override
public void onFrameUnsent(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onFrameUnsent(WebSocket websocket, WebSocketFrame frame) throws Exception {
} }
@Override @Override
public void onMessageDecompressionError(WebSocket websocket, WebSocketException cause, byte[] compressed) public void onMessageDecompressionError(WebSocket websocket, WebSocketException cause, byte[] compressed)
throws Exception { throws Exception {
} }
@Override @Override
public void onMessageError(WebSocket websocket, WebSocketException cause, List<WebSocketFrame> frames) public void onMessageError(WebSocket websocket, WebSocketException cause, List<WebSocketFrame> frames)
throws Exception { throws Exception {
} }
@Override @Override
public void onPingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onPingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
} }
@Override @Override
public void onPongFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onPongFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
} }
@Override @Override
public void onSendError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) public void onSendError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame)
throws Exception { throws Exception {
} }
@Override @Override
public void onSendingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onSendingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
} }
@Override @Override
public void onSendingHandshake(WebSocket websocket, String requestLine, List<String[]> headers) public void onSendingHandshake(WebSocket websocket, String requestLine, List<String[]> headers)
throws Exception { throws Exception {
} }
@Override @Override
public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception { public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception {
} }
@Override @Override
public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception {
StompFrame stompFrame = StompFrame.fromString(frame.getPayloadText()); StompFrame stompFrame = StompFrame.fromString(frame.getPayloadText());
switch (stompFrame.getCommand()) { switch (stompFrame.getCommand()) {
case CONNECTED: case CONNECTED:
stompConnected = true; stompConnected = true;
break; break;
case MESSAGE: case MESSAGE:
Integer subscriptionId = Integer Integer subscriptionId = Integer
.valueOf(stompFrame.getHeaders().get(StompHeader.SUBSCRIPTION.toString())); .valueOf(stompFrame.getHeaders().get(StompHeader.SUBSCRIPTION.toString()));
subscriptions.get(subscriptionId).getListener().onMessage(stompFrame.getBody()); subscriptions.get(subscriptionId).getListener().onMessage(stompFrame.getBody());
break; break;
default: default:
break; break;
} }
} }
@Override @Override
public void onTextMessage(WebSocket websocket, String text) throws Exception { public void onTextMessage(WebSocket websocket, String text) throws Exception {
} }
@Override @Override
public void onTextMessageError(WebSocket websocket, WebSocketException cause, byte[] data) public void onTextMessageError(WebSocket websocket, WebSocketException cause, byte[] data)
throws Exception { throws Exception {
} }
@Override @Override
public void onUnexpectedError(WebSocket websocket, WebSocketException cause) throws Exception { public void onUnexpectedError(WebSocket websocket, WebSocketException cause) throws Exception {
} }
}; };
} }

View File

@ -26,32 +26,32 @@ package eu.mivrenik.stomp;
* *
*/ */
enum StompCommand { enum StompCommand {
CONNECT("CONNECT"), CONNECT("CONNECT"),
CONNECTED("CONNECTED"), CONNECTED("CONNECTED"),
DISCONNECT("DISCONNECT"), DISCONNECT("DISCONNECT"),
ERROR("ERROR"), ERROR("ERROR"),
MESSAGE("MESSAGE"), MESSAGE("MESSAGE"),
RECEIPT("RECEIPT"), RECEIPT("RECEIPT"),
SEND("SEND"), SEND("SEND"),
SUBSCRIBE("SUBSCRIBE"), SUBSCRIBE("SUBSCRIBE"),
UNSUBSCRIBE("UNSUBSCRIBE"); UNSUBSCRIBE("UNSUBSCRIBE");
private final String value; private final String value;
public static StompCommand fromValue(String value) { public static StompCommand fromValue(String value) {
for (StompCommand c : StompCommand.values()) for (StompCommand c : StompCommand.values())
if (c.value.equals(value)) if (c.value.equals(value))
return c; return c;
throw new IllegalArgumentException("Unknown STOMP command:" + value); throw new IllegalArgumentException("Unknown STOMP command:" + value);
} }
private StompCommand(String value) { private StompCommand(String value) {
this.value = value; this.value = value;
} }
@Override @Override
public String toString() { public String toString() {
return this.value; return this.value;
} }
} }

View File

@ -31,88 +31,88 @@ import java.util.Map;
* *
*/ */
class StompFrame { class StompFrame {
private StompCommand command; private StompCommand command;
private Map<String, String> headers = new HashMap<>(); private Map<String, String> headers = new HashMap<>();
private String body; private String body;
public StompFrame(StompCommand command) { public StompFrame(StompCommand command) {
this(command, null, null); this(command, null, null);
} }
public StompFrame(StompCommand command, Map<String, String> headers) { public StompFrame(StompCommand command, Map<String, String> headers) {
this(command, headers, null); this(command, headers, null);
} }
public StompFrame(StompCommand command, Map<String, String> headers, String body) { public StompFrame(StompCommand command, Map<String, String> headers, String body) {
this.command = command; this.command = command;
this.body = body; this.body = body;
if (headers != null) if (headers != null)
for (String k : headers.keySet()) for (String k : headers.keySet())
this.headers.put(k, headers.get(k)); this.headers.put(k, headers.get(k));
} }
public StompCommand getCommand() { public StompCommand getCommand() {
return command; return command;
} }
public Map<String, String> getHeaders() { public Map<String, String> getHeaders() {
return headers; return headers;
} }
public String getBody() { public String getBody() {
return body; return body;
} }
public static StompFrame fromString(String data) { public static StompFrame fromString(String data) {
String[] lines = data.split("\n"); String[] lines = data.split("\n");
int pos = 0; int pos = 0;
// Ignore empty lines // Ignore empty lines
while (pos < lines.length && lines[pos].isEmpty()) while (pos < lines.length && lines[pos].isEmpty())
pos++; pos++;
// Command // Command
StompCommand command = StompCommand.fromValue(lines[pos++]); StompCommand command = StompCommand.fromValue(lines[pos++]);
// Headers // Headers
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
while (!lines[pos].isEmpty()) { while (!lines[pos].isEmpty()) {
String[] keyValue = lines[pos].split(":"); String[] keyValue = lines[pos].split(":");
headers.put(keyValue[0], keyValue[1]); headers.put(keyValue[0], keyValue[1]);
pos++; pos++;
} }
pos++; pos++;
// Body // Body
StringBuilder body = new StringBuilder(); StringBuilder body = new StringBuilder();
for (; pos < lines.length; pos++) { for (; pos < lines.length; pos++) {
body.append(lines[pos]); body.append(lines[pos]);
if (body.charAt(body.length() - 1) == '\0') { if (body.charAt(body.length() - 1) == '\0') {
body.deleteCharAt(body.length() - 1); body.deleteCharAt(body.length() - 1);
break; break;
} }
} }
return new StompFrame(command, headers, body.toString()); return new StompFrame(command, headers, body.toString());
} }
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
// Command // Command
sb.append(command); sb.append(command);
sb.append('\n'); sb.append('\n');
// Headers // Headers
for (String k : headers.keySet()) { for (String k : headers.keySet()) {
sb.append(k); sb.append(k);
sb.append(':'); sb.append(':');
sb.append(headers.get(k)); sb.append(headers.get(k));
sb.append('\n'); sb.append('\n');
} }
sb.append('\n'); sb.append('\n');
// Body // Body
if (body != null) if (body != null)
sb.append(body); sb.append(body);
sb.append('\0'); sb.append('\0');
return sb.toString(); return sb.toString();
} }
} }

View File

@ -26,26 +26,26 @@ package eu.mivrenik.stomp;
* *
*/ */
enum StompHeader { enum StompHeader {
ACCEPT_VERSION("accept-version"), ACCEPT_VERSION("accept-version"),
CONTENT_LENGTH("content-length"), CONTENT_LENGTH("content-length"),
CONTENT_TYPE("content-type"), CONTENT_TYPE("content-type"),
DESTINATION("destination"), DESTINATION("destination"),
HOST("host"), HOST("host"),
ID("id"), ID("id"),
MESSAGE_ID("message-id"), MESSAGE_ID("message-id"),
RECEIPT_ID("receipt-id"), RECEIPT_ID("receipt-id"),
RECEIPT("receipt"), RECEIPT("receipt"),
SUBSCRIPTION("subscription"), SUBSCRIPTION("subscription"),
VERSION("version"); VERSION("version");
private final String value; private final String value;
private StompHeader(String value) { private StompHeader(String value) {
this.value = value; this.value = value;
} }
@Override @Override
public String toString() { public String toString() {
return this.value; return this.value;
} }
} }

View File

@ -26,5 +26,5 @@ package eu.mivrenik.stomp;
* *
*/ */
public interface StompMessageListener { public interface StompMessageListener {
void onMessage(String message); void onMessage(String message);
} }

View File

@ -26,25 +26,25 @@ package eu.mivrenik.stomp;
* *
*/ */
public class StompSubscription { public class StompSubscription {
private final Integer id; private final Integer id;
private final String destination; private final String destination;
private final StompMessageListener listener; private final StompMessageListener listener;
public StompSubscription(Integer id, String destination, StompMessageListener listener) { public StompSubscription(Integer id, String destination, StompMessageListener listener) {
this.id = id; this.id = id;
this.destination = destination; this.destination = destination;
this.listener = listener; this.listener = listener;
} }
public Integer getId() { public Integer getId() {
return id; return id;
} }
public String getDestination() { public String getDestination() {
return destination; return destination;
} }
public StompMessageListener getListener() { public StompMessageListener getListener() {
return listener; return listener;
} }
} }