git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/uri-resolver-manager@100683 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
4f20747f5e
commit
79176ab3e5
|
@ -3,13 +3,20 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.portlets.user.uriresolvermanager;
|
package org.gcube.portlets.user.uriresolvermanager;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
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.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||||
|
@ -24,9 +31,10 @@ public class UriResolverManager {
|
||||||
private String scope;
|
private String scope;
|
||||||
private String applicationType;
|
private String applicationType;
|
||||||
|
|
||||||
|
public static final Logger logger = LoggerFactory.getLogger(UriResolverManager.class);
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param scope
|
* @param scope to read resource = "Uri-Resolver-Map";
|
||||||
* @param applicationType a key Application Type
|
* @param applicationType a key Application Type
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
|
@ -49,18 +57,44 @@ public class UriResolverManager {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLink(Map<String, String> parameters) throws IllegalArgumentException{
|
public String getLink(Map<String, String> parameters) throws IllegalArgumentException, UriResolverMapException{
|
||||||
|
|
||||||
String resourceName = this.applicationTypes.get(applicationType);
|
String resourceName = this.applicationTypes.get(applicationType);
|
||||||
|
|
||||||
|
if(parameters==null){
|
||||||
|
throw new IllegalArgumentException("Input Map parameters is null");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
RuntimeResourceReader reader = new RuntimeResourceReader(scope, resourceName);
|
RuntimeResourceReader reader = new RuntimeResourceReader(scope, resourceName);
|
||||||
|
List<ServiceParameter> resourceParameters = reader.getServiceParameters();
|
||||||
|
|
||||||
|
//CHECK PARAMETERS
|
||||||
|
for (ServiceParameter serviceParameter : resourceParameters) {
|
||||||
|
if(serviceParameter.isMandatory()){
|
||||||
|
if(!parameters.containsKey(serviceParameter.getKey())){
|
||||||
|
throw new IllegalArgumentException("Mandatory service key (parameter) '"+serviceParameter.getKey() +"' not found into input map");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String baseURI = reader.getServiceBaseURI();
|
||||||
|
|
||||||
|
// String params = UrlEncoderUtil.encodeQuery(parameters);
|
||||||
|
// String uriRequest = reader.getServiceBaseURI()+"?"+params;
|
||||||
|
// logger.info("Created HTTP URI request: "+uriRequest);
|
||||||
|
|
||||||
|
baseURI = "http://pc-mangiacrapa.isti.cnr.it:8080/uri-resolver-1.2.0-SNAPSHOT/gis";
|
||||||
|
|
||||||
|
HttpCallerUtil httpCaller = new HttpCallerUtil(baseURI, "", "");
|
||||||
|
httpCaller.callGet("", parameters);
|
||||||
|
|
||||||
|
} catch (IllegalArgumentException e){
|
||||||
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// TODO Auto-generated catch block
|
throw new UriResolverMapException("Runtime Resource reader exception");
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +118,10 @@ public class UriResolverManager {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
UriResolverManager resolver = new UriResolverManager("/gcube", "GIS");
|
UriResolverManager resolver = new UriResolverManager("/gcube", "GIS");
|
||||||
resolver.getLink(null);
|
Map<String, String> params = new HashMap<String, String>();
|
||||||
|
params.put("gis-UUID", "12345");
|
||||||
|
params.put("scope", "/gcube");
|
||||||
|
resolver.getLink(params);
|
||||||
} catch (UriResolverMapException e) {
|
} catch (UriResolverMapException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -8,7 +8,7 @@ package org.gcube.portlets.user.uriresolvermanager.entity;
|
||||||
* @Oct 14, 2014
|
* @Oct 14, 2014
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ServiceProperty {
|
public class ServiceParameter {
|
||||||
|
|
||||||
private String key;
|
private String key;
|
||||||
private String defaultValue;
|
private String defaultValue;
|
||||||
|
@ -16,11 +16,11 @@ public class ServiceProperty {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param key
|
* @param key is parameter name
|
||||||
* @param defaultValue
|
* @param defaultValue
|
||||||
* @param isMandatory
|
* @param isMandatory
|
||||||
*/
|
*/
|
||||||
public ServiceProperty(String key, boolean isMandatory) {
|
public ServiceParameter(String key, boolean isMandatory) {
|
||||||
super();
|
super();
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.isMandatory = isMandatory;
|
this.isMandatory = isMandatory;
|
|
@ -15,7 +15,7 @@ import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
||||||
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
|
||||||
import org.gcube.common.resources.gcore.utils.Group;
|
import org.gcube.common.resources.gcore.utils.Group;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceProperty;
|
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceParameter;
|
||||||
import org.gcube.portlets.user.uriresolvermanager.util.ScopeUtil;
|
import org.gcube.portlets.user.uriresolvermanager.util.ScopeUtil;
|
||||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||||
|
@ -31,12 +31,14 @@ public class RuntimeResourceReader {
|
||||||
|
|
||||||
public static final Logger logger = LoggerFactory.getLogger(RuntimeResourceReader.class);
|
public static final Logger logger = LoggerFactory.getLogger(RuntimeResourceReader.class);
|
||||||
|
|
||||||
public List<ServiceProperty> serviceProperties;
|
public List<ServiceParameter> serviceParameters;
|
||||||
|
|
||||||
private String resourceName;
|
private String resourceName;
|
||||||
|
|
||||||
private String scope;
|
private String scope;
|
||||||
|
|
||||||
|
private String serviceBaseURI;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*
|
*
|
||||||
|
@ -52,12 +54,13 @@ public class RuntimeResourceReader {
|
||||||
* @return the application URI
|
* @return the application URI
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private String readResource(String scope, String resourceName) throws Exception {
|
protected String readResource(String scope, String resourceName) throws Exception {
|
||||||
|
|
||||||
try{
|
try{
|
||||||
|
|
||||||
this.resourceName = resourceName;
|
this.resourceName = resourceName;
|
||||||
|
this.scope = scope;
|
||||||
|
|
||||||
String infraName = ScopeUtil.getInfrastructureNameFromScope(scope);
|
String infraName = ScopeUtil.getInfrastructureNameFromScope(scope);
|
||||||
|
|
||||||
logger.info("Instancing root scope: "+infraName);
|
logger.info("Instancing root scope: "+infraName);
|
||||||
|
@ -87,7 +90,7 @@ public class RuntimeResourceReader {
|
||||||
logger.warn("Properties in resource "+resourceName+" not found");
|
logger.warn("Properties in resource "+resourceName+" not found");
|
||||||
}else{
|
}else{
|
||||||
|
|
||||||
serviceProperties = new ArrayList<ServiceProperty>(properties.size());
|
serviceParameters = new ArrayList<ServiceParameter>(properties.size());
|
||||||
|
|
||||||
Iterator<Property> iter = properties.iterator();
|
Iterator<Property> iter = properties.iterator();
|
||||||
|
|
||||||
|
@ -95,12 +98,13 @@ public class RuntimeResourceReader {
|
||||||
|
|
||||||
Property prop = iter.next();
|
Property prop = iter.next();
|
||||||
|
|
||||||
serviceProperties.add(new ServiceProperty(prop.value(), true));
|
serviceParameters.add(new ServiceParameter(prop.value(), true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("returning URI: "+ap.address());
|
logger.info("returning URI: "+ap.address());
|
||||||
return ap.address();
|
this.serviceBaseURI = ap.address();
|
||||||
|
return serviceBaseURI;
|
||||||
// parameters.setUser(ap.username()); //username
|
// parameters.setUser(ap.username()); //username
|
||||||
//
|
//
|
||||||
// String decryptedPassword = StringEncrypter.getEncrypter().decrypt(ap.password());
|
// String decryptedPassword = StringEncrypter.getEncrypter().decrypt(ap.password());
|
||||||
|
@ -114,27 +118,34 @@ public class RuntimeResourceReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ServiceProperty> getServiceProperties() {
|
|
||||||
return serviceProperties;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getResourceName() {
|
public String getResourceName() {
|
||||||
return resourceName;
|
return resourceName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<ServiceParameter> getServiceParameters() {
|
||||||
|
return serviceParameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getServiceBaseURI() {
|
||||||
|
return serviceBaseURI;
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
builder.append("RuntimeResourceReader [serviceProperties=");
|
builder.append("RuntimeResourceReader [serviceParameters=");
|
||||||
builder.append(serviceProperties);
|
builder.append(serviceParameters);
|
||||||
builder.append(", resourceName=");
|
builder.append(", resourceName=");
|
||||||
builder.append(resourceName);
|
builder.append(resourceName);
|
||||||
|
builder.append(", scope=");
|
||||||
|
builder.append(scope);
|
||||||
|
builder.append(", serviceBaseURI=");
|
||||||
|
builder.append(serviceBaseURI);
|
||||||
builder.append("]");
|
builder.append("]");
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/*public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
RuntimeResourceReader resolver = new RuntimeResourceReader("/gcube", "Gis-Resolver");
|
RuntimeResourceReader resolver = new RuntimeResourceReader("/gcube", "Gis-Resolver");
|
||||||
System.out.println(resolver);
|
System.out.println(resolver);
|
||||||
|
@ -142,7 +153,6 @@ public class RuntimeResourceReader {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,202 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
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() + "Status text: " +method.getStatusText());
|
||||||
|
throw new Exception("Failed HTTP caller on "+fullUrl +" Response status line: " + method.getStatusLine() + ", body response: " +method.getResponseBodyAsString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,115 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.portlets.user.uriresolvermanager.util;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||||
|
* @Oct 13, 2014
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class UrlEncoderUtil {
|
||||||
|
|
||||||
|
public static String charset = "UTF-8";
|
||||||
|
|
||||||
|
protected static Logger logger = LoggerFactory.getLogger(UrlEncoderUtil.class);
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param url
|
||||||
|
* @param parameters
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String encodeQuery(String... parameters){
|
||||||
|
|
||||||
|
String query = "";
|
||||||
|
for (String string : parameters) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
query+=URLEncoder.encode(string, charset)+"&";
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
|
||||||
|
logger.error("encodeQuery error: ", e);
|
||||||
|
return query;
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
logger.error("encodeQuery error: ", e);
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
return removeLastChar(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param url
|
||||||
|
* @param parameters
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String encodeQuery(Map<String, String> parameters){
|
||||||
|
|
||||||
|
String query = "";
|
||||||
|
|
||||||
|
if(parameters==null)
|
||||||
|
return query;
|
||||||
|
|
||||||
|
for (String key : parameters.keySet()) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
query+=String.format(key+"=%s", URLEncoder.encode(parameters.get(key), charset))+"&";
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
|
||||||
|
logger.error("encodeQuery error: ", e);
|
||||||
|
return query;
|
||||||
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
logger.error("encodeQuery error: ", e);
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return removeLastChar(query);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String removeLastChar(String string){
|
||||||
|
|
||||||
|
if(string == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if(string.length()>0)
|
||||||
|
return string.substring(0, string.length()-1);
|
||||||
|
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// System.out.println(UrlEncoderUtil.encodeQuery("request=GetStyles", "layers=test Name", "service=WMS", "version=1.1.1"));
|
||||||
|
|
||||||
|
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||||
|
|
||||||
|
parameters.put("request", "GetStyles");
|
||||||
|
parameters.put("layers", "test Name");
|
||||||
|
parameters.put("version", "1.1.1");
|
||||||
|
|
||||||
|
System.out.println(UrlEncoderUtil.encodeQuery(parameters));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue