c335247be15b27246131421f626d700d70b736ae
[policy/common.git] /
1 /*-
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
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 contextPath context path
65      *
66      * @throws IllegalArgumentException in invalid arguments are provided
67      */
68     public JettyStaticResourceServer(String name, boolean https, String host, int port, String contextPath) {
69
70         super(name, https, host, port, contextPath);
71     }
72
73     /**
74      * Retrieves cached default servlet based on servlet path.
75      *
76      * @param servletPath servlet path
77      * @return the jetty servlet holder
78      *
79      * @throws IllegalArgumentException if invalid arguments are provided
80      */
81     protected synchronized ServletHolder getDefaultServlet(String servletPath) {
82         return super.getServlet(DefaultServlet.class, servletPath);
83     }
84
85     @Override
86     public synchronized void addServletResource(String servletPath, String resourceBase) {
87
88         if (StringUtils.isBlank(resourceBase)) {
89             throw new IllegalArgumentException("No resourceBase provided");
90         }
91
92         if (servletPath == null || servletPath.isEmpty()) {
93             servletPath = "/*";
94         }
95
96         ServletHolder defaultServlet = this.getDefaultServlet(servletPath);
97
98         defaultServlet.setInitParameter(SERVLET_HOLDER_RESOURCE_BASE, resourceBase);
99         defaultServlet.setInitParameter(SERVLET_HOLDER_DIR_ALLOWED, "false");
100         defaultServlet.setInitParameter(SERVLET_HOLDER_PATH_INFO_ONLY, "true");
101
102         if (logger.isDebugEnabled()) {
103             logger.debug("{}: added Default Servlet: {}", this, defaultServlet.dump());
104         }
105     }
106 }