CSIT Fix for SDC-2585
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / monitoring / EsGateway.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.be.monitoring;
22
23 import org.eclipse.jetty.proxy.ProxyServlet;
24 import org.openecomp.sdc.be.components.impl.MonitoringBusinessLogic;
25 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
26 import org.openecomp.sdc.common.api.Constants;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
28 import org.springframework.web.context.WebApplicationContext;
29
30 import javax.servlet.ServletContext;
31 import javax.servlet.http.HttpServletRequest;
32
33 public class EsGateway extends ProxyServlet {
34
35     private static final long serialVersionUID = 1L;
36     private static final Logger log = Logger.getLogger(EsGateway.class);
37
38     @Override
39     public String rewriteTarget(HttpServletRequest request) {
40
41         String originalUrl = request.getRequestURI();
42         String redirectedUrl = getModifiedUrl(request);
43
44         log.debug("EsGateway Redirecting request from: {} , to: {}", originalUrl, redirectedUrl);
45         return redirectedUrl;
46     }
47
48     public String getModifiedUrl(HttpServletRequest request) {
49         String esHost = null;
50         String esPort = null;
51         MonitoringBusinessLogic monitoringBL = getMonitoringBL(request.getSession().getServletContext());
52         if (monitoringBL == null) {
53             log.error("failed to retrieve monitoringBL.");
54         } else {
55             esHost = monitoringBL.getEsHost();
56             esPort = monitoringBL.getEsPort();
57         }
58
59         //String scheme = request.getScheme(); esGateway HTTP
60         String scheme = "http";
61         String contextPath = request.getContextPath(); // /mywebapp
62         String servletPath = request.getServletPath(); // /servlet/MyServlet
63         String pathInfo = request.getPathInfo(); // /a/b;c=123
64         String queryString = request.getQueryString(); // d=789
65
66         StringBuilder url = new StringBuilder();
67         url.append(scheme).append("://").append(esHost);
68         url.append(":").append(esPort);
69         url.append(contextPath).append(servletPath);
70
71         if (pathInfo != null) {
72             url.append(pathInfo);
73         }
74         if (queryString != null) {
75             url.append("?").append(queryString);
76         }
77
78         return url.toString().replace("/sdc2/esGateway/", "/");
79
80     }
81
82     protected MonitoringBusinessLogic getMonitoringBL(ServletContext context) {
83
84         WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
85         WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
86
87         return webApplicationContext.getBean(MonitoringBusinessLogic.class);
88     }
89
90 }