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