2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.common.endpoints.http.server.internal;
24 import java.util.HashMap;
26 import lombok.ToString;
27 import org.apache.commons.lang3.StringUtils;
28 import org.eclipse.jetty.servlet.DefaultServlet;
29 import org.eclipse.jetty.servlet.ServletHolder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Jetty Server that uses DefaultServlets to support web static resources management.
37 public class JettyStaticResourceServer extends JettyServletServer {
40 * Servlet Holder Resource Base Path.
42 protected static final String SERVLET_HOLDER_RESOURCE_BASE = "resourceBase";
45 * Servlet Holder Directory Allowed.
47 protected static final String SERVLET_HOLDER_DIR_ALLOWED = "dirAllowed";
50 * Servlet Holder Path Information Only.
52 protected static final String SERVLET_HOLDER_PATH_INFO_ONLY = "pathInfoOnly";
57 protected static Logger logger = LoggerFactory.getLogger(JettyStaticResourceServer.class);
60 * Container for default servlets.
62 protected final Map<String, ServletHolder> servlets = new HashMap<>();
68 * @param https enable https?
69 * @param host host server host
70 * @param port port server port
71 * @param contextPath context path
73 * @throws IllegalArgumentException in invalid arguments are provided
75 public JettyStaticResourceServer(String name, boolean https, String host, int port, String contextPath) {
77 super(name, https, host, port, contextPath);
81 * Retrieves cached default servlet based on servlet path.
83 * @param servletPath servlet path
84 * @return the jetty servlet holder
86 * @throws IllegalArgumentException if invalid arguments are provided
88 protected synchronized ServletHolder getDefaultServlet(String servletPath) {
90 return servlets.computeIfAbsent(servletPath, key -> context.addServlet(DefaultServlet.class, servletPath));
94 public synchronized void addServletResource(String servletPath, String resoureBase) {
96 if (StringUtils.isBlank(resoureBase)) {
97 throw new IllegalArgumentException("No resourceBase provided");
100 if (servletPath == null || servletPath.isEmpty()) {
104 ServletHolder defaultServlet = this.getDefaultServlet(servletPath);
106 defaultServlet.setInitParameter(SERVLET_HOLDER_RESOURCE_BASE, resoureBase);
107 defaultServlet.setInitParameter(SERVLET_HOLDER_DIR_ALLOWED, "false");
108 defaultServlet.setInitParameter(SERVLET_HOLDER_PATH_INFO_ONLY, "true");
110 if (logger.isDebugEnabled()) {
111 logger.debug("{}: added Default Servlet: {}", this, defaultServlet.dump());