Difference between revisions of "GxRest/GxJRS/Requests"

From Gcube Wiki
Jump to: navigation, search
(GXWebTargetAdapterRequest)
(GXWebTargetAdapterRequest)
Line 25: Line 25:
 
GXInboundResponse response = request.path("resource-manager")
 
GXInboundResponse response = request.path("resource-manager")
 
.queryParams(queryParams).withEntity(Entity.entity(context, MediaType.APPLICATION_JSON)).post();
 
.queryParams(queryParams).withEntity(Entity.entity(context, MediaType.APPLICATION_JSON)).post();
if (!response.hasCREATEDCode()) {
+
</source>
 +
By default, it attaches to the request the security token available in the current thread.
 +
 
 +
However, if there is the need to force a specific token to be used, it can be done by invoking the setSecurityToken() method on the request object:
 +
<source lang="Java">
 +
GXWebTargetAdapterRequest request =
 +
        GXWebTargetAdapterRequest.newRequest("http://host:port/service/").from("GXRequestTest")
 +
request.setSecurityToken("my token");
 +
</source>
 +
 
 +
== Consuming the response ==
 +
 
 +
<source lang="Java">
 +
 
 +
GXInboundResponse response = //request
 +
 
 +
if (response.hasGXError()) {
 
if (response.hasException()) {
 
if (response.hasException()) {
 
try {
 
try {
 
throw response.getException();
 
throw response.getException();
 
} catch (ClassNotFoundException e) {
 
} catch (ClassNotFoundException e) {
//can't rebuild the exception locally (not on the classpath?)
+
//that's OK, we can tolerate this
 
} catch (Exception e) {
 
} catch (Exception e) {
e.printStackTrace();
+
e.printStackTrace();
 
throw e;
 
throw e;
 
}
 
}
 +
} else {
 +
//if you want to use the original response
 +
Response jsResponse = response.getSource();
 +
//then consume the response from here
 
}
 
}
 
} else {
 
} else {
logger.info("Resource successfully created!");
+
if (response.hasCREATEDCode()) {
logger.info("Returned message: " + response.getStreamedContentAsString());
+
System.out.println("Resource successfully created!");
 +
System.out.println("Returned message: " + 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
 +
}
 
}
 
}
  
</source>
 
By default, it attaches to the request the security token available in the current thread.
 
 
However, if there is the need to force a specific token to be used, it can be done by invoking the setSecurityToken() method on the request object:
 
<source lang="Java">
 
GXWebTargetAdapterRequest request =
 
        GXWebTargetAdapterRequest.newRequest("http://host:port/service/").from("GXRequestTest")
 
request.setSecurityToken("my token");
 
 
</source>
 
</source>

Revision as of 05:01, 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();

By default, it attaches to the request the security token available in the current thread.

However, if there is the need to force a specific token to be used, it 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");

Consuming the response

GXInboundResponse response = //request
 
if (response.hasGXError()) {
	if (response.hasException()) {
		try {
			throw response.getException();
		} catch (ClassNotFoundException e) {
				//that's OK, we can tolerate this
		} 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 message: " + 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
	}
}