Bug 401 Fix NPE when POSTing empty JSON object to /jobs (elastic/elasticsearch#420)

Original commit: elastic/x-pack-elasticsearch@fa6db52c8c
This commit is contained in:
David Kyle 2016-11-30 10:19:03 +00:00 committed by GitHub
parent fd743cbfc6
commit 1c98d59db0
11 changed files with 30 additions and 23 deletions

View File

@ -109,7 +109,7 @@ public class GetBucketsAction extends Action<GetBucketsAction.Request, GetBucket
private String partitionValue;
private String start;
private String end;
private PageParams pageParams = null;
private PageParams pageParams = new PageParams();
private double anomalyScore = 0.0;
private double maxNormalizedProbability = 0.0;

View File

@ -100,7 +100,7 @@ extends Action<GetInfluencersAction.Request, GetInfluencersAction.Response, GetI
private String start;
private String end;
private boolean includeInterim = false;
private PageParams pageParams = new PageParams(0, 100);
private PageParams pageParams = new PageParams();
private double anomalyScoreFilter = 0.0;
private String sort = Influencer.ANOMALY_SCORE.getPreferredName();
private boolean decending = false;

View File

@ -96,9 +96,11 @@ public class GetJobsAction extends Action<GetJobsAction.Request, GetJobsAction.R
private boolean modelSizeStats;
private boolean schedulerStatus;
private boolean status;
private PageParams pageParams = null;
private PageParams pageParams;
public Request() {
pageParams = new PageParams();
config = true;
}
public void setJobId(String jobId) {

View File

@ -95,7 +95,7 @@ extends Action<GetModelSnapshotsAction.Request, GetModelSnapshotsAction.Response
private String start;
private String end;
private boolean desc;
private PageParams pageParams = new PageParams(0, 100);
private PageParams pageParams = new PageParams();
Request() {
}

View File

@ -98,7 +98,7 @@ public class GetRecordsAction extends Action<GetRecordsAction.Request, GetRecord
private String start;
private String end;
private boolean includeInterim = false;
private PageParams pageParams = new PageParams(0, 100);
private PageParams pageParams = new PageParams();
private double anomalyScoreFilter = 0.0;
private String sort = Influencer.ANOMALY_SCORE.getPreferredName();
private boolean decending = false;

View File

@ -23,6 +23,10 @@ public class PageParams extends ToXContentToBytes implements Writeable {
public static final ParseField FROM = new ParseField("from");
public static final ParseField SIZE = new ParseField("size");
public static final int DEFAULT_FROM = 0;
public static final int DEFAULT_SIZE = 100;
public static final ConstructingObjectParser<PageParams, ParseFieldMatcherSupplier> PARSER = new ConstructingObjectParser<>(
PAGE.getPreferredName(), a -> new PageParams((int) a[0], (int) a[1]));
@ -46,19 +50,24 @@ public class PageParams extends ToXContentToBytes implements Writeable {
out.writeVInt(size);
}
public PageParams(int from, int SIZE) {
public PageParams() {
this.from = DEFAULT_FROM;
this.size = DEFAULT_SIZE;
}
public PageParams(int from, int size) {
if (from < 0) {
throw new IllegalArgumentException("Parameter [" + FROM.getPreferredName() + "] cannot be < 0");
}
if (SIZE < 0) {
if (size < 0) {
throw new IllegalArgumentException("Parameter [" + PageParams.SIZE.getPreferredName() + "] cannot be < 0");
}
if (from + SIZE > MAX_FROM_SIZE_SUM) {
if (from + size > MAX_FROM_SIZE_SUM) {
throw new IllegalArgumentException("The sum of parameters [" + FROM.getPreferredName() + "] and ["
+ PageParams.SIZE.getPreferredName() + "] cannot be higher than " + MAX_FROM_SIZE_SUM + ".");
}
this.from = from;
this.size = SIZE;
this.size = size;
}
public int getFrom() {

View File

@ -51,8 +51,8 @@ public class RestGetInfluencersAction extends BaseRestHandler {
} else {
request = new GetInfluencersAction.Request(jobId, start, end);
request.setIncludeInterim(restRequest.paramAsBoolean(GetInfluencersAction.Request.INCLUDE_INTERIM.getPreferredName(), false));
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), 0),
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), 100)));
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM),
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)));
request.setAnomalyScore(
Double.parseDouble(restRequest.param(GetInfluencersAction.Request.ANOMALY_SCORE.getPreferredName(), "0.0")));
request.setSort(restRequest.param(GetInfluencersAction.Request.SORT_FIELD.getPreferredName(),

View File

@ -26,8 +26,6 @@ import java.io.IOException;
import java.util.Set;
public class RestGetJobsAction extends BaseRestHandler {
private static final int DEFAULT_FROM = 0;
private static final int DEFAULT_SIZE = 100;
private final GetJobsAction.TransportAction transportGetJobAction;
@ -62,8 +60,8 @@ public class RestGetJobsAction extends BaseRestHandler {
Set<String> stats = Strings.splitStringByCommaToSet(
restRequest.param(GetJobsAction.Request.METRIC.getPreferredName(), "config"));
request.setStats(stats);
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), DEFAULT_FROM),
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), DEFAULT_SIZE)));
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM),
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)));
}
return channel -> transportGetJobAction.execute(request, new RestStatusToXContentListener<>(channel));

