X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ecomp-portal-BE-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fportalapp%2Fportal%2Futils%2FEcompPortalUtils.java;h=f29f9d91b4e0bdc076eb693d93e94d9e5d0ec01c;hb=6efc2f7ffffed6c8c473caeaebb26bb087a0b6cd;hp=48c2dbe8ec34f51db94df20810f3aee907ac58e3;hpb=627badaf69987c01811c477219fd943757a635f5;p=portal.git diff --git a/ecomp-portal-BE-common/src/main/java/org/openecomp/portalapp/portal/utils/EcompPortalUtils.java b/ecomp-portal-BE-common/src/main/java/org/openecomp/portalapp/portal/utils/EcompPortalUtils.java index 48c2dbe8..f29f9d91 100644 --- a/ecomp-portal-BE-common/src/main/java/org/openecomp/portalapp/portal/utils/EcompPortalUtils.java +++ b/ecomp-portal-BE-common/src/main/java/org/openecomp/portalapp/portal/utils/EcompPortalUtils.java @@ -29,6 +29,7 @@ import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletResponse; +import javax.xml.bind.DatatypeConverter; import org.hibernate.Session; import org.hibernate.Transaction; @@ -36,8 +37,11 @@ import org.openecomp.portalapp.portal.domain.EPUser; import org.openecomp.portalapp.portal.logging.format.EPAppMessagesEnum; import org.openecomp.portalapp.portal.logging.logic.EPLogUtil; import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.openecomp.portalsdk.core.onboarding.util.CipherUtil; import org.openecomp.portalsdk.core.util.SystemProperties; import org.slf4j.MDC; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -340,5 +344,84 @@ public class EcompPortalUtils { } } } + + public static String widgetMsProtocol(){ + final String protocol; + try{ + protocol = SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL); + return (protocol == null || protocol.trim().equals("")) ? "https" : protocol ; + } + catch(IllegalStateException ese){ + //looks like SystemProperties.getProperty throws IllegalStateException if it cannot find a property you are looking for + //In order to not break the code if a non-required property is missing from system.properties, returning https as default + //when this exception is caught. + return "https"; + } + } + + public static String localOrDockerHost(){ + final String effectiveHost; + try{ + effectiveHost = SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_HOSTNAME); + return (effectiveHost == null || effectiveHost.trim().equals("")) ? "localhost" : effectiveHost ; + } + catch(IllegalStateException ese){ + //looks like SystemProperties.getProperty throws IllegalStateException if it cannot find a property you are looking for + //In order to not break the code if a non-required property is missing from system.properties, returning https as default + //when this exception is caught. + return "localhost"; + } + } + /** + * It returns headers where username and password of external central auth + * is encoded to base64 + * + * @return header which contains external central auth username and password + * base64 encoded + * @throws Exception + * if unable to decrypt the password + */ + public static HttpHeaders base64encodeKeyForAAFBasicAuth() throws Exception { + + String userName = ""; + String decryptedPass = ""; + if (EPCommonSystemProperties + .containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_AUTH_USER_NAME) && EPCommonSystemProperties + .containsProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_AUTH_PASSWORD)) { + decryptedPass = SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_AUTH_PASSWORD); + userName = SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_AUTH_USER_NAME); + } + String decPass = decrypted(decryptedPass); + String usernamePass = userName + ":" + decPass; + String encToBase64 = String.valueOf((DatatypeConverter.printBase64Binary(usernamePass.getBytes()))); + HttpHeaders headers = new HttpHeaders(); + headers.add("Authorization", "Basic " + encToBase64); + headers.setContentType(MediaType.APPLICATION_JSON); + return headers; + } + + private static String decrypted(String encrypted) throws Exception { + String result = ""; + if (encrypted != null && encrypted.length() > 0) { + try { + result = CipherUtil.decrypt(encrypted, SystemProperties.getProperty(SystemProperties.Decryption_Key)); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed", e); + throw e; + } + } + return result; + } + + public static String truncateString(String originString, int size){ + if(originString.length()>=size){ + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(originString); + stringBuilder.setLength(size); + stringBuilder.append("..."); + return stringBuilder.toString(); + } + return originString; + } }