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) {
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();
}
}

View File

@ -26,6 +26,7 @@ import org.apache.activemq.command.Command;
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 Command {
return false;
}
public String marshal() {
return toString(false);
}
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();
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 Command {
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()));
}

View File

@ -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;
}
}