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 CORS support for SE and MP applications to 2.x #1633

Merged
merged 105 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
5b54ef4
New module for CORS support in MP. Basic wiring of filters and new an…
spericas Oct 30, 2019
9eff239
Additional header support and tests.
spericas Oct 30, 2019
da2de21
Class annotations and checkstyle.
spericas Oct 30, 2019
406ecf9
Basic support for pre-flight requests. Using @CrossOrigin with @OPTIO…
spericas Oct 31, 2019
c545adf
Tests for request methods.
spericas Oct 31, 2019
ca98276
Support for other headers and more tests.
spericas Oct 31, 2019
7497b69
Test headers in pre-flight responses.
spericas Nov 4, 2019
2d4de2a
Restricted @CrossOrigin to methods only (OPTIONS really). Process oth…
spericas Nov 4, 2019
76f6e1c
Ensure comparisons use the correct case rules. Some changes to tests.
spericas Nov 5, 2019
0c4d0c6
Initial support for CORS using Config properties.
spericas Nov 6, 2019
539c092
Set priority on CrossOriginFilter.
spericas Nov 7, 2019
de3e8f5
Merge branch 'master' into cors
spericas Nov 8, 2019
9842f49
Merged with 2.0.0 master and update Cors module.
spericas Mar 25, 2020
87692d9
Merge branch 'master' into cors
spericas Apr 2, 2020
eb9d7ce
Initial commit
tjquinno Apr 2, 2020
d3db49e
A few more changes
tjquinno Apr 3, 2020
221617b
More changes, some bug fixes
tjquinno Apr 3, 2020
e96188b
Add the missing pom along with some other update
tjquinno Apr 3, 2020
d4e8be7
Clean up the pom; name the test class so it will actually run
tjquinno Apr 3, 2020
2dd1aaa
Rename custom matchers
tjquinno Apr 3, 2020
1edc14b
Commit module-info.java finally
tjquinno Apr 3, 2020
6279cdd
Instead of so many functions, etc., use two adapter interfaces for re…
tjquinno Apr 3, 2020
7347eb1
Rename some things, clean up unchecked warnings in test classes
tjquinno Apr 4, 2020
c042cb1
Minor method rename
tjquinno Apr 4, 2020
255c261
Adapt MP CORS code to layer on the new SE CORS artifact
tjquinno Apr 4, 2020
74fe362
Use some consistent naming for Optional variables; add type param and…
tjquinno Apr 5, 2020
0d2eb15
Incorporate changes to SE CORS component
tjquinno Apr 5, 2020
4f18f1c
Some more cleanup
tjquinno Apr 5, 2020
c904cb3
Adapt to fine-tuning of SE API
tjquinno Apr 5, 2020
8d88a54
Fix some javadoc
tjquinno Apr 5, 2020
d9f3d73
Some renaming
tjquinno Apr 5, 2020
4f6ae91
Adapt to SE renaming
tjquinno Apr 5, 2020
957ffb8
Refactor to simplify the contract exposed by the helper
tjquinno Apr 5, 2020
de016c3
Adapt to SE refactoring
tjquinno Apr 5, 2020
8c9fb2d
Fix some comments and javadocs
tjquinno Apr 5, 2020
752a1d2
Add cors to bom; minor edit to cors pom
tjquinno Apr 6, 2020
29aaed2
Remove explicit version for cors from pom; revise module-info so down…
tjquinno Apr 6, 2020
23a61f1
Fix tests so each app adds its own CORS service instance, since each …
tjquinno Apr 6, 2020
eee6c75
Clarify some comments, clean up one spot in the code.
tjquinno Apr 6, 2020
3c5456d
Preparing response does not need to return anything
tjquinno Apr 6, 2020
4add7c9
Clean up some doc comments
tjquinno Apr 6, 2020
f030f2b
Replace hard-coded constant
tjquinno Apr 6, 2020
2ed0560
Refactor CrossOrigin to the MP module and move associated constants f…
tjquinno Apr 6, 2020
70f7c58
Move CrossOrigin back to MP
tjquinno Apr 6, 2020
a80e5a2
Remove redundant def of ORIGIN; use Http.Header.ORIGIN instead
tjquinno Apr 6, 2020
4e362c8
Use Http.Header.ORIGIN; remove temp workaround in pom from pushed ver…
tjquinno Apr 6, 2020
e8f0b79
Convert collection of CrossOriginConfig to a map
tjquinno Apr 6, 2020
cf3e4cd
Convert collection of CrossOriginConfig to map
tjquinno Apr 6, 2020
03b1e83
Remove path from CrossOriginConfig
tjquinno Apr 6, 2020
af8ea3a
Remove path from CrossOriginConfig
tjquinno Apr 6, 2020
da27818
Add feature registration
tjquinno Apr 6, 2020
44569e2
Remove unneeded dep in pom; fix CrossOriginConfig addition to CORSSup…
tjquinno Apr 7, 2020
09e8130
Rename CrossOriginHelper to ...Internal; improve package-info
tjquinno Apr 7, 2020
51a0f3e
Adapt to change in API for CrossOriginConfig
tjquinno Apr 7, 2020
fe98eb2
Comment about replaceAll for headers in response adapter ok() method
tjquinno Apr 8, 2020
08cadce
A little clean-up
tjquinno Apr 8, 2020
dd66c59
At least for the moment, split handler and service implementations. M…
tjquinno Apr 8, 2020
affd2d6
Clean up the API a bit. More to come
tjquinno Apr 9, 2020
f6be1df
Refactoring, combining the separate service and handler into CORSSupp…
tjquinno Apr 9, 2020
3021fde
Adapt to refactoring of req and resp adapters
tjquinno Apr 9, 2020
ee61a31
Javadoc improvements
tjquinno Apr 9, 2020
297602a
Refactor SE CORS under webserver. Work on package-info JavaDoc a bit
tjquinno Apr 9, 2020
7055b7b
adjust to SE refactoring
tjquinno Apr 9, 2020
9ad4477
Concurrent changes
tjquinno Apr 9, 2020
15cccd7
Merge with changes in Santiago's fork.
tjquinno Apr 9, 2020
d80fdc1
Fix up fallout of refactoring SE cors to under webserver
tjquinno Apr 9, 2020
194a915
Refactor CrossOriginHelperInternal to .internal package and rename
tjquinno Apr 9, 2020
d8d6d44
Refactor CrossOriginHelperInternal to internal package and rename
tjquinno Apr 9, 2020
e103eca
Try to fix javadoc error in pipeline
tjquinno Apr 9, 2020
02b7ff7
Change config structure to add 'enabled' and put paths under a 'paths…
tjquinno Apr 10, 2020
c4586ac
CrossOriginHelper now instantiated, internalizing logic about whether…
tjquinno Apr 10, 2020
1061eb6
Simplify filter a bit; let the helper check whether processing is ena…
tjquinno Apr 10, 2020
508b2bd
Add logging; do a little refactoring
tjquinno Apr 10, 2020
71ca237
Some more refactoring; removing use of default config from SE impl be…
tjquinno Apr 11, 2020
a50bab1
Minor package-info edits
tjquinno Apr 11, 2020
4277f15
Add test case for .put, .any, etc. methods on Routing.Rules; delegate…
tjquinno Apr 12, 2020
c790abc
Create a PathMatcher and store it with each added CrossOriginConfig, …
tjquinno Apr 12, 2020
dd21f67
Remove internal subpackage; parameterize CORSSupport with types of re…
tjquinno Apr 12, 2020
e3f456d
Fix a couple javadoc issues
tjquinno Apr 12, 2020
7290845
Fix style check errors
tjquinno Apr 12, 2020
bfb94a3
Move adapters into internal package to highlight the intent to restri…
tjquinno Apr 12, 2020
bd26c10
Parameterize the CORSSupport.Builder so we can have a subclass of it …
tjquinno Apr 13, 2020
872c45f
Remove 'not for developer use' notes from classes that used to be pub…
tjquinno Apr 13, 2020
0baf337
Change overwriting behavior so the SE developer reigns. The order in …
tjquinno Apr 13, 2020
4b3405e
Fix javadoc
tjquinno Apr 13, 2020
e68db06
Clean up copyright notice
tjquinno Apr 13, 2020
4b5925f
The requires for java.ws.rs should not need to be transitive
tjquinno Apr 13, 2020
bf87039
Significant refactoring to reduce the proliferation of type variables…
tjquinno Apr 13, 2020
ecb7590
Refactory things j-u-s-t a bit.
tjquinno Apr 15, 2020
f98144a
Revise some JavaDoc; add convenience method for SE developers to popu…
tjquinno Apr 15, 2020
e0d148d
Clean up package-info
tjquinno Apr 15, 2020
1778c63
Review comment: use camel case everywhere, even with acronyms
tjquinno Apr 17, 2020
4238821
Review comment: method in package-local class was public; change to p…
tjquinno Apr 17, 2020
44f1dea
More camel-casing changes
tjquinno Apr 17, 2020
1d717cb
Review: add ContrainedTo(Runtime.SERVER) to CrossOriginAutoDiscoverable
tjquinno Apr 17, 2020
30a9260
Review: emphasize CrossOriginAutoDiscoverable is not for general use
tjquinno Apr 17, 2020
ebfdf32
Review: no public methods on package-private classes; those which imp…
tjquinno Apr 17, 2020
d309e99
Review: change Setter to CorsSetter
tjquinno Apr 17, 2020
4bfc33e
Fix typo
tjquinno Apr 17, 2020
e49a2d4
Review: Clean up the build and builder methods on CrossOriginConfig
tjquinno Apr 17, 2020
5ef7dc8
Change maxAge to maxAgeSeconds
tjquinno Apr 17, 2020
ca3f3bc
More clean-up of builders; MappedCrossOriginConfig this time
tjquinno Apr 17, 2020
9d4fb76
Improve toString in several places
tjquinno Apr 17, 2020
9dbd735
Slightly adjust how we do logging of CORS-related decision making
tjquinno Apr 17, 2020
536dd9c
Renaming to CorsSupport to ...Base and CorsSupportSe to CorsSupport; …
tjquinno Apr 17, 2020
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
16 changes: 16 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
<artifactId>helidon-webserver-tyrus</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-cors</artifactId>
<version>${helidon.version}</version>
</dependency>
<!-- Helidon Jersey -->
<dependency>
<groupId>io.helidon.jersey</groupId>
Expand Down Expand Up @@ -161,6 +166,11 @@
<artifactId>helidon-microprofile-grpc-client</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.microprofile</groupId>
<artifactId>helidon-microprofile-cors</artifactId>
<version>${helidon.version}</version>
</dependency>
<!-- media -->
<dependency>
<groupId>io.helidon.media</groupId>
Expand Down Expand Up @@ -801,6 +811,12 @@
<artifactId>helidon-microprofile-openapi</artifactId>
<version>${helidon.version}</version>
</dependency>
<!-- CORS support -->
<dependency>
<groupId>io.helidon.webserver.cors</groupId>
<artifactId>helidon-cors</artifactId>
<version>${helidon.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
4 changes: 4 additions & 0 deletions microprofile/bundles/helidon-microprofile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<groupId>io.helidon.microprofile.tracing</groupId>
<artifactId>helidon-microprofile-tracing</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile</groupId>
<artifactId>helidon-microprofile-cors</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
Expand Down
70 changes: 70 additions & 0 deletions microprofile/cors/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2019, 2020 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.microprofile</groupId>
<artifactId>helidon-microprofile-project</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>helidon-microprofile-cors</artifactId>
<name>Helidon Microprofile CORS</name>

<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.helidon.jersey</groupId>
<artifactId>helidon-jersey-common</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-jersey</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.config</groupId>
<artifactId>helidon-microprofile-config</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-cors</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.bundles</groupId>
<artifactId>internal-test-libs</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.server</groupId>
<artifactId>helidon-microprofile-server</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.helidon.microprofile.cors;

import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import io.helidon.webserver.cors.CorsSupportBase;
import io.helidon.webserver.cors.CrossOriginConfig;

/**
* MP implementation of {@link CorsSupportBase}.
*/
class CorsSupportMp extends CorsSupportBase {

/**
*
* @return a new builder of CorsSupportMp
*/
static Builder builder() {
return new Builder();
}

private CorsSupportMp(Builder builder) {
super(builder);
}

/**
* <em>Not for developer use.</em> Submits a request adapter and response adapter for CORS processing.
*
* @param requestAdapter wrapper around the request
* @param responseAdapter wrapper around the response
* @return Optional of the response type U; present if the response should be returned, empty if request processing should
* continue
*/
@Override
protected <T, U> Optional<U> processRequest(RequestAdapter<T> requestAdapter,
ResponseAdapter<U> responseAdapter) {
return super.processRequest(requestAdapter, responseAdapter);
}

/**
* <em>Not for developer user.</em> Gets a response ready to participate in the CORS protocol.
*
* @param requestAdapter wrapper around the request
* @param responseAdapter wrapper around the response
*/
@Override
protected <T, U> void prepareResponse(RequestAdapter<T> requestAdapter, ResponseAdapter<U> responseAdapter) {
super.prepareResponse(requestAdapter, responseAdapter);
}

static class Builder extends CorsSupportBase.Builder<CorsSupportMp, Builder> {

@Override
public CorsSupportMp build() {
return new CorsSupportMp(this);
}

@Override
protected Builder me() {
return this;
}

@Override
protected Builder secondaryLookupSupplier(
Supplier<Optional<CrossOriginConfig>> secondaryLookupSupplier) {
super.secondaryLookupSupplier(secondaryLookupSupplier);
return this;
}
}

static class RequestAdapterMp implements RequestAdapter<ContainerRequestContext> {

private final ContainerRequestContext requestContext;

RequestAdapterMp(ContainerRequestContext requestContext) {
this.requestContext = requestContext;
}

@Override
public String path() {
return requestContext.getUriInfo().getPath();
}

@Override
public Optional<String> firstHeader(String s) {
return Optional.ofNullable(requestContext.getHeaders().getFirst(s));
}

@Override
public boolean headerContainsKey(String s) {
return requestContext.getHeaders().containsKey(s);
}

@Override
public List<String> allHeaders(String s) {
return requestContext.getHeaders().get(s);
}

@Override
public String method() {
return requestContext.getMethod();
}

@Override
public ContainerRequestContext request() {
return requestContext;
}

@Override
public void next() {
}

@Override
public String toString() {
return String.format("RequestAdapterMp{path=%s, method=%s, headers=%s}", path(), method(),
requestContext.getHeaders());
}
}

static class ResponseAdapterMp implements ResponseAdapter<Response> {

private final MultivaluedMap<String, Object> headers;

ResponseAdapterMp(ContainerResponseContext responseContext) {
headers = responseContext.getHeaders();
}

ResponseAdapterMp() {
headers = new MultivaluedHashMap<>();
}

@Override
public ResponseAdapter<Response> header(String key, String value) {
headers.add(key, value);
return this;
}

@Override
public ResponseAdapter<Response> header(String key, Object value) {
headers.add(key, value);
return this;
}

@Override
public Response forbidden(String message) {
return Response.status(Response.Status.FORBIDDEN).entity(message).build();
}

@Override
public Response ok() {
Response.ResponseBuilder builder = Response.ok();
/*
* The Helidon CORS support code invokes ok() only for creating a CORS preflight response. In these cases no user
* code will have a chance to set headers in the response. That means we can use replaceAll here because the only
* headers needed in the response are the ones set using this adapter.
*/
builder.replaceAll(headers);
return builder.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.microprofile.cors;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static io.helidon.webserver.cors.CrossOriginConfig.DEFAULT_AGE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* CrossOrigin annotation.
*/
@Target(METHOD)
@Retention(RUNTIME)
@Documented
public @interface CrossOrigin {

/**
* A list of origins that are allowed such as {@code "http://foo.com"} or
* {@code "*"} to allow all origins. Corresponds to header {@code
* Access-Control-Allow-Origin}.
*
* @return Allowed origins.
*/
String[] value() default {"*"};

/**
* A list of request headers that are allowed or {@code "*"} to allow all headers.
* Corresponds to {@code Access-Control-Allow-Headers}.
*
* @return Allowed headers.
*/
String[] allowHeaders() default {"*"};

/**
* A list of response headers allowed for clients other than the "standard"
* ones. Corresponds to {@code Access-Control-Expose-Headers}.
*
* @return Exposed headers.
*/
String[] exposeHeaders() default {};

/**
* A list of supported HTTP request methods. In response to pre-flight
* requests. Corresponds to {@code Access-Control-Allow-Methods}.
*
* @return Allowed methods.
*/
String[] allowMethods() default {"*"};

/**
* Whether the client can send cookies or credentials. Corresponds to {@code
* Access-Control-Allow-Credentials}.
*
* @return Allowed credentials.
*/
boolean allowCredentials() default false;

/**
* Pre-flight response duration in seconds. After time expires, a new pre-flight
* request is required. Corresponds to {@code Access-Control-Max-Age}.
*
* @return Max age.
*/
long maxAge() default DEFAULT_AGE;
tjquinno marked this conversation as resolved.
Show resolved Hide resolved
}
Loading