View File

@ -41,8 +41,6 @@ public class RestGetModelSnapshotsAction extends BaseRestHandler {
private final String DEFAULT_END = null;
private final String DEFAULT_DESCRIPTION = null;
private final boolean DEFAULT_DESC_ORDER = true;
private final int DEFAULT_FROM = 0;
private final int DEFAULT_SIZE = 100;
private final GetModelSnapshotsAction.TransportAction transportGetModelSnapshotsAction;
@ -77,8 +75,8 @@ public class RestGetModelSnapshotsAction extends BaseRestHandler {
getModelSnapshots.setDescriptionString(restRequest.param(DESCRIPTION.getPreferredName(), DEFAULT_DESCRIPTION));
}
getModelSnapshots.setDescOrder(restRequest.paramAsBoolean(DESC_ORDER.getPreferredName(), DEFAULT_DESC_ORDER));
getModelSnapshots.setPageParams(new PageParams(restRequest.paramAsInt(FROM.getPreferredName(), DEFAULT_FROM),
restRequest.paramAsInt(SIZE.getPreferredName(), DEFAULT_SIZE)));
getModelSnapshots.setPageParams(new PageParams(restRequest.paramAsInt(FROM.getPreferredName(), PageParams.DEFAULT_FROM),
restRequest.paramAsInt(SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)));
}
return channel -> transportGetModelSnapshotsAction.execute(getModelSnapshots, new RestToXContentListener<>(channel));

View File

@ -67,8 +67,8 @@ public class RestGetBucketsAction extends BaseRestHandler {
// Multiple buckets
request.setStart(start);
request.setEnd(end);
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), 0),
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), 100)));
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM),
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)));
request.setAnomalyScore(
Double.parseDouble(restRequest.param(GetBucketsAction.Request.ANOMALY_SCORE.getPreferredName(), "0.0")));
request.setMaxNormalizedProbability(

View File

@ -38,8 +38,8 @@ public class RestGetRecordsAction extends BaseRestHandler {
restRequest.param(GetRecordsAction.Request.START.getPreferredName()),
restRequest.param(GetRecordsAction.Request.END.getPreferredName()));
request.setIncludeInterim(restRequest.paramAsBoolean(GetRecordsAction.Request.INCLUDE_INTERIM.getPreferredName(), false));
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), 0),
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), 100)));
request.setPageParams(new PageParams(restRequest.paramAsInt(PageParams.FROM.getPreferredName(), PageParams.DEFAULT_FROM),
restRequest.paramAsInt(PageParams.SIZE.getPreferredName(), PageParams.DEFAULT_SIZE)));
request.setAnomalyScore(
Double.parseDouble(restRequest.param(GetRecordsAction.Request.ANOMALY_SCORE_FILTER.getPreferredName(), "0.0")));
request.setSort(restRequest.param(GetRecordsAction.Request.SORT.getPreferredName(),