2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.common.endpoints.http.server.internal;
23 import java.util.HashMap;
25 import lombok.ToString;
26 import org.apache.commons.lang3.StringUtils;
27 import org.eclipse.jetty.servlet.DefaultServlet;
28 import org.eclipse.jetty.servlet.ServletHolder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Jetty Server that uses DefaultServlets to support web static resources management.
36 public class JettyStaticResourceServer extends JettyServletServer {
39 * Servlet Holder Resource Base Path.
41 protected static final String SERVLET_HOLDER_RESOURCE_BASE = "resourceBase";
44 * Servlet Holder Directory Allowed.
46 protected static final String SERVLET_HOLDER_DIR_ALLOWED = "dirAllowed";
49 * Servlet Holder Path Information Only.
51 protected static final String SERVLET_HOLDER_PATH_INFO_ONLY = "pathInfoOnly";
56 protected static Logger logger = LoggerFactory.getLogger(JettyStaticResourceServer.class);
59 * Container for default servlets.
61 protected final Map<String, ServletHolder> servlets = new HashMap<>();
67 * @param https enable https?
68 * @param host host server host
69 * @param port port server port
70 * @param contextPath context path
72 * @throws IllegalArgumentException in invalid arguments are provided
74 public JettyStaticResourceServer(String name, boolean https, String host, int port, String contextPath) {
76 super(name, https, host, port, contextPath);
80 * Retrieves cached default servlet based on servlet path.
82 * @param servletPath servlet path
83 * @return the jetty servlet holder
85 * @throws IllegalArgumentException if invalid arguments are provided
87 protected synchronized ServletHolder getDefaultServlet(String servPath) {
89 return servlets.computeIfAbsent(servPath, key -> context.addServlet(DefaultServlet.class, servPath));
93 public synchronized void addServletResource(String servletPath, String resoureBase) {
95 if (StringUtils.isBlank(resoureBase)) {
96 throw new IllegalArgumentException("No resourceBase provided");
99 if (servletPath == null || servletPath.isEmpty()) {
103 ServletHolder defaultServlet = this.getDefaultServlet(servletPath);
105 defaultServlet.setInitParameter(SERVLET_HOLDER_RESOURCE_BASE, resoureBase);
106 defaultServlet.setInitParameter(SERVLET_HOLDER_DIR_ALLOWED, "false");
107 defaultServlet.setInitParameter(SERVLET_HOLDER_PATH_INFO_ONLY, "true");
109 if (logger.isDebugEnabled()) {
110 logger.debug("{}: added Default Servlet: {}", this, defaultServlet.dump());