Skip to content

Commit

Permalink
[payara#3787] Added container response filter for CORS headers to Ope…
Browse files Browse the repository at this point in the history
…nAPI application.
  • Loading branch information
lreimer committed Mar 26, 2019
1 parent 90b0a61 commit 4b445e0
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ public boolean isEnabled() {
return Boolean.parseBoolean(config.getEnabled());
}

public boolean withCorsHeaders() {
return Boolean.parseBoolean(config.getCorsHeaders());
}

/**
* Listen for OpenAPI config changes.
*/
Expand Down Expand Up @@ -177,8 +181,8 @@ public void event(Event<?> event) {
}

/**
* @return the document for the most recently deployed application. Creates one
* if it hasn't already been created.
* @return the document for the most recently deployed application. Creates
* one if it hasn't already been created.
* @throws OpenAPIBuildException if creating the document failed.
*/
public OpenAPI getDocument() throws OpenAPIBuildException {
Expand All @@ -201,9 +205,9 @@ public static OpenApiService getInstance() {
*/
private static boolean isValidApp(ApplicationInfo appInfo) {
return appInfo.getMetaData(WebBundleDescriptorImpl.class) != null
&& !appInfo.getSource().getURI().getPath().contains("glassfish/lib/install")
&& !appInfo.getSource().getURI().getPath().contains("javadb/lib")
&& !appInfo.getSource().getURI().getPath().contains("mq/lib");
&& !appInfo.getSource().getURI().getPath().contains("glassfish/lib/install")
&& !appInfo.getSource().getURI().getPath().contains("javadb/lib")
&& !appInfo.getSource().getURI().getPath().contains("mq/lib");
}

/**
Expand All @@ -215,7 +219,7 @@ private static String getContextRoot(ApplicationInfo appInfo) {
}

/**
* @param archive the archive to read from.
* @param archive the archive to read from.
* @param appClassLoader the classloader to use to load the classes.
* @return a list of all loadable classes in the archive.
*/
Expand Down Expand Up @@ -319,27 +323,27 @@ private List<URL> getServerURL(String contextRoot) {
.filter(networkListener -> Boolean.parseBoolean(networkListener.getEnabled()))
.forEach(networkListener -> {

int port;
try {
// get the dynamic config port
port = habitat.getService(GrizzlyService.class).getRealPort(networkListener);
} catch (MultiException ex) {
LOGGER.log(WARNING, "Failed to get running Grizzly listener.", ex);
// get the port in the domain xml
port = Integer.parseInt(networkListener.getPort());
}
int port;
try {
// get the dynamic config port
port = habitat.getService(GrizzlyService.class).getRealPort(networkListener);
} catch (MultiException ex) {
LOGGER.log(WARNING, "Failed to get running Grizzly listener.", ex);
// get the port in the domain xml
port = Integer.parseInt(networkListener.getPort());
}

// Check if this listener is using HTTP or HTTPS
boolean securityEnabled = Boolean.parseBoolean(networkListener.findProtocol().getSecurityEnabled());
List<Integer> ports = securityEnabled ? httpPorts : httpsPorts;
// Check if this listener is using HTTP or HTTPS
boolean securityEnabled = Boolean.parseBoolean(networkListener.findProtocol().getSecurityEnabled());
List<Integer> ports = securityEnabled ? httpPorts : httpsPorts;

// If this listener isn't the admin listener, it must be an HTTP/HTTPS listener
if (!networkListener.getName().equals(adminListener)) {
ports.add(port);
} else if (instanceType.equals("MICRO")) {
// micro instances can use the admin listener as both an admin and HTTP/HTTPS port
ports.add(port);
}
// If this listener isn't the admin listener, it must be an HTTP/HTTPS listener
if (!networkListener.getName().equals(adminListener)) {
ports.add(port);
} else if (instanceType.equals("MICRO")) {
// micro instances can use the admin listener as both an admin and HTTP/HTTPS port
ports.add(port);
}
});

for (Integer httpPort : httpPorts) {
Expand All @@ -359,4 +363,4 @@ private List<URL> getServerURL(String contextRoot) {
return result;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
package fish.payara.microprofile.openapi.impl.rest.app;

import static fish.payara.microprofile.openapi.impl.rest.app.OpenApiApplication.OPEN_API_APPLICATION_PATH;
import fish.payara.microprofile.openapi.impl.rest.app.provider.CorsHeadersFilter;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;

import javax.ws.rs.ApplicationPath;
Expand All @@ -60,6 +61,7 @@ public class OpenApiApplication extends ResourceConfig {
public OpenApiApplication() {
register(OpenApiResource.class);
register(QueryFormatFilter.class);
register(CorsHeadersFilter.class);
register(YamlWriter.class);
register(JsonWriter.class);
property("payara-internal", "true");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.microprofile.openapi.impl.rest.app.provider;

import fish.payara.microprofile.openapi.impl.OpenApiService;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

/**
* A respone filter to add CORS deaders to the OpenAPI response.
*/
@Provider
public class CorsHeadersFilter implements ContainerResponseFilter {

@Override
public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException {
OpenApiService openApiService = OpenApiService.getInstance();
if (openApiService.isEnabled() && openApiService.withCorsHeaders()) {
MultivaluedMap<String, Object> headers = response.getHeaders();

headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
headers.add("Access-Control-Allow-Credentials", "true");
headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
}
}

}

0 comments on commit 4b445e0

Please sign in to comment.