Using HubSpot with Codenvy

Get started with HubSpot API project using a Codenvy Factory:


Hubspot is marketing content management software. It is a most powerful, tightly integrated and comprehensive marketing software available, putting personalized inbound marketing into the hands of any business. To use the Hubspot Sofware user needs an account at http://www.hubspot.com/. With this software user can create blogs, apps and services provided by HubSpot. In this tutorial we are going to use the blog Service supported by Hubspot.som, create some blog posts, add some comments and subscribe to blog posts for our testing.

Generally we get the created blog on the web with URL provided by Hubspot. For accessing the information about our Blog or services or Email Subscriptions.we can make use of the Hubspot API’s which is available as REST API. For using the HubSpot API one needs to register at https://developers.hubspot.com/docs. After registering at the Hubspot user will get the API key to access the HubSpot API’s or we can use the Default information provided by the API as shown below.

testapi

In this tutorial we are going to use the email REST API which retrieves the subscription information for the demo Account.

Create a Project

Project structure in Codenvy:
project_structure

Create servlet

First create a servlet to access the REST EMAIL API.

package com.codenvy;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HubSpotServlet extends HttpServlet{

private static final long serialVersionUID = 1L;
private HttpClient client = new DefaultHttpClient();

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        StringBuffer sb = null;
        try {
            String getURL = "http://api.hubapi.com/email/v1/public/subscriptions?hapikey=demo";

            HttpGet req = new HttpGet(getURL);

            HttpResponse res = client.execute(req);
            HttpEntity entity = res.getEntity();
            String responseString = new BasicResponseHandler().handleResponse(res);
            if (entity != null) {
                EntityUtils.consume(entity);
            }

            sb = new StringBuffer("<br/>Simple GET request to url : "+ getURL +"<br/><br/>");
            sb.append("*** Request headers ***").append("<br/>");
            Header[] requestHeaders = req.getAllHeaders();
            for (Header header : requestHeaders) {
                sb.append(header.toString()).append("<br/>");
            }
            sb.append("***********************").append("<br/><br/>");

            sb.append("*** Response headers ***").append("<br/>");
            sb.append(res.getStatusLine()).append("<br/>");
            Header[] headers = res.getAllHeaders();
            for (Header header : headers) {
                sb.append(header.toString()).append("<br/>");
            }
            sb.append("***********************").append("<br/><br/>");

            sb.append("*** Response Body ***").append("<br/>");
            String[] str=responseString.split(",");

           for(String s:str){
              sb.append(s).append("<br/>");
              }

        } catch (Exception ex) {
            sb = new StringBuffer(ex.toString());
        }

        request.setAttribute("data", sb.toString());
        RequestDispatcher rd = request.getRequestDispatcher("/success.jsp");
        rd.forward(request, response);
   }
}

In this servlet we are calling the Rest API of Hubspot for retrieving the Email subscription Information. In response we’ll receive information in JSON Objects as shown below.
app

Create index.jsp and success.jsp Files

We’ll call the Servlet from a Jsp.
Index.jsp:

<%    response.sendRedirect("/hubspot");%>

We’ll retrieve the information in success.jsp:
Success.jsp:

<%=request.getAttribute("data") %>

Create pom.xml File

We need to specify the dependencies in pom.xml as shown below:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.codenvy</groupId>
  <artifactId>DataSift-web</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>java-web-sample</name>
  <dependencies>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
  </dependency>
  <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.5</version>
  </dependency>
  <dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>6.0</version>
  </dependency>
  </dependencies>
  <build>
    <finalName>java-web-sample</finalName>
  </build>
</project>