re base code
[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 org.eclipse.jetty.client.api.Request;
24 import org.eclipse.jetty.client.api.Response;
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 import javax.servlet.http.HttpServletResponse;
34 import java.net.URI;
35
36 public class KibanaServlet extends ProxyServlet {
37         private static final long serialVersionUID = 1L;
38         private static Logger log = LoggerFactory.getLogger(KibanaServlet.class.getName());
39
40         @Override
41         public URI rewriteURI(HttpServletRequest request) {
42
43                 String originalUrl = request.getRequestURI();
44
45                 String redirectedUrl = getModifiedUrl(request);
46
47                 log.debug("KibanaServlet Redirecting request from: {} , to: {}", originalUrl, redirectedUrl);
48
49                 return URI.create(redirectedUrl);
50         }
51
52         @Override
53         public void customizeProxyRequest(Request proxyRequest, HttpServletRequest request) {
54                 super.customizeProxyRequest(proxyRequest, request);
55
56         }
57
58         @Override
59         protected void onResponseSuccess(HttpServletRequest request, HttpServletResponse response, Response proxyResponse) {
60
61                 super.onResponseSuccess(request, response, proxyResponse);
62         }
63
64         public String getModifiedUrl(HttpServletRequest request) {
65                 Configuration config = getConfiguration(request);
66                 if (config == null) {
67                         log.error("failed to retrive configuration.");
68                 }
69                 // String scheme = request.getScheme();
70                 String contextPath = request.getContextPath(); // /mywebapp
71                 String servletPath = request.getServletPath(); // /servlet/MyServlet
72                 String pathInfo = request.getPathInfo(); // /a/b;c=123
73                 String queryString = request.getQueryString(); // d=789
74
75                 StringBuilder url = new StringBuilder();
76                 url.append(config.getKibanaProtocol()).append("://").append(config.getKibanaHost());
77                 url.append(":").append(config.getKibanaPort());
78                 url.append(contextPath).append(servletPath);
79
80                 if (pathInfo != null) {
81                         url.append(pathInfo);
82                 }
83                 if (queryString != null) {
84                         url.append("?").append(queryString);
85                 }
86
87                 String redirectedUrl = url.toString().replace("/sdc1/kibanaProxy/", "/");
88                 return redirectedUrl;
89
90         }
91
92         private Configuration getConfiguration(HttpServletRequest request) {
93                 Configuration config = ((ConfigurationManager) request.getSession().getServletContext()
94                                 .getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).getConfiguration();
95                 return config;
96         }
97 }