2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalsdk.core.onboarding.crossapi;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
46 import java.util.stream.Collectors;
48 import javax.servlet.ServletException;
49 import javax.servlet.http.HttpServletRequest;
51 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
53 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
54 import org.onap.portalsdk.core.onboarding.rest.RestWebServiceClient;
55 import org.onap.portalsdk.core.onboarding.util.AuthUtil;
56 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
57 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
58 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
59 import org.onap.portalsdk.core.restful.domain.EcompRole;
60 import org.onap.portalsdk.core.restful.domain.EcompUser;
61 import org.apache.commons.logging.Log;
62 import org.apache.commons.logging.LogFactory;
64 import com.fasterxml.jackson.databind.ObjectMapper;
65 import com.fasterxml.jackson.databind.type.TypeFactory;
67 public class PortalRestAPICentralServiceImpl implements IPortalRestAPIService {
69 private static final Log logger = LogFactory.getLog(PortalRestAPICentralServiceImpl.class);
70 private String username;
71 private String password;
72 private String appName;
73 IPortalRestCentralService portalRestCentralService;
74 public static final String API_VERSION = "/v4";
75 private static String portalApiVersion = "/v3";
76 private static final String nameSpace = PortalApiProperties
77 .getProperty(PortalApiConstants.AUTH_NAMESPACE);
79 public PortalRestAPICentralServiceImpl() throws ServletException {
80 String centralClassName = PortalApiProperties.getProperty(PortalApiConstants.PORTAL_API_IMPL_CLASS);
81 if (centralClassName == null)
82 throw new ServletException(
83 "init: Failed to find class name property " + PortalApiConstants.PORTAL_API_IMPL_CLASS);
85 Class<?> centralImplClass = Class.forName(centralClassName);
86 portalRestCentralService = (IPortalRestCentralService) (centralImplClass.getConstructor().newInstance());
87 username = portalRestCentralService.getAppCredentials().get("username");
88 password = portalRestCentralService.getAppCredentials().get("password");
89 appName = portalRestCentralService.getAppCredentials().get("appName");
90 } catch (Exception e) {
91 throw new ClassCastException("Failed to find or instantiate class ");
95 private final ObjectMapper mapper = new ObjectMapper();
98 public void pushUser(EcompUser user) throws PortalAPIException {
99 portalRestCentralService.pushUser(user);
103 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
104 portalRestCentralService.editUser(loginId, user);
108 public EcompUser getUser(String loginId) throws PortalAPIException {
109 EcompUser user = new EcompUser();
110 String responseString = null;
112 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
113 appName, null, username, password, true);
114 logger.debug("responseString is: " + responseString);
115 user = mapper.readValue(responseString, EcompUser.class);
117 } catch (IOException e) {
118 String response = "Failed to get user from portal";
119 logger.error(response, e);
120 throw new PortalAPIException(response, e);
126 public List<EcompUser> getUsers() throws PortalAPIException {
127 List<EcompUser> usersList = new ArrayList<>();
128 String responseString = null;
130 responseString = RestWebServiceClient.getInstance().getPortalContent(portalApiVersion + "/users", null, appName, null,
131 username, password, true);
132 logger.debug("responseString is: " + responseString);
133 usersList = mapper.readValue(responseString,
134 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompUser.class));
136 } catch (IOException e) {
137 String response = "Failed to get the users from portal";
138 logger.error(response, e);
139 throw new PortalAPIException(response, e);
145 public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
146 List<EcompRole> rolesList = new ArrayList<>();
147 String responseString = null;
149 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/roles", requestedLoginId,
150 appName, null, username, password, true);
151 logger.debug("responseString is: " + responseString);
152 rolesList = mapper.readValue(responseString,
153 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompRole.class));
155 } catch (IOException e) {
156 String response = "Failed to get Roles from portal";
157 logger.error(response, e);
158 throw new PortalAPIException(response, e);
164 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
165 throw new PortalAPIException("Please use Portal for Role Management");
169 @SuppressWarnings("unchecked")
171 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
172 List<EcompRole> userRoles = new ArrayList<>();
173 EcompUser user = new EcompUser();
174 String responseString = null;
176 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
177 appName, null, username, password, true);
178 logger.debug("responseString is: " + responseString);
179 user = mapper.readValue(responseString, EcompUser.class);
180 Set roles = user.getRoles();
181 userRoles = (List<EcompRole>) roles.stream().collect(Collectors.toList());
183 } catch (IOException e) {
184 String response = "Failed to get user roles from portal";
185 logger.error(response, e);
186 throw new PortalAPIException(response, e);
192 public boolean isAppAuthenticated(HttpServletRequest request, Map<String,String> appCredentials) throws PortalAPIException {
193 boolean accessAllowed = false;
195 accessAllowed = AuthUtil.isAccessAllowed(request, nameSpace, appCredentials);
196 } catch (Exception e) {
199 return accessAllowed;
203 public String getUserId(HttpServletRequest request) throws PortalAPIException {
204 return portalRestCentralService.getUserId(request);
208 public Map<String, String> getCredentials() throws PortalAPIException{
209 Map<String, String> credentialsMap = new HashMap<>();
211 credentialsMap.put("username", username);
212 credentialsMap.put("password", password);
213 credentialsMap.put("appName", appName);
214 return credentialsMap;