2 * ================================================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ================================================================================
\r
20 package org.openecomp.portalapp.portal.service;
\r
22 import java.util.ArrayList;
\r
23 import java.util.Collections;
\r
24 import java.util.List;
\r
26 import javax.naming.NamingEnumeration;
\r
27 import javax.naming.NamingException;
\r
28 import javax.naming.directory.Attribute;
\r
29 import javax.naming.directory.Attributes;
\r
30 import javax.naming.directory.DirContext;
\r
31 import javax.naming.directory.SearchControls;
\r
33 import org.slf4j.MDC;
\r
34 import org.springframework.beans.factory.annotation.Autowired;
\r
35 import org.springframework.context.annotation.EnableAspectJAutoProxy;
\r
36 import org.springframework.stereotype.Service;
\r
37 import org.springframework.transaction.annotation.Transactional;
\r
39 import org.openecomp.portalsdk.core.command.support.SearchResult;
\r
40 import org.openecomp.portalsdk.core.domain.support.DomainVo;
\r
41 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
42 import org.openecomp.portalsdk.core.service.support.FusionService;
\r
43 import org.openecomp.portalsdk.core.service.support.ServiceLocator;
\r
44 import org.openecomp.portalsdk.core.util.SystemProperties;
\r
45 import org.openecomp.portalapp.portal.domain.EPUser;
\r
46 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
\r
47 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
\r
48 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
\r
49 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
\r
51 @Service("epLdapService")
\r
53 @org.springframework.context.annotation.Configuration
\r
54 @EnableAspectJAutoProxy
\r
55 public class EPLdapServiceImpl extends FusionService implements EPLdapService {
\r
57 private ServiceLocator serviceLocator;
\r
59 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLdapServiceImpl.class);
\r
62 @SuppressWarnings({ "rawtypes", "unchecked" })
\r
63 public SearchResult searchPost(DomainVo searchCriteria, String sortBy1, String sortBy2, String sortBy3,
\r
64 int pageNo, int dataSize, int userId) throws Exception {
\r
66 String remoteHost = "";
\r
68 // initialize the directory context to access POST
\r
69 DirContext dirContext = serviceLocator.getDirContext(SystemProperties.getProperty(SystemProperties.POST_INITIAL_CONTEXT_FACTORY),
\r
70 SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL),
\r
71 SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL));
\r
73 SearchResult searchResult = new SearchResult();
\r
77 remoteHost = String.format("%s/%s", SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL),
\r
78 SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL));
\r
79 MDC.put(EPCommonSystemProperties.FULL_URL, remoteHost);
\r
81 String[] postAttributes = {"nickname","givenName","initials","sn","employeeNumber","mail","telephoneNumber",
\r
82 "departmentNumber","a1","street","roomNumber","l","st","postalCode","zip4","physicalDeliveryOfficeName","bc",
\r
83 "friendlyCountryName","bd","bdname","bu","buname","jtname","mgrid","a2","compcode","compdesc",
\r
84 "costcenter","silo","b2"};
\r
86 SearchControls searchControls = new SearchControls();
\r
87 searchControls.setTimeLimit(5000);
\r
88 searchControls.setReturningAttributes(postAttributes);
\r
90 StringBuffer filterClause = new StringBuffer("(&(objectClass=*)");
\r
92 EPUser user = (EPUser)searchCriteria;
\r
94 if(Utilities.nvl(user.getFirstName()).length() > 0) {
\r
95 filterClause.append("(givenName=").append(user.getFirstName()).append("*)");
\r
97 if(Utilities.nvl(user.getLastName()).length() > 0) {
\r
98 filterClause.append("(sn=").append(user.getLastName()).append("*)");
\r
100 if(Utilities.nvl(user.getHrid()).length() > 0) {
\r
101 filterClause.append("(employeeNumber=").append(user.getHrid()).append("*)");
\r
103 if(Utilities.nvl(user.getOrgManagerUserId()).length() > 0) {
\r
104 filterClause.append("(mgrid=").append(user.getOrgManagerUserId()).append("*)");
\r
106 if(Utilities.nvl(user.getOrgCode()).length() > 0) {
\r
107 filterClause.append("(departmentNumber=").append(user.getOrgCode()).append("*)");
\r
109 if(Utilities.nvl(user.getEmail()).length() > 0) {
\r
110 filterClause.append("(mail=").append(user.getEmail()).append("*)");
\r
112 if(Utilities.nvl(user.getOrgUserId()).length() > 0) {
\r
113 filterClause.append("(a1=").append(user.getOrgUserId()).append("*)");
\r
115 filterClause.append("(c3=N)"); // this has been added to filter CP09 entries on the LDAP server that are duplicates of existing individuals
\r
116 filterClause.append(")");
\r
118 List list = new ArrayList();
\r
119 if (!filterClause.toString().equals("(&(objectClass=*))")) {
\r
120 NamingEnumeration e = dirContext.search(SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL) + "/" +
\r
121 SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL),
\r
122 filterClause.toString(),
\r
124 list = processResults(e);
\r
127 Collections.sort(list);
\r
129 searchResult = new SearchResult(list);
\r
130 searchResult.setPageNo(pageNo);
\r
131 if(dataSize >= 0) {
\r
132 searchResult.setDataSize(dataSize);
\r
135 searchResult.setDataSize(list.size());
\r
137 } catch(NamingException ne) {
\r
138 String stackTrace = EcompPortalUtils.getStackTrace(ne);
\r
139 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the LDAP search. Details: " + stackTrace);
\r
140 } catch(Exception e) {
\r
141 String stackTrace = EcompPortalUtils.getStackTrace(e);
\r
142 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the LDAP search. Details: " + stackTrace);
\r
145 dirContext.close();
\r
148 return searchResult;
\r
152 @SuppressWarnings({ "rawtypes", "unchecked" })
\r
154 private ArrayList processResults(NamingEnumeration e) throws NamingException {
\r
155 ArrayList results = new ArrayList();
\r
158 while (e.hasMore()) {
\r
159 javax.naming.directory.SearchResult searchResult = (javax.naming.directory.SearchResult)e.next();
\r
160 results.add(processAttributes(searchResult.getAttributes()));
\r
163 if(count > Integer.parseInt(SystemProperties.getProperty(SystemProperties.POST_MAX_RESULT_SIZE))) {
\r
171 @SuppressWarnings("rawtypes")
\r
173 private DomainVo processAttributes(Attributes resultAttributes) throws NamingException {
\r
174 EPUser user = new EPUser();
\r
177 if (resultAttributes == null) {
\r
178 logger.debug(EELFLoggerDelegate.debugLogger, "This result has no attributes");
\r
180 for (NamingEnumeration e = resultAttributes.getAll(); e.hasMore();) { //why the nested loop?
\r
181 Attribute attribute = (Attribute)e.next();
\r
182 for (NamingEnumeration ie = attribute.getAll(); ie.hasMore();) {
\r
183 if (attribute.getID().equalsIgnoreCase("nickname")) {
\r
184 user.setFirstName((String) ie.next());
\r
186 else if (attribute.getID().equalsIgnoreCase("initials")) {
\r
187 user.setMiddleInitial((String) ie.next());
\r
189 else if (attribute.getID().equalsIgnoreCase("sn")) {
\r
190 user.setLastName((String) ie.next());
\r
192 else if (attribute.getID().equalsIgnoreCase("employeeNumber")) {
\r
193 user.setHrid((String) ie.next());
\r
195 else if (attribute.getID().equalsIgnoreCase("mail")) {
\r
196 user.setEmail((String) ie.next());
\r
198 else if (attribute.getID().equalsIgnoreCase("telephoneNumber")) {
\r
199 user.setPhone((String) ie.next());
\r
201 else if (attribute.getID().equalsIgnoreCase("departmentNumber")) {
\r
202 user.setOrgCode((String) ie.next());
\r
204 else if (attribute.getID().equalsIgnoreCase("a1")) {
\r
205 user.setOrgUserId((String) ie.next());
\r
207 else if (attribute.getID().equalsIgnoreCase("street")) {
\r
208 user.setAddress1((String) ie.next());
\r
210 else if (attribute.getID().equalsIgnoreCase("roomNumber")) {
\r
211 user.setAddress2((String) ie.next());
\r
213 else if (attribute.getID().equalsIgnoreCase("l")) {
\r
214 user.setCity((String) ie.next());
\r
216 else if (attribute.getID().equalsIgnoreCase("st")) {
\r
217 user.setState((String) ie.next());
\r
219 else if (attribute.getID().equalsIgnoreCase("postalCode")) {
\r
220 user.setZipCode((String) ie.next());
\r
222 else if (attribute.getID().equalsIgnoreCase("zip4")) {
\r
223 user.setZipCodeSuffix((String) ie.next());
\r
225 else if (attribute.getID().equalsIgnoreCase("physicalDeliveryOfficeName")) {
\r
226 user.setLocationClli((String) ie.next());
\r
228 else if (attribute.getID().equalsIgnoreCase("bc")) {
\r
229 user.setBusinessCountryCode((String) ie.next());
\r
231 else if (attribute.getID().equalsIgnoreCase("friendlyCountryName")) {
\r
232 user.setBusinessCountryName((String) ie.next());
\r
234 else if (attribute.getID().equalsIgnoreCase("bd")) {
\r
235 user.setDepartment((String) ie.next());
\r
237 else if (attribute.getID().equalsIgnoreCase("bdname")) {
\r
238 user.setDepartmentName((String) ie.next());
\r
240 else if (attribute.getID().equalsIgnoreCase("jtname")) {
\r
241 user.setJobTitle((String) ie.next());
\r
243 else if (attribute.getID().equalsIgnoreCase("mgrid")) {
\r
244 user.setOrgManagerUserId((String) ie.next());
\r
246 else if (attribute.getID().equalsIgnoreCase("a2")) {
\r
247 user.setCommandChain((String) ie.next());
\r
249 else if (attribute.getID().equalsIgnoreCase("compcode")) {
\r
250 user.setCompanyCode((String) ie.next());
\r
252 else if (attribute.getID().equalsIgnoreCase("compdesc")) {
\r
253 user.setCompany((String) ie.next());
\r
255 else if (attribute.getID().equalsIgnoreCase("bu")) {
\r
256 user.setBusinessUnit((String)ie.next());
\r
258 else if (attribute.getID().equalsIgnoreCase("buname")) {
\r
259 user.setBusinessUnitName((String)ie.next());
\r
261 else if (attribute.getID().equalsIgnoreCase("silo")) {
\r
262 user.setSiloStatus((String)ie.next());
\r
264 else if (attribute.getID().equalsIgnoreCase("costcenter")) {
\r
265 user.setCostCenter((String)ie.next());
\r
267 else if (attribute.getID().equalsIgnoreCase("b2")) {
\r
268 user.setFinancialLocCode((String)ie.next());
\r
270 else { //we don't care about returned attribute, let's move on
\r
277 } catch (NamingException e) {
\r
278 String stackTrace = EcompPortalUtils.getStackTrace(e);
\r
279 logger.error(EELFLoggerDelegate.errorLogger, "An error occurred while processing the following user from POST with an Organization User ID of " + user.getOrgUserId());
\r
280 logger.error(EELFLoggerDelegate.errorLogger, "Exception occcurred while processing LDAP search results, Details: " + stackTrace);
\r
281 } catch(Exception e) {
\r
282 String stackTrace = EcompPortalUtils.getStackTrace(e);
\r
283 logger.error(EELFLoggerDelegate.errorLogger, "Exception occcurred while processing LDAP search results, Details: " + stackTrace);
\r