From 558eb8fc1392428dbabc4d6016713bff99d6425f Mon Sep 17 00:00:00 2001 From: Hima Elisetty Date: Wed, 7 Feb 2018 17:12:02 -0500 Subject: Added Junits Issue-ID: PORTAL-136, PORTAL-188 Includes FW mode central changes Change-Id: Iba6c0d48ed768e32d1fe4700d0044eb7f3fdc482 Signed-off-by: Hima Elisetty --- .../crossapi/IPortalRestCentralService.java | 103 +++++++++++ .../crossapi/PortalRestAPICentralServiceImpl.java | 200 +++++++++++++++++++++ .../onboarding/crossapi/PortalRestAPIProxy.java | 40 ++--- .../core/onboarding/util/PortalApiConstants.java | 2 + 4 files changed, 322 insertions(+), 23 deletions(-) create mode 100644 ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/IPortalRestCentralService.java create mode 100644 ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPICentralServiceImpl.java (limited to 'ecomp-sdk/epsdk-fw/src/main/java/org') diff --git a/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/IPortalRestCentralService.java b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/IPortalRestCentralService.java new file mode 100644 index 00000000..3438e787 --- /dev/null +++ b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/IPortalRestCentralService.java @@ -0,0 +1,103 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalsdk.core.onboarding.crossapi; + +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.onap.portalsdk.core.onboarding.exception.PortalAPIException; +import org.onap.portalsdk.core.restful.domain.EcompUser; + +public interface IPortalRestCentralService { + + /** + * Gets and returns the Map with username, password and appName of application .If any error + * occurs, the method should throw PortalApiException with an appropriate + * message. The FW library will catch the exception and send an appropriate + * response to Portal. + * + * @return a map with keys username, password and appName + */ + public Map getAppCredentials() throws PortalAPIException; + + /** + * Creates a user with the specified details. If any error occurs, for example + * the user exists, the method should throw PortalApiException with an + * appropriate message. The FW library will catch the exception and send an + * appropriate response to Portal. + * + * @param user + * Model object with attributes of user to be created. + * @throws PortalAPIException + * If any error occurs while processing the request; for example, + * user exists. + */ + public void pushUser(EcompUser user) throws PortalAPIException; + + /** + * Updates details about the user with the specified loginId. For example, mark + * user as inactive. If any error occurs, the method should throw + * PortalApiException with an appropriate message. The FW library will catch the + * exception and send an appropriate response to Portal. + * + * @param loginId + * EcompUser ID to be updated. + * @param user + * Model object with attributes of user to be updated. + * @throws PortalAPIException + * If any error occurs while processing the request; for example, + * unknown user. + */ + public void editUser(String loginId, EcompUser user) throws PortalAPIException; + + /** + * Gets and returns the userId for the logged-in user based on the request. If + * any error occurs, the method should throw PortalApiException with an + * appropriate message. The FW library will catch the exception and send an + * appropriate response to Portal. + * + * @param request + * HttpServletRequest + * @return true if the request contains appropriate credentials, else false. + * @throws PortalAPIException + * If an unexpected error occurs while processing the request. + */ + public String getUserId(HttpServletRequest request) throws PortalAPIException; + +} diff --git a/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPICentralServiceImpl.java b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPICentralServiceImpl.java new file mode 100644 index 00000000..732383b0 --- /dev/null +++ b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPICentralServiceImpl.java @@ -0,0 +1,200 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalsdk.core.onboarding.crossapi; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; + +import org.onap.portalsdk.core.onboarding.exception.PortalAPIException; +import org.onap.portalsdk.core.onboarding.rest.RestWebServiceClient; +import org.onap.portalsdk.core.onboarding.util.PortalApiConstants; +import org.onap.portalsdk.core.onboarding.util.PortalApiProperties; +import org.onap.portalsdk.core.restful.domain.EcompRole; +import org.onap.portalsdk.core.restful.domain.EcompUser; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.TypeFactory; + +public class PortalRestAPICentralServiceImpl implements IPortalRestAPIService { + + private static final Log logger = LogFactory.getLog(PortalRestAPICentralServiceImpl.class); + private String username; + private String password; + private String appName; + IPortalRestCentralService portalRestCentralService; + + public PortalRestAPICentralServiceImpl() throws ServletException { + String centralClassName = PortalApiProperties.getProperty(PortalApiConstants.PORTAL_API_IMPL_CLASS); + if (centralClassName == null) + throw new ServletException( + "init: Failed to find class name property " + PortalApiConstants.PORTAL_API_IMPL_CLASS); + try { + Class centralImplClass = Class.forName(centralClassName); + portalRestCentralService = (IPortalRestCentralService) (centralImplClass.getConstructor().newInstance()); + username = portalRestCentralService.getAppCredentials().get("username"); + password = portalRestCentralService.getAppCredentials().get("password"); + appName = portalRestCentralService.getAppCredentials().get("appName"); + } catch (Exception e) { + throw new ClassCastException("Failed to find or instantiate class "); + } + } + + private final ObjectMapper mapper = new ObjectMapper(); + + @Override + public void pushUser(EcompUser user) throws PortalAPIException { + portalRestCentralService.pushUser(user); + } + + @Override + public void editUser(String loginId, EcompUser user) throws PortalAPIException { + portalRestCentralService.editUser(loginId, user); + } + + @Override + public EcompUser getUser(String loginId) throws PortalAPIException { + EcompUser user = new EcompUser(); + String responseString = null; + try { + responseString = RestWebServiceClient.getInstance().getPortalContent("/ecompUser/" + loginId, null, + appName, null, username, password, true); + logger.debug("responseString is: " + responseString); + user = mapper.readValue(responseString, EcompUser.class); + + } catch (IOException e) { + String response = "PortalRestAPICentralServiceImpl.getUser failed"; + logger.error(response, e); + throw new PortalAPIException(response, e); + } + return user; + } + + @Override + public List getUsers() throws PortalAPIException { + List usersList = new ArrayList<>(); + String responseString = null; + try { + responseString = RestWebServiceClient.getInstance().getPortalContent("/users", null, appName, null, + username, password, true); + logger.debug("responseString is: " + responseString); + usersList = mapper.readValue(responseString, + TypeFactory.defaultInstance().constructCollectionType(List.class, EcompUser.class)); + + } catch (IOException e) { + String response = "PortalRestAPICentralServiceImpl.getUsers failed"; + logger.error(response, e); + throw new PortalAPIException(response, e); + } + return usersList; + } + + @Override + public List getAvailableRoles(String requestedLoginId) throws PortalAPIException { + List rolesList = new ArrayList<>(); + String responseString = null; + try { + responseString = RestWebServiceClient.getInstance().getPortalContent("/ecompRoles", requestedLoginId, + appName, null, username, password, true); + logger.debug("responseString is: " + responseString); + rolesList = mapper.readValue(responseString, + TypeFactory.defaultInstance().constructCollectionType(List.class, EcompRole.class)); + + } catch (IOException e) { + String response = "PortalRestAPICentralServiceImpl.getUsers failed"; + logger.error(response, e); + throw new PortalAPIException(response, e); + } + return rolesList; + } + + @Override + public void pushUserRole(String loginId, List roles) throws PortalAPIException { + throw new PortalAPIException("Please use Portal for Role Management"); + + } + + @SuppressWarnings("unchecked") + @Override + public List getUserRoles(String loginId) throws PortalAPIException { + List userRoles = new ArrayList<>(); + EcompUser user = new EcompUser(); + String responseString = null; + try { + responseString = RestWebServiceClient.getInstance().getPortalContent("/ecompUser/" + loginId, null, + appName, null, username, password, true); + logger.debug("responseString is: " + responseString); + user = mapper.readValue(responseString, EcompUser.class); + Set roles = user.getRoles(); + userRoles = (List) roles.stream().collect(Collectors.toList()); + + } catch (IOException e) { + String response = "PortalRestAPICentralServiceImpl.getUserRoles failed"; + logger.error(response, e); + throw new PortalAPIException(response, e); + } + return userRoles; + } + + @Override + public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException { + boolean response = false; + try { + String restUser = request.getHeader("username"); + String restPw = request.getHeader("password"); + response = restUser != null && restPw != null && restUser.equals(username) && restPw.equals(password); + logger.debug("isAppAuthenticated: " + response); + } catch (Exception ex) { + throw new PortalAPIException("isAppAuthenticated failed", ex); + } + return response; + } + + @Override + public String getUserId(HttpServletRequest request) throws PortalAPIException { + return portalRestCentralService.getUserId(request); + } + +} diff --git a/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxy.java b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxy.java index 2ceb8e7c..810771bd 100644 --- a/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxy.java +++ b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/crossapi/PortalRestAPIProxy.java @@ -126,7 +126,11 @@ public class PortalRestAPIProxy extends HttpServlet implements IPortalRestAPISer try { logger.debug("init: creating instance of class " + className); Class implClass = Class.forName(className); - portalRestApiServiceImpl = (IPortalRestAPIService) (implClass.getConstructor().newInstance()); + if (!isCentralized.equals(isAccessCentralized)) + portalRestApiServiceImpl = (IPortalRestAPIService) (implClass.getConstructor().newInstance()); + else { + portalRestApiServiceImpl = new PortalRestAPICentralServiceImpl(); + } } catch (Exception ex) { throw new ServletException("init: Failed to find or instantiate class " + className, ex); } @@ -243,17 +247,12 @@ public class PortalRestAPIProxy extends HttpServlet implements IPortalRestAPISer // Example: /user <-- create user if (requestUri.endsWith(PortalApiConstants.API_PREFIX + "/user")) { try { - if (isCentralized.equals(isAccessCentralized)) { - responseJson = buildJsonResponse(true, errorMessage); - response.setStatus(HttpServletResponse.SC_OK); - } else { - EcompUser user = mapper.readValue(requestBody, EcompUser.class); - pushUser(user); - if (logger.isDebugEnabled()) - logger.debug("doPost: pushUser: success"); - responseJson = buildJsonResponse(true, null); - response.setStatus(HttpServletResponse.SC_OK); - } + EcompUser user = mapper.readValue(requestBody, EcompUser.class); + pushUser(user); + if (logger.isDebugEnabled()) + logger.debug("doPost: pushUser: success"); + responseJson = buildJsonResponse(true, null); + response.setStatus(HttpServletResponse.SC_OK); } catch (Exception ex) { responseJson = buildJsonResponse(ex); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); @@ -264,17 +263,12 @@ public class PortalRestAPIProxy extends HttpServlet implements IPortalRestAPISer if (requestUri.contains(PortalApiConstants.API_PREFIX + "/user/") && !(requestUri.endsWith("/roles"))) { String loginId = requestUri.substring(requestUri.lastIndexOf('/') + 1); try { - if (isCentralized.equals(isAccessCentralized)) { - responseJson = buildJsonResponse(true, errorMessage); - response.setStatus(HttpServletResponse.SC_OK); - } else { - EcompUser user = mapper.readValue(requestBody, EcompUser.class); - editUser(loginId, user); - if (logger.isDebugEnabled()) - logger.debug("doPost: editUser: success"); - responseJson = buildJsonResponse(true, null); - response.setStatus(HttpServletResponse.SC_OK); - } + EcompUser user = mapper.readValue(requestBody, EcompUser.class); + editUser(loginId, user); + if (logger.isDebugEnabled()) + logger.debug("doPost: editUser: success"); + responseJson = buildJsonResponse(true, null); + response.setStatus(HttpServletResponse.SC_OK); } catch (Exception ex) { responseJson = buildJsonResponse(ex); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); diff --git a/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/util/PortalApiConstants.java b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/util/PortalApiConstants.java index eb931f5c..e27cbbff 100644 --- a/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/util/PortalApiConstants.java +++ b/ecomp-sdk/epsdk-fw/src/main/java/org/onap/portalsdk/core/onboarding/util/PortalApiConstants.java @@ -80,4 +80,6 @@ public interface PortalApiConstants { public static final String EXT_REQUEST_CONNECTION_TIMEOUT = "ext_req_connection_timeout"; public static final String EXT_REQUEST_READ_TIMEOUT = "ext_req_read_timeout"; + public static final String ROLE_ACCESS_CENTRALIZED_FLAG = "role_access_centralized"; + } -- cgit 1.2.3-korg