Difference between revisions of "GxRest/GxJRS/Requests"

From Gcube Wiki
Jump to: navigation, search
(GXWebTargetAdapterRequest)
Line 6: Line 6:
  
 
= GXWebTargetAdapterRequest =
 
= GXWebTargetAdapterRequest =
 +
 +
<source lang="Java">
 +
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;
 +
 +
//send a post request with content
 +
 +
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("gxrest")
 +
.queryParams(queryParams).withEntity(Entity.entity(context, MediaType.APPLICATION_JSON)).post();
 +
if (!response.hasCREATEDCode()) {
 +
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 {
 +
logger.info("Resource successfully created!");
 +
logger.info("Returned message: " + response.getStreamedContentAsString());
 +
}
 +
 +
</source>

Revision as of 04:30, 24 April 2018

Introduction

Types of Requests

GXHTTPRequest

GXWebTargetAdapterRequest

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;
 
//send a post request with content
 
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("gxrest")
		.queryParams(queryParams).withEntity(Entity.entity(context, MediaType.APPLICATION_JSON)).post();
if (!response.hasCREATEDCode()) {
	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 {
	logger.info("Resource successfully created!");
	logger.info("Returned message: " + response.getStreamedContentAsString());
}