uCosminexus Application Server, Web Service Development Guide
Create Implementation Class of the client that uses the HttpURLConnection class.
The following example describes how to create an implementation class of a Web resource client:
package com.sample.client;
import java.net.URL;
import java.net.URLEncoder;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;
//sample:starting the Web resource client
public class SampleClient {
public static void main(String[] args) {
final String HOST = args[0];
final String PORT = args[1];
SampleClient sampleClient = new SampleClient();
try{
sampleClient.demonstration1(HOST, PORT);
sampleClient.demonstration2(HOST, PORT);
System.out.println("\n----- Successfully Ended -----");
}catch(Exception e){
//displaying a detailed exception message
System.out.println(e.getMessage());
}
}
private void demonstration1(String HOST, String PORT) {
System.out.println("\n Demonstration 1 started.");
System.out.println(" This demonstrates injection of " +
"javax.ws.rs.core.Request instance " +
"onto resource class field by using @Context.");
URL url = null;
HttpURLConnection httpConn = null;
Map<String, List<String>> headers = null;
List<String> status = null;
String responseEntity = "";
|
//calling the Web resource
try {
//setting the URI of the Web resource to be called
url = new URL("http://" + HOST + ":" + PORT+
"/tutorial/root");
//setting up the initial connection
httpConn = (HttpURLConnection) url.openConnection();
//setting the HTTP() method as "GET"
httpConn.setRequestMethod("GET");
//starting the connection
httpConn.connect();
}catch (MalformedURLException e) {
System.out.println(" ERROR: " + e.getClass() + " was thrown. ");
//displaying the stack trace
e.printStackTrace();
throw new RuntimeException(" Demonstration 1 failed.");
}catch (IOException e) {
System.out.println(" ERROR: " + e.getClass() + " was thrown. ");
//displaying the stack trace
e.printStackTrace();
throw new RuntimeException(" Demonstration 1 failed.");
}catch (Exception e) {
System.out.println(" ERROR: " + e.getClass() + " was thrown. ");
//displaying the stack trace
e.printStackTrace();
throw new RuntimeException(" Demonstration 1 failed.");
}
System.out.println(" The target URL is \"" + url + "\".");
System.out.println(" The HTTP method is " + "\"GET\"" + ".");
try {
//acquiring the HTTP response header
headers = httpConn.getHeaderFields();
//acquiring the HTTP status from the header
status = headers.get(null);
//acquiring the input stream reader and read HTTP response //entity body
BufferedReader rd = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String line = "";
while ((line = rd.readLine()) != null) {
responseEntity += line;
}
//closing the input stream reader
rd.close();
//closing the connection
httpConn.disconnect();
}catch (Exception e) {
System.out.println(" ERROR: " + e.getClass() + " was thrown. ");
//displaying the stack trace
e.printStackTrace();
|
throw new RuntimeException(" Demonstration 1 failed.");
}
System.out.println(" Connection and interaction ended successfully.");
//sets the expected value of the response status code and entity body
String statusExpect = "[HTTP/1.1 200 OK]";
String responseEntityExpect = "RequestMethod: GET";
//checks whether the value of the status code and entity body is equal to the expected value
if (status.toString().equals(statusExpect)
& responseEntity.equals(responseEntityExpect)) {
//displaying the HTTP header
System.out.println(" Response headers are " + headers.toString() + ".");
//displaying the entity body
System.out.println(" Response entity is " + responseEntity + ",");
System.out.println(" which means target resource completed " +
"the process described above without any problem.");
System.out.println(" Demonstration 1 ended successfully.");
}else {
System.out.println(" The response is not as expected.");
throw new RuntimeException(" Demonstration 1 failed.");
}
private void demonstration2(String HOST, String PORT) {
System.out.println("\n Demonstration 2 started.");
System.out.println(" This demonstrates injection of QueryParam " +
"onto resource bean setter method by using @QueryParam.");
URL url = null;
HttpURLConnection httpConn = null;
String responseEntity = "";
Map<String, List<String>> headers = null;
List<String> status = null;
//calling the Web resource
try {
//setting the URI of the Web resource to be called
//assigning the query parameter to the URI
url = new URL("http://" + HOST + ":" + PORT+
"/tutorial/root/getQueryParam?queryParam=queryValue");
//setting up the initial connection
httpConn = (HttpURLConnection) url.openConnection();
//setting the HTTP() method as"GET"
httpConn.setRequestMethod("GET");
//starting the connection
httpConn.connect();
|
}catch (MalformedURLException e) {
System.out.println(" ERROR: " + e.getClass() + " was thrown. ");
//displaying the stack trace
e.printStackTrace();
throw new RuntimeException(" Demonstration 2 failed.");
}catch (IOException e) {
System.out.println(" ERROR: " + e.getClass() + " was thrown. ");
//displaying the stack trace
e.printStackTrace();
throw new RuntimeException(" Demonstration 2 failed.");
}catch (Exception e) {
System.out.println(" ERROR: " + e.getClass() + " was thrown. ");
//displaying the stack trace
e.printStackTrace();
throw new RuntimeException(" Demonstration 2 failed.");
}
System.out.println(" The target URL is \"" + url + "\".");
System.out.println(" The HTTP method is " + "\"GET\"" + ".");
try {
//acquiring the HTTP header
headers = httpConn.getHeaderFields();
//acquiring the HTTP status code from the HTTP header
status = headers.get(null);
//acquiring the input stream reader and reading the response //entity body of the HTTP response
BufferedReader rd = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String line = "";
while ((line = rd.readLine()) != null) {
responseEntity += line;
}
//closing the input stream reader
rd.close();
//closing the connection
httpConn.disconnect();
}catch (Exception e) {
System.out.println(" ERROR: " + e.getClass() + " was thrown. ");
//displaying the stack trace
e.printStackTrace();
throw new RuntimeException(" Demonstration 2 failed.");
}
System.out.println(" Connection and interaction ended successfully.");
//setting the expected value of the response status code and entity body
String statusExpect = "[HTTP/1.1 200 OK]";
String responseEntityExpect = "QueryParameter: queryValue";
//checking whether the status code and entity body are same as the expected values
if (status.toString().equals(statusExpect)
& responseEntity.equals(responseEntityExpect)) {
//displaying the HTTP header
System.out.println(" Response headers are " + headers.toString() + ".");
//displaying the entity body
System.out.println(" Response entity is " + responseEntity + ",");
System.out.println(" which means target resource completed " +
"the process described above without any problem.");
System.out.println(" Demonstration 2 ended successfully.");
}else {
System.out.println(" The response is not as expected.");
throw new RuntimeException(" Demonstration 2 failed.");
}
}
}
}
|
You save the created SampleClient.java in the c:\temp\jaxrs\works\tutorial\client\src\com\sample\client\ directory in the UTF-8 format.
All Rights Reserved. Copyright (C) 2013, Hitachi, Ltd.