Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / EPLdapServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.service;
39
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
43
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;
50
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;
61 import org.slf4j.MDC;
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;
66
67 @Service("epLdapService")
68 @Transactional
69 @org.springframework.context.annotation.Configuration
70 @EnableAspectJAutoProxy
71 public class EPLdapServiceImpl extends FusionService implements EPLdapService {
72         @Autowired
73         private ServiceLocator serviceLocator;
74
75         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLdapServiceImpl.class);
76
77         @EPAuditLog
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 {
81
82                 String remoteHost = "";
83
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));
89
90                 SearchResult searchResult = new SearchResult();
91
92                 try {
93
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);
97
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" };
102
103                         SearchControls searchControls = new SearchControls();
104                         searchControls.setTimeLimit(5000);
105                         searchControls.setReturningAttributes(postAttributes);
106
107                         StringBuffer filterClause = new StringBuffer("(&(objectClass=*)");
108
109                         EPUser user = (EPUser) searchCriteria;
110
111                         if (Utilities.nvl(user.getFirstName()).length() > 0) {
112                                 filterClause.append("(givenName=").append(user.getFirstName()).append("*)");
113                         }
114                         if (Utilities.nvl(user.getLastName()).length() > 0) {
115                                 filterClause.append("(sn=").append(user.getLastName()).append("*)");
116                         }
117                         if (Utilities.nvl(user.getHrid()).length() > 0) {
118                                 filterClause.append("(employeeNumber=").append(user.getHrid()).append("*)");
119                         }
120                         if (Utilities.nvl(user.getOrgManagerUserId()).length() > 0) {
121                                 filterClause.append("(mgrid=").append(user.getOrgManagerUserId()).append("*)");
122                         }
123                         if (Utilities.nvl(user.getOrgCode()).length() > 0) {
124                                 filterClause.append("(departmentNumber=").append(user.getOrgCode()).append("*)");
125                         }
126                         if (Utilities.nvl(user.getEmail()).length() > 0) {
127                                 filterClause.append("(mail=").append(user.getEmail()).append("*)");
128                         }
129                         if (Utilities.nvl(user.getOrgUserId()).length() > 0) {
130                                 filterClause.append("(a1=").append(user.getOrgUserId()).append("*)");
131                         }
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(")");
135
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);
143                         }
144
145                         Collections.sort(list);
146
147                         searchResult = new SearchResult(list);
148                         searchResult.setPageNo(pageNo);
149                         if (dataSize >= 0) {
150                                 searchResult.setDataSize(dataSize);
151                         } else {
152                                 searchResult.setDataSize(list.size());
153                         }
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);
158                 } finally {
159                         dirContext.close();
160                 }
161
162                 return searchResult;
163         }
164
165         @SuppressWarnings({ "rawtypes", "unchecked" })
166         @EPMetricsLog
167         private ArrayList processResults(NamingEnumeration e) throws NamingException {
168                 ArrayList results = new ArrayList();
169                 int count = 0;
170
171                 while (e.hasMore()) {
172                         javax.naming.directory.SearchResult searchResult = (javax.naming.directory.SearchResult) e.next();
173                         results.add(processAttributes(searchResult.getAttributes()));
174                         count++;
175
176                         if (count > Integer.parseInt(SystemProperties.getProperty(SystemProperties.POST_MAX_RESULT_SIZE))) {
177                                 break;
178                         }
179                 }
180                 return results;
181         }
182
183         @SuppressWarnings("rawtypes")
184         @EPMetricsLog
185         private DomainVo processAttributes(Attributes resultAttributes) throws NamingException {
186                 EPUser user = new EPUser();
187
188                 try {
189                         if (resultAttributes == null) {
190                                 logger.debug(EELFLoggerDelegate.debugLogger, "This result has no attributes");
191                         } else {
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
254                                                         ie.next();
255                                                 }
256
257                                         }
258                                 }
259                         }
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);
264                 }
265
266                 return user;
267         }
268 }