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

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1084020 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary Tully 2011-03-21 23:34:32 +00:00
parent 8314d8651f
commit d618ab37b0
3 changed files with 100 additions and 89 deletions

View File

@ -38,7 +38,7 @@ public class StompConnection {
} }
public void open(Socket socket) { public void open(Socket socket) {
stompSocket = socket; stompSocket = socket;
} }
public void close() throws IOException { public void close() throws IOException {
@ -70,8 +70,8 @@ public class StompConnection {
} }
public StompFrame receive(long timeOut) throws Exception { public StompFrame receive(long timeOut) throws Exception {
stompSocket.setSoTimeout((int)timeOut); stompSocket.setSoTimeout((int)timeOut);
InputStream is = stompSocket.getInputStream(); InputStream is = stompSocket.getInputStream();
StompWireFormat wf = new StompWireFormat(); StompWireFormat wf = new StompWireFormat();
DataInputStream dis = new DataInputStream(is); DataInputStream dis = new DataInputStream(is);
return (StompFrame)wf.unmarshal(dis); return (StompFrame)wf.unmarshal(dis);
@ -104,143 +104,143 @@ public class StompConnection {
} }
} }
private String stringFromBuffer(ByteArrayOutputStream inputBuffer) throws Exception { private String stringFromBuffer(ByteArrayOutputStream inputBuffer) throws Exception {
byte[] ba = inputBuffer.toByteArray(); byte[] ba = inputBuffer.toByteArray();
inputBuffer.reset(); inputBuffer.reset();
return new String(ba, "UTF-8"); return new String(ba, "UTF-8");
} }
public Socket getStompSocket() { public Socket getStompSocket() {
return stompSocket; return stompSocket;
} }
public void setStompSocket(Socket stompSocket) { public void setStompSocket(Socket stompSocket) {
this.stompSocket = stompSocket; this.stompSocket = stompSocket;
} }
public void connect(String username, String password) throws Exception { public void connect(String username, String password) throws Exception {
connect(username, password, null); connect(username, password, null);
} }
public void connect(String username, String password, String client) throws Exception { public void connect(String username, String password, String client) throws Exception {
HashMap<String, String> headers = new HashMap<String, String>(); HashMap<String, String> headers = new HashMap();
headers.put("login", username); headers.put("login", username);
headers.put("passcode", password); headers.put("passcode", password);
if (client != null) { if (client != null) {
headers.put("client-id", client); headers.put("client-id", client);
} }
StompFrame frame = new StompFrame("CONNECT", headers); StompFrame frame = new StompFrame("CONNECT", headers);
sendFrame(frame.marshal()); sendFrame(frame.format());
StompFrame connect = receive(); StompFrame connect = receive();
if (!connect.getAction().equals(Stomp.Responses.CONNECTED)) { 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 { public void disconnect() throws Exception {
StompFrame frame = new StompFrame("DISCONNECT"); StompFrame frame = new StompFrame("DISCONNECT");
sendFrame(frame.toString()); sendFrame(frame.format());
} }
public void send(String destination, String message) throws Exception { 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 { public void send(String destination, String message, String transaction, HashMap<String, String> headers) throws Exception {
if (headers == null) { if (headers == null) {
headers = new HashMap<String, String>(); headers = new HashMap<String, String>();
} }
headers.put("destination", destination); headers.put("destination", destination);
if (transaction != null) { if (transaction != null) {
headers.put("transaction", transaction); headers.put("transaction", transaction);
} }
StompFrame frame = new StompFrame("SEND", headers, message.getBytes()); StompFrame frame = new StompFrame("SEND", headers, message.getBytes());
sendFrame(frame.toString()); sendFrame(frame.format());
} }
public void subscribe(String destination) throws Exception { public void subscribe(String destination) throws Exception {
subscribe(destination, null, null); subscribe(destination, null, null);
} }
public void subscribe(String destination, String ack) throws Exception { 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 { public void subscribe(String destination, String ack, HashMap<String, String> headers) throws Exception {
if (headers == null) { if (headers == null) {
headers = new HashMap<String, String>(); headers = new HashMap<String, String>();
} }
headers.put("destination", destination); headers.put("destination", destination);
if (ack != null) { if (ack != null) {
headers.put("ack", ack); headers.put("ack", ack);
} }
StompFrame frame = new StompFrame("SUBSCRIBE", headers); StompFrame frame = new StompFrame("SUBSCRIBE", headers);
sendFrame(frame.toString()); sendFrame(frame.format());
} }
public void unsubscribe(String destination) throws Exception { public void unsubscribe(String destination) throws Exception {
unsubscribe(destination, null); unsubscribe(destination, null);
} }
public void unsubscribe(String destination, HashMap<String, String> headers) throws Exception { public void unsubscribe(String destination, HashMap<String, String> headers) throws Exception {
if (headers == null) { if (headers == null) {
headers = new HashMap<String, String>(); headers = new HashMap<String, String>();
} }
headers.put("destination", destination); headers.put("destination", destination);
StompFrame frame = new StompFrame("UNSUBSCRIBE", headers); StompFrame frame = new StompFrame("UNSUBSCRIBE", headers);
sendFrame(frame.toString()); sendFrame(frame.format());
} }
public void begin(String transaction) throws Exception { public void begin(String transaction) throws Exception {
HashMap<String, String> headers = new HashMap<String, String>(); HashMap<String, String> headers = new HashMap<String, String>();
headers.put("transaction", transaction); headers.put("transaction", transaction);
StompFrame frame = new StompFrame("BEGIN", headers); StompFrame frame = new StompFrame("BEGIN", headers);
sendFrame(frame.toString()); sendFrame(frame.format());
} }
public void abort(String transaction) throws Exception { public void abort(String transaction) throws Exception {
HashMap<String, String> headers = new HashMap<String, String>(); HashMap<String, String> headers = new HashMap<String, String>();
headers.put("transaction", transaction); headers.put("transaction", transaction);
StompFrame frame = new StompFrame("ABORT", headers); StompFrame frame = new StompFrame("ABORT", headers);
sendFrame(frame.toString()); sendFrame(frame.format());
} }
public void commit(String transaction) throws Exception { public void commit(String transaction) throws Exception {
HashMap<String, String> headers = new HashMap<String, String>(); HashMap<String, String> headers = new HashMap<String, String>();
headers.put("transaction", transaction); headers.put("transaction", transaction);
StompFrame frame = new StompFrame("COMMIT", headers); StompFrame frame = new StompFrame("COMMIT", headers);
sendFrame(frame.toString()); sendFrame(frame.format());
} }
public void ack(StompFrame frame) throws Exception { 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 { 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 { public void ack(String messageId) throws Exception {
ack(messageId, null); ack(messageId, null);
} }
public void ack(String messageId, String transaction) throws Exception { public void ack(String messageId, String transaction) throws Exception {
HashMap<String, String> headers = new HashMap<String, String>(); HashMap<String, String> headers = new HashMap<String, String>();
headers.put("message-id", messageId); headers.put("message-id", messageId);
if (transaction != null) if (transaction != null)
headers.put("transaction", transaction); headers.put("transaction", transaction);
StompFrame frame = new StompFrame("ACK", headers); StompFrame frame = new StompFrame("ACK", headers);
sendFrame(frame.toString()); sendFrame(frame.format());
} }
protected String appendHeaders(HashMap<String, Object> headers) { protected String appendHeaders(HashMap<String, Object> headers) {
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
for (String key : headers.keySet()) { for (String key : headers.keySet()) {
result.append(key + ":" + headers.get(key) + "\n"); result.append(key + ":" + headers.get(key) + "\n");
} }
result.append("\n"); result.append("\n");
return result.toString(); return result.toString();
} }
} }

View File

@ -26,6 +26,7 @@ import org.apache.activemq.command.Command;
import org.apache.activemq.command.Endpoint; import org.apache.activemq.command.Endpoint;
import org.apache.activemq.command.Response; import org.apache.activemq.command.Response;
import org.apache.activemq.state.CommandVisitor; import org.apache.activemq.state.CommandVisitor;
import org.apache.activemq.util.MarshallingSupport;
/** /**
* Represents all the data in a STOMP frame. * Represents all the data in a STOMP frame.
@ -170,24 +171,24 @@ public class StompFrame implements Command {
return false; return false;
} }
public String marshal() {
return toString(false);
}
public String toString() { public String toString() {
return toString(true); return format(true);
} }
private String toString(boolean hidePasscode) { public String format() {
return format(false);
}
public String format(boolean forLogging) {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
buffer.append(getAction()); buffer.append(getAction());
buffer.append("\n"); buffer.append("\n");
Map<String, String> headers = getHeaders(); Map headers = getHeaders();
for (Iterator<Map.Entry<String,String>> iter = headers.entrySet().iterator(); iter.hasNext();) { for (Iterator iter = headers.entrySet().iterator(); iter.hasNext();) {
Map.Entry<String, String> entry = (Map.Entry<String,String>)iter.next(); Map.Entry entry = (Map.Entry)iter.next();
buffer.append(entry.getKey()); buffer.append(entry.getKey());
buffer.append(":"); 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("*****"); buffer.append("*****");
} else { } else {
buffer.append(entry.getValue()); buffer.append(entry.getValue());
@ -197,7 +198,11 @@ public class StompFrame implements Command {
buffer.append("\n"); buffer.append("\n");
if (getContent() != null) { if (getContent() != null) {
try { 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) { } catch (Throwable e) {
buffer.append(Arrays.toString(getContent())); buffer.append(Arrays.toString(getContent()));
} }

View File

@ -400,4 +400,10 @@ public final class MarshallingSupport {
return result; return result;
} }
public static String truncate64(String text) {
if (text.length() > 63) {
text = text.substring(0, 45) + "..." + text.substring(text.length() - 12);
}
return text;
}
} }