Using Runscope with Codenvy

Use this Codenvy Factory to instantly start working with a sample Runscope API project:


This tutorial will explain the details of integrating the Runscope API into Codenvy. Runscope is an automated testing and API traffic inspection tool allowing you to create automated tests for your customer application and also monitoring traffic flow within your customer application URL. Runscope API provides the programmatic access to the data in your Runscope account thereby allowing you to retrieve information about the automated tests and traffic flow. The explanation has been done with a very basic Java servlet project that makes use of the Runscope API to explain some basic features provided by the API.

Register With Runscope

Before proceeding to the creation of the Java project, please register at https://www.runscope.com/signup with your email id:

runscope-register

While registering, you will need to create an application which will then generate the API keys which can be found under your account:

runscope-apikeys

You can get more information about the Runscope API at
https://www.runscope.com/docs. There are different subsections providing information about automated testing, traffic inspection and various methods available in the API to retrieve information about these from your Runscope account. Each API method subsection at https://www.runscope.com/docs/api/resources will detail the request URL required to invoke the specific API method that you can use in your integration process and the list of input parameters and the response fields. As can be seen from the documentation, Runscope uses OAuth mechanism to derive the access token for authentication to use the API. The access token is derived using the API keys and so you will need to derive an access token within your application to start using the API.
For development purposes, Runscope generates an access token along with the API keys when you create your application and this access token is available under your account:

runscope-accesstoken

You can click on the ‘Try it in the Request Editor’ button to generate the API method request URLs and parameters that you want to use in your application:

runscope-requesteditor

After creating your Runscope account, you can see your account dashboard at https://www.runscope.com/stream/hgtn9691q8rw. This will give you a view of all the tests and traffic flow within your runscope account and will also allow you to create tests and activate traffic flow for your application using the process defined in the documentation. You can access the account dashboard information programmatically using the API methods:

runscope-dashboard

Create a Project

Now, login to your Codenvy workspace and create a WAR project named as ‘RunscopeSample’. This will have the following structure:
runscope-structure

Create pom.xml File

To make use of the Runscope API through HttpClient as a servlet, please update the pom.xml to include the HttpClient and Java EE Web API jars and the servletapi jar into your project as Maven dependencies:

  <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>
  <repositories>
     <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <url>http://repo1.maven.org/maven2</url>
     </repository>
  </repositories>

This will include these jars in your project as follows:
runscope-dependencies

Create Java Class

Now create a Java servlet class named RunscopeServlet.java that will talk to the Runscope API with the Http request URL and input parameters(obtained from the Request Editor or documentation) to request all the messages available in your bucket – test runs as well as website traffic flows(the same information which is available in your account dashboard) and then get a Http response back with the response header and the response body which will be then displayed on the browser through a JSP. All this is done inside the method doGet() in the sample source Java class.

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

        StringBuffer sb = null;
        try {
            String getURL = "https://api.runscope.com/buckets/hgtn9691q8rw/messages";
            HttpGet req = new HttpGet(getURL);
            req.addHeader("Authorization", "Bearer sdfresrfsefrsefsd35345345345");
            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/>");
            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);
   }

Create index.jsp File

Your Codenvy project will have an index.jsp which will invoke the Servlet class -

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

Create success.jsp File

Your Codenvy project will have a success.jsp which will be invoked from the RunscopeServlet class to display the response on the browser -

<html>
  <body>
    <div>
      <p>Data : <br/><%=request.getAttribute("data") %></p>
      <br/>
    </div>
</body>
</html>

Create web.xml File

The servlet mapping ‘/hello’ is defined in the web.xml as follows –

<web-app>
  <display-name>Runscope Servlet App</display-name>
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.codenvy.RunscopeServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

Now you build and run your Codenvy project and the Http response will be displayed in the browser:

runscope-output

In a similar way, you can use other available API methods and integrate the Runscope API into your application to meet your application needs.