2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.fe.servlets;
23 import com.google.common.cache.Cache;
24 import com.google.common.cache.CacheBuilder;
25 import org.eclipse.jetty.client.api.Response;
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.openecomp.sdc.fe.config.FeEcompErrorManager;
30 import org.openecomp.sdc.fe.config.PluginsConfiguration;
31 import org.openecomp.sdc.fe.config.PluginsConfiguration.Plugin;
32 import org.openecomp.sdc.fe.impl.MdcData;
33 import org.openecomp.sdc.fe.utils.BeProtocol;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import java.net.MalformedURLException;
42 import java.util.concurrent.TimeUnit;
44 public class FeProxyServlet extends SSLProxyServlet {
45 private static final long serialVersionUID = 1L;
46 private static final String URL = "%s://%s%s%s";
47 private static final String ONBOARDING_CONTEXT = "/onboarding-api";
48 private static final String DCAED_CONTEXT = "/dcae-api";
49 private static final String WORKFLOW_CONTEXT = "/wf";
50 private static final String SDC1_FE_PROXY = "/sdc1/feProxy";
51 private static final String PLUGIN_ID_WORKFLOW = "WORKFLOW";
53 private static final Logger log = LoggerFactory.getLogger(FeProxyServlet.class.getName());
54 private static Cache<String, MdcData> mdcDataCache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).build();
58 protected String rewriteTarget(HttpServletRequest request) {
59 String originalUrl="";
60 String redirectedUrl = "";
63 logFeRequest(request);
65 originalUrl = request.getRequestURL().toString();
66 redirectedUrl = getModifiedUrl(request);
68 } catch(MalformedURLException mue){
69 FeEcompErrorManager.getInstance().logFeHttpLoggingError("FE Request");
70 log.error("Unexpected FE request processing error :", mue);
73 FeEcompErrorManager.getInstance().logFeHttpLoggingError("FE Request");
74 log.error("Unexpected FE request logging error :", e);
77 log.debug("FeProxyServlet Redirecting request from: {} , to: {}", originalUrl, redirectedUrl);
83 protected void onProxyResponseSuccess(HttpServletRequest request, HttpServletResponse proxyResponse, Response response) {
85 logFeResponse(request, response);
86 } catch (Exception e) {
87 FeEcompErrorManager.getInstance().logFeHttpLoggingError("FE Response");
88 log.error("Unexpected FE response logging error :", e);
90 super.onProxyResponseSuccess(request, proxyResponse, response);
93 private void logFeRequest(HttpServletRequest httpRequest) {
97 Long transactionStartTime = System.currentTimeMillis();
98 // UUID - In FE, we are supposed to get the below header from UI.
99 // We do not generate it if it's missing - BE does.
100 String uuid = httpRequest.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER);
101 String serviceInstanceID = httpRequest.getHeader(Constants.X_ECOMP_SERVICE_ID_HEADER);
103 if (uuid != null && uuid.length() > 0) {
104 // UserId for logging
105 String userId = httpRequest.getHeader(Constants.USER_ID_HEADER);
107 String remoteAddr = httpRequest.getRemoteAddr();
108 String localAddr = httpRequest.getLocalAddr();
110 mdcDataCache.put(uuid, new MdcData(serviceInstanceID, userId, remoteAddr, localAddr, transactionStartTime));
112 updateMdc(uuid, serviceInstanceID, userId, remoteAddr, localAddr, null);
114 inHttpRequest(httpRequest);
117 private void logFeResponse(HttpServletRequest request, Response proxyResponse) {
118 String uuid = request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER);
119 String transactionRoundTime = null;
122 MdcData mdcData = mdcDataCache.getIfPresent(uuid);
123 if (mdcData != null) {
124 Long transactionStartTime = mdcData.getTransactionStartTime();
125 if (transactionStartTime != null) {// should'n ever be null, but
126 // just to be defensive
127 transactionRoundTime = Long.toString(System.currentTimeMillis() - transactionStartTime);
129 updateMdc(uuid, mdcData.getServiceInstanceID(), mdcData.getUserId(), mdcData.getRemoteAddr(), mdcData.getLocalAddr(), transactionRoundTime);
132 outHttpResponse(proxyResponse);
137 // Extracted for purpose of clear method name, for logback %M parameter
138 private void inHttpRequest(HttpServletRequest httpRequest) {
139 log.info("{} {} {}", httpRequest.getMethod(), httpRequest.getRequestURI(), httpRequest.getProtocol());
142 // Extracted for purpose of clear method name, for logback %M parameter
143 private void outHttpResponse(Response proxyResponse) {
144 log.info("SC=\"{}\"", proxyResponse.getStatus());
147 private void updateMdc(String uuid, String serviceInstanceID, String userId, String remoteAddr, String localAddr, String transactionStartTime) {
148 MDC.put("uuid", uuid);
149 MDC.put("serviceInstanceID", serviceInstanceID);
150 MDC.put("userId", userId);
151 MDC.put("remoteAddr", remoteAddr);
152 MDC.put("localAddr", localAddr);
153 MDC.put("timer", transactionStartTime);
158 private String getModifiedUrl(HttpServletRequest request) throws MalformedURLException {
159 Configuration config = getConfiguration(request);
160 if (config == null) {
161 log.error("failed to retrive configuration.");
162 throw new RuntimeException("failed to read FE configuration");
164 String uri = request.getRequestURI();
168 if (uri.contains(ONBOARDING_CONTEXT)){
169 uri = uri.replace(SDC1_FE_PROXY+ONBOARDING_CONTEXT,ONBOARDING_CONTEXT);
170 protocol = config.getOnboarding().getProtocolBe();
171 host = config.getOnboarding().getHostBe();
172 port = config.getOnboarding().getPortBe().toString();
173 }else if(uri.contains(DCAED_CONTEXT)){
174 uri = uri.replace(SDC1_FE_PROXY+DCAED_CONTEXT,DCAED_CONTEXT);
175 protocol = config.getBeProtocol();
176 host = config.getBeHost();
177 if (config.getBeProtocol().equals(BeProtocol.HTTP.getProtocolName())) {
178 port = config.getBeHttpPort().toString();
180 port = config.getBeSslPort().toString();
183 else if (uri.contains(WORKFLOW_CONTEXT)){
184 uri = uri.replace(SDC1_FE_PROXY +WORKFLOW_CONTEXT,WORKFLOW_CONTEXT);
185 String workflowPluginURL = getPluginConfiguration(request).getPluginsList()
187 .filter(plugin -> plugin.getPluginId().equalsIgnoreCase(PLUGIN_ID_WORKFLOW))
188 .map(Plugin::getPluginDiscoveryUrl)
189 .findFirst().orElse(null);
191 java.net.URL workflowURL = new URL(workflowPluginURL);
192 protocol = workflowURL.getProtocol();
193 host = workflowURL.getHost();
194 port = String.valueOf(workflowURL.getPort());
197 uri = uri.replace(SDC1_FE_PROXY,"/sdc2");
198 protocol = config.getBeProtocol();
199 host = config.getBeHost();
200 if (config.getBeProtocol().equals(BeProtocol.HTTP.getProtocolName())) {
201 port = config.getBeHttpPort().toString();
203 port = config.getBeSslPort().toString();
207 String authority = getAuthority(host, port);
208 String queryString = getQueryString(request);
209 return String.format(URL,protocol,authority,uri,queryString);
213 private PluginsConfiguration getPluginConfiguration(HttpServletRequest request) {
214 return ((ConfigurationManager) request.getSession().getServletContext().getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).getPluginsConfiguration();
216 private Configuration getConfiguration(HttpServletRequest request) {
217 return ((ConfigurationManager) request.getSession().getServletContext().getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).getConfiguration();
220 private String getAuthority(String host, String port) {
226 authority=host+":"+port;
231 private String getQueryString(HttpServletRequest request) {
232 String queryString = request.getQueryString();
233 if (queryString != null) {
234 queryString="?"+queryString;