19 July, 2017

REST Client OSGi Bundle

Hello Friends,

If you are planing to create HTTP REST client using OSGi, Just follow the steps.

1) Create bundle project of type API using IDE
2) Write one Class called RestClient.java
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
//import org.apache.commons.logging.LogFactory;

/**
 * This example demonstrates how to create HTTP client for secure connection
 * context.
 */
/**
 * @author jignesh.vachhani
 *
 */
public class CustomHTTPClient {
private static final String username = "test";
private static final String password = "test";
private static final String host = "my.host.name";
private static final int port = 443;
private static final String protocol = "https";
static CloseableHttpClient httpclient = null;
static HttpHost targetHost = new HttpHost(host, port, protocol);

static HttpClientContext context = HttpClientContext.create();

public CustomHTTPClient() {
// Setting Credentials
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(host, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthSchemes.BASIC),
new UsernamePasswordCredentials(username, password));

// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);

// Add AuthCache to the execution context
context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);

httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
}

static {
new CustomHTTPClient();
}

public final static void main(String[] args) throws Exception {
try {

boolean post = true;
boolean get = true;
if (post) {
HttpPost httpPost = new HttpPost(
"https://my.host.name/iam/governance/selfservice/api/v1/unauthservice/forgotusername");
String json = "{\r\n  \"email\": \"jigs.vachhani@gmail.com\"\r\n}";
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

CloseableHttpResponse response = httpclient.execute(httpPost);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
EntityUtils.consume(entity);

} finally {
response.close();
}
}
if (get) {
HttpGet httpget = new HttpGet(
"https://my.host.name/iam/governance/selfservice/api/v1/users");
CloseableHttpResponse response = httpclient.execute(targetHost, httpget, context);
try {
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
EntityUtils.consume(entity);

} finally {
response.close();
}
}
} finally {
httpclient.close();
}
}

}

3) define below dependencies in build.gradle
dependencies {
    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "2.0.0"
    compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "2.0.0"
    compileOnly group: "javax.portlet", name: "portlet-api", version: "2.0"
    compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
    compileOnly group: "jstl", name: "jstl", version: "1.2"
    compileOnly group: "org.osgi", name: "osgi.cmpn", version: "6.0.0"
    compile group: 'commons-codec', name: 'commons-codec', version: '1.9'
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5'
    compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.1'

}
4) In bnd.bnd
Bundle-Name: sample-rest-client
Bundle-SymbolicName: sample.rest.client
Bundle-Version: 1.0.0
Export-Package: \
    sample.rest.client.constants,\
    sample.rest.client.api
 Include-Resource:\
@httpclient-4.5.jar,\
@httpcore-4.4.1.jar,\
@commons-codec-1.9.jar 



Note : Below dependencies required in class path :
httpclient-4.5.jar,\
httpcore-4.4.1.jar,\
commons-codec-1.9.jar 


Just compile or deploy and you are done. Its simple  interesting isn't it ?

Rate Me:

0 Discussion: