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

FISH-123 Add an option to configure the URL for MP OpenAPI endpoint #5323

Merged
merged 3 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ openAPI.configuration.securityEnabled=Enable Secure Access
openAPI.configuration.securityEnabledHelp=If checked, <code>/openapi</code> endpoint can be accessed only via HTTPS and disables the HTTP method with 403 HTTP response. And you will be required to log in before accessing the OpenAPI endpoint.
openAPI.configuration.roles=Roles:
openAPI.configuration.rolesHelp=Separate multiple roles with comma
openAPI.configuration.endpoint=EndPoint
openAPI.configuration.endpointHelp=The context root used to expose the OpenAPI.
openAPI.configuration.virtualServers=Virtual Servers:
openAPI.configuration.virtualServersHelp=Associates an Internet domain name with a physical server.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
helpText="$resource{i18n_microprofile.openAPI.configuration.rolesHelp}">
<sun:textField id="roles" maxLength="250" text="#{pageSession.valueMap['roles']}"/>
</sun:property>
<sun:property id="endPointProp" labelAlign="left" noWrap="#{true}" overlapLabel="#{false}"
label="$resource{i18n_microprofile.openAPI.configuration.endpoint}"
helpText="$resource{i18n_microprofile.openAPI.configuration.endpointHelp}">
<sun:textField id="endPoint" columns="$int{40}" maxLength="250"
text="#{pageSession.valueMap['endpoint']}"/>
</sun:property>
<sun:property id="virtualServersProp" labelAlign="left" noWrap="#{true}" overlapLabel="#{false}"
label="$resource{i18n_microprofile.openAPI.configuration.virtualServers}"
helpText="$resource{i18n_microprofile.openAPI.configuration.virtualServersHelp}" >
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018-2019] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2018-2021] 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
Expand Down Expand Up @@ -76,7 +76,7 @@
})
public class GetOpenApiConfigurationCommand implements AdminCommand {

private final String[] OUTPUT_HEADERS = {"Enabled", "VirtualServers", "CorsHeaders", "Security Enabled", "Roles"};
private final String[] OUTPUT_HEADERS = {"Enabled", "EndPoint", "VirtualServers", "CorsHeaders", "Security Enabled", "Roles"};

@Inject
private Target targetUtil;
Expand All @@ -101,6 +101,7 @@ public void execute(AdminCommandContext context) {
ColumnFormatter columnFormatter = new ColumnFormatter(OUTPUT_HEADERS);
Object[] outputValues = {
openApiConfig.getEnabled(),
openApiConfig.getEndpoint(),
openApiConfig.getVirtualServers(),
openApiConfig.getCorsHeaders(),
openApiConfig.getSecurityEnabled(),
Expand All @@ -112,6 +113,7 @@ public void execute(AdminCommandContext context) {

Map<String, Object> extraPropertiesMap = new HashMap<>();
extraPropertiesMap.put("enabled", openApiConfig.getEnabled());
extraPropertiesMap.put("endpoint", openApiConfig.getEndpoint());
extraPropertiesMap.put("virtualServers", openApiConfig.getVirtualServers());
extraPropertiesMap.put("corsHeaders", openApiConfig.getCorsHeaders());
extraPropertiesMap.put("securityenabled", openApiConfig.getSecurityEnabled());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018-2019] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2018-2021] 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
Expand Down Expand Up @@ -58,6 +58,14 @@ public interface OpenApiServiceConfiguration extends ConfigExtension {
String getEnabled();

void setEnabled(String value) throws PropertyVetoException;

/**
* @return a String value defines the endpoint of openapi service.
*/
@Attribute(defaultValue = "openapi")
String getEndpoint();

void setEndpoint(String value) throws PropertyVetoException;

/**
* Defines if CORS headers are set on the OpenApi response.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018-2019] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2018-2021] 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
Expand Down Expand Up @@ -85,6 +85,9 @@ public class SetOpenApiConfigurationCommand extends SetSecureMicroprofileConfigu
@Param(name = "enabled", optional = true)
private Boolean enabled;

@Param(name = "endpoint", optional = true)
private String endpoint;

@Param(name = "virtualServers", optional = true)
private String virtualServers;

Expand Down Expand Up @@ -126,6 +129,9 @@ public void execute(AdminCommandContext context) {
if (enabled != null) {
configProxy.setEnabled(Boolean.toString(enabled));
}
if (endpoint != null) {
configProxy.setEndpoint(endpoint);
}
if (virtualServers != null) {
configProxy.setVirtualServers(virtualServers);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2018-2021] 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
Expand Down Expand Up @@ -39,7 +39,7 @@
*/
package fish.payara.microprofile.openapi.impl.rest.app.provider;

import static fish.payara.microprofile.openapi.impl.rest.app.OpenApiApplication.APPLICATION_YAML;
import static fish.payara.microprofile.openapi.rest.app.OpenApiApplication.APPLICATION_YAML;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2018-2021] 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
Expand Down Expand Up @@ -39,7 +39,7 @@
*/
package fish.payara.microprofile.openapi.impl.rest.app.provider.writer;

import static fish.payara.microprofile.openapi.impl.rest.app.OpenApiApplication.APPLICATION_YAML;
import static fish.payara.microprofile.openapi.rest.app.OpenApiApplication.APPLICATION_YAML;
import fish.payara.microprofile.openapi.impl.rest.app.provider.ObjectMapperFactory;
import javax.ws.rs.Produces;
import javax.ws.rs.ext.Provider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018-2020] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2018-2021] 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
Expand Down Expand Up @@ -44,7 +44,7 @@
import fish.payara.microprofile.openapi.impl.model.OpenAPIImpl;
import fish.payara.microprofile.openapi.impl.processor.BaseProcessor;

