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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.core.onboarding.crossapi;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
44 import java.util.stream.Collectors;
46 import javax.servlet.ServletException;
47 import javax.servlet.http.HttpServletRequest;
49 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
50 import org.onap.portalsdk.core.onboarding.rest.RestWebServiceClient;
51 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
52 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
53 import org.onap.portalsdk.core.restful.domain.EcompRole;
54 import org.onap.portalsdk.core.restful.domain.EcompUser;
55 import org.apache.commons.logging.Log;
56 import org.apache.commons.logging.LogFactory;
58 import com.fasterxml.jackson.databind.ObjectMapper;
59 import com.fasterxml.jackson.databind.type.TypeFactory;
61 public class PortalRestAPICentralServiceImpl implements IPortalRestAPIService {
63 private static final Log logger = LogFactory.getLog(PortalRestAPICentralServiceImpl.class);
64 private String username;
65 private String password;
66 private String appName;
67 IPortalRestCentralService portalRestCentralService;
69 public PortalRestAPICentralServiceImpl() throws ServletException {
70 String centralClassName = PortalApiProperties.getProperty(PortalApiConstants.PORTAL_API_IMPL_CLASS);
71 if (centralClassName == null)
72 throw new ServletException(
73 "init: Failed to find class name property " + PortalApiConstants.PORTAL_API_IMPL_CLASS);
75 Class<?> centralImplClass = Class.forName(centralClassName);
76 portalRestCentralService = (IPortalRestCentralService) (centralImplClass.getConstructor().newInstance());
77 username = portalRestCentralService.getAppCredentials().get("username");
78 password = portalRestCentralService.getAppCredentials().get("password");
79 appName = portalRestCentralService.getAppCredentials().get("appName");
80 } catch (Exception e) {
81 throw new ClassCastException("Failed to find or instantiate class ");
85 private final ObjectMapper mapper = new ObjectMapper();
88 public void pushUser(EcompUser user) throws PortalAPIException {
89 portalRestCentralService.pushUser(user);
93 public void editUser(String loginId, EcompUser user) throws PortalAPIException {
94 portalRestCentralService.editUser(loginId, user);
98 public EcompUser getUser(String loginId) throws PortalAPIException {
99 EcompUser user = new EcompUser();
100 String responseString = null;
102 responseString = RestWebServiceClient.getInstance().getPortalContent("/v2/user/" + loginId, null,
103 appName, null, username, password, true);
104 logger.debug("responseString is: " + responseString);
105 user = mapper.readValue(responseString, EcompUser.class);
107 } catch (IOException e) {
108 String response = "PortalRestAPICentralServiceImpl.getUser failed";
109 logger.error(response, e);
110 throw new PortalAPIException(response, e);
116 public List<EcompUser> getUsers() throws PortalAPIException {
117 List<EcompUser> usersList = new ArrayList<>();
118 String responseString = null;
120 responseString = RestWebServiceClient.getInstance().getPortalContent("/users", null, appName, null,
121 username, password, true);
122 logger.debug("responseString is: " + responseString);
123 usersList = mapper.readValue(responseString,
124 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompUser.class));
126 } catch (IOException e) {
127 String response = "PortalRestAPICentralServiceImpl.getUsers failed";
128 logger.error(response, e);
129 throw new PortalAPIException(response, e);
135 public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
136 List<EcompRole> rolesList = new ArrayList<>();
137 String responseString = null;
139 responseString = RestWebServiceClient.getInstance().getPortalContent("/v2/roles", requestedLoginId,
140 appName, null, username, password, true);
141 logger.debug("responseString is: " + responseString);
142 rolesList = mapper.readValue(responseString,
143 TypeFactory.defaultInstance().constructCollectionType(List.class, EcompRole.class));
145 } catch (IOException e) {
146 String response = "PortalRestAPICentralServiceImpl.getUsers failed";
147 logger.error(response, e);
148 throw new PortalAPIException(response, e);
154 public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
155 throw new PortalAPIException("Please use Portal for Role Management");
159 @SuppressWarnings("unchecked")
161 public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
162 List<EcompRole> userRoles = new ArrayList<>();
163 EcompUser user = new EcompUser();
164 String responseString = null;
166 responseString = RestWebServiceClient.getInstance().getPortalContent("/v2/user/" + loginId, null,
167 appName, null, username, password, true);
168 logger.debug("responseString is: " + responseString);
169 user = mapper.readValue(responseString, EcompUser.class);
170 Set roles = user.getRoles();
171 userRoles = (List<EcompRole>) roles.stream().collect(Collectors.toList());
173 } catch (IOException e) {
174 String response = "PortalRestAPICentralServiceImpl.getUserRoles failed";
175 logger.error(response, e);
176 throw new PortalAPIException(response, e);
182 public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
183 boolean response = false;
185 String restUser = request.getHeader("username");
186 String restPw = request.getHeader("password");
187 response = restUser != null && restPw != null && restUser.equals(username) && restPw.equals(password);
188 logger.debug("isAppAuthenticated: " + response);
189 } catch (Exception ex) {
190 throw new PortalAPIException("isAppAuthenticated failed", ex);
196 public String getUserId(HttpServletRequest request) throws PortalAPIException {
197 return portalRestCentralService.getUserId(request);