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.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
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.PortalApiConstants;
57 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
58 import org.onap.portalsdk.core.restful.domain.EcompRole;
59 import org.onap.portalsdk.core.restful.domain.EcompUser;
61 import com.fasterxml.jackson.databind.ObjectMapper;
62 import com.fasterxml.jackson.databind.type.TypeFactory;
64 public class PortalRestAPICentralServiceImpl implements IPortalRestAPIService {
66 private static final Log logger = LogFactory.getLog(PortalRestAPICentralServiceImpl.class);
67 private String username;
68 private String password;
69 private String appName;
70 IPortalRestCentralService portalRestCentralService;
71 public static final String API_VERSION = "/v4";
72 private static String portalApiVersion = "/v3";
73 private static final String nameSpace = PortalApiProperties
74 .getProperty(PortalApiConstants.AUTH_NAMESPACE);
76 public PortalRestAPICentralServiceImpl() throws ServletException {
77 String centralClassName = PortalApiProperties.getProperty(PortalApiConstants.PORTAL_API_IMPL_CLASS);
78 if (centralClassName == null)
79 throw new ServletException(
80 "init: Failed to find class name property " + PortalApiConstants.PORTAL_API_IMPL_CLASS);
82 Class<?> centralImplClass = Class.forName(centralClassName);
83 portalRestCentralService = (IPortalRestCentralService) (centralImplClass.getConstructor().newInstance());
84 username = portalRestCentralService.getAppCredentials().get("username");
85 password = portalRestCentralService.getAppCredentials().get("password");
86 appName = portalRestCentralService.getAppCredentials().get("appName");
87 } catch (Exception e) {
88 throw new ClassCastException("Failed to find or instantiate class ");
92 private final ObjectMapper mapper = new ObjectMapper();
95 public void pushUser(EcompUser user) throws PortalAPIException {
96 portalRestCentralService.pushUser(user);
100 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
101 portalRestCentralService.editUser(loginId, user);
105 public EcompUser getUser(String loginId) throws PortalAPIException {
106 EcompUser user = new EcompUser();
107 String responseString = null;
109 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
110 appName, null, username, password, true);
111 logger.debug("responseString is: " + responseString);
112 user = mapper.readValue(responseString, EcompUser.class);
114 } catch (IOException e) {
115 String response = "Failed to get user from portal";
116 logger.error(response, e);
117 throw new PortalAPIException(response, e);
123 public List<EcompUser> getUsers() throws PortalAPIException {
124 List<EcompUser> usersList = new ArrayList<>();
125 String responseString = null;
127 responseString = RestWebServiceClient.getInstance().getPortalContent(portalApiVersion + "/users", null, appName, null,
128 username, password, true);
129 logger.debug("responseString is: " + responseString);
130 usersList = mapper.readValue(responseString,
131 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompUser.class));
133 } catch (IOException e) {
134 String response = "Failed to get the users from portal";
135 logger.error(response, e);
136 throw new PortalAPIException(response, e);
142 public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
143 List<EcompRole> rolesList = new ArrayList<>();
144 String responseString = null;
146 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/roles", requestedLoginId,
147 appName, null, username, password, true);
148 logger.debug("responseString is: " + responseString);
149 rolesList = mapper.readValue(responseString,
150 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompRole.class));
152 } catch (IOException e) {
153 String response = "Failed to get Roles from portal";
154 logger.error(response, e);
155 throw new PortalAPIException(response, e);
161 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
162 throw new PortalAPIException("Please use Portal for Role Management");
166 @SuppressWarnings("unchecked")
168 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
169 List<EcompRole> userRoles = new ArrayList<>();
170 EcompUser user = new EcompUser();
171 String responseString = null;
173 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
174 appName, null, username, password, true);
175 logger.debug("responseString is: " + responseString);
176 user = mapper.readValue(responseString, EcompUser.class);
177 Set roles = user.getRoles();
178 userRoles = (List<EcompRole>) roles.stream().collect(Collectors.toList());
180 } catch (IOException e) {
181 String response = "Failed to get user roles from portal";
182 logger.error(response, e);
183 throw new PortalAPIException(response, e);
189 public boolean isAppAuthenticated(HttpServletRequest request, Map<String,String> appCredentials) throws PortalAPIException {
190 boolean accessAllowed = false;
192 accessAllowed = AuthUtil.isAccessAllowed(request, nameSpace, appCredentials);
193 } catch (Exception e) {
196 return accessAllowed;
200 public String getUserId(HttpServletRequest request) throws PortalAPIException {
201 return portalRestCentralService.getUserId(request);
205 public Map<String, String> getCredentials() throws PortalAPIException{
206 Map<String, String> credentialsMap = new HashMap<>();
208 credentialsMap.put("username", username);
209 credentialsMap.put("password", password);
210 credentialsMap.put("appName", appName);
211 return credentialsMap;