208e8c3d39bb4e917a165535e45894a8be1d171c
[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.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Set;
46 import java.util.stream.Collectors;
47
48 import javax.servlet.ServletException;
49 import javax.servlet.http.HttpServletRequest;
50
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;
61
62 import com.fasterxml.jackson.databind.ObjectMapper;
63 import com.fasterxml.jackson.databind.type.TypeFactory;
64
65 public class PortalRestAPICentralServiceImpl implements IPortalRestAPIService {
66
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";
74
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);
80                 try {
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 ");
88                 }
89         }
90
91         private final ObjectMapper mapper = new ObjectMapper();
92
93         @Override
94         public void pushUser(EcompUser user) throws PortalAPIException {
95                 portalRestCentralService.pushUser(user);
96         }
97
98         @Override
99         public void editUser(String loginId, EcompUser user) throws PortalAPIException {
100                 portalRestCentralService.editUser(loginId, user);
101         }
102
103         @Override
104         public EcompUser getUser(String loginId) throws PortalAPIException {
105                 EcompUser user = new EcompUser();
106                 String responseString = null;
107                 try {
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);
112
113                 } catch (IOException e) {
114                         String response = "PortalRestAPICentralServiceImpl.getUser failed";
115                         logger.error(response, e);
116                         throw new PortalAPIException(response, e);
117                 }
118                 return user;
119         }
120
121         @Override
122         public List<EcompUser> getUsers() throws PortalAPIException {
123                 List<EcompUser> usersList = new ArrayList<>();
124                 String responseString = null;
125                 try {
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));
131
132                 } catch (IOException e) {
133                         String response = "PortalRestAPICentralServiceImpl.getUsers failed";
134                         logger.error(response, e);
135                         throw new PortalAPIException(response, e);
136                 }
137                 return usersList;
138         }
139
140         @Override
141         public List<EcompRole> getAvailableRoles(String requestedLoginId) throws PortalAPIException {
142                 List<EcompRole> rolesList = new ArrayList<>();
143                 String responseString = null;
144                 try {
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));
150
151                 } catch (IOException e) {
152                         String response = "PortalRestAPICentralServiceImpl.getRoles failed";
153                         logger.error(response, e);
154                         throw new PortalAPIException(response, e);
155                 }
156                 return rolesList;
157         }
158
159         @Override
160         public void pushUserRole(String loginId, List<EcompRole> roles) throws PortalAPIException {
161                 throw new PortalAPIException("Please use Portal for Role Management");
162
163         }
164
165         @SuppressWarnings("unchecked")
166         @Override
167         public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
168                 List<EcompRole> userRoles = new ArrayList<>();
169                 EcompUser user = new EcompUser();
170                 String responseString = null;
171                 try {
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());
178
179                 } catch (IOException e) {
180                         String response = "PortalRestAPICentralServiceImpl.getUserRoles failed";
181                         logger.error(response, e);
182                         throw new PortalAPIException(response, e);
183                 }
184                 return userRoles;
185         }
186
187         @Override
188         public boolean isAppAuthenticated(HttpServletRequest request) throws PortalAPIException {
189                 boolean response = false;
190                 try {
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);
197                 }
198                 return response;
199         }
200
201         @Override
202         public String getUserId(HttpServletRequest request) throws PortalAPIException {
203                 return portalRestCentralService.getUserId(request);
204         }
205         
206         @Override
207         public Map<String, String> getCredentials() throws PortalAPIException{
208                 Map<String, String> credentialsMap = new HashMap<>();
209
210                 credentialsMap.put("username", username);
211                 credentialsMap.put("password", password);
212                 credentialsMap.put("appName", appName);
213                 return credentialsMap;
214         }
215
216 }