2014-02-26 08:44:22 +01:00
|
|
|
package com.rarchives.ripme.utils;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2014-03-04 14:35:35 +01:00
|
|
|
import java.net.URISyntaxException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.jar.JarEntry;
|
|
|
|
import java.util.jar.JarFile;
|
2014-02-26 08:44:22 +01:00
|
|
|
|
|
|
|
import org.apache.commons.configuration.Configuration;
|
|
|
|
import org.apache.commons.configuration.ConfigurationException;
|
|
|
|
import org.apache.commons.configuration.PropertiesConfiguration;
|
2014-02-27 04:54:44 +01:00
|
|
|
import org.apache.log4j.Logger;
|
2014-02-26 08:44:22 +01:00
|
|
|
|
2014-03-09 09:20:22 +01:00
|
|
|
/**
|
|
|
|
* Common utility functions used in various places throughout the project.
|
|
|
|
*/
|
2014-02-26 08:44:22 +01:00
|
|
|
public class Utils {
|
|
|
|
|
2014-03-09 09:20:22 +01:00
|
|
|
public static final String RIP_DIRECTORY = "rips";
|
2014-02-27 10:28:23 +01:00
|
|
|
private static final File configFile = new File("src/main/resources/rip.properties");
|
2014-02-27 04:54:44 +01:00
|
|
|
private static final Logger logger = Logger.getLogger(Utils.class);
|
2014-02-26 08:44:22 +01:00
|
|
|
|
2014-02-27 10:28:23 +01:00
|
|
|
private static Configuration config;
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
config = new PropertiesConfiguration(configFile);
|
|
|
|
} catch (ConfigurationException e) {
|
2014-02-28 04:49:28 +01:00
|
|
|
logger.error("[!] Failed to load properties file from " + configFile, e);
|
2014-02-27 10:28:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-09 09:20:22 +01:00
|
|
|
/**
|
|
|
|
* Get the root rips directory.
|
|
|
|
* @return
|
|
|
|
* Root directory to save rips to.
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2014-02-26 08:44:22 +01:00
|
|
|
public static File getWorkingDirectory() throws IOException {
|
|
|
|
String path = new File(".").getCanonicalPath() + File.separator;
|
|
|
|
path += RIP_DIRECTORY + File.separator;
|
|
|
|
File workingDir = new File(path);
|
|
|
|
if (!workingDir.exists()) {
|
|
|
|
workingDir.mkdirs();
|
|
|
|
}
|
|
|
|
return workingDir;
|
|
|
|
}
|
2014-02-27 04:54:44 +01:00
|
|
|
|
|
|
|
public static String getConfigString(String key, String defaultValue) {
|
2014-02-27 10:28:23 +01:00
|
|
|
return config.getString(key, defaultValue);
|
2014-02-26 08:44:22 +01:00
|
|
|
}
|
2014-02-27 04:54:44 +01:00
|
|
|
|
|
|
|
public static int getConfigInteger(String key, int defaultValue) {
|
2014-02-27 10:28:23 +01:00
|
|
|
return config.getInt(key, defaultValue);
|
2014-02-26 08:44:22 +01:00
|
|
|
}
|
2014-02-27 04:54:44 +01:00
|
|
|
|
|
|
|
public static boolean getConfigBoolean(String key, boolean defaultValue) {
|
2014-02-27 10:28:23 +01:00
|
|
|
return config.getBoolean(key, defaultValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setConfigBoolean(String key, boolean value) {
|
|
|
|
config.setProperty(key, value);
|
2014-02-27 04:54:44 +01:00
|
|
|
}
|
|
|
|
|
2014-02-28 12:04:03 +01:00
|
|
|
/**
|
|
|
|
* Removes the current working directory (CWD) from a File.
|
|
|
|
* @param saveAs
|
|
|
|
* The File path
|
|
|
|
* @return
|
|
|
|
* saveAs in relation to the CWD
|
|
|
|
*/
|
|
|
|
public static String removeCWD(File saveAs) {
|
|
|
|
String prettySaveAs;
|
|
|
|
try {
|
|
|
|
String cwd = new File(".").getCanonicalPath() + File.separator;
|
|
|
|
prettySaveAs = saveAs.getCanonicalPath().replace(
|
|
|
|
cwd,
|
|
|
|
"");
|
|
|
|
} catch (Exception e) {
|
|
|
|
prettySaveAs = saveAs.toString();
|
|
|
|
}
|
|
|
|
return prettySaveAs;
|
|
|
|
}
|
2014-03-13 20:13:01 +01:00
|
|
|
|
|
|
|
public static String stripURLParameter(String url, String parameter) {
|
|
|
|
int paramIndex = url.indexOf("?" + parameter);
|
|
|
|
boolean wasFirstParam = true;
|
|
|
|
if(paramIndex < 0) {
|
|
|
|
wasFirstParam = false;
|
|
|
|
paramIndex = url.indexOf("&" + parameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(paramIndex > 0) {
|
|
|
|
int nextParam = url.indexOf("&", paramIndex+1);
|
|
|
|
if(nextParam != -1) {
|
|
|
|
String c = "&";
|
|
|
|
if(wasFirstParam) c = "?";
|
|
|
|
url = url.substring(0, paramIndex) + c + url.substring(nextParam+1, url.length());
|
|
|
|
} else {
|
|
|
|
url = url.substring(0, paramIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
2014-02-28 12:04:03 +01:00
|
|
|
|
2014-03-09 09:20:22 +01:00
|
|
|
/**
|
|
|
|
* Removes the current working directory from a given filename
|
|
|
|
* @param file
|
|
|
|
* @return
|
|
|
|
* 'file' without the leading current working directory
|
|
|
|
*/
|
2014-02-28 12:04:03 +01:00
|
|
|
public static String removeCWD(String file) {
|
|
|
|
return removeCWD(new File(file));
|
|
|
|
}
|
2014-03-04 14:35:35 +01:00
|
|
|
|
2014-03-09 09:20:22 +01:00
|
|
|
/**
|
|
|
|
* Get a list of all Classes within a package.
|
|
|
|
* Works with file system projects and jar files!
|
|
|
|
* Borrowed from StackOverflow, but I don't have a link :[
|
|
|
|
* @param pkgname
|
|
|
|
* The name of the package
|
|
|
|
* @return
|
|
|
|
* List of classes within the package
|
|
|
|
*/
|
2014-03-04 14:35:35 +01:00
|
|
|
public static ArrayList<Class<?>> getClassesForPackage(String pkgname) {
|
|
|
|
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
|
|
|
|
String relPath = pkgname.replace('.', '/');
|
|
|
|
URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
|
|
|
|
if (resource == null) {
|
|
|
|
throw new RuntimeException("No resource for " + relPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
String fullPath = resource.getFile();
|
|
|
|
File directory = null;
|
|
|
|
try {
|
|
|
|
directory = new File(resource.toURI());
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
throw new RuntimeException(pkgname + " (" + resource + ") does not appear to be a valid URL / URI. Strange, since we got it from the system...", e);
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
directory = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (directory != null && directory.exists()) {
|
|
|
|
// Get the list of the files contained in the package
|
|
|
|
String[] files = directory.list();
|
|
|
|
for (String file : files) {
|
|
|
|
if (file.endsWith(".class") && !file.contains("$")) {
|
|
|
|
String className = pkgname + '.' + file.substring(0, file.length() - 6);
|
|
|
|
try {
|
|
|
|
classes.add(Class.forName(className));
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
throw new RuntimeException("ClassNotFoundException loading " + className);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
|
|
|
|
JarFile jarFile = new JarFile(jarPath);
|
|
|
|
Enumeration<JarEntry> entries = jarFile.entries();
|
|
|
|
while(entries.hasMoreElements()) {
|
|
|
|
String entryName = entries.nextElement().getName();
|
|
|
|
if(entryName.startsWith(relPath)
|
|
|
|
&& entryName.length() > (relPath.length() + "/".length())) {
|
|
|
|
String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
|
|
|
|
try {
|
|
|
|
classes.add(Class.forName(className));
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
throw new RuntimeException("ClassNotFoundException loading " + className);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return classes;
|
|
|
|
}
|
2014-02-26 08:44:22 +01:00
|
|
|
}
|