2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
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 * ================================================================================
20 package org.openecomp.portalsdk.core.service;
22 import java.util.List;
24 import org.openecomp.portalsdk.core.domain.App;
25 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
27 import org.openecomp.portalsdk.core.util.SystemProperties;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Service;
30 import org.springframework.transaction.annotation.Transactional;
32 @Service("webServiceCallService")
34 public class WebServiceCallServiceImpl implements WebServiceCallService{
37 private DataAccessService dataAccessService;
40 AppService appService;
42 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WebServiceCallServiceImpl.class);
45 * Verify REST Credential
46 * @return true if the credential is accepted; else false.
49 public boolean verifyRESTCredential(String secretKey, String requestAppName, String requestPassword)throws Exception {
50 App app = appService.getDefaultApp();
52 String encriptedPwdDB = app.getAppPassword();
53 String appUserName = app.getUsername();
54 String decreptedPwd = CipherUtil.decrypt(encriptedPwdDB, secretKey==null?SystemProperties.getProperty(SystemProperties.Decryption_Key):secretKey);
55 if(decreptedPwd.equals(requestPassword) && appUserName.equals(requestAppName)) {
63 * Getting App information from FN_APP table
64 * @return App domain object, or null if not found.
68 StringBuffer criteria = new StringBuffer();
69 criteria.append(" where id = 1");
70 list = getDataAccessService().getList(App.class, criteria.toString(), null, null);
71 return (list == null || list.size() == 0) ? null : (App) list.get(0);
74 public DataAccessService getDataAccessService() {
75 return dataAccessService;
78 public void setDataAccessService(DataAccessService dataAccessService) {
79 this.dataAccessService = dataAccessService;
84 public String get(String restURL, String restPath) {
85 String appUserName = "";
86 String appUebKey = "";
87 String decreptedPwd = "";
89 String inputLine = "";
90 String serviceName = "";
92 StringBuffer jsonResponse = new StringBuffer();
94 StopWatch stopWatch = new StopWatch("WebServiceCallServiceImpl.get");
97 logger.info(EELFLoggerDelegate.metricsLogger, "WebServiceCallServiceImpl.get (" + restPath + ") operation is started.");
98 logger.debug(EELFLoggerDelegate.debugLogger, "WebServiceCallServiceImpl.get (" + restPath + ") operation is started.");
99 loginId = MDC.get("LoginId");
100 appUebKey = PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY);
101 App app = appService.getDefaultApp();
103 appName = app.getName();
104 appUserName = app.getUsername();
106 decreptedPwd = CipherUtil.decrypt(app.getAppPassword(), SystemProperties.getProperty(SystemProperties.Decryption_Key));
107 } catch(Exception e) {
108 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in WebServiceCallServiceImpl.get while decrypting the password. Details: " + e.getMessage());
111 logger.warn(EELFLoggerDelegate.errorLogger, "Unable to locate the app information from the database.");
112 appName = SystemProperties.SERVICE_NAME;
115 //Create the connection object
116 URL obj = new URL(restURL + restPath);
117 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
118 con.setRequestMethod("GET");
119 con.setConnectTimeout(3000);
120 con.setReadTimeout(8000);
123 con.setRequestProperty("username", appUserName);
124 con.setRequestProperty("password", decreptedPwd);
125 con.setRequestProperty("uebkey", appUebKey);
126 con.setRequestProperty(SystemProperties.LOGIN_ID, loginId);
127 con.setRequestProperty(SystemProperties.USERAGENT_NAME, appName);
128 con.setRequestProperty(SystemProperties.ECOMP_REQUEST_ID, MDC.get(MDC_KEY_REQUEST_ID));
130 //set MDC context for outgoing audit logging
131 serviceName = String.format("%s:%s.%s", appName, SystemProperties.ECOMP_PORTAL_BE, restPath);
132 MDC.put(Configuration.MDC_SERVICE_NAME, serviceName);
133 MDC.put(Configuration.MDC_REMOTE_HOST, restURL);
134 MDC.put(SystemProperties.MDC_APPNAME, appName);
135 MDC.put(SystemProperties.MDC_REST_PATH, restPath);
136 MDC.put(SystemProperties.MDC_REST_METHOD, "GET");
138 int responseCode = con.getResponseCode();
139 logger.info(EELFLoggerDelegate.errorLogger, "Received the response code '" + responseCode + "' while getting the '" + restPath + "' for user: " + loginId);
141 BufferedReader in = new BufferedReader(
142 new InputStreamReader(con.getInputStream()));
144 while ((inputLine = in.readLine()) != null) {
145 jsonResponse.append(inputLine);
149 logSecurityMessage(RESULT_ENUM.SUCCESS);
150 logger.debug(EELFLoggerDelegate.debugLogger, restPath + " response: " + jsonResponse.toString());
151 logger.debug(EELFLoggerDelegate.debugLogger, "WebServiceCallServiceImpl.get (" + restPath + ") operation is started.");
152 } catch(UrlAccessRestrictedException e) {
153 logger.error(EELFLoggerDelegate.errorLogger, "Authentication exception occurred in WebServiceCallServiceImpl.get (" + restPath + "). Details: " + e.getMessage());
154 logSecurityMessage(RESULT_ENUM.FAILURE);
155 } catch(Exception e) {
156 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in WebServiceCallServiceImpl.get (" + restPath + "). Details: " + e.getMessage());
157 logSecurityMessage(RESULT_ENUM.FAILURE);
159 if (stopWatch.isRunning()) stopWatch.stop();
160 MDC.put(SystemProperties.MDC_TIMER, stopWatch.getTotalTimeMillis() + "ms");
161 logger.info(EELFLoggerDelegate.metricsLogger, "WebServiceCallServiceImpl.get (" + restPath + ") operation is completed.");
163 //clear the temporary MDC context values
164 MDC.remove(SystemProperties.MDC_TIMER);
165 MDC.remove(SystemProperties.MDC_REST_METHOD);
166 MDC.remove(SystemProperties.MDC_REST_PATH);
167 MDC.remove(SystemProperties.MDC_APPNAME);
168 MDC.remove(Configuration.MDC_REMOTE_HOST);
169 MDC.remove(Configuration.MDC_SERVICE_NAME);
172 return jsonResponse.toString();
175 //Handles all the outgoing rest/ueb messages.
176 public void logSecurityMessage(RESULT_ENUM isSuccess) {
177 String additionalInfo = "";
178 String protocol = "HTTP";
179 String loginId = MDC.get("LoginId");
180 additionalInfo = String.format("Rest API=%s, Rest Method=%s, App-Name=%s, Request-URL=%s",
181 MDC.get(SystemProperties.MDC_REST_PATH), MDC.get(SystemProperties.MDC_REST_METHOD),
182 MDC.get(SystemProperties.MDC_APPNAME), MDC.get(Configuration.MDC_REMOTE_HOST));
184 logger.info(EELFLoggerDelegate.auditLogger, AuditLogFormatter.getInstance().createMessage(
185 protocol, SecurityEventTypeEnum.OUTGOING_REST_MESSAGE.name(), loginId, SystemProperties.SERVICE_NAME,
186 isSuccess.name(), additionalInfo));