Skip to content

Commit 9a520f0

Browse files
committed
APIKIT-2511: Move validation to Parser Wrapper
1 parent 435a8ad commit 9a520f0

File tree

87 files changed

+6623
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+6623
-15
lines changed

interface-impl-amf/src/test/resources/org/mule/amf/impl/10-american-flights/api.raml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ types:
1616
enum: [SFO, LAX, CLE]
1717
responses:
1818
200:
19-
body:
19+
body:
2020
application/json:
2121
type: AmericanFlight[]
2222
examples: !include exchange_modules/68ef9520-24e9-4cf2-b2f5-620025690913/training-american-flights-example/1.0.1/AmericanFlightsExample.raml
2323
post:
2424
displayName: Add a flight
25-
body:
25+
body:
2626
application/json:
2727
type: AmericanFlight
2828
examples: !include examples/AmericanFlightNoIDExample.raml
2929
responses:
3030
201:
31-
body:
31+
body:
3232
application/json:
3333
example:
3434
message: Flight added (but not really)
@@ -42,7 +42,7 @@ types:
4242
displayName: Get a flight by ID
4343
responses:
4444
200:
45-
body:
45+
body:
4646
application/json:
4747
type: AmericanFlight
4848
examples: !include examples/AmericanFlightExample.raml

parser-service/src/test/resources/apis/api-with-exchange/08/exchange_modules/base.raml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
get?:
22
responses:
33
200:
4-
body:
4+
body:
55
application/atom+xml:
66
schema: atom
77
post?:
88
responses:
99
200?:
10-
body:
10+
body:
1111
application/atom+xml:
1212
schema: atom
1313
201?:
14-
body:
14+
body:
1515
application/atom+xml:
1616
schema: atom
1717
put?:
1818
responses:
1919
200:
20-
body:
20+
body:
2121
application/atom+xml:
2222
schema: atom
2323
412:

parser-service/src/test/resources/apis/api-with-references/08/resourceTypes/base.raml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
get?:
22
responses:
33
200:
4-
body:
4+
body:
55
application/atom+xml:
66
schema: atom
77
post?:
88
responses:
99
200?:
10-
body:
10+
body:
1111
application/atom+xml:
1212
schema: atom
1313
201?:
14-
body:
14+
body:
1515
application/atom+xml:
1616
schema: atom
1717
put?:
1818
responses:
1919
200:
20-
body:
20+
body:
2121
application/atom+xml:
2222
schema: atom
2323
412:

parser-service/src/test/resources/apis/api-with-references/10/api.raml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ types:
1111
get:
1212
responses:
1313
200:
14-
body:
14+
body:
1515
application/json:
1616
type: !include schemas/atom.xsd
1717

parser-service/src/test/resources/apis/api-with-spaces/10/api spaces.raml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ types:
1111
get:
1212
responses:
1313
200:
14-
body:
14+
body:
1515
application/json:
1616

1717

parser-service/src/test/resources/apis/api-with-spaces/space in path api/folder/baseResource.raml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ get?:
33
200:
44
description: |
55
Request succeeded
6-
body:
6+
body:
77
text/xml:
88
application/json:
99
schema: !include ../more spaces/schema.json

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<module>interface-impl-v2</module>
1818
<module>interface-impl-amf</module>
1919
<module>parser-service</module>
20+
<module>validation</module>
2021
</modules>
2122

2223
<properties>

validation/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>raml-parser-interface-parent</artifactId>
7+
<groupId>org.mule.apikit</groupId>
8+
<version>2.1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>validation</artifactId>
13+
14+
<properties>
15+
<http.connector.version>1.5.6</http.connector.version>
16+
<mule.version>4.1.5</mule.version>
17+
<httpmime.version>4.5.10</httpmime.version>
18+
<fileupload.version>1.4</fileupload.version>
19+
20+
<licensePath>../LICENSE_HEADER.txt</licensePath>
21+
<formatterConfigPath>../formatter.xml</formatterConfigPath>
22+
<sonar.jacoco.reportPaths>../target/jacoco.exec</sonar.jacoco.reportPaths>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.mule.apikit</groupId>
28+
<artifactId>parser-service</artifactId>
29+
<version>${project.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.mule.connectors</groupId>
33+
<artifactId>mule-http-connector</artifactId>
34+
<classifier>mule-plugin</classifier>
35+
<version>${http.connector.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.mule.runtime</groupId>
39+
<artifactId>mule-core</artifactId>
40+
<version>${mule.version}</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.apache.httpcomponents</groupId>
45+
<artifactId>httpmime</artifactId>
46+
<version>${httpmime.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>commons-fileupload</groupId>
50+
<artifactId>commons-fileupload</artifactId>
51+
<version>${fileupload.version}</version>
52+
<exclusions>
53+
<exclusion>
54+
<groupId>commons-io</groupId>
55+
<artifactId>commons-io</artifactId>
56+
</exclusion>
57+
</exclusions>
58+
</dependency>
59+
</dependencies>
60+
61+
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.mule.validation;/*
2+
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
3+
* The software in this package is published under the terms of the CPAL v1.0
4+
* license, a copy of which has been included with this distribution in the
5+
* LICENSE.txt file.
6+
*/
7+
import org.mule.validation.attributes.AttributesValidator;
8+
import org.mule.validation.attributes.DefaultsHandler;
9+
import org.mule.validation.attributes.HeadersDefaultsHandler;
10+
import org.mule.validation.attributes.HeadersValidator;
11+
import org.mule.validation.attributes.QueryParameterValidator;
12+
import org.mule.validation.attributes.QueryParamsDefaultsHandler;
13+
import org.mule.validation.attributes.QueryStringValidator;
14+
import org.mule.validation.attributes.UriParametersValidator;
15+
import org.mule.validation.body.BodyValidator;
16+
import org.mule.validation.body.BodyValidatorFactory;
17+
import com.google.common.collect.ImmutableList;
18+
import org.mule.validation.config.Configuration;
19+
import org.mule.validation.exception.BadRequestException;
20+
import org.mule.validation.exception.NotAcceptableException;
21+
import org.mule.validation.exception.UnsupportedMediaTypeException;
22+
import java.util.List;
23+
import org.mule.apikit.model.Action;
24+
import org.mule.extension.http.api.HttpRequestAttributes;
25+
import org.mule.runtime.api.metadata.TypedValue;
26+
27+
public class ActionValidator {
28+
// TODO : cache org.mule.validation.body validator
29+
private final List<AttributesValidator> attributesValidators;
30+
private final List<DefaultsHandler> defaultsHandlers;
31+
private final BodyValidatorFactory bodyValidatorFactory;
32+
33+
public ActionValidator(Action action, Configuration config) {
34+
this.attributesValidators = initialiseValidators(action, config);
35+
this.defaultsHandlers = initialiseDefaultsHandlers(action);
36+
this.bodyValidatorFactory = new BodyValidatorFactory(action, config.getApiSpecification(), config.getExpressionManager());
37+
}
38+
39+
public HttpRequestAttributes validateAttributes(HttpRequestAttributes attributes)
40+
throws BadRequestException, NotAcceptableException {
41+
42+
HttpRequestAttributes withDefaults = attributes;
43+
44+
for (DefaultsHandler defaultsHandler : defaultsHandlers) {
45+
withDefaults = defaultsHandler.addDefaultValues(withDefaults);
46+
}
47+
48+
for (AttributesValidator attributesValidator : attributesValidators) {
49+
attributesValidator.validate(withDefaults);
50+
}
51+
return withDefaults;
52+
}
53+
54+
public TypedValue validateBody(String requestMimeType, String charset, TypedValue payload)
55+
throws UnsupportedMediaTypeException, BadRequestException {
56+
BodyValidator bodyValidator = bodyValidatorFactory.resolveValidator(requestMimeType);
57+
return bodyValidator.validate(charset, payload);
58+
}
59+
60+
private List<AttributesValidator> initialiseValidators(Action action, Configuration configuration) {
61+
return ImmutableList.of(
62+
new UriParametersValidator(action),
63+
new HeadersValidator(action, configuration.isHeadersStrictValidation()),
64+
new QueryStringValidator(action),
65+
new QueryParameterValidator(action, configuration.isQueryParamsStrictValidation()));
66+
}
67+
68+
private List<DefaultsHandler> initialiseDefaultsHandlers(Action action) {
69+
return ImmutableList.of(
70+
new QueryParamsDefaultsHandler(action),
71+
new HeadersDefaultsHandler(action));
72+
}
73+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
3+
* The software in this package is published under the terms of the CPAL v1.0
4+
* license, a copy of which has been included with this distribution in the
5+
* LICENSE.txt file.
6+
*/
7+
package org.mule.validation;
8+
9+
import static org.mule.validation.helpers.AttributesHelper.getMediaType;
10+
import static org.mule.validation.helpers.CharsetUtils.getCharset;
11+
import static org.mule.validation.helpers.UrlUtils.getRequestPath;
12+
13+
import org.mule.validation.config.Configuration;
14+
import org.mule.validation.exception.BadRequestException;
15+
import org.mule.validation.exception.NotAcceptableException;
16+
import org.mule.validation.exception.ResourceNotFoundException;
17+
import org.mule.validation.exception.UnsupportedMediaTypeException;
18+
import java.util.Map;
19+
import javafx.util.Pair;
20+
import org.apache.commons.collections.MapUtils;
21+
import org.mule.apikit.model.Action;
22+
import org.mule.extension.http.api.HttpRequestAttributes;
23+
import org.mule.extension.http.api.HttpRequestAttributesBuilder;
24+
import org.mule.runtime.api.metadata.TypedValue;
25+
26+
public class RequestValidator {
27+
// TODO : cache actions validators
28+
private final Configuration config;
29+
private final ResourceFinder resourceFinder;
30+
31+
public RequestValidator(Configuration config) {
32+
this.config = config;
33+
this.resourceFinder = new ResourceFinder(config.getApiSpecification());
34+
}
35+
36+
public ValidRequest validateRequest(HttpRequestAttributes attributes, TypedValue payload)
37+
throws ResourceNotFoundException, BadRequestException, NotAcceptableException, UnsupportedMediaTypeException {
38+
39+
String path = getRequestPath(attributes);
40+
41+
Pair<Action, Map<String, String>> pair = resourceFinder.findAction(attributes.getMethod().toLowerCase(), path);
42+
43+
HttpRequestAttributes attributesWithUriParams = addUriParams(attributes, pair.getValue());
44+
45+
ActionValidator actionValidator = new ActionValidator(pair.getKey(), config);
46+
47+
HttpRequestAttributes validatedAttributes = actionValidator.validateAttributes(attributesWithUriParams);
48+
49+
if (pair.getKey().hasBody()) {
50+
payload = actionValidator
51+
.validateBody(getMediaType(attributes), getCharset(attributes.getHeaders(), payload),
52+
payload);
53+
}
54+
55+
return new ValidRequest(validatedAttributes, payload);
56+
}
57+
58+
59+
private HttpRequestAttributes addUriParams(HttpRequestAttributes attributes, Map<String, String> uriParams) {
60+
if (MapUtils.isEmpty(uriParams)) {
61+
return attributes;
62+
}
63+
return new HttpRequestAttributesBuilder(attributes).uriParams(uriParams).build();
64+
}
65+
66+
}

0 commit comments

Comments
 (0)