diff options
author | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2020-03-03 15:10:50 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2020-03-03 15:10:50 +0000 |
commit | 361e714960f444d9319dd54186af0e8efede4011 (patch) | |
tree | 0ec60bb548ff2894b6f043fbddeef728ac97b3d2 /policy-endpoints/src/main/java | |
parent | 95f16eb44ac773576836ed15bc65960f3e38e8a9 (diff) | |
parent | 2fda610a5b5c4b1b906eaf72405e225df19dbc02 (diff) |
Merge "Add JettyStaticResourceServer to policy-endpoints"
Diffstat (limited to 'policy-endpoints/src/main/java')
6 files changed, 191 insertions, 76 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java index 030bf93a..49dfea06 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServer.java @@ -102,7 +102,7 @@ public interface HttpServletServer extends Startable { void addServletPackage(String servletPath, String restPackage); /** - * Add org.eclipse.jetty.servlet.DefaultServlet into context + * Add a static resource path to manage static resources. * * @param servletPath servlet path * @param resourceBase static resources folder diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java index 0c30e3e5..90c0db2e 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/HttpServletServerFactory.java @@ -3,13 +3,14 @@ * policy-endpoints * ================================================================================ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * 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. @@ -29,8 +30,8 @@ import java.util.Properties; public interface HttpServletServerFactory { /** - * Builds an http or https server with support for servlets. - * + * Builds an http or https rest server with support for servlets. + * * @param name name * @param https use secured http over tls connection * @param host binding host @@ -45,7 +46,7 @@ public interface HttpServletServerFactory { boolean managed); /** - * Builds an http server with support for servlets. + * Builds an http rest server with support for servlets. * * @param name name * @param host binding host @@ -59,8 +60,8 @@ public interface HttpServletServerFactory { HttpServletServer build(String name, String host, int port, String contextPath, boolean swagger, boolean managed); /** - * Build a list of http servers per properties. - * + * Build a list of http rest servers per properties. + * * @param properties properties based configuration * @return list of http servers * @throws IllegalArgumentException when invalid parameters are provided @@ -68,8 +69,23 @@ public interface HttpServletServerFactory { List<HttpServletServer> build(Properties properties); /** + * Builds an http or https server to manage static resources. + * + * @param name name + * @param https use secured http over tls connection + * @param host binding host + * @param port port + * @param contextPath server base path + * @param managed is it managed by infrastructure + * @return http server + * @throws IllegalArgumentException when invalid parameters are provided + */ + HttpServletServer buildStaticResourceServer(String name, boolean https, String host, int port, String contextPath, + boolean managed); + + /** * Gets a server based on the port. - * + * * @param port port * @return http server */ @@ -77,14 +93,14 @@ public interface HttpServletServerFactory { /** * Provides an inventory of servers. - * + * * @return inventory of servers */ List<HttpServletServer> inventory(); /** * Destroys server bound to a port. - * + * * @param port the port the server is bound to */ void destroy(int port); diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java index 517ad208..445a3515 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/IndexedHttpServletServerFactory.java @@ -3,6 +3,7 @@ * ONAP Policy Engine - Common Modules * ================================================================================ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +27,7 @@ import java.util.List; import java.util.Properties; import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.endpoints.http.server.internal.JettyJerseyServer; +import org.onap.policy.common.endpoints.http.server.internal.JettyStaticResourceServer; import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.onap.policy.common.endpoints.utils.PropertyUtils; import org.slf4j.Logger; @@ -88,6 +90,23 @@ class IndexedHttpServletServerFactory implements HttpServletServerFactory { return serviceList; } + + + @Override + public HttpServletServer buildStaticResourceServer(String name, boolean https, String host, int port, + String contextPath, boolean managed) { + if (servers.containsKey(port)) { + return servers.get(port); + } + + JettyStaticResourceServer server = new JettyStaticResourceServer(name, https, host, port, contextPath); + if (managed) { + servers.put(port, server); + } + + return server; + } + private void addService(ArrayList<HttpServletServer> serviceList, String serviceName, Properties properties) { String servicePrefix = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + serviceName; diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java index 462f0790..fb43ce4c 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyJerseyServer.java @@ -23,8 +23,8 @@ package org.onap.policy.common.endpoints.http.server.internal; import io.swagger.jersey.config.JerseyJaxrsConfig; import java.util.HashMap; +import java.util.Map; import org.apache.commons.lang3.StringUtils; -import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.ServletHolder; import org.glassfish.jersey.server.ServerProperties; import org.onap.policy.common.utils.network.NetworkUtil; @@ -74,34 +74,14 @@ public class JettyJerseyServer extends JettyServletServer { "io.swagger.jaxrs.listing.ApiListingResource," + "io.swagger.jaxrs.listing.SwaggerSerializers"; /** - * Servlet Holder Resource Base Path. - */ - protected static final String SERVLET_HOLDER_RESOURCE_BASE = "resourceBase"; - - /** - * Servlet Holder Directory Allowed. - */ - protected static final String SERVLET_HOLDER_DIR_ALLOWED = "dirAllowed"; - - /** - * Servlet Holder Path Information Only. - */ - protected static final String SERVLET_HOLDER_PATH_INFO_ONLY = "pathInfoOnly"; - - /** * Logger. */ protected static Logger logger = LoggerFactory.getLogger(JettyJerseyServer.class); /** - * Container for jersey servlets. + * Container for servlets. */ - protected HashMap<String, ServletHolder> jerseyServlets = new HashMap<>(); - - /** - * Container for default servlets. - */ - protected HashMap<String, ServletHolder> defaultServlets = new HashMap<>(); + protected final Map<String, ServletHolder> servlets = new HashMap<>(); /** * Swagger ID. @@ -166,9 +146,9 @@ public class JettyJerseyServer extends JettyServletServer { * * @throws IllegalArgumentException if invalid arguments are provided */ - protected synchronized ServletHolder getJerseyServlet(String servletPath) { + protected synchronized ServletHolder getServlet(String servletPath) { - return jerseyServlets.computeIfAbsent(servletPath, key -> { + return servlets.computeIfAbsent(servletPath, key -> { ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, servletPath); @@ -178,19 +158,6 @@ public class JettyJerseyServer extends JettyServletServer { }); } - /** - * Retrieves cached default servlet based on servlet path. - * - * @param servletPath servlet path - * @return the jetty servlet holder - * - * @throws IllegalArgumentException if invalid arguments are provided - */ - protected synchronized ServletHolder getDefaultServlet(String servPath) { - - return defaultServlets.computeIfAbsent(servPath, key -> context.addServlet(DefaultServlet.class, servPath)); - } - @Override public synchronized void addServletPackage(String servletPath, String restPackage) { String servPath = servletPath; @@ -202,7 +169,7 @@ public class JettyJerseyServer extends JettyServletServer { servPath = "/*"; } - ServletHolder jerseyServlet = this.getJerseyServlet(servPath); + ServletHolder jerseyServlet = this.getServlet(servPath); initStandardParams(jerseyServlet); @@ -232,7 +199,7 @@ public class JettyJerseyServer extends JettyServletServer { servletPath = "/*"; } - ServletHolder jerseyServlet = this.getJerseyServlet(servletPath); + ServletHolder jerseyServlet = this.getServlet(servletPath); initStandardParams(jerseyServlet); @@ -251,28 +218,6 @@ public class JettyJerseyServer extends JettyServletServer { } } - @Override - public synchronized void addServletResource(String servletPath, String resoureBase) { - - if (StringUtils.isBlank(resoureBase)) { - throw new IllegalArgumentException("No resourceBase provided"); - } - - if (servletPath == null || servletPath.isEmpty()) { - servletPath = "/*"; - } - - ServletHolder defaultServlet = this.getDefaultServlet(servletPath); - - defaultServlet.setInitParameter(SERVLET_HOLDER_RESOURCE_BASE, resoureBase); - defaultServlet.setInitParameter(SERVLET_HOLDER_DIR_ALLOWED, "false"); - defaultServlet.setInitParameter(SERVLET_HOLDER_PATH_INFO_ONLY, "true"); - - if (logger.isDebugEnabled()) { - logger.debug("{}: added REST class: {}", this, defaultServlet.dump()); - } - } - /** * Adds "standard" parameters to the initParameter set. Sets swagger parameters, if specified, and sets the class * provider property. This can be invoked multiple times, but only the first actually causes any changes to the @@ -312,9 +257,8 @@ public class JettyJerseyServer extends JettyServletServer { @Override public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("JettyJerseyServer [Jerseyservlets=").append(jerseyServlets).append(", Defaultservlets=") - .append(defaultServlets).append(", swaggerId=").append(swaggerId).append(", toString()=") - .append(super.toString()).append("]"); + builder.append("JettyJerseyServer [Jerseyservlets=").append(servlets).append(", swaggerId=").append(swaggerId) + .append(", toString()=").append(super.toString()).append("]"); return builder.toString(); } } diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java index 133adade..f7f2fc15 100644 --- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,6 +65,8 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable */ private static Logger logger = LoggerFactory.getLogger(JettyServletServer.class); + private static final String NOT_SUPPORTED = " is not supported on this type of jetty server"; + /** * Server name. */ @@ -495,6 +498,26 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable } @Override + public void setSerializationProvider(String provider) { + throw new UnsupportedOperationException("setSerializationProvider()" + NOT_SUPPORTED); + } + + @Override + public void addServletClass(String servletPath, String restClass) { + throw new UnsupportedOperationException("addServletClass()" + NOT_SUPPORTED); + } + + @Override + public void addServletPackage(String servletPath, String restPackage) { + throw new UnsupportedOperationException("addServletPackage()" + NOT_SUPPORTED); + } + + @Override + public void addServletResource(String servletPath, String resourceBase) { + throw new UnsupportedOperationException("addServletResource()" + NOT_SUPPORTED); + } + + @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("JettyServer [name=").append(name).append(", host=").append(host).append(", port=").append(port) diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyStaticResourceServer.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyStaticResourceServer.java new file mode 100644 index 00000000..15d9990b --- /dev/null +++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyStaticResourceServer.java @@ -0,0 +1,113 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.common.endpoints.http.server.internal; + +import java.util.HashMap; +import java.util.Map; +import lombok.ToString; +import org.apache.commons.lang3.StringUtils; +import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ServletHolder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Jetty Server that uses DefaultServlets to support web static resources management. + */ +@ToString +public class JettyStaticResourceServer extends JettyServletServer { + + /** + * Servlet Holder Resource Base Path. + */ + protected static final String SERVLET_HOLDER_RESOURCE_BASE = "resourceBase"; + + /** + * Servlet Holder Directory Allowed. + */ + protected static final String SERVLET_HOLDER_DIR_ALLOWED = "dirAllowed"; + + /** + * Servlet Holder Path Information Only. + */ + protected static final String SERVLET_HOLDER_PATH_INFO_ONLY = "pathInfoOnly"; + + /** + * Logger. + */ + protected static Logger logger = LoggerFactory.getLogger(JettyStaticResourceServer.class); + + /** + * Container for default servlets. + */ + protected final Map<String, ServletHolder> servlets = new HashMap<>(); + + /** + * Constructor. + * + * @param name name + * @param https enable https? + * @param host host server host + * @param port port server port + * @param contextPath context path + * + * @throws IllegalArgumentException in invalid arguments are provided + */ + public JettyStaticResourceServer(String name, boolean https, String host, int port, String contextPath) { + + super(name, https, host, port, contextPath); + } + + /** + * Retrieves cached default servlet based on servlet path. + * + * @param servletPath servlet path + * @return the jetty servlet holder + * + * @throws IllegalArgumentException if invalid arguments are provided + */ + protected synchronized ServletHolder getDefaultServlet(String servPath) { + + return servlets.computeIfAbsent(servPath, key -> context.addServlet(DefaultServlet.class, servPath)); + } + + @Override + public synchronized void addServletResource(String servletPath, String resoureBase) { + + if (StringUtils.isBlank(resoureBase)) { + throw new IllegalArgumentException("No resourceBase provided"); + } + + if (servletPath == null || servletPath.isEmpty()) { + servletPath = "/*"; + } + + ServletHolder defaultServlet = this.getDefaultServlet(servletPath); + + defaultServlet.setInitParameter(SERVLET_HOLDER_RESOURCE_BASE, resoureBase); + defaultServlet.setInitParameter(SERVLET_HOLDER_DIR_ALLOWED, "false"); + defaultServlet.setInitParameter(SERVLET_HOLDER_PATH_INFO_ONLY, "true"); + + if (logger.isDebugEnabled()) { + logger.debug("{}: added Default Servlet: {}", this, defaultServlet.dump()); + } + } +} |