Created
November 5, 2015 00:20
-
-
Save ryanschuhler/7da363dc7e31af43569b 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.hubspot.service.impl; | |
| import com.liferay.compat.portal.kernel.servlet.HttpHeaders; | |
| import com.liferay.compat.portal.kernel.util.HttpUtil; | |
| import com.liferay.hubspot.HubSpotServerException; | |
| import com.liferay.hubspot.NoSuchHSFormException; | |
| import com.liferay.hubspot.model.HSForm; | |
| import com.liferay.hubspot.model.impl.HSFormImpl; | |
| import com.liferay.hubspot.server.HubSpotServerUtil; | |
| import com.liferay.hubspot.service.base.HSFormLocalServiceBaseImpl; | |
| import com.liferay.hubspot.util.HubSpotURIUtil; | |
| import com.liferay.hubspot.util.HubSpotUtil; | |
| import com.liferay.portal.kernel.exception.PortalException; | |
| import com.liferay.portal.kernel.json.JSONArray; | |
| import com.liferay.portal.kernel.json.JSONFactoryUtil; | |
| import com.liferay.portal.kernel.json.JSONObject; | |
| import com.liferay.portal.kernel.messaging.Message; | |
| import com.liferay.portal.kernel.util.ContentTypes; | |
| import com.liferay.portal.kernel.util.Http.Body; | |
| import com.liferay.portal.kernel.util.StringPool; | |
| import com.liferay.portal.kernel.util.Validator; | |
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| import org.apache.commons.httpclient.HttpStatus; | |
| /** | |
| * @author Peter Shin | |
| */ | |
| public class HSFormLocalServiceImpl extends HSFormLocalServiceBaseImpl { | |
| public HSForm fetchHSFormByGUID(String guid) throws PortalException { | |
| String encodedGUID = HttpUtil.encodeURL(guid, true); | |
| if (!HubSpotUtil.isEnabled()) { | |
| return null; | |
| } | |
| String uri = HubSpotURIUtil.get("forms/" + encodedGUID); | |
| Map<String, String> headers = getHeaders(ContentTypes.APPLICATION_JSON); | |
| Message message = HubSpotServerUtil.executeGet(uri, headers); | |
| if (message.getInteger("statusCode") == HttpStatus.SC_NOT_FOUND) { | |
| return null; | |
| } | |
| if (message.getInteger("statusCode") != HttpStatus.SC_OK) { | |
| throw new HubSpotServerException(message); | |
| } | |
| return new HSFormImpl( | |
| JSONFactoryUtil.createJSONObject(message.getString("response"))); | |
| } | |
| public List<HSForm> getAllHSForms() throws PortalException { | |
| String uri = HubSpotURIUtil.get("forms"); | |
| Map<String, String> headers = getHeaders(ContentTypes.APPLICATION_JSON); | |
| if (!HubSpotUtil.isEnabled()) { | |
| return Collections.emptyList(); | |
| } | |
| Message message = HubSpotServerUtil.executeGet(uri, headers); | |
| if (message.getInteger("statusCode") != HttpStatus.SC_OK) { | |
| throw new HubSpotServerException(message); | |
| } | |
| JSONArray jsonArray = JSONFactoryUtil.createJSONArray( | |
| message.getString("response")); | |
| List<HSForm> hsForms = new ArrayList<HSForm>(); | |
| for (int i = 0; i < jsonArray.length(); i++) { | |
| hsForms.add(new HSFormImpl(jsonArray.getJSONObject(i))); | |
| } | |
| return hsForms; | |
| } | |
| public HSForm getHSFormByGUID(String guid) throws PortalException { | |
| String encodedGUID = HttpUtil.encodeURL(guid, true); | |
| if (!HubSpotUtil.isEnabled()) { | |
| return null; | |
| } | |
| String uri = HubSpotURIUtil.get("forms/" + encodedGUID); | |
| Map<String, String> headers = getHeaders(ContentTypes.APPLICATION_JSON); | |
| Message message = HubSpotServerUtil.executeGet(uri, headers); | |
| if (message.getInteger("statusCode") == HttpStatus.SC_NOT_FOUND) { | |
| throw new NoSuchHSFormException(message); | |
| } | |
| if (message.getInteger("statusCode") != HttpStatus.SC_OK) { | |
| throw new HubSpotServerException(message); | |
| } | |
| return new HSFormImpl( | |
| JSONFactoryUtil.createJSONObject(message.getString("response"))); | |
| } | |
| public HSForm submitHSForm( | |
| String guid, String userToken, String ipAddress, String pageURL, | |
| String pageName, String redirectURL, String salesforceCampaignId, | |
| String[] fields) | |
| throws PortalException { | |
| if (!HubSpotUtil.isEnabled()) { | |
| return null; | |
| } | |
| if ((fields.length == 0) || ((fields.length % 2) != 0)) { | |
| throw new IllegalArgumentException( | |
| "Fields length must be an even number"); | |
| } | |
| String uri = HubSpotURIUtil.getSubmitForm(guid); | |
| Map<String, String> headers = getHeaders( | |
| ContentTypes.APPLICATION_X_WWW_FORM_URLENCODED); | |
| Body body = getBody( | |
| userToken, ipAddress, pageURL, pageName, redirectURL, | |
| salesforceCampaignId, fields); | |
| Message message = HubSpotServerUtil.executePost(uri, headers, body); | |
| if (message.getInteger("statusCode") == HttpStatus.SC_NOT_FOUND) { | |
| throw new NoSuchHSFormException(message); | |
| } | |
| if (message.getInteger("statusCode") != HttpStatus.SC_NO_CONTENT) { | |
| throw new HubSpotServerException(message); | |
| } | |
| return getHSFormByGUID(guid); | |
| } | |
| public HSForm submitHSForm(String guid, String[] fields) | |
| throws PortalException { | |
| return submitHSForm(guid, null, null, null, null, null, null, fields); | |
| } | |
| protected Body getBody( | |
| String userToken, String ipAddress, String pageURL, String pageName, | |
| String redirectURL, String salesforceCampaignId, String[] fields) { | |
| String content = StringPool.BLANK; | |
| for (int i = 0; i < fields.length; i += 2) { | |
| String fieldName = fields[i]; | |
| String fieldValue = fields[i + 1]; | |
| if (Validator.isNotNull(fieldName) && (fieldValue != null)) { | |
| fieldName = HttpUtil.encodeURL(fieldName, true); | |
| fieldValue = HttpUtil.encodeURL(fieldValue, true); | |
| if (content.equals(StringPool.BLANK)) { | |
| content = fieldName + "=" + fieldValue; | |
| } | |
| else { | |
| content = content + "&" + fieldName + "=" + fieldValue; | |
| } | |
| } | |
| } | |
| if (Validator.isNotNull(userToken) || Validator.isNotNull(ipAddress) || | |
| Validator.isNotNull(pageURL) || Validator.isNotNull(pageName) || | |
| Validator.isNotNull(redirectURL) || | |
| Validator.isNotNull(salesforceCampaignId)) { | |
| JSONObject jsonObject = JSONFactoryUtil.createJSONObject(); | |
| if (Validator.isNotNull(userToken)) { | |
| jsonObject.put("hutk", userToken); | |
| } | |
| if (Validator.isNotNull(ipAddress)) { | |
| jsonObject.put("ipAddress", ipAddress); | |
| } | |
| if (Validator.isNotNull(pageURL)) { | |
| jsonObject.put("pageUrl", pageURL); | |
| } | |
| if (Validator.isNotNull(pageName)) { | |
| jsonObject.put("pageName", pageName); | |
| } | |
| if (Validator.isNotNull(redirectURL)) { | |
| jsonObject.put("redirectUrl", redirectURL); | |
| } | |
| if (Validator.isNotNull(salesforceCampaignId)) { | |
| jsonObject.put("sfdcCampaignId", salesforceCampaignId); | |
| } | |
| String encodedContext = HttpUtil.encodeURL( | |
| jsonObject.toString(), true); | |
| if (content.equals(StringPool.BLANK)) { | |
| content = "hs_context=" + encodedContext; | |
| } | |
| else { | |
| content = content + "&hs_context=" + encodedContext; | |
| } | |
| } | |
| return new Body(content, ContentTypes.TEXT_PLAIN, StringPool.UTF8); | |
| } | |
| protected Map<String, String> getHeaders(String contentType) { | |
| Map<String, String> headers = new HashMap<String, String>(); | |
| headers.put(HttpHeaders.CONTENT_TYPE, contentType); | |
| headers.put(HttpHeaders.USER_AGENT, "HS_Forms"); | |
| return headers; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment