a82517d824c52bc19f2743400f9d9c1a76268b86
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalsdk.core.onboarding.crossapi;
39
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.Set;
44 import java.util.stream.Collectors;
45
46 import javax.servlet.ServletException;
47 import javax.servlet.http.HttpServletRequest;
48
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;
57
58 import com.fasterxml.jackson.databind.ObjectMapper;
59 import com.fasterxml.jackson.databind.type.TypeFactory;
60
61 public class PortalRestAPICentralServiceImpl implements IPortalRestAPIService {
62
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;
68
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);
74                 try {
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 ");
82                 }
83         }
84
85         private final ObjectMapper mapper = new ObjectMapper();
86
87         @Override
88         public void pushUser(EcompUser user) throws PortalAPIException {
89                 portalRestCentralService.pushUser(user);
90         }
91
92         @Override
93         public void editUser(String loginId, EcompUser user) throws PortalAPIException {
94                 portalRestCentralService.editUser(loginId, user);
95         }
96
97         @Override
98         public EcompUser getUser(String loginId) throws PortalAPIException {
99                 EcompUser user = new EcompUser();
100                 String responseString = null;
101                 try {
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);
106
107                 } catch (IOException e) {
108                         String response = "PortalRestAPICentralServiceImpl.getUser failed";
109                         logger.error(response, e);
110                         throw new PortalAPIException(response, e);
111                 }
112                 return user;
113         }
114
115         @Override
116         public List<EcompUser> getUsers() throws PortalAPIException {
117                 List<EcompUser> usersList = new ArrayList<>();
118                 String responseString = null;
119                 try {
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));
125
126                 } catch (IOException e) {
127                         String response = "PortalRestAPICentralServiceImpl.getUsers failed";
128                         logger.error(response, e);
129                         throw new PortalAPIException(response, e);
130                 }
131                 return usersList;
132         }
133
134         @Override
135         public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
136                 List<EcompRole> rolesList = new ArrayList<>();
137                 String responseString = null;
138                 try {
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));
144
145                 } catch (IOException e) {
146                         String response = "PortalRestAPICentralServiceImpl.getRoles failed";
147                         logger.error(response, e);
148                         throw new PortalAPIException(response, e);
149                 }
150                 return rolesList;
151         }
152
153         @Override
154         public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
155                 throw new PortalAPIException("Please use Portal for Role Management");
156
157         }
158
159         @SuppressWarnings("unchecked")
160         @Override
161         public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
162                 List<EcompRole> userRoles = new ArrayList<>();
163                 EcompUser user = new EcompUser();
164                 String responseString = null;
165                 try {
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());
172
173                 } catch (IOException e) {
174                         String response = "PortalRestAPICentralServiceImpl.getUserRoles failed";
175                         logger.error(response, e);
176                         throw new PortalAPIException(response, e);
177                 }
178                 return userRoles;
179         }
180
181         @Override
182         public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
183                 boolean response = false;
184                 try {
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);
191                 }
192                 return response;
193         }
194
195         @Override
196         public String getUserId(HttpServletRequest request) throws PortalAPIException {
197                 return portalRestCentralService.getUserId(request);
198         }
199
200 }