Sync Integ to Master
[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.client.api.Request;
24 import org.eclipse.jetty.client.api.Response;
25 import org.eclipse.jetty.proxy.ProxyServlet;
26 import org.openecomp.sdc.be.components.impl.MonitoringBusinessLogic;
27 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
28 import org.openecomp.sdc.common.api.Constants;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.web.context.WebApplicationContext;
32
33 import javax.servlet.ServletContext;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import java.net.URI;
37
38 public class EsGateway extends ProxyServlet {
39
40     private static final long serialVersionUID = 1L;
41     private static final Logger log = LoggerFactory.getLogger(EsGateway.class);
42
43     @Override
44     public URI rewriteURI(HttpServletRequest request) {
45
46         String originalUrl = request.getRequestURI();
47
48         String redirectedUrl = getModifiedUrl(request);
49
50         log.debug("EsGateway Redirecting request from: {} , to: {}", originalUrl, redirectedUrl);
51         return URI.create(redirectedUrl);
52     }
53
54     @Override
55     public void customizeProxyRequest(Request proxyRequest, HttpServletRequest request) {
56         super.customizeProxyRequest(proxyRequest, request);
57
58     }
59
60     @Override
61     protected void onResponseSuccess(HttpServletRequest request, HttpServletResponse response, Response proxyResponse) {
62         super.onResponseSuccess(request, response, proxyResponse);
63     }
64
65     public String getModifiedUrl(HttpServletRequest request) {
66         String esHost = null;
67         String esPort = null;
68         MonitoringBusinessLogic monitoringBL = getMonitoringBL(request.getSession().getServletContext());
69         if (monitoringBL == null) {
70             log.error("failed to retrive monitoringBL.");
71         } else {
72             esHost = monitoringBL.getEsHost();
73             esPort = monitoringBL.getEsPort();
74         }
75
76         //String scheme = request.getScheme(); esGateway HTTP
77         String scheme = "http";
78         String contextPath = request.getContextPath(); // /mywebapp
79         String servletPath = request.getServletPath(); // /servlet/MyServlet
80         String pathInfo = request.getPathInfo(); // /a/b;c=123
81         String queryString = request.getQueryString(); // d=789
82
83         StringBuilder url = new StringBuilder();
84         url.append(scheme).append("://").append(esHost);
85         url.append(":").append(esPort);
86         url.append(contextPath).append(servletPath);
87
88         if (pathInfo != null) {
89             url.append(pathInfo);
90         }
91         if (queryString != null) {
92             url.append("?").append(queryString);
93         }
94
95         String redirectedUrl = url.toString().replace("/sdc2/esGateway/", "/");
96         return redirectedUrl;
97
98     }
99
100     protected MonitoringBusinessLogic getMonitoringBL(ServletContext context) {
101
102         WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
103         WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
104         MonitoringBusinessLogic monitoringBusinessLogic = webApplicationContext.getBean(MonitoringBusinessLogic.class);
105
106         return monitoringBusinessLogic;
107     }
108
109 }