Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
Universal viewing of digital microscopy, whole slide imaging and digital pathology data.
pma-java is a Java wrapper library for PMA.start (http://www.free.pathomation.com), a universal viewer by Pathomation for whole slide imaging and microscopy.
YOU NEED TO HAVE PMA.START (OR PMA.CORE) RUNNING IN ORDER TO USE THIS LIBRARY. PMA-JAVA IS NOT A STAND-ALONE LIBRARY FOR WSI.
* Java wrapper library for PMA.start, a universal viewer for whole slide
* imaging and microscopy
*
*
* @author Yassine Iddaoui
* @version 2.0.0.41
*/
public class Core {
private static Map pmaSessions = new HashMap();
private static Map pmaSlideInfos = new HashMap();
private static final String pmaCoreLiteURL = "http://localhost:54001/";
private static final String pmaCoreLiteSessionID = "SDK.Java";
private static Boolean pmaUseCacheWhenRetrievingTiles = true;
@SuppressWarnings("serial")
private static Map pmaAmountOfDataDownloaded = new HashMap() {
{
put(pmaCoreLiteSessionID, 0);
}
};
// for logging purposes
public static Logger logger = null;
// To store the Disk labels
private static Map diskLabels = new HashMap();
// Object Mapper for Jackson library
private static ObjectMapper objectMapper = new ObjectMapper();
private static Map urlContent = new HashMap<>();
/**
* This method is used to get the session's ID
*
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
* @return The same sessionID if explicited, otherwise it recovers a session's
* ID
*/
private static String sessionId(String... varargs) {
// setting the default value when argument's value is omitted
String sessionID = varargs.length > 0 ? varargs[0] : null;
if (sessionID == null) {
// if the sessionID isn't specified, maybe we can still recover it somehow
return firstSessionId();
} else {
// nothing to do in this case; a SessionID WAS passed along, so just continue
// using it
return sessionID;
}
}
/**
* This method is used to get PMA.core active session
*
* @return PMA.core active session
*/
private static String firstSessionId() {
// do we have any stored sessions from earlier login events?
if (pmaSessions.size() > 0) {
// yes we do! This means that when there's a PMA.core active session AND
// PMA.core.lite version running,
// the PMA.core active will be selected and returned
return pmaSessions.keySet().toArray()[0].toString();
} else {
// ok, we don't have stored sessions; not a problem per se...
if (pmaIsLite()) {
if (!pmaSlideInfos.containsKey(pmaCoreLiteSessionID)) {
pmaSlideInfos.put(pmaCoreLiteSessionID, new HashMap());
}
if (!pmaAmountOfDataDownloaded.containsKey(pmaCoreLiteSessionID)) {
pmaAmountOfDataDownloaded.put(pmaCoreLiteSessionID, 0);
}
return pmaCoreLiteSessionID;
} else {
// no stored PMA.core sessions found NOR PMA.core.lite
return null;
}
}
}
/**
* This method is used to get the url related to the session's ID
*
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
* @return Url related to the session's ID
* @throws Exception if sessionID is invalid
*/
private static String pmaUrl(String... varargs) throws Exception {
// setting the default value when argument's value is omitted
String sessionID = varargs.length > 0 ? varargs[0] : null;
sessionID = sessionId(sessionID);
if (sessionID == null) {
// sort of a hopeless situation; there is no URL to refer to
return null;
} else if (sessionID.equals(pmaCoreLiteSessionID)) {
return pmaCoreLiteURL;
} else {
// assume sessionID is a valid session; otherwise the following will generate an
// error
if (pmaSessions.containsKey(sessionID)) {
String url = pmaSessions.get(sessionID).toString();
if (!url.endsWith("/")) {
url = url + "/";
}
return url;
} else {
if (logger != null) {
logger.severe("Invalid sessionID:" + sessionID);
}
throw new Exception("Invalid sessionID:" + sessionID);
}
}
}
/**
* This method is used to retrieve HTML Code from URL
*
* @param url to get HTML code from
* @return HTML code generated from the url argument
*/
public static String urlReader(String url) {
try {
URL urlResource = new URL(url);
URLConnection con = urlResource.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
return IOUtils.toString(in, encoding);
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
}
/**
* This method is used to parse a XML content
*
* @param s XML content to parse
* @return Document parsed XML
*/
public static Document domParser(String s) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
InputSource inputStream = new InputSource();
inputStream.setCharacterStream(new StringReader(s));
return documentBuilder.parse(inputStream);
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
}
/**
* This method is used to check if an instance of PMA.core is running (by
* checking the existence of value "true" in a XML file)
*
* @param varargs Array of optional arguments
*
* pmaCoreURL : First optional argument(String), default
* value(Class field pmaCoreLiteURL), url of PMA.core instance
*
* @return True if an instance of PMA.core is running, false otherwise
*/
private static Boolean pmaIsLite(String... varargs) {
// setting the default value when argument's value is omitted
String pmaCoreURL = varargs.length > 0 ? varargs[0] : pmaCoreLiteURL;
String url = join(pmaCoreURL, "api/xml/IsLite");
String contents = "";
try {
contents = urlReader(url);
return domParser(contents).getChildNodes().item(0).getChildNodes().item(0).getNodeValue().toLowerCase()
.toString().equals("true");
} catch (Exception e) {
// this happens when NO instance of PMA.core is detected
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
}
/**
* This method is used to define which content will be received "XML" or "Json"
* for "API" Web service calls
*
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
*
* xml : Second optional argument(Boolean), default value(true),
* define if method will return XML or Json content
*
* @return Add a sequence to the url to specify which content to be received
* (XML or Json)
*/
private static String apiUrl(Object... varargs) {
// setting the default values when arguments' values are omitted
String sessionID = null;
Boolean xml = true;
if (varargs.length > 0) {
if (!(varargs[0] instanceof String) && varargs[0] != null) {
if (logger != null) {
logger.severe("apiUrl() : Illegal argument");
}
throw new IllegalArgumentException("...");
}
sessionID = (String) varargs[0];
}
if (varargs.length > 1) {
if (!(varargs[1] instanceof Boolean) && varargs[1] != null) {
if (logger != null) {
logger.severe("apiUrl() : Illegal argument");
}
throw new IllegalArgumentException("...");
}
xml = (Boolean) varargs[1];
}
// let's get the base URL first for the specified session
String url;
try {
url = pmaUrl(sessionID);
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
url = null;
}
if (url == null) {
// sort of a hopeless situation; there is no URL to refer to
return null;
}
// remember, _pma_url is guaranteed to return a URL that ends with "/"
if (xml) {
return join(url, "api/xml/");
} else {
return join(url, "api/json/");
}
}
/**
* This method is used to define which content will be received "XML" or "Json"
* for "Admin" Web service calls
*
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
*
* xml : Second optional argument(Boolean), default value(true),
* define if method will return XML or Json content
*
* @return Adds sequence to the url to specify which content to be received (XML
* or Json)
*/
private static String adminUrl(Object... varargs) {
// setting the default values when arguments' values are omitted
String sessionID = null;
Boolean xml = true;
if (varargs.length > 0) {
if (!(varargs[0] instanceof String) && varargs[0] != null) {
if (logger != null) {
logger.severe("adminUrl() : Illegal argument");
}
throw new IllegalArgumentException("...");
}
sessionID = (String) varargs[0];
}
if (varargs.length > 1) {
if (!(varargs[1] instanceof Boolean) && varargs[1] != null) {
if (logger != null) {
logger.severe("adminUrl() : Illegal argument");
}
throw new IllegalArgumentException("...");
}
xml = (Boolean) varargs[1];
}
// let's get the base URL first for the specified session
String url;
try {
url = pmaUrl(sessionID);
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
url = null;
}
if (url == null) {
// sort of a hopeless situation; there is no URL to refer to
return null;
}
// remember, _pma_url is guaranteed to return a URL that ends with "/"
if (xml) {
return join(url, "admin/xml/");
} else {
return join(url, "admin/json/");
}
}
/**
* This method is used to get a list of the values of "String" tags of a XML
* document
*
* @param root XML document
* @param varargs Array of optional arguments
*
* limit : First optional argument(int), default value(0),
* defines a limit
*
* @return Values' list of tags named "string" in a XML document
*/
private static List xmlToStringArray(Document root, Integer... varargs) {
// setting the default value when argument's value is omitted
int limit = varargs.length > 0 ? varargs[0] : 0;
NodeList eLs = root.getElementsByTagName("string");
List l = new ArrayList<>();
if (limit > 0) {
for (int i = 0; i < limit; i++) {
l.add(eLs.item(i).getFirstChild().getNodeValue());
}
} else {
for (int i = 0; i < eLs.getLength(); i++) {
l.add(eLs.item(i).getFirstChild().getNodeValue());
}
}
return l;
}
/**
* This method is an overload of method xmlToStringArray to cope with "root"
* argument as an "Element" instead of a "Document"
*
* @param root XML document
* @param varargs Array of optional arguments
*
* limit : First optional argument(int), default value(0),
* defines a limit
*
* @return Values' list of tags named "string" in a XML document
*/
private static List xmlToStringArray(Element root, Integer... varargs) {
// setting the default value when argument's value is omitted
int limit = varargs.length > 0 ? varargs[0] : 0;
NodeList eLs = root.getElementsByTagName("string");
List l = new ArrayList<>();
if (limit > 0) {
for (int i = 0; i < limit; i++) {
l.add(eLs.item(i).getFirstChild().getNodeValue());
}
} else {
for (int i = 0; i < eLs.getLength(); i++) {
l.add(eLs.item(i).getFirstChild().getNodeValue());
}
}
return l;
}
/**
* This method is used to cache requested URLs
*
* @param url URL to request
* @param property Header value
* @return Data returned following a request to a specific URL
*/
public static String httpGet(String url, String property) {
if (!urlContent.containsKey(url)) {
try {
URL urlResource = new URL(url);
HttpURLConnection con;
if (url.startsWith("https")) {
con = (HttpsURLConnection) urlResource.openConnection();
} else {
con = (HttpURLConnection) urlResource.openConnection();
}
con.setRequestMethod("GET");
con.setRequestProperty("Accept", property);
urlContent.put(url, Core.getJSONAsStringBuffer(con).toString());
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
}
return urlContent.get(url).toString();
}
/**
* This method is used to concatenate a couple of Strings while replacing "\\"
* by "/"
*
* @param varargs Array of String optional arguments, each argument is a string
* to be concatenated
* @return Concatenation of a couple of String while making sure the first
* string always ends with "/"
*/
public static String join(String... varargs) {
String joinString = "";
for (String ss : varargs) {
if (!joinString.endsWith("/") && (!joinString.equals(""))) {
joinString = joinString.concat("/");
}
if (ss != null) {
joinString = joinString.concat(ss);
}
}
return joinString;
}
/**
* This method is used to encode a String to be compatible as a url
*
* @param arg string to be encoded
* @return Encoded String to be compatible as a url
*/
public static String pmaQ(String arg) {
if (arg == null) {
return "";
} else {
try {
return URLEncoder.encode(arg, "UTF-8").replace("+", "%20");
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return "";
}
}
}
/**
* This method is used to check if there is a PMA.core.lite or PMA.core instance
* running
*
* @param varargs Array of optional arguments
*
* pmaCoreURL : First optional argument(String), default
* value(Class field pmaCoreLiteURL), url of PMA.core instance
*
* @return Checks if there is a PMA.core.lite or PMA.core instance running
*/
public static Boolean isLite(String... varargs) {
// setting the default value when argument's value is omitted
String pmaCoreURL = varargs.length > 0 ? varargs[0] : pmaCoreLiteURL;
// See if there's a PMA.core.lite or PMA.core instance running at pmacoreURL
return pmaIsLite(pmaCoreURL);
}
/**
* This method is used to get the version number
*
* @param varargs Array of optional arguments
*
* pmaCoreURL : First optional argument(String), default
* value(Class field pmaCoreLiteURL), url of PMA.core instance
*
* @return Version number
*/
public static String getVersionInfo(String... varargs) {
// setting the default value when argument's value is omitted
String pmaCoreURL = varargs.length > 0 ? varargs[0] : pmaCoreLiteURL;
// Get version info from PMA.core instance running at pmacoreURL
// purposefully DON'T use helper function apiUrl() here:
// why? because GetVersionInfo can be invoked WITHOUT a valid SessionID;
// apiUrl() takes session information into account
String url = join(pmaCoreURL, "api/xml/GetVersionInfo");
String contents = "";
try {
contents = urlReader(url);
return domParser(contents).getChildNodes().item(0).getChildNodes().item(0).getNodeValue().toString();
} catch (Exception e) {
// this happens when NO instance of PMA.core is detected
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
}
/**
* This method is used to authenticate & connect to a PMA.core instance
* using credentials
*
* @param varargs Array of optional arguments
*
* pmacoreURL : First optional argument(String), default
* value(Class field pmaCoreLiteURL), url of PMA.core instance
*
*
* pmacoreUsername : Second optional argument(String), default
* value(""), username for PMA.core instance
*
*
* pmacorePassword : Third optional argument(String), default
* value(""), password for PMA.core instance
*
* @return session's ID if session was created successfully, otherwise null
*/
public static String connect(String... varargs) {
// setting the default values when arguments' values are omitted
String pmaCoreURL = varargs.length > 0 ? varargs[0] : pmaCoreLiteURL;
String pmaCoreUsername = varargs.length > 1 ? varargs[1] : "";
String pmaCorePassword = varargs.length > 2 ? varargs[2] : "";
// Attempt to connect to PMA.core instance; success results in a SessionID
if (pmaCoreURL.equals(pmaCoreLiteURL)) {
if (isLite()) {
// no point authenticating localhost / PMA.core.lite
return pmaCoreLiteSessionID;
} else {
return null;
}
}
// purposefully DON'T use helper function apiUrl() here:
// why? Because apiUrl() takes session information into account (which we
// don't have yet)
String url = join(pmaCoreURL, "api/xml/authenticate?caller=SDK.Java");
if (!pmaCoreUsername.equals("")) {
url = url.concat("&username=").concat(pmaQ(pmaCoreUsername));
}
if (!pmaCorePassword.equals("")) {
url = url.concat("&password=").concat(pmaQ(pmaCorePassword));
}
String contents;
Document dom;
try {
contents = urlReader(url);
dom = domParser(contents);
} catch (Exception e) {
// Something went wrong; unable to communicate with specified endpoint
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
try {
Element loginResult = (Element) dom.getChildNodes().item(0);
Node succ = loginResult.getElementsByTagName("Success").item(0);
String sessionID;
if (succ.getFirstChild().getNodeValue().toLowerCase().equals("false")) {
sessionID = null;
} else {
sessionID = loginResult.getElementsByTagName("SessionId").item(0).getFirstChild().getNodeValue()
.toString();
pmaSessions.put(sessionID, pmaCoreURL);
pmaSlideInfos.put(sessionID, new HashMap());
pmaAmountOfDataDownloaded.put(sessionID, contents.length());
}
return sessionID;
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
}
/**
* This method is used to authenticate & connect as an admin to a PMA.core
* instance using admin credentials
*
* @param varargs Array of optional arguments
*
* pmacoreURL : First optional argument(String), default
* value(Class field pmaCoreLiteURL), url of PMA.core instance
*
*
* pmacoreUsername : Second optional argument(String), default
* value(""), username for PMA.core instance
*
*
* pmacorePassword : Third optional argument(String), default
* value(""), password for PMA.core instance
*
* @return session's ID if session was created successfully, otherwise null
*/
public static String adminConnect(String... varargs) {
// setting the default values when arguments' values are omitted
String pmaCoreURL = varargs.length > 0 ? varargs[0] : pmaCoreLiteURL;
String pmaCoreUsername = varargs.length > 1 ? varargs[1] : "";
String pmaCorePassword = varargs.length > 2 ? varargs[2] : "";
// Attempt to connect to PMA.core instance; success results in a SessionID
if (pmaCoreURL.equals(pmaCoreLiteURL)) {
if (isLite()) {
// no point authenticating localhost / PMA.core.lite
return pmaCoreLiteSessionID;
} else {
return null;
}
}
// purposefully DON'T use helper function apiUrl() here:
// why? Because apiUrl() takes session information into account (which we
// don't have yet)
String url = join(pmaCoreURL, "admin/json/AdminAuthenticate?caller=SDK.Java");
if (!pmaCoreUsername.equals("")) {
url = url.concat("&username=").concat(pmaQ(pmaCoreUsername));
}
if (!pmaCorePassword.equals("")) {
url = url.concat("&password=").concat(pmaQ(pmaCorePassword));
}
try {
URL urlResource = new URL(url);
HttpURLConnection con;
if (url.startsWith("https")) {
con = (HttpsURLConnection) urlResource.openConnection();
} else {
con = (HttpURLConnection) urlResource.openConnection();
}
con.setRequestMethod("GET");
String jsonString = getJSONAsStringBuffer(con).toString();
if (isJSONObject(jsonString)) {
JSONObject jsonResponse = getJSONResponse(jsonString);
if (jsonResponse.getBoolean("Success")) {
String sessionID = jsonResponse.getString("SessionId");
pmaSessions.put(sessionID, pmaCoreURL);
pmaSlideInfos.put(sessionID, new HashMap());
pmaAmountOfDataDownloaded.put(sessionID, jsonResponse.length());
return sessionID;
} else {
return null;
}
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
}
/**
* This method is used to disconnect from a running PMA.core instance
*
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
* @return true if there was a PMA.core instance running to disconnect from,
* false otherwise
*/
public static Boolean disconnect(String... varargs) {
// setting the default value when argument's value is omitted
String sessionID = varargs.length > 0 ? varargs[0] : null;
// Disconnect from a PMA.core instance; return True if session exists; return
// False if session didn't exist (anymore)
sessionID = sessionId(sessionID);
String url = apiUrl(sessionID) + "DeAuthenticate?sessionID=" + pmaQ((sessionID));
String contents = urlReader(url);
pmaAmountOfDataDownloaded.put(sessionID, pmaAmountOfDataDownloaded.get(sessionID) + contents.length());
if (pmaSessions.size() > 0) {
// yes we do! This means that when there's a PMA.core active session AND
// PMA.core.lite version running,
// the PMA.core active will be selected and returned
pmaSessions.remove(sessionID);
pmaSlideInfos.remove(sessionID);
return true;
} else {
return false;
}
}
/**
* Getter for Class field pmaCoreLiteSessionID
*
* @return value of Class field pmaCoreLiteSessionID
*/
public static String getPmaCoreLiteSessionID() {
return pmaCoreLiteSessionID;
}
/**
* Getter for Class field pmaCoreLiteURL
*
* @return value of Class field pmaCoreLiteURL
*/
public static String getPmaCoreLiteUrl() {
return pmaCoreLiteURL;
}
/**
* This method is used to test if sessionID is valid and the server is online
* and reachable This method works only for PMA.core, don't use it for PMA.start
* for it will return always false
*
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
* @return true if sessionID is valid and the server is online and reachable,
* false otherwise
*/
public static boolean ping(String... varargs) {
// setting the default value when argument's value is omitted
String sessionID = varargs.length > 0 ? varargs[0] : null;
sessionID = sessionId(sessionID);
String url = apiUrl(sessionID, false) + "Ping?sessionID=" + pmaQ(sessionID);
try {
URL urlResource = new URL(url);
HttpURLConnection con;
if (url.startsWith("https")) {
con = (HttpsURLConnection) urlResource.openConnection();
} else {
con = (HttpURLConnection) urlResource.openConnection();
}
con.setRequestMethod("GET");
con.setRequestProperty("Accept", "application/json");
String jsonString = Core.getJSONAsStringBuffer(con).toString();
return jsonString.equals("true") ? true : false;
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return false;
}
}
/**
* This method is used to get root-directories available for a sessionID
*
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
* @return Array of root-directories available to a session's ID
*/
public static List getRootDirectories(String... varargs) {
// setting the default value when argument's value is omitted
String sessionID = varargs.length > 0 ? varargs[0] : null;
// Return a list of root-directories available to sessionID
sessionID = sessionId(sessionID);
String url = apiUrl(sessionID) + "GetRootDirectories?sessionID=" + pmaQ(sessionID);
String contents = urlReader(url);
pmaAmountOfDataDownloaded.put(sessionID, pmaAmountOfDataDownloaded.get(sessionID) + contents.length());
Document dom = domParser(contents);
return xmlToStringArray((Element) dom.getFirstChild());
}
/**
* This method is used to create a new directory on PMA.core
*
* @param sessionID a session ID
* @param path path to create the new directory in
* @return true if directory was created successfully, false otherwise
*/
public static boolean createDirectory(String sessionID, String path) {
try {
// we only create folders on PMA.core
if (isLite(pmaUrl(sessionID))) {
return false;
} else {
String url = adminUrl(sessionID, false) + "CreateDirectory";
URL urlResource = new URL(url);
HttpURLConnection con;
if (url.startsWith("https")) {
con = (HttpsURLConnection) urlResource.openConnection();
} else {
con = (HttpURLConnection) urlResource.openConnection();
}
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setUseCaches(false);
con.setDoOutput(true);
String input = "{ \"sessionID\": \"" + sessionID + "\", \"path\": \"" + path + "\" }";
OutputStream os = con.getOutputStream();
os.write(input.getBytes("UTF-8"));
os.close();
String jsonString = Core.getJSONAsStringBuffer(con).toString();
return jsonString.equals("true") ? true : false;
}
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return false;
}
}
/**
* This method is used to get sub-directories available to sessionID in the
* start directory following a recursive (or not) approach
*
* @param startDir Start directory
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
*
* recursivity : Second optional argument(Boolean or Integer),
* default value(Boolean, false), if it's a Boolean if defines
* either no recursivity or a limitless recursivity, if it's an
* Integer it defines a limited in depth recursivity or no
* recursivity at all if this Integer equals 0
*
* @return Sub-directories available to a session's ID in a start directory
*/
public static List getDirectories(String startDir, Object... varargs) {
// setting the default values when arguments' values are omitted
String sessionID = null;
// we can either choose to have a non recursive call, a complete recursive call
// or a recursive call to a certain depth, in the last case we use an integer to
// define
// depth
// the following three variables intend to implement this
Boolean recursive = false;
Integer integerRecursive = 0;
String booleanOrInteger = "";
if (varargs.length > 0) {
if (!(varargs[0] instanceof String) && varargs[0] != null) {
if (logger != null) {
logger.severe("getDirectories() : Illegal argument");
}
throw new IllegalArgumentException("...");
}
sessionID = (String) varargs[0];
}
if (varargs.length > 1) {
if ((!(varargs[1] instanceof Integer) && !(varargs[1] instanceof Boolean)) && (varargs[1] != null)) {
if (logger != null) {
logger.severe("getDirectories() : Illegal argument");
}
throw new IllegalArgumentException("...");
}
if (varargs[1] instanceof Boolean) {
recursive = (Boolean) varargs[1];
booleanOrInteger = "boolean";
}
if (varargs[1] instanceof Integer) {
integerRecursive = (Integer) varargs[1];
recursive = ((Integer) varargs[1]) > 0 ? true : false;
booleanOrInteger = "integer";
}
}
// Return a list of sub-directories available to sessionID in the startDir
// directory
sessionID = sessionId(sessionID);
String url = apiUrl(sessionID, false) + "GetDirectories?sessionID=" + pmaQ(sessionID) + "&path="
+ pmaQ(startDir);
try {
URL urlResource = new URL(url);
HttpURLConnection con;
if (url.startsWith("https")) {
con = (HttpsURLConnection) urlResource.openConnection();
} else {
con = (HttpURLConnection) urlResource.openConnection();
}
con.setRequestMethod("GET");
String jsonString = getJSONAsStringBuffer(con).toString();
List dirs;
if (isJSONObject(jsonString)) {
JSONObject jsonResponse = getJSONResponse(jsonString);
pmaAmountOfDataDownloaded.put(sessionID,
pmaAmountOfDataDownloaded.get(sessionID) + jsonResponse.length());
if (jsonResponse.has("Code")) {
if (logger != null) {
logger.severe("get_directories to " + startDir + " resulted in: " + jsonResponse.get("Message")
+ " (keep in mind that startDir is case sensitive!)");
}
throw new Exception("get_directories to " + startDir + " resulted in: "
+ jsonResponse.get("Message") + " (keep in mind that startDir is case sensitive!)");
} else if (jsonResponse.has("d")) {
JSONArray array = jsonResponse.getJSONArray("d");
dirs = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
dirs.add(array.optString(i));
}
// return dirs;
} else {
return null;
}
} else {
JSONArray jsonResponse = getJSONArrayResponse(jsonString);
pmaAmountOfDataDownloaded.put(sessionID,
pmaAmountOfDataDownloaded.get(sessionID) + jsonResponse.length());
dirs = new ArrayList<>();
for (int i = 0; i < jsonResponse.length(); i++) {
dirs.add(jsonResponse.optString(i));
}
// return dirs;
}
// we test if call is recursive, and if yes to which depth
if (recursive) {
for (String dir : getDirectories(startDir, sessionID)) {
if (booleanOrInteger.equals("boolean")) {
dirs.addAll(getDirectories(dir, sessionID, recursive));
}
if (booleanOrInteger.equals("integer")) {
dirs.addAll(getDirectories(dir, sessionID, integerRecursive - 1));
}
}
}
return dirs;
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
}
/**
* This method is used to get the first non empty directory
*
* @param varargs Array of optional arguments
*
* sessionID : Second optional argument(String), default
* value(null), session's ID
*
* @return Path to the first non empty directory found
*/
public static String getFirstNonEmptyDirectory(String... varargs) {
// setting the default values when arguments' values are omitted
String startDir = varargs.length > 0 ? varargs[0] : null;
String sessionID = varargs.length > 1 ? varargs[1] : null;
if ((startDir == null) || (startDir.equals(""))) {
startDir = "/";
}
List slides = getSlides(startDir, sessionID);
if (slides.size() > 0) {
return startDir;
} else {
if (startDir.equals("/")) {
for (String dir : getRootDirectories(sessionID)) {
String nonEmptyDir = getFirstNonEmptyDirectory(dir, sessionID);
if (nonEmptyDir != null) {
return nonEmptyDir;
}
}
} else {
for (String dir : getDirectories(startDir, sessionID)) {
String nonEmptyDir = getFirstNonEmptyDirectory(dir, sessionID);
if (nonEmptyDir != null) {
return nonEmptyDir;
}
}
}
}
return null;
}
/**
* This method is used to get a list of slides available to sessionID in the
* start directory following a recursive (or not) approach
*
* @param startDir Start directory
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
*
* recursivity : Second optional argument(Boolean or Integer),
* default value(Boolean, false), if it's a Boolean if defines
* either no recursivity or a limitless recursivity, if it's an
* Integer it defines a limited in depth recursivity or no
* recursivity at all if this Integer equals 0
*
* @return List of slides available to a session's ID in a start directory
*/
public static List getSlides(String startDir, Object... varargs) {
// setting the default values when arguments' values are omitted
String sessionID = null;
// we can either choose to have a non recursive call, a complete recursive call
// or a recursive call to a certain depth, in the last case we use an integer to
// define
// depth
// the following three variables intend to implement this
String booleanOrInteger = "";
Boolean recursive = false;
Integer integerRecursive = 0;
if (varargs.length > 0) {
if (!(varargs[0] instanceof String) && varargs[0] != null) {
if (logger != null) {
logger.severe("getSlides() : Illegal argument");
}
throw new IllegalArgumentException("...");
}
sessionID = (String) varargs[0];
}
if (varargs.length > 1) {
if ((!(varargs[1] instanceof Integer) && !(varargs[1] instanceof Boolean)) && (varargs[1] != null)) {
if (logger != null) {
logger.severe("getSlides() : Illegal argument");
}
throw new IllegalArgumentException("...");
}
if (varargs[1] instanceof Boolean) {
recursive = (Boolean) varargs[1];
booleanOrInteger = "boolean";
}
if (varargs[1] instanceof Integer) {
integerRecursive = (Integer) varargs[1];
recursive = ((Integer) varargs[1]) > 0 ? true : false;
booleanOrInteger = "integer";
}
}
// Return a list of slides available to sessionID in the startDir directory
sessionID = sessionId(sessionID);
if (startDir.startsWith("/")) {
startDir = startDir.substring(1);
}
String url = apiUrl(sessionID, false) + "GetFiles?sessionID=" + pmaQ(sessionID) + "&path=" + pmaQ(startDir);
try {
URL urlResource = new URL(url);
HttpURLConnection con;
if (url.startsWith("https")) {
con = (HttpsURLConnection) urlResource.openConnection();
} else {
con = (HttpURLConnection) urlResource.openConnection();
}
con.setRequestMethod("GET");
String jsonString = getJSONAsStringBuffer(con).toString();
List slides;
if (isJSONObject(jsonString)) {
JSONObject jsonResponse = getJSONResponse(jsonString);
pmaAmountOfDataDownloaded.put(sessionID,
pmaAmountOfDataDownloaded.get(sessionID) + jsonResponse.length());
if (jsonResponse.has("Code")) {
if (logger != null) {
logger.severe("get_slides from " + startDir + " resulted in: " + jsonResponse.get("Message")
+ " (keep in mind that startDir is case sensitive!)");
}
throw new Exception("get_slides from " + startDir + " resulted in: " + jsonResponse.get("Message")
+ " (keep in mind that startDir is case sensitive!)");
} else if (jsonResponse.has("d")) {
JSONArray array = jsonResponse.getJSONArray("d");
slides = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
slides.add(array.optString(i));
}
// return slides;
} else {
return null;
}
} else {
JSONArray jsonResponse = getJSONArrayResponse(jsonString);
pmaAmountOfDataDownloaded.put(sessionID,
pmaAmountOfDataDownloaded.get(sessionID) + jsonResponse.length());
slides = new ArrayList<>();
for (int i = 0; i < jsonResponse.length(); i++) {
slides.add(jsonResponse.optString(i));
}
// return slides;
}
// we test if call is recursive, and if yes to which depth
if (recursive) {
for (String dir : getDirectories(startDir, sessionID)) {
if (booleanOrInteger.equals("boolean")) {
slides.addAll(getSlides(dir, sessionID, recursive));
}
if (booleanOrInteger.equals("integer")) {
slides.addAll(getSlides(dir, sessionID, integerRecursive - 1));
}
}
}
return slides;
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
}
/**
* This method is used to determine the file extension for a slide's path
*
* @param slideRef slide's path
* @return File extension extracted from a slide's path
*/
public static String getSlideFileExtension(String slideRef) {
// Determine the file extension for this slide
return FilenameUtils.getExtension(slideRef);
}
/**
* This method is used to determine file name (with extension) for a slide's
* path
*
* @param slideRef slide's path
* @return File name extracted from a slide's path
*/
public static String getSlideFileName(String slideRef) {
// Determine the file name (with extension) for this slide
return FilenameUtils.getName(slideRef);
}
/**
* This method is used to get the UID for a defined slide
*
* @param slideRef slide's path
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
* @return UID for a defined slide's path
*/
public static String getUid(String slideRef, String... varargs) {
// setting the default value when arguments' value is omitted
String sessionID = varargs.length > 0 ? varargs[0] : null;
// Get the UID for a specific slide
sessionID = sessionId(sessionID);
String url = apiUrl(sessionID) + "GetUID?sessionID=" + pmaQ(sessionID) + "&path=" + pmaQ(slideRef);
String contents = urlReader(url);
pmaAmountOfDataDownloaded.put(sessionID, pmaAmountOfDataDownloaded.get(sessionID) + contents.length());
Document dom = domParser(contents);
return xmlToStringArray(dom).get(0);
}
/**
* This method is used to get the fingerprint for a specific slide
*
* @param slideRef slide's path
* @param varargs Array of optional arguments
*
* strict : First optional argument(Boolean), default
* value(false), loose fingerprint if false, strict fingerprint
* if true
*
*
* sessionID : Second optional argument(String), default
* value(null), session's ID
*
* @return Fingerprint of the slide
*/
public static String getFingerPrint(String slideRef, Object... varargs) {
// Get the fingerprint for a specific slide
Boolean strict = false;
String sessionID = null;
if (varargs.length > 0) {
if (!(varargs[0] instanceof Boolean) && varargs[0] != null) {
if (logger != null) {
logger.severe("getFingerPrint() : Illegal argument");
}
throw new IllegalArgumentException("...");
}
strict = (Boolean) varargs[0];
}
if (varargs.length > 1) {
if (!(varargs[1] instanceof String) && varargs[1] != null) {
if (logger != null) {
logger.severe("getFingerPrint() : Illegal argument");
}
throw new IllegalArgumentException("...");
}
sessionID = (String) varargs[1];
}
// Get the fingerprint for a specific slide
sessionID = sessionId(sessionID);
String fingerprint;
String url = apiUrl(sessionID, false) + "GetFingerprint?sessionID=" + pmaQ(sessionID) + "&strict="
+ pmaQ(strict.toString()) + "&pathOrUid=" + pmaQ(slideRef);
try {
URL urlResource = new URL(url);
HttpURLConnection con;
if (url.startsWith("https")) {
con = (HttpsURLConnection) urlResource.openConnection();
} else {
con = (HttpURLConnection) urlResource.openConnection();
}
con.setRequestMethod("GET");
String jsonString = getJSONAsStringBuffer(con).toString();
if (isJSONObject(jsonString)) {
JSONObject jsonResponse = getJSONResponse(jsonString);
pmaAmountOfDataDownloaded.put(sessionID,
pmaAmountOfDataDownloaded.get(sessionID) + jsonResponse.length());
if (jsonResponse.has("Code")) {
if (logger != null) {
logger.severe("get_fingerprint on " + slideRef + " resulted in: " + jsonResponse.get("Message")
+ " (keep in mind that slideRef is case sensitive!)");
}
throw new Exception("get_fingerprint on " + slideRef + " resulted in: "
+ jsonResponse.get("Message") + " (keep in mind that slideRef is case sensitive!)");
} else {
return jsonResponse.getString("d");
}
} else {
pmaAmountOfDataDownloaded.put(sessionID,
pmaAmountOfDataDownloaded.get(sessionID) + jsonString.length());
fingerprint = jsonString.replace("\"", "");
}
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
return fingerprint;
}
/**
* This method is under construction
*
* @return Information about session (Under construction)
*/
public static String whoAmI() {
// Getting information about your Session (under construction)
System.out.println("Under construction");
return "Under construction";
}
/**
* This method is a getter for class field pmaSessions
*
* @return Value of class field pmaSessions
*/
public static Map sessions() {
return pmaSessions;
}
/**
* This method is used to get tile size information for sessionID
*
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
* @return A list of two items (duplicated) relative to the tile size
* information for a session's ID
*/
@SuppressWarnings("unchecked")
public static List getTileSize(String... varargs) {
// setting the default value when arguments' value is omitted
String sessionID = varargs.length > 0 ? varargs[0] : null;
sessionID = sessionId(sessionID);
Map info;
if (((Map) pmaSlideInfos.get(sessionID)).size() < 1) {
String dir = getFirstNonEmptyDirectory(sessionID);
List slides = getSlides(dir, sessionID);
info = getSlideInfo(slides.get(0), sessionID);
} else {
int getLength = ((Map) pmaSlideInfos.get(sessionID)).values().toArray().length;
info = (Map) ((Map) pmaSlideInfos.get(sessionID)).values()
.toArray()[new Random().nextInt(getLength)];
}
List result = new ArrayList<>();
result.add(Integer.parseInt(info.get("TileSize").toString()));
result.add(Integer.parseInt(info.get("TileSize").toString()));
return result;
}
/**
* This method is used to create a StringBuffer from a connection URL
*
* @param con http connection URL to retrieve JSON from
* @return string buffer created from the connection URL
*/
public static StringBuffer getJSONAsStringBuffer(HttpURLConnection con) {
try {
BufferedReader in;
if (Integer.toString(con.getResponseCode()).startsWith("2")) {
in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
} else {
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
String inputline;
StringBuffer response = new StringBuffer();
while ((inputline = in.readLine()) != null) {
response.append(inputline);
}
in.close();
return response;
} catch (Exception e) {
e.printStackTrace();
if (logger != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.severe(sw.toString());
}
return null;
}
}
/**
* This method is used to check if a json returned is an object
*
* @param value json in String format
* @return True if it's a JSONObject, false otherwise
*/
public static Boolean isJSONObject(String value) {
if (value.startsWith("{")) {
return true;
} else {
return false;
}
}
/**
* This method is used to check if a json returned is an array
*
* @param value json in String format
* @return True if it's a JSONObject, false otherwise
*/
public static Boolean isJSONArray(String value) {
if (value.startsWith("[")) {
return true;
} else {
return false;
}
}
/**
* This method is used to create a json Object out of a string
*
* @param value json in String format
* @return Creates a Json object from a string
*/
public static JSONObject getJSONResponse(String value) {
JSONObject jsonResponse = new JSONObject(value.toString());
return jsonResponse;
}
/**
* This method is used to creates a json Array out of a string
*
* @param value json in String format
* @return Creates a Json array from a string
*/
public static JSONArray getJSONArrayResponse(String value) {
JSONArray jsonResponse = new JSONArray(value);
return jsonResponse;
}
/**
* This method is used to get a raw image in the form of nested maps
*
* @param slideRef slide's path or UID
* @param varargs Array of optional arguments
*
* sessionID : First optional argument(String), default
* value(null), session's ID
*
* @return Nested maps forming a raw image
*/
@SuppressWarnings("unchecked")
public static Map getSlideInfo(String slideRef, String... varargs) {
// setting the default value when arguments' value is omitted
String sessionID = varargs.length > 0 ? varargs[0] : null;
// Return raw image information in the form of nested maps
sessionID = sessionId(sessionID);
if (slideRef.startsWith("/")) {
slideRef = slideRef.substring(1);
}
if (!((Map) pmaSlideInfos.get(sessionID)).containsKey(slideRef)) {
try {
String url = apiUrl(sessionID, false) + "GetImageInfo?SessionID=" + pmaQ(sessionID) + "&pathOrUid="
+ pmaQ(slideRef);
URL urlResource = new URL(url);
HttpURLConnection con;
if (url.startsWith("https")) {
con = (HttpsURLConnection) urlResource.openConnection();
} else {
con = (HttpURLConnection) urlResource.openConnection();
}
con.setRequestMethod("GET");
String jsonString = getJSONAsStringBuffer(con).toString();
if (isJSONObject(jsonString)) {
JSONObject jsonResponse = getJSONResponse(jsonString);
pmaAmountOfDataDownloaded.put(sessionID,
pmaAmountOfDataDownloaded.get(sessionID) + jsonResponse.length());
if (jsonResponse.has("Code")) {
if (logger != null) {
logger.severe("ImageInfo to " + slideRef + " resulted in: " + jsonResponse.get("Message")
+ " (keep in mind that slideRef is case sensitive!)");
}
throw new Exception("ImageInfo to " + slideRef + " resulted in: " + jsonResponse.get("Message")
+ " (keep in mind that slideRef is case sensitive!)");
} else if (jsonResponse.has("d")) {
// we convert the Json object to a Map
Map jsonMap = objectMapper.readerFor(new TypeReference