Skip to content

Commit 3f0761d

Browse files
committed
#1 Improved metering of endpoints and error checking
1 parent 1a31c02 commit 3f0761d

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/main/java/org/multibit/hd/error_reporting/resources/PublicErrorReportingResource.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import com.google.common.base.Charsets;
66
import com.google.common.base.Optional;
7-
import com.google.common.base.Preconditions;
87
import com.google.common.base.Strings;
98
import com.yammer.dropwizard.jersey.caching.CacheControl;
10-
import com.yammer.metrics.annotation.Timed;
9+
import com.yammer.metrics.annotation.ExceptionMetered;
10+
import com.yammer.metrics.annotation.Metered;
1111
import org.elasticsearch.action.index.IndexResponse;
1212
import org.elasticsearch.client.Client;
1313
import org.multibit.hd.brit.crypto.PGPUtils;
@@ -49,7 +49,7 @@ public class PublicErrorReportingResource extends BaseResource {
4949
private final Client elasticClient;
5050

5151
/**
52-
* The maximum length of the payload (typical value is 680 bytes)
52+
* The maximum length of the payload (typical value before encryption is 200Kb so compressed should be much smaller)
5353
*/
5454
private final static int MAX_PAYLOAD_LENGTH = 2_000_000;
5555

@@ -91,7 +91,8 @@ public PublicErrorReportingResource(byte[] secring, char[] password, String serv
9191
@Path("/public-key")
9292
@Consumes("text/plain")
9393
@Produces("text/plain")
94-
@Timed
94+
@Metered
95+
@ExceptionMetered
9596
@CacheControl(maxAge = 1, maxAgeUnit = TimeUnit.DAYS)
9697
public Response getPublicKey() {
9798
return Response.ok(SERVICE_PUBLIC_KEY).build();
@@ -107,12 +108,17 @@ public Response getPublicKey() {
107108
@POST
108109
@Consumes("text/plain")
109110
@Produces("application/json")
110-
@Timed
111+
@Metered
112+
@ExceptionMetered
111113
@CacheControl(noCache = true)
112114
public Response submitEncryptedErrorReport(String payload) {
113115

114-
Preconditions.checkNotNull(payload, "'payload' must be present");
115-
Preconditions.checkState(payload.length() < MAX_PAYLOAD_LENGTH, "'payload' is too long");
116+
if (Strings.isNullOrEmpty(payload)) {
117+
throw new WebApplicationException(Response.Status.BAD_REQUEST);
118+
}
119+
if (payload.length() > MAX_PAYLOAD_LENGTH) {
120+
throw new WebApplicationException(Response.Status.BAD_REQUEST);
121+
}
116122

117123
ErrorReportResult result = processEncryptedErrorReport(payload.getBytes(Charsets.UTF_8));
118124

0 commit comments

Comments
 (0)