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.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
26 import javax.naming.NamingEnumeration;
27 import javax.naming.NamingException;
28 import javax.naming.directory.Attribute;
29 import javax.naming.directory.Attributes;
30 import javax.naming.directory.DirContext;
31 import javax.naming.directory.SearchControls;
33 import org.openecomp.portalapp.portal.domain.EPUser;
34 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
35 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
36 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
37 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
38 import org.openecomp.portalsdk.core.command.support.SearchResult;
39 import org.openecomp.portalsdk.core.domain.support.DomainVo;
40 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
41 import org.openecomp.portalsdk.core.service.support.FusionService;
42 import org.openecomp.portalsdk.core.service.support.ServiceLocator;
43 import org.openecomp.portalsdk.core.util.SystemProperties;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.context.annotation.EnableAspectJAutoProxy;
47 import org.springframework.stereotype.Service;
48 import org.springframework.transaction.annotation.Transactional;
50 @Service("epLdapService")
52 @org.springframework.context.annotation.Configuration
53 @EnableAspectJAutoProxy
54 public class EPLdapServiceImpl extends FusionService implements EPLdapService {
56 private ServiceLocator serviceLocator;
58 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLdapServiceImpl.class);
61 @SuppressWarnings({ "rawtypes", "unchecked" })
62 public SearchResult searchPost(DomainVo searchCriteria, String sortBy1, String sortBy2, String sortBy3,
63 int pageNo, int dataSize, int orgUserId) throws Exception {
65 String remoteHost = "";
67 // initialize the directory context to access POST
68 DirContext dirContext = serviceLocator.getDirContext(SystemProperties.getProperty(SystemProperties.POST_INITIAL_CONTEXT_FACTORY),
69 SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL),
70 SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL));
72 SearchResult searchResult = new SearchResult();
76 remoteHost = String.format("%s/%s", SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL),
77 SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL));
78 MDC.put(EPSystemProperties.FULL_URL, remoteHost);
80 String[] postAttributes = {"nickname","givenName","initials","sn","employeeNumber","mail","telephoneNumber",
81 "departmentNumber","a1","street","roomNumber","l","st","postalCode","zip4","physicalDeliveryOfficeName","bc",
82 "friendlyCountryName","bd","bdname","bu","buname","jtname","mgrid","a2","compcode","compdesc",
83 "costcenter","silo","b2"};
85 SearchControls searchControls = new SearchControls();
86 searchControls.setTimeLimit(5000);
87 searchControls.setReturningAttributes(postAttributes);
89 StringBuffer filterClause = new StringBuffer("(&(objectClass=*)");
91 EPUser user = (EPUser)searchCriteria;
93 if(Utilities.nvl(user.getFirstName()).length() > 0) {
94 filterClause.append("(givenName=").append(user.getFirstName()).append("*)");
96 if(Utilities.nvl(user.getLastName()).length() > 0) {
97 filterClause.append("(sn=").append(user.getLastName()).append("*)");
99 if(Utilities.nvl(user.getHrid()).length() > 0) {
100 filterClause.append("(employeeNumber=").append(user.getHrid()).append("*)");
102 if(Utilities.nvl(user.getOrgManagerUserId()).length() > 0) {
103 filterClause.append("(mgrid=").append(user.getOrgManagerUserId()).append("*)");
105 if(Utilities.nvl(user.getOrgCode()).length() > 0) {
106 filterClause.append("(departmentNumber=").append(user.getOrgCode()).append("*)");
108 if(Utilities.nvl(user.getEmail()).length() > 0) {
109 filterClause.append("(mail=").append(user.getEmail()).append("*)");
111 if(Utilities.nvl(user.getOrgUserId()).length() > 0) {
112 filterClause.append("(a1=").append(user.getOrgUserId()).append("*)");
114 filterClause.append("(c3=N)"); // this has been added to filter CP09 entries on the LDAP server that are duplicates of existing individuals
115 filterClause.append(")");
117 List list = new ArrayList();
118 if (!filterClause.toString().equals("(&(objectClass=*))")) {
119 NamingEnumeration e = dirContext.search(SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL) + "/" +
120 SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL),
121 filterClause.toString(),
123 list = processResults(e);
126 Collections.sort(list);
128 searchResult = new SearchResult(list);
129 searchResult.setPageNo(pageNo);
131 searchResult.setDataSize(dataSize);
134 searchResult.setDataSize(list.size());
136 } catch(NamingException ne) {
137 String stackTrace = EcompPortalUtils.getStackTrace(ne);
138 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the LDAP search. Details: " + stackTrace);
139 } catch(Exception e) {
140 String stackTrace = EcompPortalUtils.getStackTrace(e);
141 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the LDAP search. Details: " + stackTrace);
151 @SuppressWarnings({ "rawtypes", "unchecked" })
153 private ArrayList processResults(NamingEnumeration e) throws NamingException {
154 ArrayList results = new ArrayList();
157 while (e.hasMore()) {
158 javax.naming.directory.SearchResult searchResult = (javax.naming.directory.SearchResult)e.next();
159 results.add(processAttributes(searchResult.getAttributes()));
162 if(count > Integer.parseInt(SystemProperties.getProperty(SystemProperties.POST_MAX_RESULT_SIZE))) {
170 @SuppressWarnings("rawtypes")
172 private DomainVo processAttributes(Attributes resultAttributes) throws NamingException {
173 EPUser user = new EPUser();
176 if (resultAttributes == null) {
177 logger.debug(EELFLoggerDelegate.debugLogger, "This result has no attributes");
179 for (NamingEnumeration e = resultAttributes.getAll(); e.hasMore();) { //why the nested loop?
180 Attribute attribute = (Attribute)e.next();
181 for (NamingEnumeration ie = attribute.getAll(); ie.hasMore();) {
182 if (attribute.getID().equalsIgnoreCase("nickname")) {
183 user.setFirstName((String) ie.next());
185 else if (attribute.getID().equalsIgnoreCase("initials")) {
186 user.setMiddleInitial((String) ie.next());
188 else if (attribute.getID().equalsIgnoreCase("sn")) {
189 user.setLastName((String) ie.next());
191 else if (attribute.getID().equalsIgnoreCase("employeeNumber")) {
192 user.setHrid((String) ie.next());
194 else if (attribute.getID().equalsIgnoreCase("mail")) {
195 user.setEmail((String) ie.next());
197 else if (attribute.getID().equalsIgnoreCase("telephoneNumber")) {
198 user.setPhone((String) ie.next());
200 else if (attribute.getID().equalsIgnoreCase("departmentNumber")) {
201 user.setOrgCode((String) ie.next());
203 else if (attribute.getID().equalsIgnoreCase("a1")) {
204 user.setOrgUserId((String) ie.next());
206 else if (attribute.getID().equalsIgnoreCase("street")) {
207 user.setAddress1((String) ie.next());
209 else if (attribute.getID().equalsIgnoreCase("roomNumber")) {
210 user.setAddress2((String) ie.next());
212 else if (attribute.getID().equalsIgnoreCase("l")) {
213 user.setCity((String) ie.next());
215 else if (attribute.getID().equalsIgnoreCase("st")) {
216 user.setState((String) ie.next());
218 else if (attribute.getID().equalsIgnoreCase("postalCode")) {
219 user.setZipCode((String) ie.next());
221 else if (attribute.getID().equalsIgnoreCase("zip4")) {
222 user.setZipCodeSuffix((String) ie.next());
224 else if (attribute.getID().equalsIgnoreCase("physicalDeliveryOfficeName")) {
225 user.setLocationClli((String) ie.next());
227 else if (attribute.getID().equalsIgnoreCase("bc")) {
228 user.setBusinessCountryCode((String) ie.next());
230 else if (attribute.getID().equalsIgnoreCase("friendlyCountryName")) {
231 user.setBusinessCountryName((String) ie.next());
233 else if (attribute.getID().equalsIgnoreCase("bd")) {
234 user.setDepartment((String) ie.next());
236 else if (attribute.getID().equalsIgnoreCase("bdname")) {
237 user.setDepartmentName((String) ie.next());
239 else if (attribute.getID().equalsIgnoreCase("jtname")) {
240 user.setJobTitle((String) ie.next());
242 else if (attribute.getID().equalsIgnoreCase("mgrid")) {
243 user.setOrgManagerUserId((String) ie.next());
245 else if (attribute.getID().equalsIgnoreCase("a2")) {
246 user.setCommandChain((String) ie.next());
248 else if (attribute.getID().equalsIgnoreCase("compcode")) {
249 user.setCompanyCode((String) ie.next());
251 else if (attribute.getID().equalsIgnoreCase("compdesc")) {
252 user.setCompany((String) ie.next());
254 else if (attribute.getID().equalsIgnoreCase("bu")) {
255 user.setBusinessUnit((String)ie.next());
257 else if (attribute.getID().equalsIgnoreCase("buname")) {
258 user.setBusinessUnitName((String)ie.next());
260 else if (attribute.getID().equalsIgnoreCase("silo")) {
261 user.setSiloStatus((String)ie.next());
263 else if (attribute.getID().equalsIgnoreCase("costcenter")) {
264 user.setCostCenter((String)ie.next());
266 else if (attribute.getID().equalsIgnoreCase("b2")) {
267 user.setFinancialLocCode((String)ie.next());
269 else { //we don't care about returned attribute, let's move on
276 } catch (NamingException e) {
277 String stackTrace = EcompPortalUtils.getStackTrace(e);
278 logger.error(EELFLoggerDelegate.errorLogger, "An error occurred while processing the following user from POST with an userId of " + user.getOrgUserId());
279 logger.error(EELFLoggerDelegate.errorLogger, "Exception occcurred while processing LDAP search results, Details: " + stackTrace);
280 } catch(Exception e) {
281 String stackTrace = EcompPortalUtils.getStackTrace(e);
282 logger.error(EELFLoggerDelegate.errorLogger, "Exception occcurred while processing LDAP search results, Details: " + stackTrace);