Initial OpenECOMP SDC commit
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.fe.servlets;
22
23 import java.net.URI;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.eclipse.jetty.client.api.Request;
29 import org.eclipse.jetty.client.api.Response;
30 import org.eclipse.jetty.proxy.ProxyServlet;
31 import org.openecomp.sdc.common.api.Constants;
32 import org.openecomp.sdc.fe.config.Configuration;
33 import org.openecomp.sdc.fe.config.ConfigurationManager;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class KibanaServlet extends ProxyServlet {
38         private static final long serialVersionUID = 1L;
39         private static Logger log = LoggerFactory.getLogger(KibanaServlet.class.getName());
40
41         @Override
42         public URI rewriteURI(HttpServletRequest request) {
43
44                 String originalUrl = request.getRequestURI();
45
46                 String redirectedUrl = getModifiedUrl(request);
47
48                 log.debug("KibanaServlet Redirecting request from: {} , to: {}", originalUrl, redirectedUrl);
49
50                 return URI.create(redirectedUrl);
51         }
52
53         @Override
54         public void customizeProxyRequest(Request proxyRequest, HttpServletRequest request) {
55                 super.customizeProxyRequest(proxyRequest, request);
56
57         }
58
59         @Override
60         protected void onResponseSuccess(HttpServletRequest request, HttpServletResponse response, Response proxyResponse) {
61
62                 super.onResponseSuccess(request, response, proxyResponse);
63         }
64
65         public String getModifiedUrl(HttpServletRequest request) {
66                 Configuration config = getConfiguration(request);
67                 if (config == null) {
68                         log.error("failed to retrive configuration.");
69                 }
70                 // String scheme = request.getScheme();
71                 String contextPath = request.getContextPath(); // /mywebapp
72                 String servletPath = request.getServletPath(); // /servlet/MyServlet
73                 String pathInfo = request.getPathInfo(); // /a/b;c=123
74                 String queryString = request.getQueryString(); // d=789
75
76                 StringBuilder url = new StringBuilder();
77                 url.append(config.getKibanaProtocol()).append("://").append(config.getKibanaHost());
78                 url.append(":").append(config.getKibanaPort());
79                 url.append(contextPath).append(servletPath);
80
81                 if (pathInfo != null) {
82                         url.append(pathInfo);
83                 }
84                 if (queryString != null) {
85                         url.append("?").append(queryString);
86                 }
87
88                 String redirectedUrl = url.toString().replace("/sdc1/kibanaProxy/", "/");
89                 return redirectedUrl;
90
91         }
92
93         private Configuration getConfiguration(HttpServletRequest request) {
94                 Configuration config = ((ConfigurationManager) request.getSession().getServletContext()
95                                 .getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).getConfiguration();
96                 return config;
97         }
98 }