Created
November 5, 2015 00:25
-
-
Save ryanschuhler/441af8e92ec285add15c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Copyright (c) 2000-present Liferay, Inc. All rights reserved. | |
| * | |
| * This library is free software; you can redistribute it and/or modify it under | |
| * the terms of the GNU Lesser General Public License as published by the Free | |
| * Software Foundation; either version 2.1 of the License, or (at your option) | |
| * any later version. | |
| * | |
| * This library is distributed in the hope that it will be useful, but WITHOUT | |
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | |
| * details. | |
| */ | |
| package com.liferay.osb.www.hook.events; | |
| import com.liferay.hubspot.model.HSContact; | |
| import com.liferay.hubspot.service.HSContactLocalServiceUtil; | |
| import com.liferay.ipgeocoder.model.IPInfo; | |
| import com.liferay.ipgeocoder.util.IPGeocoderUtil; | |
| import com.liferay.osb.www.hook.util.WebKeys; | |
| import com.liferay.portal.kernel.events.Action; | |
| import com.liferay.portal.kernel.events.ActionException; | |
| import com.liferay.portal.kernel.util.CookieKeys; | |
| import com.liferay.portal.kernel.util.StringPool; | |
| import com.liferay.portal.kernel.util.Validator; | |
| import javax.servlet.http.Cookie; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| /** | |
| * @author Brian Wing Shun Chan | |
| * @author Ryan Schuhler | |
| */ | |
| public class ServicePreAction extends Action { | |
| @Override | |
| public void run(HttpServletRequest request, HttpServletResponse response) | |
| throws ActionException { | |
| try { | |
| servicePre(request, response); | |
| } | |
| catch (Exception e) { | |
| throw new ActionException(e); | |
| } | |
| } | |
| protected String getCountry( | |
| HttpServletRequest request, HttpServletResponse response) { | |
| String country = CookieKeys.getCookie(request, WebKeys.OSB_WWW_COUNTRY); | |
| if (Validator.isNotNull(country)) { | |
| return country; | |
| } | |
| String hubspotUtk = CookieKeys.getCookie(request, "hubspotutk"); | |
| HSContact hsContact = null; | |
| try { | |
| hsContact = HSContactLocalServiceUtil.fetchHSContactByUserToken( | |
| hubspotUtk); | |
| } | |
| catch (Exception e) { | |
| } | |
| if (hsContact != null) { | |
| return hsContact.getCountry(); | |
| } | |
| IPInfo ipInfo = null; | |
| try { | |
| ipInfo = IPGeocoderUtil.getIPInfo(request.getRemoteAddr()); | |
| } | |
| catch (Exception e) { | |
| } | |
| if (ipInfo != null) { | |
| return ipInfo.getCountryName(); | |
| } | |
| return StringPool.BLANK; | |
| } | |
| protected String getPersona( | |
| HttpServletRequest request, HttpServletResponse response) { | |
| String persona = CookieKeys.getCookie(request, WebKeys.OSB_WWW_PERSONA); | |
| if (Validator.isNotNull(persona)) { | |
| return persona; | |
| } | |
| String hubspotUtk = CookieKeys.getCookie(request, "hubspotutk"); | |
| HSContact hsContact = null; | |
| try { | |
| hsContact = HSContactLocalServiceUtil.fetchHSContactByUserToken( | |
| hubspotUtk); | |
| } | |
| catch (Exception e) { | |
| } | |
| if (hsContact != null ) { | |
| return hsContact.getPersona(); | |
| } | |
| return StringPool.BLANK; | |
| } | |
| protected void servicePre( | |
| HttpServletRequest request, HttpServletResponse response) { | |
| String country = getCountry(request, response); | |
| request.setAttribute(WebKeys.OSB_WWW_COUNTRY, country); | |
| if (Validator.isNotNull(country)) { | |
| response.addCookie(new Cookie(WebKeys.OSB_WWW_COUNTRY, country)); | |
| } | |
| String hubspotUtk = CookieKeys.getCookie(request, "hubspotutk"); | |
| request.setAttribute(WebKeys.OSB_WWW_HUBSPOT_UTK, hubspotUtk); | |
| String persona = getPersona(request, response); | |
| request.setAttribute(WebKeys.OSB_WWW_PERSONA, persona); | |
| if (Validator.isNotNull(persona)) { | |
| response.addCookie(new Cookie(WebKeys.OSB_WWW_PERSONA, persona)); | |
| } | |
| request.setAttribute( | |
| WebKeys.OSB_WWW_REMOTE_ADDRESS, request.getRemoteAddr()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment