CSIT Fix for SDC-2585
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / servlets / KibanaServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.openecomp.sdc.fe.servlets;
24
25 import org.eclipse.jetty.proxy.ProxyServlet;
26 import org.openecomp.sdc.common.api.Constants;
27 import org.openecomp.sdc.fe.config.Configuration;
28 import org.openecomp.sdc.fe.config.ConfigurationManager;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import javax.servlet.http.HttpServletRequest;
33
34 public class KibanaServlet extends ProxyServlet {
35
36     private static final long serialVersionUID = 1L;
37     private static final Logger LOGGER = LoggerFactory.getLogger(KibanaServlet.class.getName());
38
39     @Override
40     public String rewriteTarget(HttpServletRequest request) {
41         String originalUrl = request.getRequestURI();
42         String redirectedUrl = getModifiedUrl(request);
43
44         LOGGER.debug("KibanaServlet Redirecting request from: {} , to: {}", originalUrl, redirectedUrl);
45
46         return redirectedUrl;
47     }
48
49     public String getModifiedUrl(HttpServletRequest request) {
50         Configuration config = getConfiguration(request);
51
52         if (config == null) {
53             LOGGER.error("Failed to retrieve configuration.");
54             throw new NullPointerException("Failed to retrieve configuration.");
55         }
56
57         String contextPath = request.getContextPath();
58         String servletPath = request.getServletPath();
59         String pathInfo = request.getPathInfo();
60         String queryString = request.getQueryString();
61
62         StringBuilder url = new StringBuilder();
63         url.append(config.getKibanaProtocol()).append("://").append(config.getKibanaHost());
64         url.append(":").append(config.getKibanaPort());
65         url.append(contextPath).append(servletPath);
66
67         if (pathInfo != null) {
68             url.append(pathInfo);
69         }
70
71         if (queryString != null) {
72             url.append("?").append(queryString);
73         }
74
75         return url.toString().replace("/sdc1/kibanaProxy/", "/");
76     }
77
78     private Configuration getConfiguration(HttpServletRequest request) {
79         return ((ConfigurationManager) request.getSession().getServletContext()
80                 .getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).getConfiguration();
81     }
82 }