Difference between revisions of "GxRest/GxJRS/Requests"

From Gcube Wiki
Jump to: navigation, search
(GXWebTargetAdapterRequest)
(GXWebTargetAdapterRequest)
Line 39: Line 39:
 
== Registering JAX-RS components ==
 
== Registering JAX-RS components ==
  
Register an instance of a custom JAX-RS component (such as an extension provider or a feature (javax.ws.rs.core.Feature) to be instantiated and used in the scope of this request:
+
The following example shows how to register an instance of a custom JAX-RS component (a feature in this case) to be instantiated and used in the scope of the request:
 
<source lang="Java">
 
<source lang="Java">
 +
import javax.ws.rs.core.Feature;
 +
 +
public class MyFeature implements Feature {
 +
//feature implementation
 +
}
 +
 +
GXWebTargetAdapterRequest request =
 +
        GXWebTargetAdapterRequest.newRequest("http://host:port/service/").from("GXRequestTest");
 +
request.register(MyFeature.class)
  
 
</source>
 
</source>

Revision as of 13:05, 24 April 2018

Introduction

Types of Requests

GXHTTPRequest

GXWebTargetAdapterRequest

A basic example that sends a Post request to create a new resource:

import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
 
//...
GXWebTargetAdapterRequest request = 
        GXWebTargetAdapterRequest.newRequest("http://host:port/service/").from("GXRequestTest");
 
String context ="json serialization (not shown)";
Map<String,String> queryParams = new WeakHashMap<>();
queryParams.put("rrURL", DEFAULT_RR_URL);
String DEFAULT_RR_URL = "url of resource registry to contact";
 
 
GXInboundResponse response = request.path("resource-manager")
		.queryParams(queryParams).withEntity(Entity.entity(context, MediaType.APPLICATION_JSON)).post();

Overriding the security context

By default, the security token available in the current thread is attached to the request.

However, if there is the need to force a specific token to be used, this can be done by invoking the setSecurityToken() method on the request object:

GXWebTargetAdapterRequest request = 
        GXWebTargetAdapterRequest.newRequest("http://host:port/service/").from("GXRequestTest");
request.setSecurityToken("my token");

Registering JAX-RS components

The following example shows how to register an instance of a custom JAX-RS component (a feature in this case) to be instantiated and used in the scope of the request:

import javax.ws.rs.core.Feature;
 
public class MyFeature implements Feature {
 //feature implementation
}
 
GXWebTargetAdapterRequest request = 
        GXWebTargetAdapterRequest.newRequest("http://host:port/service/").from("GXRequestTest");
request.register(MyFeature.class)

Consuming the response

Here is an example about how to consume the returned response:

import javax.ws.rs.core.Response;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
 
 
GXInboundResponse response = //request
 
if (response.hasGXError()) {
//this means that the error response has been generated at service side with gxRest as well
	if (response.hasException()) {
		try {
			throw response.getException();
		} catch (ClassNotFoundException e) {
			// can't recreate the original exception (not on the classpath?)
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	} else {
		//if you want to use the original response
		Response jsResponse = response.getSource();
		//then consume the response from here
	}
} else {
	if (response.hasCREATEDCode()) {
		System.out.println("Resource successfully created!");
		System.out.println("Returned content: " + response.getStreamedContentAsString());
	} else {
		System.out.println("Resource creation failed. Returned status:" + response.getHTTPCode());
		//if you want to use the original response
		Response jsResponse = response.getSource();
		//then consume the response from here
	}
}