2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020,2023 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 lombok.ToString;
25 import org.apache.commons.lang3.StringUtils;
26 import org.eclipse.jetty.servlet.DefaultServlet;
27 import org.eclipse.jetty.servlet.ServletHolder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Jetty Server that uses DefaultServlets to support web static resources management.
35 public class JettyStaticResourceServer extends JettyServletServer {
38 * Servlet Holder Resource Base Path.
40 protected static final String SERVLET_HOLDER_RESOURCE_BASE = "resourceBase";
43 * Servlet Holder Directory Allowed.
45 protected static final String SERVLET_HOLDER_DIR_ALLOWED = "dirAllowed";
48 * Servlet Holder Path Information Only.
50 protected static final String SERVLET_HOLDER_PATH_INFO_ONLY = "pathInfoOnly";
55 protected static Logger logger = LoggerFactory.getLogger(JettyStaticResourceServer.class);
61 * @param https enable https?
62 * @param host host server host
63 * @param port port server port
64 * @param sniHostCheck SNI Host checking flag
65 * @param contextPath context path
66 * @throws IllegalArgumentException in invalid arguments are provided
68 public JettyStaticResourceServer(String name, boolean https, String host, int port, boolean sniHostCheck,
71 super(name, https, host, port, sniHostCheck, contextPath);
75 * Retrieves cached default servlet based on servlet path.
77 * @param servletPath servlet path
78 * @return the jetty servlet holder
80 * @throws IllegalArgumentException if invalid arguments are provided
82 protected synchronized ServletHolder getDefaultServlet(String servletPath) {
83 return super.getServlet(DefaultServlet.class, servletPath);
87 public synchronized void addServletResource(String servletPath, String resourceBase) {
89 if (StringUtils.isBlank(resourceBase)) {
90 throw new IllegalArgumentException("No resourceBase provided");
93 if (servletPath == null || servletPath.isEmpty()) {
97 ServletHolder defaultServlet = this.getDefaultServlet(servletPath);
99 defaultServlet.setInitParameter(SERVLET_HOLDER_RESOURCE_BASE, resourceBase);
100 defaultServlet.setInitParameter(SERVLET_HOLDER_DIR_ALLOWED, "false");
101 defaultServlet.setInitParameter(SERVLET_HOLDER_PATH_INFO_ONLY, "true");
103 if (logger.isDebugEnabled()) {
104 logger.debug("{}: added Default Servlet: {}", this, defaultServlet.dump());