2018-05-11 17:50:37 +02:00
|
|
|
package org.gcube.dataharvest.utils;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
|
2018-05-18 12:00:10 +02:00
|
|
|
import org.gcube.common.authorization.client.Constants;
|
|
|
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
|
|
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2018-05-11 17:50:37 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2018-05-18 14:17:54 +02:00
|
|
|
/**
|
|
|
|
* @author Eric Perrone (ISTI - CNR)
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
2018-05-11 17:50:37 +02:00
|
|
|
public class Utils {
|
2018-05-17 16:04:22 +02:00
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(Utils.class);
|
2018-05-18 18:33:28 +02:00
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
public static String getJson(String url) throws MalformedURLException, IOException {
|
|
|
|
URL address = new URL(url);
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) address.openConnection();
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
2018-05-18 18:33:28 +02:00
|
|
|
String json = "";
|
|
|
|
String line = "";
|
|
|
|
|
|
|
|
while(line != null) {
|
2018-05-11 17:50:37 +02:00
|
|
|
line = reader.readLine();
|
2018-05-18 18:33:28 +02:00
|
|
|
if(line != null) {
|
2018-05-11 17:50:37 +02:00
|
|
|
json += line.trim();
|
2018-05-18 18:33:28 +02:00
|
|
|
}
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
2018-05-18 12:00:10 +02:00
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
public static String getCurrentContext() throws ObjectNotFound, Exception {
|
2018-05-18 12:00:10 +02:00
|
|
|
return getCurrentContext(SecurityTokenProvider.instance.get());
|
|
|
|
}
|
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
public static String getCurrentContext(String token) throws ObjectNotFound, Exception {
|
2018-05-18 12:00:10 +02:00
|
|
|
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
|
|
|
String context = authorizationEntry.getContext();
|
|
|
|
logger.info("Context of token {} is {}", token, context);
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2018-05-18 18:33:28 +02:00
|
|
|
public static void setContext(String token) throws ObjectNotFound, Exception {
|
2018-05-18 12:00:10 +02:00
|
|
|
SecurityTokenProvider.instance.set(token);
|
|
|
|
ScopeProvider.instance.set(getCurrentContext(token));
|
|
|
|
}
|
2018-05-18 18:33:28 +02:00
|
|
|
|
2018-05-11 17:50:37 +02:00
|
|
|
}
|