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.CipherUtil;
55 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
56 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
57 import org.onap.portalsdk.core.restful.domain.EcompRole;
58 import org.onap.portalsdk.core.restful.domain.EcompUser;
59 import org.apache.commons.logging.Log;
60 import org.apache.commons.logging.LogFactory;
62 import com.fasterxml.jackson.databind.ObjectMapper;
63 import com.fasterxml.jackson.databind.type.TypeFactory;
65 public class PortalRestAPICentralServiceImpl implements IPortalRestAPIService {
67 private static final Log logger = LogFactory.getLog(PortalRestAPICentralServiceImpl.class);
68 private String username;
69 private String password;
70 private String appName;
71 IPortalRestCentralService portalRestCentralService;
72 public static final String API_VERSION = "/v4";
73 private static String portalApiVersion = "/v3";
75 public PortalRestAPICentralServiceImpl() throws ServletException {
76 String centralClassName = PortalApiProperties.getProperty(PortalApiConstants.PORTAL_API_IMPL_CLASS);
77 if (centralClassName == null)
78 throw new ServletException(
79 "init: Failed to find class name property " + PortalApiConstants.PORTAL_API_IMPL_CLASS);
81 Class<?> centralImplClass = Class.forName(centralClassName);
82 portalRestCentralService = (IPortalRestCentralService) (centralImplClass.getConstructor().newInstance());
83 username = portalRestCentralService.getAppCredentials().get("username");
84 password = portalRestCentralService.getAppCredentials().get("password");
85 appName = portalRestCentralService.getAppCredentials().get("appName");
86 } catch (Exception e) {
87 throw new ClassCastException("Failed to find or instantiate class ");
91 private final ObjectMapper mapper = new ObjectMapper();
94 public void pushUser(EcompUser user) throws PortalAPIException {
95 portalRestCentralService.pushUser(user);
99 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
100 portalRestCentralService.editUser(loginId, user);
104 public EcompUser getUser(String loginId) throws PortalAPIException {
105 EcompUser user = new EcompUser();
106 String responseString = null;
108 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
109 appName, null, username, password, true);
110 logger.debug("responseString is: " + responseString);
111 user = mapper.readValue(responseString, EcompUser.class);
113 } catch (IOException e) {
114 String response = "PortalRestAPICentralServiceImpl.getUser failed";
115 logger.error(response, e);
116 throw new PortalAPIException(response, e);
122 public List<EcompUser> getUsers() throws PortalAPIException {
123 List<EcompUser> usersList = new ArrayList<>();
124 String responseString = null;
126 responseString = RestWebServiceClient.getInstance().getPortalContent(portalApiVersion + "/users", null, appName, null,
127 username, password, true);
128 logger.debug("responseString is: " + responseString);
129 usersList = mapper.readValue(responseString,
130 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompUser.class));
132 } catch (IOException e) {
133 String response = "PortalRestAPICentralServiceImpl.getUsers failed";
134 logger.error(response, e);
135 throw new PortalAPIException(response, e);
141 public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
142 List<EcompRole> rolesList = new ArrayList<>();
143 String responseString = null;
145 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/roles", requestedLoginId,
146 appName, null, username, password, true);
147 logger.debug("responseString is: " + responseString);
148 rolesList = mapper.readValue(responseString,
149 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompRole.class));
151 } catch (IOException e) {
152 String response = "PortalRestAPICentralServiceImpl.getRoles failed";
153 logger.error(response, e);
154 throw new PortalAPIException(response, e);
160 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
161 throw new PortalAPIException("Please use Portal for Role Management");
165 @SuppressWarnings("unchecked")
167 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
168 List<EcompRole> userRoles = new ArrayList<>();
169 EcompUser user = new EcompUser();
170 String responseString = null;
172 responseString = RestWebServiceClient.getInstance().getPortalContent(API_VERSION + "/user/" + loginId, null,
173 appName, null, username, password, true);
174 logger.debug("responseString is: " + responseString);
175 user = mapper.readValue(responseString, EcompUser.class);
176 Set roles = user.getRoles();
177 userRoles = (List<EcompRole>) roles.stream().collect(Collectors.toList());
179 } catch (IOException e) {
180 String response = "PortalRestAPICentralServiceImpl.getUserRoles failed";
181 logger.error(response, e);
182 throw new PortalAPIException(response, e);
188 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
189 boolean response = false;
191 String restUser = request.getHeader("username");
192 String restPw = request.getHeader("password");
193 response = restUser != null && restPw != null && restUser.equals(username) && restPw.equals(password);
194 logger.debug("isAppAuthenticated: " + response);
195 } catch (Exception ex) {
196 throw new PortalAPIException("isAppAuthenticated failed", ex);
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;