Skip to content

Commit

Permalink
Parameter validation for aws-wps requests
Browse files Browse the repository at this point in the history
  • Loading branch information
anguss00 authored and anguss00 committed Jan 8, 2018
1 parent 8d8a64e commit 42ca8fe
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ s3LambdaCodeFilename=request-handler.zip
s3LambdaCodeURL=https://s3-ap-southeast-2.amazonaws.com/wps-lambda/request-handler-0.01-lambda-package.zip
templatesURL=https://raw.githubusercontent.com/aodn/aws-wps/master/wps-common/src/main/resources/config/templates.xml
administratorEmail[email protected]
geonetworkCatalogueURL=http://catalogue-sandbox.aodn.org.au/geonetwork
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import au.org.aodn.aws.wps.AwsApiResponse.ResponseBuilder;
import au.org.aodn.aws.wps.RequestParser;
import au.org.aodn.aws.wps.RequestParserFactory;
import au.org.aodn.aws.wps.exception.ValidationException;
import au.org.aodn.aws.util.JobFileUtil;
import au.org.aodn.aws.wps.operation.Operation;
import com.amazonaws.services.lambda.runtime.Context;
Expand Down Expand Up @@ -79,6 +80,11 @@ public AwsApiResponse handleRequest(AwsApiRequest request, Context context) {
String exceptionReportString = JobFileUtil.getExceptionReportString(oe.getExceptionText(),
oe.getExceptionCode(), oe.getLocator());
responseBuilder.body(exceptionReportString);
} catch (ValidationException ve) {
LOGGER.error("Error in request parameters " + ve.getMessage(), ve);
responseBuilder.statusCode(500);
String exceptionReportString = JobFileUtil.getExceptionReportString(ve.getMessage(), "ValidationError");
responseBuilder.body(exceptionReportString);
} catch (Exception e) {
LOGGER.error("Exception : " + e.getMessage(), e);
responseBuilder.statusCode(500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import au.org.aodn.aws.util.EmailService;
import au.org.aodn.aws.util.JobFileUtil;
import au.org.aodn.aws.wps.request.ExecuteRequestHelper;
import au.org.aodn.aws.wps.exception.ValidationException;
import au.org.aodn.aws.wps.status.*;
import com.amazonaws.services.batch.AWSBatch;
import com.amazonaws.services.batch.AWSBatchClientBuilder;
Expand Down Expand Up @@ -30,7 +31,7 @@ public ExecuteOperation(Execute executeRequest) {


@Override
public String execute() {
public String execute() throws ValidationException {

// Config items:
// queue names
Expand All @@ -54,6 +55,7 @@ public String execute() {
LOGGER.info("awsRegion: " + awsRegion);

ExecuteRequestHelper helper = new ExecuteRequestHelper(executeRequest);
helper.validateInputs();
String email = helper.getEmail();

String processIdentifier = executeRequest.getIdentifier().getValue(); // code spaces not supported for the moment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package au.org.aodn.aws.wps.operation;

import au.org.aodn.aws.exception.OGCException;
import au.org.aodn.aws.wps.exception.ValidationException;

public interface Operation {
String execute() throws OGCException;
String execute() throws OGCException, ValidationException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package au.org.aodn.aws.wps.exception;

public class ValidationException extends Exception {

public ValidationException()
{
super();
}

public ValidationException(String message)
{
super(message);
}

public ValidationException(String message, Throwable ex)
{
super(message, ex);
}

public ValidationException(Throwable ex)
{
super(ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import java.util.List;

import au.org.aodn.aws.wps.exception.ValidationException;

public class ExecuteRequestHelper {
private final Execute request;

Expand Down Expand Up @@ -34,6 +36,37 @@ public boolean hasRequestedOutput(String identifier) {
return false;
}

public void validateInputs() throws ValidationException {
String layerName = getLiteralInputValue("layer");
String subset = getLiteralInputValue("subset");
String email = getEmail();

if (layerName == null) {
throw new ValidationException("Request must have a layer name");
}

if (subset == null) {
throw new ValidationException("Request must have a subset");
} else {
String[] subsetFields = subset.split(";", -1);
String[] requiredFields = {"TIME", "LATITUDE", "LONGITUDE"};

if (subsetFields.length != requiredFields.length) {
throw new ValidationException("Subset is incorrectly formatted");
} else {
for (int i = 0; i < subsetFields.length; i++) {
if (!subsetFields[i].startsWith(requiredFields[i])) {
throw new ValidationException("Subset is missing required field: " + requiredFields[i]);
}
}
}
}

if (email == null) {
throw new ValidationException("Request must have a callback email");
}
}

public String getRequestedMimeType(String identifier) {
for (DocumentOutputDefinitionType output: getResponseFormOutputs()) {
if (output.getIdentifier().getValue().equals(identifier)) {
Expand Down

0 comments on commit 42ca8fe

Please sign in to comment.