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.onboarding.exception.CipherUtilException;
52 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
53 import org.onap.portalsdk.core.onboarding.rest.RestWebServiceClient;
54 import org.onap.portalsdk.core.onboarding.util.AuthUtil;
55 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
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;
60 import org.apache.commons.logging.Log;
61 import org.apache.commons.logging.LogFactory;
63 import com.fasterxml.jackson.databind.ObjectMapper;
64 import com.fasterxml.jackson.databind.type.TypeFactory;
66 public class PortalRestAPICentralServiceImpl implements IPortalRestAPIService {
68 private static final Log logger = LogFactory.getLog(PortalRestAPICentralServiceImpl.class);
69 private String username;
70 private String password;
71 private String appName;
72 IPortalRestCentralService portalRestCentralService;
73 public static final String API_VERSION = "/v4";
74 private static String portalApiVersion = "/v3";
75 private static final String nameSpace = PortalApiProperties
76 .getProperty(PortalApiConstants.AUTH_NAMESPACE);
78 public PortalRestAPICentralServiceImpl() throws ServletException {
79 String centralClassName = PortalApiProperties.getProperty(PortalApiConstants.PORTAL_API_IMPL_CLASS);
80 if (centralClassName == null)
81 throw new ServletException(
82 "init: Failed to find class name property " + PortalApiConstants.PORTAL_API_IMPL_CLASS);
84 Class<?> centralImplClass = Class.forName(centralClassName);
85 portalRestCentralService = (IPortalRestCentralService) (centralImplClass.getConstructor().newInstance());
86 username = portalRestCentralService.getAppCredentials().get("username");
87 password = portalRestCentralService.getAppCredentials().get("password");
88 appName = portalRestCentralService.getAppCredentials().get("appName");
89 } catch (Exception e) {
90 throw new ClassCastException("Failed to find or instantiate class ");
94 private final ObjectMapper mapper = new ObjectMapper();
97 public void pushUser(EcompUser user) throws PortalAPIException {
98 portalRestCentralService.pushUser(user);
102 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
103 portalRestCentralService.editUser(loginId, user);
107 public EcompUser getUser(String loginId) throws PortalAPIException {
108 EcompUser user = new EcompUser();
109 String responseString = null;
111 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
112 appName, null, username, password, true);
113 logger.debug("responseString is: " + responseString);
114 user = mapper.readValue(responseString, EcompUser.class);
116 } catch (IOException e) {
117 String response = "PortalRestAPICentralServiceImpl.getUser failed";
118 logger.error(response, e);
119 throw new PortalAPIException(response, e);
125 public List<EcompUser> getUsers() throws PortalAPIException {
126 List<EcompUser> usersList = new ArrayList<>();
127 String responseString = null;
129 responseString = RestWebServiceClient.getInstance().getPortalContent(portalApiVersion + "/users", null, appName, null,
130 username, password, true);
131 logger.debug("responseString is: " + responseString);
132 usersList = mapper.readValue(responseString,
133 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompUser.class));
135 } catch (IOException e) {
136 String response = "PortalRestAPICentralServiceImpl.getUsers failed";
137 logger.error(response, e);
138 throw new PortalAPIException(response, e);
144 public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
145 List<EcompRole> rolesList = new ArrayList<>();
146 String responseString = null;
148 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/roles", requestedLoginId,
149 appName, null, username, password, true);
150 logger.debug("responseString is: " + responseString);
151 rolesList = mapper.readValue(responseString,
152 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompRole.class));
154 } catch (IOException e) {
155 String response = "PortalRestAPICentralServiceImpl.getRoles failed";
156 logger.error(response, e);
157 throw new PortalAPIException(response, e);
163 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
164 throw new PortalAPIException("Please use Portal for Role Management");
168 @SuppressWarnings("unchecked")
170 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
171 List<EcompRole> userRoles = new ArrayList<>();
172 EcompUser user = new EcompUser();
173 String responseString = null;
175 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
176 appName, null, username, password, true);
177 logger.debug("responseString is: " + responseString);
178 user = mapper.readValue(responseString, EcompUser.class);
179 Set roles = user.getRoles();
180 userRoles = (List<EcompRole>) roles.stream().collect(Collectors.toList());
182 } catch (IOException e) {
183 String response = "PortalRestAPICentralServiceImpl.getUserRoles failed";
184 logger.error(response, e);
185 throw new PortalAPIException(response, e);
191 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
192 boolean accessAllowed = false;
194 accessAllowed = AuthUtil.isAccessAllowed(request, nameSpace);
195 } catch (Exception e) {
198 return accessAllowed;
202 public String getUserId(HttpServletRequest request) throws PortalAPIException {
203 return portalRestCentralService.getUserId(request);
207 public Map<String, String> getCredentials() throws PortalAPIException{
208 Map<String, String> credentialsMap = new HashMap<>();
210 credentialsMap.put("username", username);
211 credentialsMap.put("password", password);
212 credentialsMap.put("appName", appName);
213 return credentialsMap;