Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Rest Client

DaviRyan edited this page Nov 14, 2014 · 34 revisions

#The SBT Rest Client The RestClient is a lightweight HTTP library for performing most types of HTTP requests. Features

  • GET, POST ,PUT and DELETE operations.
  • Handles JSON, XML, and Atom responses as well as String/Raw responses.
  • Supports URL parameters, file uploads and custom body entities.
  • Supports Basic Authentication.
  • Supports custom headers.

Set Up Your Project

With Maven

You need the com.ibm.sbt.core.jar dependency. You can add this to your project by creating a maven project and listing the sbt.core.jar as a dependency.Below is the dependency you need to add to the pom.xml

<dependencies>
<dependency>
<groupId>com.ibm.sbt</groupId>
<artifactId>com.ibm.sbt.core</artifactId>
<version>1.1.1.20141111-1200</version>
</dependency>
</dependencies>

Here is an example of the full pom.xml including the sbt.core dependencies

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>com.ibm.sbt.core</artifactId>
<packaging>eclipse-plugin</packaging>
<parent>
    <groupId>com.ibm.sbt</groupId>
    <artifactId>com.ibm.sbt.sdk</artifactId>
    <version>1.1.1.20141111-1200</version>
</parent>
<dependencies>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
    </dependency>
    <dependency>
        <groupId>com.ibm.sbt</groupId>
        <artifactId>com.ibm.commons.runtime</artifactId>
        <version>1.1.1.20141111-1200</version>
    </dependency>
    <dependency>
        <groupId>com.ibm.sbt</groupId>
        <artifactId>com.ibm.commons.xml</artifactId>
        <version>9.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.ibm.sbt</groupId>
        <artifactId>com.ibm.commons</artifactId>
        <version>9.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.james</groupId>
        <artifactId>apache-mime4j</artifactId>
    </dependency>
</dependencies>

Without Maven

The required jar files can be downloaded from the maven central repository. Search for com.ibm.sbt.core , this jar file needs to be downloaded along with it's dependencies which can be found by clicking on the version number of the jar and viewing the Project Object Model (POM).

Use The Rest Client

RestClient restClient = new RestClient("https://apps.na.collabserv.com","username","password");
Response<AtomFeed> responseFeed = restClient.doGet("/communities/service/atom/communities/my").asAtomFeed();
System.out.println(responseFeed.getData().toXmlString());

Or Statically

RestClient.get("https://apps.na.collabserv.com/communities/service/atom/communities/my").basicAuth("username","password").asJson();

You can also create a client using an existing endpoint.

BasicEndpoint endpoint = new BasicEndpoint();
endpoint.setUrl("https://apps.na.collabserv.com");
endpoint.setUser("username");
endpoint.setPassword("password");
RestClient client = new RestClient(endpoint);

Or if your project is a JEE application you can create the RestClient using an endpoint defined in a managed-beans.xml file.

RestClient restClient = new RestClient("connections");

The managed-beans.xml file should be added to the Web-INF directory. Below is an example of a managed-beans.xml

<?xml version="1.0"?>
<faces-config>
<!-- Password store physical implementation -->
<managed-bean>
	<managed-bean-name>PwdStore</managed-bean-name>
	<managed-bean-class>com.ibm.sbt.security.authentication.password.consumer.store.MemoryPasswordStore</managed-bean-class>
	<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<!--  Default Environment -->
<managed-bean>
	<managed-bean-name>defaultEnvironment</managed-bean-name>
	<managed-bean-class>com.ibm.sbt.jslibrary.SBTEnvironment</managed-bean-class>
	<managed-bean-scope>application</managed-bean-scope>
	<managed-property>
		<property-name>endpoints</property-name>
		<value>connections</value>
	</managed-property>
	<managed-property>
		<property-name>properties</property-name>
		<value></value>
	</managed-property>
</managed-bean>	
<!-- Connections -->
<managed-bean>
	<managed-bean-name>connections</managed-bean-name>
	<managed-bean-class>com.ibm.sbt.services.endpoints.ConnectionsBasicEndpoint</managed-bean-class>
	<managed-bean-scope>session</managed-bean-scope>
	<managed-property>
		<property-name>url</property-name>
		<value>%{connections.url}</value>
	</managed-property>
    <managed-property>
        <property-name>user</property-name>
        <value>%{connections.user}</value>
    </managed-property>
    <managed-property>
        <property-name>password</property-name>
        <value>%{connections.password}</value>
    </managed-property>
	<managed-property>
		<property-name>passwordStore</property-name>
		<value>PwdStore</value>
	</managed-property>
	<managed-property>
		<property-name>forceTrustSSLCertificate</property-name>
		<value>true</value>
	</managed-property>
</managed-bean>
</faces-config>

The managed-beans.xml references properties such as url, username and password.A properties file called sbt.properties needs to be added to the root of the project and whatever properties are references in the managed-beans.xml need to be added to the sbt.properties file.

##Add Basic Authentication

RestClient.get("https://apps.na.collabserv.com/communities/service/atom/communities/my").basicAuth("username", "password");

Create Requests and Get Responses

To create a request using a static client call the clients get(), post(), put() or delete() methods or doGet(), doPost(), doPut() and doDelete() if using non statically. Then call one of the Data handler methods to return the response. For example asString(), asXML().

Response<String> response = RestClient.get("https://apps.na.collabserv.com/communities/service/atom/communities/my").asString();

##Add Parameters, Headers and Request Body To add a URL Parameter

RestClient.get("https://apps.na.collabserv.com/communities/service/atom/communities/my").parameter("ps","10");

Or if you have multiple parameters you can create a map of parameters and add this to the request.

Map<String, String> params = new HashMap<String, String>();
params.put("ps", "5");
params.put("page", "2");
RestClient.get("https://apps.na.collabserv.com/communities/service/atom/communities/my").parameters(params);

To add headers

RestClient.post("https://apps.na.collabserv.com/communities/service/atom/communities/my").header("Content-type","application/atom+xml");

You can also add a map of headers in the same way as adding a map of parameters.

To add the request body, pass the body as a string as well as the Content Type.

RestClient.post("https://apps.na.collabserv.com/communities/service/atom/communities/my").body(requestBody, "Application/atom+xml");