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