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.portalapp.portal.utils;
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24 import java.net.InetAddress;
25 import java.net.UnknownHostException;
26 import java.text.SimpleDateFormat;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.List;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.xml.bind.DatatypeConverter;
34 import org.hibernate.Session;
35 import org.hibernate.Transaction;
36 import org.openecomp.portalapp.portal.domain.EPUser;
37 import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum;
38 import org.openecomp.portalapp.portal.logging.logic.EPLogUtil;
39 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
40 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
41 import org.openecomp.portalsdk.core.util.SystemProperties;
43 import org.springframework.http.HttpHeaders;
44 import org.springframework.http.MediaType;
46 import com.fasterxml.jackson.core.JsonProcessingException;
47 import com.fasterxml.jackson.databind.ObjectMapper;
49 public class EcompPortalUtils {
51 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EcompPortalUtils.class);
55 * @return true if orgUserId is not empty and contains only alphanumeric,
58 public static boolean legitimateUserId(String orgUserId) {
59 return orgUserId.matches("^[a-zA-Z0-9]+$");
63 * Splits the string into a list of tokens using the specified regular
68 * @return List of tokens split from the source
70 public static List<String> parsingByRegularExpression(String source, String regex) {
71 List<String> tokens = new ArrayList<String>();
72 if (source != null && source.length() > 0) {
73 String[] parsed = source.split(regex);
74 for (String token : parsed) {
75 if (token.length() > 0) {
84 * Builds a JSON object with error code and message information.
88 * @return JSON object as a String
90 public static String jsonErrorMessageResponse(int errorCode, String errorMessage) {
91 return "{\"error\":{\"code\":" + errorCode + "," + "\"message\":\"" + errorMessage + "\"}}";
95 * Builds a JSON object with the specified message
98 * @return JSON object as a String
100 public static String jsonMessageResponse(String message) {
101 return String.format("{\"message\":\"%s\"}", message);
105 * Serializes the specified object as JSON and writes the result to the
106 * debug log. If serialization fails, logs a message to the error logger.
109 * Logger for the class where the object was built; the logger
110 * carries the class name.
112 * First portion of the log message
114 * Second portion of the log message
116 * Object to serialize as JSON
118 public static void logAndSerializeObject(EELFLoggerDelegate logger, String source, String msg, Object obj) {
120 String objectAsJson = new ObjectMapper().writeValueAsString(obj);
121 logger.debug(EELFLoggerDelegate.debugLogger,
122 String.format("source= [%s]; %s [%s];", source, msg, objectAsJson));
123 } catch (JsonProcessingException e) {
124 logger.warn(EELFLoggerDelegate.errorLogger, "logAndSerializedObject failed to serialize", e);
125 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, e);
126 } catch (Exception e) {
127 logger.error(EELFLoggerDelegate.errorLogger, "logAndSerializedObject failed", e);
128 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, e);
133 * Serializes the specified object as JSON and writes the result to the
134 * debug log. If serialization fails, logs a message to the error logger.
137 * First portion of the log message
139 * Second portion of the log message
141 * Object to serialize as JSON
143 public static void logAndSerializeObject(String source, String msg, Object obj) {
144 logAndSerializeObject(logger, source, msg, obj);
147 public static void rollbackTransaction(Transaction transaction, String errorMessage) {
148 logger.error(EELFLoggerDelegate.errorLogger, errorMessage);
150 if (transaction != null) {
151 transaction.rollback();
153 } catch (Exception e) {
154 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeExecuteRollbackError, e);
155 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing a rollback transaction",
160 public static void closeLocalSession(Session localSession, String errorMessage) {
161 logger.error(EELFLoggerDelegate.errorLogger, errorMessage);
163 if (localSession != null) {
164 localSession.close();
166 } catch (Exception e) {
167 EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoCloseSessionError, e);
168 logger.error(EELFLoggerDelegate.errorLogger, errorMessage + ", closeLocalSession exception", e);
172 // TODO: GLOBAL_LOGIN_URL is the same as in SessionTimeoutInterceptor.
173 // It should be defined in SystemProperties.
174 private static final String GLOBAL_LOGIN_URL = "global-login-url";
177 * Set response status to Unauthorized if user == null and to Forbidden in
178 * all (!) other cases. Logging is not performed if invocator == null
185 public static void setBadPermissions(EPUser user, HttpServletResponse response, String invocator) {
187 String loginUrl = SystemProperties.getProperty(EPCommonSystemProperties.LOGIN_URL_NO_RET_VAL);
188 response.setHeader(GLOBAL_LOGIN_URL, loginUrl);
189 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
190 MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(HttpServletResponse.SC_UNAUTHORIZED));
192 response.setStatus(HttpServletResponse.SC_FORBIDDEN);
193 MDC.put(EPCommonSystemProperties.RESPONSE_CODE, Integer.toString(HttpServletResponse.SC_FORBIDDEN));
195 if (invocator != null) {
196 logger.warn(EELFLoggerDelegate.errorLogger,
197 invocator + ", permissions problem, response status = " + response.getStatus());
201 public static int getExternalAppResponseCode() {
202 String responseCode = MDC.get(EPCommonSystemProperties.EXTERNAL_API_RESPONSE_CODE);
203 int responseCodeInt = 0;
205 if (responseCode != null && responseCode != "") {
206 responseCodeInt = Integer.valueOf(responseCode);
208 } catch (Exception e) {
209 logger.error(EELFLoggerDelegate.errorLogger,
210 "Exception occurred in getResponseCode(). Details: " + EcompPortalUtils.getStackTrace(e));
212 return responseCodeInt;
215 // This method might be just for testing purposes.
216 public static void setExternalAppResponseCode(int responseCode) {
218 String responseCodeString = String.valueOf(responseCode);
219 MDC.put(EPCommonSystemProperties.EXTERNAL_API_RESPONSE_CODE, responseCodeString);
220 } catch (Exception e) {
221 logger.error(EELFLoggerDelegate.errorLogger,
222 "Exception occurred in setResponseCode(). Details: " + EcompPortalUtils.getStackTrace(e));
226 public static String getHTTPStatusString(int httpStatusCode) {
227 String httpStatusString = "unknown_error";
229 httpStatusString = org.springframework.http.HttpStatus.valueOf(httpStatusCode).name();
230 if (httpStatusString != null) {
231 httpStatusString = httpStatusString.toLowerCase();
233 } catch (Exception e) {
234 logger.error(EELFLoggerDelegate.errorLogger,
235 "Exception occurred in getHTTPStatusString(). Details: " + EcompPortalUtils.getStackTrace(e));
237 return httpStatusString;
240 public static String getFEErrorString(Boolean internal, int responseCode) {
241 // Return a String like the following:
242 // "Internal Ecomp Error: 500 internal_server_error" or
243 // "External App Error: 404 not_found"
244 // TODO: create our own Ecomp error codes, starting with 1000 and up.
245 String responseString = "";
246 String internalExternalString = internal ? "Ecomp Error: " : "App Error: ";
247 String httpStatusString = "unknown_error";
249 if (responseCode < 1000) {
250 httpStatusString = getHTTPStatusString(responseCode);
252 } catch (Exception e) {
253 logger.error(EELFLoggerDelegate.errorLogger,
254 "Exception occurred in getFEErrorString(). Details: " + EcompPortalUtils.getStackTrace(e));
256 responseString = internalExternalString + responseCode + " " + httpStatusString;
257 return responseString;
260 public static boolean isProductionBuild() {
261 boolean productionBuild = true;
262 String epVersion = EcompVersion.buildNumber;
263 if (epVersion != null) {
264 int buildNum = epVersion.lastIndexOf('.');
266 int buildNumber = Integer.parseInt(epVersion.substring(buildNum + 1));
267 if (buildNumber < 3000) // Production versions are 3000+, (ie
270 productionBuild = false;
274 return productionBuild;
277 private static final Object stackTraceLock = new Object();
279 public static String getStackTrace(Throwable t) {
280 synchronized (stackTraceLock) {
281 StringWriter sw = new StringWriter();
282 PrintWriter pw = new PrintWriter(sw);
283 t.printStackTrace(pw);
284 return sw.toString();
288 public static String getMyIpAdddress() {
292 ip = InetAddress.getLocalHost();
293 localIp = ip.getHostAddress();
294 } catch (UnknownHostException e) {
296 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
301 public static String getMyHostName() {
305 ip = InetAddress.getLocalHost();
306 hostName = ip.getHostName();
307 } catch (UnknownHostException e) {
308 hostName = "unknown";
309 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
314 /* return a default property if the expected one is not available */
315 public static String getPropertyOrDefault(String property, String defaultValue) {
316 return ((null == SystemProperties.getProperty(property) || SystemProperties.getProperty(property).equals(""))
317 ? defaultValue : SystemProperties.getProperty(property));
321 * Calculates the time duration of a function call for logging purpose. It
322 * stores the result by using "MDC.put(SystemProperties.MDC_TIMER,
323 * timeDifference);" It is important to call
324 * "MDC.remove(SystemProperties.MDC_TIMER);" after this method call to clean
325 * up the record in MDC
327 * @param beginDateTime
328 * the given begin time for the call
330 * the given end time for the call
333 public static void calculateDateTimeDifferenceForLog(String beginDateTime, String endDateTime) {
334 if (beginDateTime != null && endDateTime != null) {
336 SimpleDateFormat ecompLogDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
338 Date beginDate = ecompLogDateFormat.parse(beginDateTime);
339 Date endDate = ecompLogDateFormat.parse(endDateTime);
340 String timeDifference = String.format("%d", endDate.getTime() - beginDate.getTime());
341 MDC.put(SystemProperties.MDC_TIMER, timeDifference);
342 } catch (Exception e) {
343 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
348 public static String widgetMsProtocol(){
349 final String protocol;
351 protocol = SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL);
352 return (protocol == null || protocol.trim().equals("")) ? "https" : protocol ;
354 catch(IllegalStateException ese){
355 //looks like SystemProperties.getProperty throws IllegalStateException if it cannot find a property you are looking for
356 //In order to not break the code if a non-required property is missing from system.properties, returning https as default
357 //when this exception is caught.
362 public static String localOrDockerHost(){
363 final String effectiveHost;
365 effectiveHost = SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_HOSTNAME);
366 return (effectiveHost == null || effectiveHost.trim().equals("")) ? "localhost" : effectiveHost ;
368 catch(IllegalStateException ese){
369 //looks like SystemProperties.getProperty throws IllegalStateException if it cannot find a property you are looking for
370 //In order to not break the code if a non-required property is missing from system.properties, returning https as default
371 //when this exception is caught.
377 * It returns headers where username and password of external central auth
378 * is encoded to base64
380 * @return header which contains external central auth username and password
383 * if unable to decrypt the password
385 public static HttpHeaders base64encodeKeyForAAFBasicAuth() throws Exception {
387 String userName = "";
388 String decryptedPass = "";
389 if (EPCommonSystemProperties
390 .containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_AUTH_USER_NAME) && EPCommonSystemProperties
391 .containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_AUTH_PASSWORD)) {
392 decryptedPass = SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_AUTH_PASSWORD);
393 userName = SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_AUTH_USER_NAME);
395 String decPass = decrypted(decryptedPass);
396 String usernamePass = userName + ":" + decPass;
397 String encToBase64 = String.valueOf((DatatypeConverter.printBase64Binary(usernamePass.getBytes())));
398 HttpHeaders headers = new HttpHeaders();
399 headers.add("Authorization", "Basic " + encToBase64);
400 headers.setContentType(MediaType.APPLICATION_JSON);
404 private static String decrypted(String encrypted) throws Exception {
406 if (encrypted != null && encrypted.length() > 0) {
408 result = CipherUtil.decrypt(encrypted, SystemProperties.getProperty(SystemProperties.Decryption_Key));
409 } catch (Exception e) {
410 logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e);
417 public static String truncateString(String originString, int size){
418 if(originString.length()>=size){
419 StringBuilder stringBuilder = new StringBuilder();
420 stringBuilder.append(originString);
421 stringBuilder.setLength(size);
422 stringBuilder.append("...");
423 return stringBuilder.toString();