1 25 package org.ofbiz.securityext.thirdparty.truition; 26 27 import java.util.Collection ; 28 import java.io.IOException ; 29 import java.io.UnsupportedEncodingException ; 30 import java.net.URLEncoder ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.http.HttpSession ; 35 import javax.servlet.http.Cookie ; 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 51 public class TruitionCoReg { 52 53 public static final String module = TruitionCoReg.class.getName(); 54 public static final String logPrefix = "Truition Cookie Info: "; 55 56 public static String truitionReg(HttpServletRequest req, HttpServletResponse resp) { 57 HttpSession session = req.getSession(); 58 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 59 StringBuffer cookieNameB = new StringBuffer (); 60 StringBuffer cookieValue = new StringBuffer (); 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 String domainName = UtilProperties.getPropertyValue("truition.properties", "truition.domain.name"); 72 String cookiePath = UtilProperties.getPropertyValue("truition.properties", "truition.cookie.path"); 73 String 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 String thisValue = cookieValue.toString(); 94 thisValue = thisValue.replaceAll("\n", ""); 95 thisValue = thisValue.replaceAll("\r", ""); 96 97 if (cookieOk) { 99 try { 100 Cookie tru = new Cookie (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 e) { 107 Debug.logError(e, module); 108 return "error"; 109 } 110 } 111 return "success"; 112 } 113 114 public static String truitionLogoff(HttpServletRequest req, HttpServletResponse resp) { 115 String domainName = UtilProperties.getPropertyValue("truition.properties", "truition.domain.name"); 117 String 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 [] 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 truitionRedirect(HttpServletRequest req, HttpServletResponse resp) { 142 String redirectUrlName = UtilProperties.getPropertyValue("truition.properties", "truition.redirect.urlName"); 144 String 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 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 cookieName, StringBuffer cookieValue) { 162 String domainName = UtilProperties.getPropertyValue("truition.properties", "truition.domain.name"); 163 String 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 String nickName = userLogin.getString("userLoginId"); 176 String password = userLogin.getString("currentPassword"); 177 String 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 title = null; 191 String firstName = null; 192 String 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 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 String emailAddress = null; 216 Collection 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 String address1 = null; 232 String address2 = null; 233 String city = null; 234 String state = null; 235 String zipCode = null; 236 String country = null; 237 Collection 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 String phoneNumber = null; 273 Collection 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 = ""; String cc = tele.getString("countryCode"); 290 String ac = tele.getString("areaCode"); 291 String 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 |