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

View File

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

View File

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

View File

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

View File

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

View File

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