activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gtu...@apache.org
Subject svn commit: r1084020 - in /activemq/trunk/activemq-core/src/main/java/org/apache/activemq: transport/stomp/StompConnection.java transport/stomp/StompFrame.java util/MarshallingSupport.java
Date Mon, 21 Mar 2011 23:34:32 GMT
Author: gtully
Date: Mon Mar 21 23:34:32 2011
New Revision: 1084020

URL: http://svn.apache.org/viewvc?rev=1084020&view=rev
Log:
https://issues.apache.org/jira/browse/AMQ-3231 - have stompconnection use format instead of
toString for marshalling such that toString can be used for logging; mask password and truncate
content like text message

Modified:
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/stomp/StompConnection.java
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/stomp/StompFrame.java
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/util/MarshallingSupport.java

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/stomp/StompConnection.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/stomp/StompConnection.java?rev=1084020&r1=1084019&r2=1084020&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/stomp/StompConnection.java
(original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/stomp/StompConnection.java
Mon Mar 21 23:34:32 2011
@@ -38,7 +38,7 @@ public class StompConnection {
     }
 
     public void open(Socket socket) {
-        stompSocket = socket;
+    	stompSocket = socket;
     }
 
     public void close() throws IOException {
@@ -70,8 +70,8 @@ public class StompConnection {
     }
 
     public StompFrame receive(long timeOut) throws Exception {
-        stompSocket.setSoTimeout((int)timeOut);
-        InputStream is = stompSocket.getInputStream();
+    	stompSocket.setSoTimeout((int)timeOut);
+    	InputStream is = stompSocket.getInputStream();
         StompWireFormat wf = new StompWireFormat();
         DataInputStream dis = new DataInputStream(is);
         return (StompFrame)wf.unmarshal(dis);
@@ -104,143 +104,143 @@ public class StompConnection {
         }
     }
 
-    private String stringFromBuffer(ByteArrayOutputStream inputBuffer) throws Exception {
-        byte[] ba = inputBuffer.toByteArray();
+	private String stringFromBuffer(ByteArrayOutputStream inputBuffer) throws Exception {
+	    byte[] ba = inputBuffer.toByteArray();
         inputBuffer.reset();
         return new String(ba, "UTF-8");
     }
 
     public Socket getStompSocket() {
-        return stompSocket;
-    }
+		return stompSocket;
+	}
 
-    public void setStompSocket(Socket stompSocket) {
-        this.stompSocket = stompSocket;
-    }
+	public void setStompSocket(Socket stompSocket) {
+		this.stompSocket = stompSocket;
+	}
 
     public void connect(String username, String password) throws Exception {
         connect(username, password, null);
     }
 
     public void connect(String username, String password, String client) throws Exception
{
-        HashMap<String, String> headers = new HashMap<String, String>();
-        headers.put("login", username);
-        headers.put("passcode", password);
-        if (client != null) {
-            headers.put("client-id", client);
-        }
-        StompFrame frame = new StompFrame("CONNECT", headers);
-        sendFrame(frame.marshal());
+    	HashMap<String, String> headers = new HashMap();
+    	headers.put("login", username);
+    	headers.put("passcode", password);
+    	if (client != null) {
+    		headers.put("client-id", client);
+    	}
+    	StompFrame frame = new StompFrame("CONNECT", headers);
+        sendFrame(frame.format());
 
         StompFrame connect = receive();
         if (!connect.getAction().equals(Stomp.Responses.CONNECTED)) {
-            throw new Exception ("Not connected: " + connect.getBody());
+        	throw new Exception ("Not connected: " + connect.getBody());
         }
     }
 
     public void disconnect() throws Exception {
-        StompFrame frame = new StompFrame("DISCONNECT");
-        sendFrame(frame.toString());
+    	StompFrame frame = new StompFrame("DISCONNECT");
+        sendFrame(frame.format());
     }
 
     public void send(String destination, String message) throws Exception {
-        send(destination, message, null, null);
+    	send(destination, message, null, null);
     }
 
     public void send(String destination, String message, String transaction, HashMap<String,
String> headers) throws Exception {
-        if (headers == null) {
-            headers = new HashMap<String, String>();
-        }
-        headers.put("destination", destination);
-        if (transaction != null) {
-            headers.put("transaction", transaction);
-        }
-        StompFrame frame = new StompFrame("SEND", headers, message.getBytes());
-        sendFrame(frame.toString());
+    	if (headers == null) {
+    		headers = new HashMap<String, String>();
+    	}
+    	headers.put("destination", destination);
+    	if (transaction != null) {
+    		headers.put("transaction", transaction);
+    	}
+    	StompFrame frame = new StompFrame("SEND", headers, message.getBytes());
+        sendFrame(frame.format());
     }
 
     public void subscribe(String destination) throws Exception {
-        subscribe(destination, null, null);
+    	subscribe(destination, null, null);
     }
 
     public void subscribe(String destination, String ack) throws Exception {
-        subscribe(destination, ack, new HashMap<String, String>());
+    	subscribe(destination, ack, new HashMap<String, String>());
     }
 
     public void subscribe(String destination, String ack, HashMap<String, String> headers)
throws Exception {
-        if (headers == null) {
-            headers = new HashMap<String, String>();
-        }
-        headers.put("destination", destination);
-        if (ack != null) {
-            headers.put("ack", ack);
-        }
-        StompFrame frame = new StompFrame("SUBSCRIBE", headers);
-        sendFrame(frame.toString());
+		if (headers == null) {
+			headers = new HashMap<String, String>();
+		}
+		headers.put("destination", destination);
+    	if (ack != null) {
+    		headers.put("ack", ack);
+    	}
+    	StompFrame frame = new StompFrame("SUBSCRIBE", headers);
+        sendFrame(frame.format());
     }
 
     public void unsubscribe(String destination) throws Exception {
-        unsubscribe(destination, null);
+    	unsubscribe(destination, null);
     }
 
     public void unsubscribe(String destination, HashMap<String, String> headers) throws
Exception {
-        if (headers == null) {
-            headers = new HashMap<String, String>();
-        }
-        headers.put("destination", destination);
-        StompFrame frame = new StompFrame("UNSUBSCRIBE", headers);
-        sendFrame(frame.toString());
-    }
-
+		if (headers == null) {
+			headers = new HashMap<String, String>();
+		}
+		headers.put("destination", destination);
+    	StompFrame frame = new StompFrame("UNSUBSCRIBE", headers);
+        sendFrame(frame.format());
+    }    
+    
     public void begin(String transaction) throws Exception {
-        HashMap<String, String> headers = new HashMap<String, String>();
-        headers.put("transaction", transaction);
-        StompFrame frame = new StompFrame("BEGIN", headers);
-        sendFrame(frame.toString());
+    	HashMap<String, String> headers = new HashMap<String, String>();
+    	headers.put("transaction", transaction);
+    	StompFrame frame = new StompFrame("BEGIN", headers);
+    	sendFrame(frame.format());
     }
 
     public void abort(String transaction) throws Exception {
-        HashMap<String, String> headers = new HashMap<String, String>();
-        headers.put("transaction", transaction);
-        StompFrame frame = new StompFrame("ABORT", headers);
-        sendFrame(frame.toString());
+    	HashMap<String, String> headers = new HashMap<String, String>();
+    	headers.put("transaction", transaction);
+    	StompFrame frame = new StompFrame("ABORT", headers);
+    	sendFrame(frame.format());
     }
 
     public void commit(String transaction) throws Exception {
-        HashMap<String, String> headers = new HashMap<String, String>();
-        headers.put("transaction", transaction);
-        StompFrame frame = new StompFrame("COMMIT", headers);
-        sendFrame(frame.toString());
+    	HashMap<String, String> headers = new HashMap<String, String>();
+    	headers.put("transaction", transaction);
+    	StompFrame frame = new StompFrame("COMMIT", headers);
+    	sendFrame(frame.format());
     }
 
     public void ack(StompFrame frame) throws Exception {
-        ack(frame.getHeaders().get("message-id"), null);
+    	ack(frame.getHeaders().get("message-id"), null);
     }
 
     public void ack(StompFrame frame, String transaction) throws Exception {
-        ack(frame.getHeaders().get("message-id"), transaction);
+    	ack(frame.getHeaders().get("message-id"), transaction);
     }
 
     public void ack(String messageId) throws Exception {
-        ack(messageId, null);
+    	ack(messageId, null);
     }
 
     public void ack(String messageId, String transaction) throws Exception {
-        HashMap<String, String> headers = new HashMap<String, String>();
-        headers.put("message-id", messageId);
-        if (transaction != null)
-            headers.put("transaction", transaction);
-        StompFrame frame = new StompFrame("ACK", headers);
-        sendFrame(frame.toString());
+    	HashMap<String, String> headers = new HashMap<String, String>();
+    	headers.put("message-id", messageId);
+    	if (transaction != null)
+    		headers.put("transaction", transaction);
+    	StompFrame frame = new StompFrame("ACK", headers);
+    	sendFrame(frame.format());
     }
 
     protected String appendHeaders(HashMap<String, Object> headers) {
-        StringBuffer result = new StringBuffer();
-        for (String key : headers.keySet()) {
-            result.append(key + ":" + headers.get(key) + "\n");
-        }
-        result.append("\n");
-        return result.toString();
+    	StringBuffer result = new StringBuffer();
+    	for (String key : headers.keySet()) {
+    		result.append(key + ":" + headers.get(key) + "\n");
+    	}
+    	result.append("\n");
+    	return result.toString();
     }
 
 }

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/stomp/StompFrame.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/stomp/StompFrame.java?rev=1084020&r1=1084019&r2=1084020&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/stomp/StompFrame.java
(original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/transport/stomp/StompFrame.java
Mon Mar 21 23:34:32 2011
@@ -26,6 +26,7 @@ import org.apache.activemq.command.Comma
 import org.apache.activemq.command.Endpoint;
 import org.apache.activemq.command.Response;
 import org.apache.activemq.state.CommandVisitor;
+import org.apache.activemq.util.MarshallingSupport;
 
 /**
  * Represents all the data in a STOMP frame.
@@ -170,24 +171,24 @@ public class StompFrame implements Comma
         return false;
     }
 
-    public String marshal() {
-        return toString(false);
+    public String toString() {
+        return format(true);
     }
 
-    public String toString() {
-        return toString(true);
+    public String format() {
+        return format(false);
     }
 
-    private String toString(boolean hidePasscode) {
+    public String format(boolean forLogging) {
         StringBuffer buffer = new StringBuffer();
         buffer.append(getAction());
         buffer.append("\n");
-        Map<String, String> headers = getHeaders();
-        for (Iterator<Map.Entry<String,String>> iter = headers.entrySet().iterator();
iter.hasNext();) {
-            Map.Entry<String, String> entry = (Map.Entry<String,String>)iter.next();
+        Map headers = getHeaders();
+        for (Iterator iter = headers.entrySet().iterator(); iter.hasNext();) {
+            Map.Entry entry = (Map.Entry)iter.next();
             buffer.append(entry.getKey());
             buffer.append(":");
-            if (hidePasscode && entry.getKey().toString().toLowerCase().contains(Stomp.Headers.Connect.PASSCODE))
{
+            if (forLogging && entry.getKey().toString().toLowerCase().contains(Stomp.Headers.Connect.PASSCODE))
{
                 buffer.append("*****");
             } else {
                 buffer.append(entry.getValue());
@@ -197,7 +198,11 @@ public class StompFrame implements Comma
         buffer.append("\n");
         if (getContent() != null) {
             try {
-                buffer.append(new String(getContent(), "UTF-8"));
+                String contentString = new String(getContent(), "UTF-8");
+                if (forLogging) {
+                    contentString = MarshallingSupport.truncate64(contentString);
+                }
+                buffer.append(contentString);
             } catch (Throwable e) {
                 buffer.append(Arrays.toString(getContent()));
             }

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/util/MarshallingSupport.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/util/MarshallingSupport.java?rev=1084020&r1=1084019&r2=1084020&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/util/MarshallingSupport.java
(original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/util/MarshallingSupport.java
Mon Mar 21 23:34:32 2011
@@ -400,4 +400,10 @@ public final class MarshallingSupport {
         return result;
     }
 
+    public static String truncate64(String text) {
+        if (text.length() > 63) {
+            text = text.substring(0, 45) + "..." + text.substring(text.length() - 12);
+        }
+        return text;
+    }
 }



Mime
View raw message