70ac1417a63799c75ebe0a551296f820320f97b6
[policy/common.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.common.endpoints.http.server.internal;
23
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;
30
31 /**
32  * Jetty Server that uses DefaultServlets to support web static resources management.
33  */
34 @ToString
35 public class JettyStaticResourceServer extends JettyServletServer {
36
37     /**
38      * Servlet Holder Resource Base Path.
39      */
40     protected static final String SERVLET_HOLDER_RESOURCE_BASE = "resourceBase";
41
42     /**
43      * Servlet Holder Directory Allowed.
44      */
45     protected static final String SERVLET_HOLDER_DIR_ALLOWED = "dirAllowed";
46
47     /**
48      * Servlet Holder Path Information Only.
49      */
50     protected static final String SERVLET_HOLDER_PATH_INFO_ONLY = "pathInfoOnly";
51
52     /**
53      * Logger.
54      */
55     protected static Logger logger = LoggerFactory.getLogger(JettyStaticResourceServer.class);
56
57     /**
58      * Constructor.
59      *
60      * @param name name
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
67      */
68     public JettyStaticResourceServer(String name, boolean https, String host, int port, boolean sniHostCheck,
69         String contextPath) {
70
71         super(name, https, host, port, sniHostCheck, contextPath);
72     }
73
74     /**
75      * Retrieves cached default servlet based on servlet path.
76      *
77      * @param servletPath servlet path
78      * @return the jetty servlet holder
79      *
80      * @throws IllegalArgumentException if invalid arguments are provided
81      */
82     protected synchronized ServletHolder getDefaultServlet(String servletPath) {
83         return super.getServlet(DefaultServlet.class, servletPath);
84     }
85
86     @Override
87     public synchronized void addServletResource(String servletPath, String resourceBase) {
88
89         if (StringUtils.isBlank(resourceBase)) {
90             throw new IllegalArgumentException("No resourceBase provided");
91         }
92
93         if (servletPath == null || servletPath.isEmpty()) {
94             servletPath = "/*";
95         }
96
97         ServletHolder defaultServlet = this.getDefaultServlet(servletPath);
98
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");
102
103         if (logger.isDebugEnabled()) {
104             logger.debug("{}: added Default Servlet: {}", this, defaultServlet.dump());
105         }
106     }
107 }