import static fish.payara.microprofile.openapi.impl.rest.app.OpenApiApplication.APPLICATION_YAML;
import static fish.payara.microprofile.openapi.rest.app.OpenApiApplication.APPLICATION_YAML;
import java.io.IOException;
import static java.util.logging.Level.WARNING;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2018-2021] 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
Expand Down Expand Up @@ -40,8 +40,7 @@
package fish.payara.microprofile.openapi.impl.rest.init;

import fish.payara.microprofile.openapi.impl.admin.OpenApiServiceConfiguration;
import fish.payara.microprofile.openapi.impl.rest.app.OpenApiApplication;
import static fish.payara.microprofile.openapi.impl.rest.app.OpenApiApplication.OPEN_API_APPLICATION_PATH;
import fish.payara.microprofile.openapi.rest.app.OpenApiApplication;
import static java.util.Arrays.asList;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -55,6 +54,7 @@
import static javax.servlet.annotation.ServletSecurity.TransportGuarantee.CONFIDENTIAL;
import static org.glassfish.common.util.StringHelper.isEmpty;
import org.glassfish.internal.api.Globals;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer;

/**
Expand All @@ -70,16 +70,16 @@ public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletExcepti
if (!"".equals(ctx.getContextPath())) {
return;
}
OpenApiServiceConfiguration configuration = Globals.getDefaultHabitat().getService(OpenApiServiceConfiguration.class);

// Check if there is already an endpoint for OpenAPI
Map<String, ? extends ServletRegistration> registrations = ctx.getServletRegistrations();
for (ServletRegistration reg : registrations.values()) {
if (reg.getMappings().contains(OPEN_API_APPLICATION_PATH)) {
if (reg.getMappings().contains(configuration.getEndpoint())) {
return;
}
}

OpenApiServiceConfiguration configuration = Globals.getDefaultHabitat().getService(OpenApiServiceConfiguration.class);
String virtualServers = configuration.getVirtualServers();
if (!isEmpty(virtualServers)
&& !asList(virtualServers.split(",")).contains(ctx.getVirtualServerName())) {
Expand All @@ -89,7 +89,9 @@ public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletExcepti
// Start the OpenAPI application
new JerseyServletContainerInitializer().onStartup(new HashSet<>(asList(OpenApiApplication.class)), ctx);

ServletRegistration.Dynamic reg = (ServletRegistration.Dynamic) ctx.getServletRegistrations().get(OpenApiApplication.class.getName());
ServletContainer servletContainer = new ServletContainer(new OpenApiApplication());
ServletRegistration.Dynamic reg = ctx.addServlet("microprofile-openapi-servlet", servletContainer);
reg.addMapping("/" + configuration.getEndpoint() + "/*");
if (Boolean.parseBoolean(configuration.getSecurityEnabled())) {
String[] roles = configuration.getRoles().split(",");
reg.setServletSecurity(new ServletSecurityElement(new HttpConstraintElement(CONFIDENTIAL, roles)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018--2020] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2018-2021] 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
Expand Down Expand Up @@ -37,22 +37,18 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.microprofile.openapi.impl.rest.app;
package fish.payara.microprofile.openapi.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 fish.payara.microprofile.openapi.impl.rest.app.provider.QueryFormatFilter;
import fish.payara.microprofile.openapi.impl.rest.app.provider.writer.JsonWriter;
import fish.payara.microprofile.openapi.impl.rest.app.provider.writer.YamlWriter;
import fish.payara.microprofile.openapi.impl.rest.app.service.OpenApiResource;
import javax.ws.rs.ApplicationPath;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath(OPEN_API_APPLICATION_PATH)
public class OpenApiApplication extends ResourceConfig {

public static final String OPEN_API_APPLICATION_PATH = "/openapi";
public static final String APPLICATION_YAML = TEXT_PLAIN;

public OpenApiApplication() {
Expand Down