Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional 'body' field to HttpInterface. #334

Merged
merged 5 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class HttpInterface implements SentryInterface {
private final String authType;
private final String remoteUser;
private final Map<String, Collection<String>> headers;
private final String body;

/**
* This constructor is for compatibility reasons and should not be used.
Expand All @@ -49,6 +50,17 @@ public HttpInterface(HttpServletRequest request) {
* @param remoteAddressResolver RemoteAddressResolver
*/
public HttpInterface(HttpServletRequest request, RemoteAddressResolver remoteAddressResolver) {
this(request, remoteAddressResolver, null);
}

/**
* Creates a an HTTP element for an {@link com.getsentry.raven.event.Event}.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"a an"

*
* @param request Captured HTTP request to send to Sentry.
* @param remoteAddressResolver RemoteAddressResolver
* @param body HTTP request body (optional)
*/
public HttpInterface(HttpServletRequest request, RemoteAddressResolver remoteAddressResolver, String body) {
this.requestUrl = request.getRequestURL().toString();
this.method = request.getMethod();
this.parameters = new HashMap<>();
Expand Down Expand Up @@ -79,6 +91,7 @@ public HttpInterface(HttpServletRequest request, RemoteAddressResolver remoteAdd
for (String headerName : Collections.list(request.getHeaderNames())) {
this.headers.put(headerName, Collections.list(request.getHeaders(headerName)));
}
this.body = body;
}

@Override
Expand Down Expand Up @@ -150,6 +163,10 @@ public String getRemoteUser() {
return remoteUser;
}

public String getBody() {
return body;
}

public Map<String, Collection<String>> getHeaders() {
return Collections.unmodifiableMap(headers);
}
Expand Down Expand Up @@ -226,6 +243,9 @@ public boolean equals(Object o) {
if (serverName != null ? !serverName.equals(that.serverName) : that.serverName != null) {
return false;
}
if (body != null ? !body.equals(that.body) : that.body != null) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever

return false;
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonGenerator;
import com.getsentry.raven.event.interfaces.HttpInterface;
import com.getsentry.raven.util.Util;

import java.io.IOException;
import java.util.Collection;
Expand All @@ -11,9 +12,18 @@
* Binding system allowing to convert an {@link HttpInterface} into a JSON stream.
*/
public class HttpInterfaceBinding implements InterfaceBinding<HttpInterface> {
/**
* Maximum length for the HTTP request body.
*
* See <a href="https://docs.sentry.io/clientdev/data-handling/#variable-size">the SDK reference</a>
* for more information.
*/
public static final int MAX_BODY_LENGTH = 2048;

private static final String URL = "url";
private static final String METHOD = "method";
private static final String DATA = "data";
private static final String BODY = "body";
private static final String QUERY_STRING = "query_string";
private static final String COOKIES = "cookies";
private static final String HEADERS = "headers";
Expand All @@ -36,7 +46,7 @@ public void writeInterface(JsonGenerator generator, HttpInterface httpInterface)
generator.writeStringField(URL, httpInterface.getRequestUrl());
generator.writeStringField(METHOD, httpInterface.getMethod());
generator.writeFieldName(DATA);
writeData(generator, httpInterface.getParameters());
writeData(generator, httpInterface.getParameters(), httpInterface.getBody());
generator.writeStringField(QUERY_STRING, httpInterface.getQueryString());
generator.writeFieldName(COOKIES);
writeCookies(generator, httpInterface.getCookies());
Expand Down Expand Up @@ -91,19 +101,26 @@ private void writeCookies(JsonGenerator generator, Map<String, String> cookies)
generator.writeEndObject();
}

private void writeData(JsonGenerator generator, Map<String, Collection<String>> parameterMap) throws IOException {
if (parameterMap == null) {
private void writeData(JsonGenerator generator,
Map<String, Collection<String>> parameterMap,
String body) throws IOException {
if (parameterMap == null && body == null) {
generator.writeNull();
return;
}

generator.writeStartObject();
for (Map.Entry<String, Collection<String>> parameter : parameterMap.entrySet()) {
generator.writeArrayFieldStart(parameter.getKey());
for (String parameterValue : parameter.getValue()) {
generator.writeString(parameterValue);
if (body != null) {
generator.writeStringField(BODY, Util.trimString(body, MAX_BODY_LENGTH));
}
if (parameterMap != null) {
for (Map.Entry<String, Collection<String>> parameter : parameterMap.entrySet()) {
generator.writeArrayFieldStart(parameter.getKey());
for (String parameterValue : parameter.getValue()) {
generator.writeString(parameterValue);
}
generator.writeEndArray();
}
generator.writeEndArray();
}
generator.writeEndObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.getsentry.raven.event.Event;
import com.getsentry.raven.event.interfaces.SentryInterface;
import com.getsentry.raven.marshaller.Marshaller;
import com.getsentry.raven.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -167,7 +168,7 @@ private void writeContent(JsonGenerator generator, Event event) throws IOExcepti
generator.writeStartObject();

generator.writeStringField(EVENT_ID, formatId(event.getId()));
generator.writeStringField(MESSAGE, trimMessage(event.getMessage()));
generator.writeStringField(MESSAGE, Util.trimString(event.getMessage(), maxMessageLength));
generator.writeStringField(TIMESTAMP, ISO_FORMAT.get().format(event.getTimestamp()));
generator.writeStringField(LEVEL, formatLevel(event.getLevel()));
generator.writeStringField(LOGGER, event.getLogger());
Expand Down Expand Up @@ -333,22 +334,6 @@ private void writeContexts(JsonGenerator generator, Map<String, Map<String, Obje
generator.writeEndObject();
}

/**
* Trims a message, ensuring that the maximum length {@link #maxMessageLength} isn't reached.
*
* @param message message to format.
* @return trimmed message (shortened if necessary).
*/
private String trimMessage(String message) {
if (message == null) {
return null;
} else if (message.length() > maxMessageLength) {
return message.substring(0, maxMessageLength);
} else {
return message;
}
}

/**
* Formats the {@code UUID} to send only the 32 necessary characters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonGenerator;
import com.getsentry.raven.event.interfaces.MessageInterface;
import com.getsentry.raven.util.Util;

import java.io.IOException;

Expand Down Expand Up @@ -38,33 +39,18 @@ public MessageInterfaceBinding(int maxMessageLength) {
this.maxMessageLength = maxMessageLength;
}

/**
* Trims a message, ensuring that the maximum length {@link #maxMessageLength} isn't reached.
*
* @param message message to format.
* @return trimmed message (shortened if necessary).
*/
private String trimMessage(String message) {
if (message == null) {
return null;
} else if (message.length() > maxMessageLength) {
return message.substring(0, maxMessageLength);
} else {
return message;
}
}

@Override
public void writeInterface(JsonGenerator generator, MessageInterface messageInterface) throws IOException {
generator.writeStartObject();
generator.writeStringField(MESSAGE_PARAMETER, trimMessage(messageInterface.getMessage()));
generator.writeStringField(MESSAGE_PARAMETER, Util.trimString(messageInterface.getMessage(), maxMessageLength));
generator.writeArrayFieldStart(PARAMS_PARAMETER);
for (String parameter : messageInterface.getParameters()) {
generator.writeString(parameter);
}
generator.writeEndArray();
if (messageInterface.getFormatted() != null) {
generator.writeStringField(FORMATTED_PARAMETER, trimMessage(messageInterface.getFormatted()));
generator.writeStringField(FORMATTED_PARAMETER,
Util.trimString(messageInterface.getFormatted(), maxMessageLength));
}
generator.writeEndObject();
}
Expand Down
18 changes: 18 additions & 0 deletions raven/src/main/java/com/getsentry/raven/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,22 @@ public static Double parseDouble(String value, Double defaultValue) {
}
return Double.parseDouble(value);
}

/**
* Trims a String, ensuring that the maximum length isn't reached.
*
* @param string string to trim
* @param maxMessageLength maximum length of the string
* @return trimmed string
*/
public static String trimString(String string, int maxMessageLength) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth considering ellipisizing this? I'm not sure what behavior is consistent among the other SDKs…?

if (string == null) {
return null;
} else if (string.length() > maxMessageLength) {
return string.substring(0, maxMessageLength);
} else {
return string;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public void testHeaders() throws Exception {
result = "5.6.7.8";
mockMessageInterface.getLocalName();
result = "local-name";
mockMessageInterface.getBody();
result = "body";
}};

interfaceBinding.writeInterface(jsonGeneratorParser.generator(), mockMessageInterface);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"url": "http://host/url",
"method": "GET",
"data": {},
"data": {
"body": "body"
},
"query_string": "query",
"cookies": {
"Cookie1": "Value1"
Expand Down