2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.portal.service;
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.net.HttpURLConnection;
27 import java.util.ArrayList;
28 import java.util.List;
30 import org.json.JSONArray;
31 import org.json.JSONObject;
32 import org.openecomp.portalapp.portal.domain.EPUser;
33 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
34 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
35 import org.openecomp.portalsdk.core.FusionObject.Utilities;
36 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 import org.openecomp.portalsdk.core.service.DataAccessService;
38 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
39 import org.openecomp.portalsdk.core.util.SystemProperties;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Service;
42 import org.springframework.transaction.annotation.Transactional;
44 @Service("userService")
46 public class UserServiceImpl implements UserService {
48 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserServiceImpl.class);
51 private DataAccessService dataAccessService;
53 public DataAccessService getDataAccessService() {
54 return dataAccessService;
57 public void setDataAccessService(DataAccessService dataAccessService) {
58 this.dataAccessService = dataAccessService;
61 @SuppressWarnings("rawtypes")
63 public List getUserByUserId(String userId) {
66 if(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")){
67 List<EPUser> users=new ArrayList<EPUser>();
68 List<EPUser> filterdUsers=new ArrayList<EPUser>();
69 BufferedReader in = null;
70 HttpURLConnection con = null;
72 String url = EPSystemProperties.getProperty(EPSystemProperties.AUTH_USER_SERVER);
73 URL obj = new URL(url);
75 con = (HttpURLConnection) obj.openConnection();
77 // optional default is GET
78 con.setRequestMethod("GET");
79 con.setConnectTimeout(3000);
80 con.setReadTimeout(8000);
82 StringBuffer response = new StringBuffer();
84 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
86 while ((inputLine = in.readLine()) != null)
87 response.append(inputLine);
88 JSONObject jObject = new JSONObject(response.toString()); // json
89 JSONArray jsonUsers = jObject.getJSONArray("response"); // get data object
90 for (int i = 0; i < jsonUsers.length(); i++) {
91 JSONObject eachObject = jsonUsers.getJSONObject(i);
92 EPUser eachUser = new EPUser();
93 eachUser.setOrgUserId(eachObject.get("id").toString());// getString("id"));
94 eachUser.setFirstName(eachObject.get("givenName").toString());
95 eachUser.setLastName(eachObject.get("familyName").toString());
96 eachUser.setEmail(eachObject.get("email").toString());
100 for(int i = 0 ; i < users.size(); i ++){
102 if(Utilities.nvl(userId).length() > 0){
103 if(!userId.equalsIgnoreCase(users.get(i).getOrgUserId())){
107 filterdUsers.add(users.get(i));
111 }catch (Exception e){
112 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
117 } catch (IOException e) {
118 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
127 StringBuffer criteria = new StringBuffer();
128 criteria.append(" where org_user_id = '").append(userId).append("'");
129 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
130 return (list == null || list.size() == 0) ? null : list;
136 @SuppressWarnings("rawtypes")
138 public List getUserByFirstLastName(String firstName, String lastName) {
140 if(!SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM).trim().equalsIgnoreCase("OIDC")){
143 StringBuffer criteria = new StringBuffer();
145 criteria.append(" where first_name = '").append(firstName).append("'");
147 criteria.append(" where last_name = '").append(lastName).append("'");
148 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
149 return (list == null || list.size() == 0) ? null : list;
153 List<EPUser> users=new ArrayList<EPUser>();
154 List<EPUser> filterdUsers=new ArrayList<EPUser>();
155 BufferedReader in = null;
156 HttpURLConnection con = null;
158 String url = EPSystemProperties.getProperty(EPSystemProperties.AUTH_USER_SERVER);
159 URL obj = new URL(url);
161 con = (HttpURLConnection) obj.openConnection();
163 // optional default is GET
164 con.setRequestMethod("GET");
165 con.setConnectTimeout(3000);
166 con.setReadTimeout(8000);
168 StringBuffer response = new StringBuffer();
170 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
172 while ((inputLine = in.readLine()) != null)
173 response.append(inputLine);
174 JSONObject jObject = new JSONObject(response.toString()); // json
175 JSONArray jsonUsers = jObject.getJSONArray("response"); // get data object
176 for (int i = 0; i < jsonUsers.length(); i++) {
177 JSONObject eachObject = jsonUsers.getJSONObject(i);
178 EPUser eachUser = new EPUser();
179 eachUser.setOrgUserId(eachObject.get("id").toString());// getString("id"));
180 eachUser.setFirstName(eachObject.get("givenName").toString());
181 eachUser.setLastName(eachObject.get("familyName").toString());
182 eachUser.setEmail(eachObject.get("email").toString());
186 for(int i = 0 ; i < users.size(); i ++){
188 if(Utilities.nvl(firstName).length() > 0){
189 if(!firstName.equalsIgnoreCase(users.get(i).getFirstName())){
193 if(Utilities.nvl(lastName).length() > 0){
194 if(!lastName.equalsIgnoreCase(users.get(i).getLastName())){
200 filterdUsers.add(users.get(i));
204 }catch (Exception e){
205 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
210 } catch (IOException e) {
211 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
220 public String saveNewUser(EPUser newUser, String checkDuplicate) throws Exception{
225 StringBuffer criteria = new StringBuffer();
226 criteria.append(" where org_user_id = '").append(newUser.getLoginId()).append("'");
227 list = getDataAccessService().getList(EPUser.class, criteria.toString(), null, null);
228 if(list == null || list.size()==0){
229 newUser.setActive(true);
230 newUser.setOrgUserId(newUser.getLoginId());
231 newUser.setLoginPwd(CipherUtil.encrypt(newUser.getLoginPwd()));
232 getDataAccessService().saveDomainObject(newUser, null);
234 if(checkDuplicate.equals("Yes")){
235 // userId already exist in database
236 return "Record already exist";
239 EPUser oldUser = (EPUser) list.get(0);
240 oldUser.setFirstName(newUser.getFirstName());
241 oldUser.setLastName(newUser.getLastName());
242 oldUser.setMiddleInitial(newUser.getMiddleInitial());
243 if(!oldUser.getLoginPwd().equals(newUser.getLoginPwd()))
244 oldUser.setLoginPwd(CipherUtil.encrypt(newUser.getLoginPwd()));
246 oldUser.setLoginPwd(newUser.getLoginPwd());
247 getDataAccessService().saveDomainObject(oldUser, null);
253 }catch (Exception e) {
254 logger.error(EELFLoggerDelegate.errorLogger, "", e);
255 throw new Exception(e);