Using Zeit with Codenvy

Talk to Zeit API using Codenvy Factory:


ZEIT is one of the biggest German newspapers. They provide ZEIT ONLINE Content API which gives developers access to hundreds of thousands of ZEIT articles and their corresponding metadata. The API provides the information about author search, articles, newspaper departments, keywords, products and series. The API uses RESTful calls and responses will be in JSON. Before accessing the Rest API a developer needs a API key . Developers can get he API Key at http://developer.zeit.de/quickstart/.

API Request:
api
API Key we have requested:

1d78b3a9bd4ad4c8bf5c1b461410c8df20a494e08c722c0360f4

More info on API Documentation regarding methods and parameters is available here. Method details are presented below:
endpoints

In this Tutorial we are going to Use “department” Method to access the all Departments Information. The department method has 4 parameters:
endpoints1

Create a Project

Project structure in Codenvy:
project

Create Java Class

Create a servlet to access the REST API Provided by ZEIT. In the REST call we are limiting the result data to 2 (limit=2) and search parameter is “*” (q=*) which looks for the entire department Information, remaining parameters will be initialize to default.

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 ZeitOnlineServlet 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.zeit.de/department?q=*&limit=2";

            HttpGet req = new HttpGet(getURL);
            req.addHeader("X-Authorization", "1d78b3a9bd4ad4c8bf5c1b461410c8df20a494e08c722c0360f4");
            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/>");
            }
           // sb.append(responseString).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 ZEIT Online for retrieving the Department Information. In response we’ll receive information in JSON Objects as shown below.

Create index.jsp and success.jsp Files

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

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

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>ZEITOnline-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>