KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > securityext > thirdparty > truition > TruitionCoReg


1 /*
2  * $Id: TruitionCoReg.java 5586 2005-08-25 13:47:38Z jaz $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.securityext.thirdparty.truition;
26
27 import java.util.Collection JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.UnsupportedEncodingException JavaDoc;
30 import java.net.URLEncoder JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.http.HttpSession JavaDoc;
35 import javax.servlet.http.Cookie JavaDoc;
36
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.UtilMisc;
39 import org.ofbiz.base.util.UtilProperties;
40 import org.ofbiz.base.util.UtilValidate;
41 import org.ofbiz.entity.GenericEntityException;
42 import org.ofbiz.entity.GenericValue;
43 import org.ofbiz.party.contact.ContactHelper;
44
45 /**
46  *
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @version $Rev: 5586 $
49  * @since 3.3
50  */

51 public class TruitionCoReg {
52
53     public static final String JavaDoc module = TruitionCoReg.class.getName();
54     public static final String JavaDoc logPrefix = "Truition Cookie Info: ";
55
56     public static String JavaDoc truitionReg(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) {
57         HttpSession JavaDoc session = req.getSession();
58         GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
59         StringBuffer JavaDoc cookieNameB = new StringBuffer JavaDoc();
60         StringBuffer JavaDoc cookieValue = new StringBuffer JavaDoc();
61         if (!truitionEnabled()) {
62             return "success";
63         }
64
65         boolean cookieOk = false;
66         if (userLogin != null) {
67             cookieOk = TruitionCoReg.makeTruitionCookie(userLogin, cookieNameB, cookieValue);
68         }
69
70         // locate the domain/cookie name setting
71
String JavaDoc domainName = UtilProperties.getPropertyValue("truition.properties", "truition.domain.name");
72         String JavaDoc cookiePath = UtilProperties.getPropertyValue("truition.properties", "truition.cookie.path");
73         String JavaDoc cookieName = UtilProperties.getPropertyValue("truition.properties", "truition.cookie.name");
74         int time = (int) UtilProperties.getPropertyNumber("truition.properties", "truition.cookie.time");
75         if (UtilValidate.isEmpty(domainName)) {
76             Debug.logError("Truition is not properly configured; domainName missing; see truition.properties", module);
77             return "error";
78         }
79         if (UtilValidate.isEmpty(cookiePath)) {
80             Debug.logError("Truition is not properly configured; cookiePath missing; see truition.properties", module);
81             return "error";
82         }
83         if (UtilValidate.isEmpty(cookieName)) {
84             Debug.logError("Truition is not properly configured; cookieName missing; see truition.properties", module);
85             return "error";
86         }
87         if (time == 0) {
88             Debug.logError("Truition is not properly configured; cookieTime missing; see trution.properties", module);
89             return "error";
90         }
91
92         // strip out CR/LF from cookie value
93
String JavaDoc thisValue = cookieValue.toString();
94         thisValue = thisValue.replaceAll("\n", "");
95         thisValue = thisValue.replaceAll("\r", "");
96
97         // create the cookie
98
if (cookieOk) {
99             try {
100                 Cookie JavaDoc tru = new Cookie JavaDoc(cookieName, URLEncoder.encode(thisValue, "UTF-8"));
101                 tru.setDomain(domainName);
102                 tru.setPath(cookiePath);
103                 tru.setMaxAge(time);
104                 resp.addCookie(tru);
105                 Debug.log("Set Truition Cookie [" + tru.getName() + "/" + tru.getDomain() + " @ " + tru.getPath() + "] - " + tru.getValue(), module);
106             } catch (UnsupportedEncodingException JavaDoc e) {
107                 Debug.logError(e, module);
108                 return "error";
109             }
110         }
111         return "success";
112     }
113
114     public static String JavaDoc truitionLogoff(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) {
115         // locate the domain/cookie name setting
116
String JavaDoc domainName = UtilProperties.getPropertyValue("truition.properties", "truition.domain.name");
117         String JavaDoc cookieName = UtilProperties.getPropertyValue("truition.properties", "truition.cookie.name");
118         if (UtilValidate.isEmpty(domainName)) {
119             Debug.logError("Truition is not properly configured; domainName missing; see truition.properties", module);
120             return "error";
121         }
122         if (UtilValidate.isEmpty(cookieName)) {
123             Debug.logError("Truition is not properly configured; cookieName missing; see truition.properties", module);
124             return "error";
125         }
126
127         if (truitionEnabled()) {
128         Cookie JavaDoc[] cookies = req.getCookies();
129             for (int i = 0; i < cookies.length; i++) {
130                 if (cookieName.equals(cookies[i].getName())) {
131                     cookies[i].setMaxAge(0);
132                     resp.addCookie(cookies[i]);
133                     Debug.log("Set truition cookie [" + cookieName + " to expire now.", module);
134                 }
135             }
136         }
137
138         return "success";
139     }
140
141     public static String JavaDoc truitionRedirect(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) {
142         // redirect URL form field
143
String JavaDoc redirectUrlName = UtilProperties.getPropertyValue("truition.properties", "truition.redirect.urlName");
144         String JavaDoc redirectUrl = req.getParameter(redirectUrlName);
145         Debug.log("Redirect to : " + redirectUrl, module);
146         if (truitionEnabled() && redirectUrl != null) {
147             try {
148                 resp.sendRedirect(redirectUrl);
149             } catch (IOException JavaDoc e) {
150                 Debug.logError(e, module);
151                 req.setAttribute("_ERROR_MESSAGE_", e.getMessage());
152                 return "error";
153             }
154
155             Debug.log("Sending truition redirect - " + redirectUrl, module);
156             return "redirect";
157         }
158         return "success";
159     }
160
161     public static boolean makeTruitionCookie(GenericValue userLogin, StringBuffer JavaDoc cookieName, StringBuffer JavaDoc cookieValue) {
162         String JavaDoc domainName = UtilProperties.getPropertyValue("truition.properties", "truition.domain.name");
163         String JavaDoc siteId = UtilProperties.getPropertyValue("truition.properties", "truition.siteId");
164
165         if (UtilValidate.isEmpty(domainName)) {
166             Debug.logError("Truition is not properly configured; domainName missing; see truition.properties!", module);
167             return false;
168         }
169         if (UtilValidate.isEmpty(siteId)) {
170             Debug.logError("Truition is not properly configured; siteId missing; see truition.properties!", module);
171             return false;
172         }
173
174         // user login information
175
String JavaDoc nickName = userLogin.getString("userLoginId");
176         String JavaDoc password = userLogin.getString("currentPassword");
177         String JavaDoc partyId = userLogin.getString("partyId");
178         Debug.log(logPrefix + "nickName: " + nickName, module);
179         Debug.log(logPrefix + "password: " + password, module);
180         Debug.log(logPrefix + "partyId: " + partyId, module);
181
182         GenericValue party = null;
183         try {
184             party = userLogin.getRelatedOne("Party");
185         } catch (GenericEntityException e) {
186             Debug.logError(e, module);
187         }
188
189         if (party != null) {
190             String JavaDoc title = null;
191             String JavaDoc firstName = null;
192             String JavaDoc lastName = null;
193             if ("PERSON".equals(party.getString("partyTypeId"))) {
194                 GenericValue person = null;
195                 try {
196                     person = party.getRelatedOne("Person");
197                 } catch (GenericEntityException e) {
198                     Debug.logError(e, module);
199                 }
200
201                 // first/last name
202
if (person != null) {
203                     title = person.getString("personalTitle");
204                     firstName = person.getString("firstName");
205                     lastName = person.getString("lastName");
206                     if (title == null) {
207                         title = "";
208                     }
209                 }
210                 Debug.log(logPrefix + "title: " + title, module);
211                 Debug.log(logPrefix + "firstName: " + firstName, module);
212                 Debug.log(logPrefix + "lastName: " + lastName, module);
213
214                 // email address
215
String JavaDoc emailAddress = null;
216                 Collection JavaDoc emCol = ContactHelper.getContactMech(party, "PRIMARY_EMAIL", "EMAIL_ADDRESS", false);
217                 if (emCol == null || emCol.size() == 0) {
218                     emCol = ContactHelper.getContactMech(party, null, "EMAIL_ADDRESS", false);
219                 }
220                 if (emCol != null && emCol.size() > 0) {
221                     GenericValue emVl = (GenericValue) emCol.iterator().next();
222                     if (emVl != null) {
223                         emailAddress = emVl.getString("infoString");
224                     }
225                 } else {
226                     emailAddress = "";
227                 }
228                 Debug.log(logPrefix + "emailAddress: " + emailAddress, module);
229
230                 // shipping address
231
String JavaDoc address1 = null;
232                 String JavaDoc address2 = null;
233                 String JavaDoc city = null;
234                 String JavaDoc state = null;
235                 String JavaDoc zipCode = null;
236                 String JavaDoc country = null;
237                 Collection JavaDoc adCol = ContactHelper.getContactMech(party, "SHIPPING_LOCATION", "POSTAL_ADDRESS", false);
238                 if (adCol == null || adCol.size() == 0) {
239                     adCol = ContactHelper.getContactMech(party, null, "POSTAL_ADDRESS", false);
240                 }
241                 if (adCol != null && adCol.size() > 0) {
242                     GenericValue adVl = (GenericValue) adCol.iterator().next();
243                     if (adVl != null) {
244                         GenericValue addr = null;
245                         try {
246                             addr = adVl.getDelegator().findByPrimaryKey("PostalAddress", UtilMisc.toMap("contactMechId",
247                                     adVl.getString("contactMechId")));
248                         } catch (GenericEntityException e) {
249                             Debug.logError(e, module);
250                         }
251                         if (addr != null) {
252                             address1 = addr.getString("address1");
253                             address2 = addr.getString("address2");
254                             city = addr.getString("city");
255                             state = addr.getString("stateProvinceGeoId");
256                             zipCode = addr.getString("postalCode");
257                             country = addr.getString("countryGeoId");
258                             if (address2 == null) {
259                                 address2 = "";
260                             }
261                         }
262                     }
263                 }
264                 Debug.log(logPrefix + "address1: " + address1, module);
265                 Debug.log(logPrefix + "address2: " + address2, module);
266                 Debug.log(logPrefix + "city: " + city, module);
267                 Debug.log(logPrefix + "state: " + state, module);
268                 Debug.log(logPrefix + "zipCode: " + zipCode, module);
269                 Debug.log(logPrefix + "country: " + country, module);
270
271                 // phone number
272
String JavaDoc phoneNumber = null;
273                 Collection JavaDoc phCol = ContactHelper.getContactMech(party, "PHONE_HOME", "TELECOM_NUMBER", false);
274                 if (phCol == null || phCol.size() == 0) {
275                     phCol = ContactHelper.getContactMech(party, null, "TELECOM_NUMBER", false);
276                 }
277                 if (phCol != null && phCol.size() > 0) {
278                     GenericValue phVl = (GenericValue) phCol.iterator().next();
279                     if (phVl != null) {
280                         GenericValue tele = null;
281                         try {
282                             tele = phVl.getDelegator().findByPrimaryKey("TelecomNumber", UtilMisc.toMap("contactMechId",
283                                     phVl.getString("contactMechId")));
284                         } catch (GenericEntityException e) {
285                             Debug.logError(e, module);
286                         }
287                         if (tele != null) {
288                             phoneNumber = ""; // reset the string
289
String JavaDoc cc = tele.getString("countryCode");
290                             String JavaDoc ac = tele.getString("areaCode");
291                             String JavaDoc nm = tele.getString("contactNumber");
292                             if (UtilValidate.isNotEmpty(cc)) {
293                                 phoneNumber = phoneNumber + cc + "-";
294                             }
295                             if (UtilValidate.isNotEmpty(ac)) {
296                                 phoneNumber = phoneNumber + ac + "-";
297                             }
298                             phoneNumber = phoneNumber + nm;
299                         } else {
300                             phoneNumber = "";
301                         }
302                     }
303                 }
304                 Debug.log(logPrefix + "phoneNumber: " + phoneNumber, module);
305
306                 int retCode = -1;
307
308                 if (lastName != null && address1 != null) {
309                     retCode = edeal.coreg.EdCoReg.ed_create_cookie_nvp(nickName, "pwd", title, firstName,
310                         lastName, emailAddress, address1, address2, city, state, zipCode, country, phoneNumber,
311                         siteId, cookieName, cookieValue, "", "", "", partyId, "", "");
312                 }
313
314                 if (retCode < 0) {
315                     Debug.logError("EDeal cookie not set; API return code: " + retCode, module);
316                     return false;
317                 } else {
318                     Debug.logInfo("EDeal cookie success; API return code: " + retCode, module);
319                 }
320             } else {
321                 Debug.logError("Truition requires a Person to be logged in. First/Last name required!", module);
322                 return false;
323             }
324         }
325         return true;
326     }
327
328     public static boolean truitionEnabled() {
329         return "Y".equalsIgnoreCase(UtilProperties.getPropertyValue("truition.properties", "truition.enabled", "N"));
330     }
331 }
332
Popular Tags