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