2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.openecomp.portalapp.portal.service;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
44 import javax.naming.NamingEnumeration;
45 import javax.naming.NamingException;
46 import javax.naming.directory.Attribute;
47 import javax.naming.directory.Attributes;
48 import javax.naming.directory.DirContext;
49 import javax.naming.directory.SearchControls;
51 import org.openecomp.portalapp.portal.domain.EPUser;
52 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
53 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
54 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
55 import org.openecomp.portalsdk.core.command.support.SearchResult;
56 import org.openecomp.portalsdk.core.domain.support.DomainVo;
57 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
58 import org.openecomp.portalsdk.core.service.support.FusionService;
59 import org.openecomp.portalsdk.core.service.support.ServiceLocator;
60 import org.openecomp.portalsdk.core.util.SystemProperties;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.context.annotation.EnableAspectJAutoProxy;
64 import org.springframework.stereotype.Service;
65 import org.springframework.transaction.annotation.Transactional;
67 @Service("epLdapService")
69 @org.springframework.context.annotation.Configuration
70 @EnableAspectJAutoProxy
71 public class EPLdapServiceImpl extends FusionService implements EPLdapService {
73 private ServiceLocator serviceLocator;
75 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLdapServiceImpl.class);
78 @SuppressWarnings({ "rawtypes", "unchecked" })
79 public SearchResult searchPost(DomainVo searchCriteria, String sortBy1, String sortBy2, String sortBy3, int pageNo,
80 int dataSize, int userId) throws Exception {
82 String remoteHost = "";
84 // initialize the directory context to access POST
85 DirContext dirContext = serviceLocator.getDirContext(
86 SystemProperties.getProperty(SystemProperties.POST_INITIAL_CONTEXT_FACTORY),
87 SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL),
88 SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL));
90 SearchResult searchResult = new SearchResult();
94 remoteHost = String.format("%s/%s", SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL),
95 SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL));
96 MDC.put(EPCommonSystemProperties.FULL_URL, remoteHost);
98 String[] postAttributes = { "nickname", "givenName", "initials", "sn", "employeeNumber", "mail",
99 "telephoneNumber", "departmentNumber", "a1", "street", "roomNumber", "l", "st", "postalCode",
100 "zip4", "physicalDeliveryOfficeName", "bc", "friendlyCountryName", "bd", "bdname", "bu", "buname",
101 "jtname", "mgrid", "a2", "compcode", "compdesc", "costcenter", "silo", "b2" };
103 SearchControls searchControls = new SearchControls();
104 searchControls.setTimeLimit(5000);
105 searchControls.setReturningAttributes(postAttributes);
107 StringBuffer filterClause = new StringBuffer("(&(objectClass=*)");
109 EPUser user = (EPUser) searchCriteria;
111 if (Utilities.nvl(user.getFirstName()).length() > 0) {
112 filterClause.append("(givenName=").append(user.getFirstName()).append("*)");
114 if (Utilities.nvl(user.getLastName()).length() > 0) {
115 filterClause.append("(sn=").append(user.getLastName()).append("*)");
117 if (Utilities.nvl(user.getHrid()).length() > 0) {
118 filterClause.append("(employeeNumber=").append(user.getHrid()).append("*)");
120 if (Utilities.nvl(user.getOrgManagerUserId()).length() > 0) {
121 filterClause.append("(mgrid=").append(user.getOrgManagerUserId()).append("*)");
123 if (Utilities.nvl(user.getOrgCode()).length() > 0) {
124 filterClause.append("(departmentNumber=").append(user.getOrgCode()).append("*)");
126 if (Utilities.nvl(user.getEmail()).length() > 0) {
127 filterClause.append("(mail=").append(user.getEmail()).append("*)");
129 if (Utilities.nvl(user.getOrgUserId()).length() > 0) {
130 filterClause.append("(a1=").append(user.getOrgUserId()).append("*)");
132 filterClause.append("(c3=N)"); // this has been added to filter CP09 entries on the LDAP server that are
133 // duplicates of existing individuals
134 filterClause.append(")");
136 List list = new ArrayList();
137 if (!filterClause.toString().equals("(&(objectClass=*))")) {
138 NamingEnumeration e = dirContext.search(
139 SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL) + "/"
140 + SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL),
141 filterClause.toString(), searchControls);
142 list = processResults(e);
145 Collections.sort(list);
147 searchResult = new SearchResult(list);
148 searchResult.setPageNo(pageNo);
150 searchResult.setDataSize(dataSize);
152 searchResult.setDataSize(list.size());
154 } catch (NamingException ne) {
155 logger.error(EELFLoggerDelegate.errorLogger, "searchPost failed with naming exception", ne);
156 } catch (Exception e) {
157 logger.error(EELFLoggerDelegate.errorLogger, "searchPost failed", e);
165 @SuppressWarnings({ "rawtypes", "unchecked" })
167 private ArrayList processResults(NamingEnumeration e) throws NamingException {
168 ArrayList results = new ArrayList();
171 while (e.hasMore()) {
172 javax.naming.directory.SearchResult searchResult = (javax.naming.directory.SearchResult) e.next();
173 results.add(processAttributes(searchResult.getAttributes()));
176 if (count > Integer.parseInt(SystemProperties.getProperty(SystemProperties.POST_MAX_RESULT_SIZE))) {
183 @SuppressWarnings("rawtypes")
185 private DomainVo processAttributes(Attributes resultAttributes) throws NamingException {
186 EPUser user = new EPUser();
189 if (resultAttributes == null) {
190 logger.debug(EELFLoggerDelegate.debugLogger, "This result has no attributes");
192 for (NamingEnumeration e = resultAttributes.getAll(); e.hasMore();) { // why the nested loop?
193 Attribute attribute = (Attribute) e.next();
194 for (NamingEnumeration ie = attribute.getAll(); ie.hasMore();) {
195 if (attribute.getID().equalsIgnoreCase("nickname")) {
196 user.setFirstName((String) ie.next());
197 } else if (attribute.getID().equalsIgnoreCase("initials")) {
198 user.setMiddleInitial((String) ie.next());
199 } else if (attribute.getID().equalsIgnoreCase("sn")) {
200 user.setLastName((String) ie.next());
201 } else if (attribute.getID().equalsIgnoreCase("employeeNumber")) {
202 user.setHrid((String) ie.next());
203 } else if (attribute.getID().equalsIgnoreCase("mail")) {
204 user.setEmail((String) ie.next());
205 } else if (attribute.getID().equalsIgnoreCase("telephoneNumber")) {
206 user.setPhone((String) ie.next());
207 } else if (attribute.getID().equalsIgnoreCase("departmentNumber")) {
208 user.setOrgCode((String) ie.next());
209 } else if (attribute.getID().equalsIgnoreCase("a1")) {
210 user.setOrgUserId((String) ie.next());
211 } else if (attribute.getID().equalsIgnoreCase("street")) {
212 user.setAddress1((String) ie.next());
213 } else if (attribute.getID().equalsIgnoreCase("roomNumber")) {
214 user.setAddress2((String) ie.next());
215 } else if (attribute.getID().equalsIgnoreCase("l")) {
216 user.setCity((String) ie.next());
217 } else if (attribute.getID().equalsIgnoreCase("st")) {
218 user.setState((String) ie.next());
219 } else if (attribute.getID().equalsIgnoreCase("postalCode")) {
220 user.setZipCode((String) ie.next());
221 } else if (attribute.getID().equalsIgnoreCase("zip4")) {
222 user.setZipCodeSuffix((String) ie.next());
223 } else if (attribute.getID().equalsIgnoreCase("physicalDeliveryOfficeName")) {
224 user.setLocationClli((String) ie.next());
225 } else if (attribute.getID().equalsIgnoreCase("bc")) {
226 user.setBusinessCountryCode((String) ie.next());
227 } else if (attribute.getID().equalsIgnoreCase("friendlyCountryName")) {
228 user.setBusinessCountryName((String) ie.next());
229 } else if (attribute.getID().equalsIgnoreCase("bd")) {
230 user.setDepartment((String) ie.next());
231 } else if (attribute.getID().equalsIgnoreCase("bdname")) {
232 user.setDepartmentName((String) ie.next());
233 } else if (attribute.getID().equalsIgnoreCase("jtname")) {
234 user.setJobTitle((String) ie.next());
235 } else if (attribute.getID().equalsIgnoreCase("mgrid")) {
236 user.setOrgManagerUserId((String) ie.next());
237 } else if (attribute.getID().equalsIgnoreCase("a2")) {
238 user.setCommandChain((String) ie.next());
239 } else if (attribute.getID().equalsIgnoreCase("compcode")) {
240 user.setCompanyCode((String) ie.next());
241 } else if (attribute.getID().equalsIgnoreCase("compdesc")) {
242 user.setCompany((String) ie.next());
243 } else if (attribute.getID().equalsIgnoreCase("bu")) {
244 user.setBusinessUnit((String) ie.next());
245 } else if (attribute.getID().equalsIgnoreCase("buname")) {
246 user.setBusinessUnitName((String) ie.next());
247 } else if (attribute.getID().equalsIgnoreCase("silo")) {
248 user.setSiloStatus((String) ie.next());
249 } else if (attribute.getID().equalsIgnoreCase("costcenter")) {
250 user.setCostCenter((String) ie.next());
251 } else if (attribute.getID().equalsIgnoreCase("b2")) {
252 user.setFinancialLocCode((String) ie.next());
253 } else { // we don't care about returned attribute, let's move on
260 } catch (NamingException e) {
261 logger.error(EELFLoggerDelegate.errorLogger, "processAttributes failed with naming exception", e);
262 } catch (Exception e) {
263 logger.error(EELFLoggerDelegate.errorLogger, "processAttributes failed", e);