Category Archives: java

Getting Started: Creating a Java-Liberty App with Watson on Bluemix

An extended version of this tutorial, adding a Cloudant NoSQL Database and D3js data visualization, is available here.

Requirements:

  • Bluemix account
  • Github account

Steps:

  1. Create the StarterApp
  2. Add the Toolchain or Continuous Integration (CI)
  3. REST API Primer
  4. Authentication in REST API
  5. Getting the Bluemix Configuration
  6. Add the AlchemyData News API Client
  7. Add a Web Form
  8. Implement the AlchemyData News API Request
  9. Create Authorization Header for Basic Auth

1. Create the StarterApp

Continue reading

Print Friendly, PDF & Email

Creating a Java-Liberty App with Watson AlchemyAPI, CloudantDB and D3js

Requirements:

  • Git
  • Github account
  • Java
  • Eclipse for J2EE
  • WebSphere Liberty with JavaEE7
  • WebSphere Liberty plugin for Eclipse
  • Add *.cloudant.com.crt to the WepSphere Liberty Profile (wlp) server’s Java Key Store (JKS)
  • Optional: WAS AdminCenter 1.0
  • Optional: Maven

Steps:

  1. Create the StarterApp
  2. Add the Toolchain or Continuous Integration (CI)
  3. Setup Localhost
  4. Add JSPs and Servlet for News Search
  5. Add REST API that calls the AlchemyData News API
  6. Save the AlchemyData News Search Results in CloudantDB
  7. Add a D3js Sentiment Score Graph
  8. Configure the CloudantDB for Querying
  9. Show History of Search Results

1. Create the StarterApp

  1. Sign in to Bluemix.net,
  2. Go to Catalog,
  3. Under ‘Boilerplates’, click the ‘Java Cloudant Web Starter’,
  4. For ‘App name’ and ‘Host name’ enter ‘<username>-liberty-watson’,
  5. Click the ‘Create’ button,
  6. In ‘Application Details’ click the ‘Overview’ link,
    bluemix_application_details
  7. Review the application configuration,
  8. Click ‘Connections’ and click ‘Connect New’ to create the following services:
  9. If no AlchemyAPI service already exists, create a new AlchemyAPI service by clicking the ‘Connect New’ button,
  10. from the Catalog, filter by ‘Watson’, select the AlchemyAPI service, click the ‘Create’ button, and click ‘Restage’
  11. Note: by default you can only create 1 instance of the AlchemyAPI service under your organization in a single space, creating a second instance or connecting an existing service in another space will cause an error. If an instance already exists, click the ‘Connect Existing’ instead, select the existing AlchemyAPI service and click the ‘Connect’ button, click ‘Restage’,

2. Add the Toolchain or Continuous Integration (CI)

Continue reading

Print Friendly, PDF & Email

Calling a Watson service in Java

Overview

Note: this API needs to be updated to v2. Identify is now a resource on the Language Translation API.
http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/language-translation/api/v2/

Making an HTTP REST API POST request in Java is in principle as simple this:
Request request = Request.Post(serviceURI)
.addHeader("Authorization", basicAuthorization)
.bodyString(body, ContentType.APPLICATION_FORM_URLENCODED);
Executor executor = Executor.newInstance();
String response = executor.execute(request).returnContent().asString();

This code, uses the Apache Fluent API, which exposes only the essentials of the Apache HTTPClient.

The Authorization header is required for authentication by the Watson API, and is a base64 encoded string.
String auth = username + ":" + password;
String basicAuthorization = "Basic "+ Base64.encodeBase64String(auth.getBytes());

The POST request is “x-www-form-urlencoded” and in Java consists of a List<NameValuePair>:
List<NameValuePair> params = new ArrayList<NameValuePair>();

Continue reading

Print Friendly, PDF & Email

Java: Eclipse building Java projects on Mac OS X

Add the following Java archives to your build path in Eclipse on Mac OS X:

Java SE
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/classes.jar
Java EE
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/jsse.jar

Print Friendly, PDF & Email