updated code using ScopeProvider.instance.get
git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/uri-resolver-manager@100694 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
d62d428bc3
commit
1767471cae
|
@ -8,12 +8,13 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceParameter;
|
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceParameter;
|
||||||
import org.gcube.portlets.user.uriresolvermanager.exception.IllegalArgumentException;
|
import org.gcube.portlets.user.uriresolvermanager.exception.IllegalArgumentException;
|
||||||
import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException;
|
import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException;
|
||||||
import org.gcube.portlets.user.uriresolvermanager.readers.RuntimeResourceReader;
|
import org.gcube.portlets.user.uriresolvermanager.readers.RuntimeResourceReader;
|
||||||
import org.gcube.portlets.user.uriresolvermanager.readers.UriResolverMapReader;
|
import org.gcube.portlets.user.uriresolvermanager.readers.UriResolverMapReader;
|
||||||
import org.gcube.portlets.user.uriresolvermanager.util.HttpCallerUtil;
|
import org.gcube.portlets.user.uriresolvermanager.util.UrlEncoderUtil;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -29,18 +30,25 @@ public class UriResolverManager {
|
||||||
private Map<String, String> applicationTypes;
|
private Map<String, String> applicationTypes;
|
||||||
private String scope;
|
private String scope;
|
||||||
private String applicationType;
|
private String applicationType;
|
||||||
|
private Object infra;
|
||||||
|
|
||||||
public static final Logger logger = LoggerFactory.getLogger(UriResolverManager.class);
|
public static final Logger logger = LoggerFactory.getLogger(UriResolverManager.class);
|
||||||
/**
|
/**
|
||||||
*
|
* Instance a UriResolverManager
|
||||||
* @param scope to read resource = "Uri-Resolver-Map";
|
* Precondition: set the scope provider {@link ScopeProvider.instance.get()}
|
||||||
|
* The scope is used to look up the generic resource {@link UriResolverMapReader#URI_RESOLVER_MAP} available in the infrastructure to map ApplicationType with its Resolver
|
||||||
* @param applicationType a key Application Type
|
* @param applicationType a key Application Type
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public UriResolverManager(String scope, String applicationType) throws UriResolverMapException, IllegalArgumentException{
|
public UriResolverManager(String applicationType) throws UriResolverMapException, IllegalArgumentException{
|
||||||
try {
|
try {
|
||||||
|
|
||||||
this.uriResolverMapReader = new UriResolverMapReader(scope);
|
this.scope = ScopeProvider.instance.get();
|
||||||
|
|
||||||
|
if(this.scope == null)
|
||||||
|
throw new UriResolverMapException("Scope is null, set scope provider!");
|
||||||
|
|
||||||
|
this.uriResolverMapReader = new UriResolverMapReader(this.scope);
|
||||||
this.applicationTypes = uriResolverMapReader.getApplicationTypes();
|
this.applicationTypes = uriResolverMapReader.getApplicationTypes();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -52,7 +60,7 @@ public class UriResolverManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.applicationType = applicationType;
|
this.applicationType = applicationType;
|
||||||
this.scope = scope;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,8 +73,8 @@ public class UriResolverManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
ScopeProvider.instance.set(this.scope);
|
||||||
RuntimeResourceReader reader = new RuntimeResourceReader(scope, resourceName);
|
RuntimeResourceReader reader = new RuntimeResourceReader(this.scope, resourceName);
|
||||||
List<ServiceParameter> resourceParameters = reader.getServiceParameters();
|
List<ServiceParameter> resourceParameters = reader.getServiceParameters();
|
||||||
|
|
||||||
//CHECK PARAMETERS
|
//CHECK PARAMETERS
|
||||||
|
@ -80,14 +88,14 @@ public class UriResolverManager {
|
||||||
|
|
||||||
String baseURI = reader.getServiceBaseURI();
|
String baseURI = reader.getServiceBaseURI();
|
||||||
|
|
||||||
// String params = UrlEncoderUtil.encodeQuery(parameters);
|
String params = UrlEncoderUtil.encodeQuery(parameters);
|
||||||
// String uriRequest = reader.getServiceBaseURI()+"?"+params;
|
String uriRequest = reader.getServiceBaseURI()+"?"+params;
|
||||||
// logger.info("Created HTTP URI request: "+uriRequest);
|
logger.info("Created HTTP URI request: "+uriRequest);
|
||||||
|
|
||||||
baseURI = "http://pc-mangiacrapa.isti.cnr.it:8080/uri-resolver-1.2.0-SNAPSHOT/gis";
|
// baseURI = "http://pc-mangiacrapa.isti.cnr.it:8080/uri-resolver-1.2.0-SNAPSHOT/gis";
|
||||||
|
//
|
||||||
HttpCallerUtil httpCaller = new HttpCallerUtil(baseURI, "", "");
|
// HttpCallerUtil httpCaller = new HttpCallerUtil(baseURI, "", "");
|
||||||
httpCaller.callGet("", parameters);
|
// httpCaller.callGet("", parameters);
|
||||||
|
|
||||||
} catch (IllegalArgumentException e){
|
} catch (IllegalArgumentException e){
|
||||||
throw e;
|
throw e;
|
||||||
|
@ -117,7 +125,8 @@ public class UriResolverManager {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
UriResolverManager resolver = new UriResolverManager("/gcube", "GIS");
|
ScopeProvider.instance.set("/gcube/devsec/devVRE");
|
||||||
|
UriResolverManager resolver = new UriResolverManager("GIS");
|
||||||
Map<String, String> params = new HashMap<String, String>();
|
Map<String, String> params = new HashMap<String, String>();
|
||||||
params.put("gis-UUID", "12345");
|
params.put("gis-UUID", "12345");
|
||||||
params.put("scope", "/gcube");
|
params.put("scope", "/gcube");
|
||||||
|
|
|
@ -57,6 +57,7 @@ public class RuntimeResourceReader {
|
||||||
protected String readResource(String scope, String resourceName) throws Exception {
|
protected String readResource(String scope, String resourceName) throws Exception {
|
||||||
|
|
||||||
try{
|
try{
|
||||||
|
logger.info("Tentative read resource: "+resourceName+", scope: "+scope);
|
||||||
|
|
||||||
this.resourceName = resourceName;
|
this.resourceName = resourceName;
|
||||||
this.scope = scope;
|
this.scope = scope;
|
||||||
|
|
|
@ -29,6 +29,14 @@ import org.xml.sax.InputSource;
|
||||||
*/
|
*/
|
||||||
public class UriResolverMapReader {
|
public class UriResolverMapReader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final String URIRESOLVERMAP = "UriResolverMap";
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final String URI_RESOLVER_MAP = "Uri-Resolver-Map";
|
||||||
private Logger logger = LoggerFactory.getLogger(UriResolverMapReader.class);
|
private Logger logger = LoggerFactory.getLogger(UriResolverMapReader.class);
|
||||||
private String secondaryType;
|
private String secondaryType;
|
||||||
private String scope;
|
private String scope;
|
||||||
|
@ -43,14 +51,20 @@ public class UriResolverMapReader {
|
||||||
*/
|
*/
|
||||||
public UriResolverMapReader(String scope) throws Exception {
|
public UriResolverMapReader(String scope) throws Exception {
|
||||||
this.scope = scope;
|
this.scope = scope;
|
||||||
this.resourceName = "Uri-Resolver-Map";
|
|
||||||
this.secondaryType = "UriResolverMap";
|
if(scope==null){
|
||||||
|
throw new Exception("Scope is null, set scope provider!");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.resourceName = URI_RESOLVER_MAP;
|
||||||
|
this.secondaryType = URIRESOLVERMAP;
|
||||||
readProfileFromInfrastrucure();
|
readProfileFromInfrastrucure();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* this method looks up the applicationProfile profile among the ones available in the infrastructure
|
* this method looks up the generic resource among the ones available in the infrastructure using scope provider {@link ScopeProvider.instance.get()}
|
||||||
|
* resource name {@value #URI_RESOLVER_MAP} and secondaryType {@value #URIRESOLVERMAP}
|
||||||
* @param portletClassName your servlet class name will be used ad unique identifier for your applicationProfile
|
* @param portletClassName your servlet class name will be used ad unique identifier for your applicationProfile
|
||||||
* @return the applicationProfile profile
|
* @return the applicationProfile profile
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
|
@ -64,6 +78,7 @@ public class UriResolverMapReader {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
String infra = ScopeUtil.getInfrastructureNameFromScope(this.scope);
|
String infra = ScopeUtil.getInfrastructureNameFromScope(this.scope);
|
||||||
|
|
||||||
ScopeProvider.instance.set(infra);
|
ScopeProvider.instance.set(infra);
|
||||||
logger.info("scope provider set instance: "+infra);
|
logger.info("scope provider set instance: "+infra);
|
||||||
|
|
||||||
|
|
|
@ -1,207 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.gcube.portlets.user.uriresolvermanager.util;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.apache.commons.httpclient.Credentials;
|
|
||||||
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
|
|
||||||
import org.apache.commons.httpclient.HttpClient;
|
|
||||||
import org.apache.commons.httpclient.HttpException;
|
|
||||||
import org.apache.commons.httpclient.HttpStatus;
|
|
||||||
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
|
||||||
import org.apache.commons.httpclient.UsernamePasswordCredentials;
|
|
||||||
import org.apache.commons.httpclient.auth.AuthScope;
|
|
||||||
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
|
|
||||||
import org.apache.commons.httpclient.methods.GetMethod;
|
|
||||||
import org.apache.commons.httpclient.methods.PostMethod;
|
|
||||||
import org.apache.commons.httpclient.params.HttpMethodParams;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
||||||
* @Jun 4, 2013
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class HttpCallerUtil {
|
|
||||||
|
|
||||||
public static final Logger logger = LoggerFactory.getLogger(HttpCallerUtil.class);
|
|
||||||
|
|
||||||
public static final int TIME_OUT_REQUESTS = 5000;
|
|
||||||
|
|
||||||
|
|
||||||
private String urlService = "";
|
|
||||||
private HttpClient httpClient = null;
|
|
||||||
private String username = "";
|
|
||||||
private String password = "";
|
|
||||||
|
|
||||||
|
|
||||||
public HttpCallerUtil(String url, String user, String password) {
|
|
||||||
this.urlService = url;
|
|
||||||
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
|
|
||||||
connectionManager.getParams().setSoTimeout(TIME_OUT_REQUESTS);
|
|
||||||
this.httpClient = new HttpClient(connectionManager);
|
|
||||||
|
|
||||||
this.username = user;
|
|
||||||
this.password = password;
|
|
||||||
Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
|
|
||||||
httpClient.getState().setCredentials(AuthScope.ANY, defaultcreds);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public String callGet(String urlMethod, Map<String, String> parameters) throws Exception {
|
|
||||||
|
|
||||||
// Create an instance of HttpClient.
|
|
||||||
HttpClient client = new HttpClient();
|
|
||||||
|
|
||||||
String query = UrlEncoderUtil.encodeQuery(parameters);
|
|
||||||
|
|
||||||
String fullUrl;
|
|
||||||
|
|
||||||
if(urlMethod==null || urlMethod.isEmpty())
|
|
||||||
fullUrl = urlService+"?"+query;
|
|
||||||
else
|
|
||||||
fullUrl = urlService+"/"+urlMethod+"?"+query;
|
|
||||||
|
|
||||||
logger.info("call get .... " + fullUrl);
|
|
||||||
|
|
||||||
// Create a method instance.
|
|
||||||
GetMethod method = new GetMethod(fullUrl);
|
|
||||||
|
|
||||||
// Provide custom retry handler is necessary
|
|
||||||
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Execute the method.
|
|
||||||
int statusCode = client.executeMethod(method);
|
|
||||||
|
|
||||||
if (statusCode != HttpStatus.SC_OK) {
|
|
||||||
logger.error("Method failed: " + method.getStatusLine());
|
|
||||||
// try{
|
|
||||||
// method.getResponseBodyAsString();
|
|
||||||
// }catch(Exception e){
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
throw new Exception("Failed HTTP caller on "+fullUrl +" Response status line: " + method.getStatusLine());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read the response body.
|
|
||||||
byte[] responseBody = method.getResponseBody();
|
|
||||||
|
|
||||||
// Deal with the response.
|
|
||||||
// Use caution: ensure correct character encoding and is not binary
|
|
||||||
// data
|
|
||||||
return new String(responseBody);
|
|
||||||
|
|
||||||
} catch (HttpException e) {
|
|
||||||
logger.error("Fatal protocol violation: " + e.getMessage());
|
|
||||||
throw new Exception("Fatal protocol violation: " + e.getMessage());
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error("Fatal transport error: " + e.getMessage());
|
|
||||||
throw new Exception("Fatal transport violation: " + e.getMessage());
|
|
||||||
} finally {
|
|
||||||
// Release the connection.
|
|
||||||
method.releaseConnection();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public String callPost(String urlMethod, String body, String contentType) throws Exception {
|
|
||||||
|
|
||||||
// Create a method instance.
|
|
||||||
PostMethod method = new PostMethod(this.urlService + "/" + urlMethod);
|
|
||||||
|
|
||||||
method.setRequestHeader("Content-type", contentType);
|
|
||||||
|
|
||||||
logger.trace("call post .... " + this.urlService + "/" + urlMethod);
|
|
||||||
logger.debug(" call post body.... " + body);
|
|
||||||
// System.out.println("post body .... " + body);
|
|
||||||
|
|
||||||
method.setRequestEntity(new ByteArrayRequestEntity(body.getBytes()));
|
|
||||||
|
|
||||||
// Provide custom retry handler is necessary
|
|
||||||
// method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(3, false));
|
|
||||||
|
|
||||||
byte[] responseBody = null;
|
|
||||||
try {
|
|
||||||
// Execute the method.
|
|
||||||
int statusCode = httpClient.executeMethod(method);
|
|
||||||
|
|
||||||
if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
|
|
||||||
logger.error("Method failed: " + method.getStatusLine()+"; Response bpdy: "+method.getResponseBody());
|
|
||||||
method.releaseConnection();
|
|
||||||
throw new Exception("Method failed: " + method.getStatusLine()+"; Response body: "+new String(method.getResponseBody()));
|
|
||||||
}
|
|
||||||
// Read the response body.
|
|
||||||
responseBody = method.getResponseBody();
|
|
||||||
|
|
||||||
// Deal with the response.
|
|
||||||
// Use caution: ensure correct character encoding and is not binary data
|
|
||||||
// System.out.println(new String(responseBody));
|
|
||||||
} catch (HttpException e) {
|
|
||||||
logger.error("Fatal protocol violation: ", e);
|
|
||||||
// e.printStackTrace();
|
|
||||||
method.releaseConnection();
|
|
||||||
throw new Exception("Fatal protocol violation: " + e.getMessage());
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Fatal transport error: ", e);
|
|
||||||
// e.printStackTrace();
|
|
||||||
method.releaseConnection();
|
|
||||||
throw new Exception("Fatal transport error: " + e.getMessage());
|
|
||||||
}
|
|
||||||
method.releaseConnection();
|
|
||||||
return new String(responseBody);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUrlservice() {
|
|
||||||
return urlService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUrlservice(String urlservice) {
|
|
||||||
this.urlService = urlservice;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpClient getClient() {
|
|
||||||
return httpClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setClient(HttpClient client) {
|
|
||||||
this.httpClient = client;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUsername() {
|
|
||||||
return username;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setUsername(String username) {
|
|
||||||
this.username = username;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPassword() {
|
|
||||||
return password;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPassword(String password) {
|
|
||||||
this.password = password;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
builder.append("HttpCallerUtil [urlService=");
|
|
||||||
builder.append(urlService);
|
|
||||||
builder.append(", httpClient=");
|
|
||||||
builder.append(httpClient);
|
|
||||||
builder.append(", username=");
|
|
||||||
builder.append(username);
|
|
||||||
builder.append(", password=");
|
|
||||||
builder.append(password);
|
|
||||||
builder.append("]");
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue