This commit is contained in:
jendib 2013-09-03 09:19:47 +02:00
parent b399e4081f
commit f5079e83cb
2 changed files with 201 additions and 201 deletions

View File

@ -1,102 +1,102 @@
package com.sismics.util;
import com.google.common.collect.Lists;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.text.MessageFormat;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* Resource utilities.
*
* @author jtremeaux
*/
public class ResourceUtil {
/**
* List files inside a directory. The path can be a directory on the filesystem, or inside a JAR.
*
* @param clazz Class
* @param path Path
* @param filter Filter
* @return List of files
* @throws URISyntaxException
* @throws IOException
*/
public static List<String> list(Class<?> clazz, String path, FilenameFilter filter) throws URISyntaxException, IOException {
// Path is a directory on the filesystem
URL dirUrl = clazz.getResource(path);
if (dirUrl != null && dirUrl.getProtocol().equals("file")) {
return Arrays.asList(new File(dirUrl.toURI()).list(filter));
}
// Path is a directory inside the same JAR as clazz
if (dirUrl == null) {
String className = clazz.getName().replace(".", "/") + ".class";
dirUrl = clazz.getClassLoader().getResource(className);
}
if (dirUrl.getProtocol().equals("jar")) {
if (path.startsWith("/")) {
path = path.substring(1);
}
if (!path.endsWith("/")) {
path = path + "/";
}
// Extract the JAR path
String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!"));
JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
Set<String> fileSet = new HashSet<String>();
try {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
// Filter according to the path
String entryName = entries.nextElement().getName();
if (!entryName.startsWith(path)) {
continue;
}
String name = entryName.substring(path.length());
if (!"".equals(name)) {
// If it is a subdirectory, just return the directory name
int checkSubdir = name.indexOf("/");
if (checkSubdir >= 0) {
name = name.substring(0, checkSubdir);
}
if (filter == null || filter.accept(null, name)) {
fileSet.add(name);
}
}
}
} finally {
jar.close();
}
return Lists.newArrayList(fileSet);
}
throw new UnsupportedOperationException(MessageFormat.format("Cannot list files for URL {0}", dirUrl));
}
/**
* List files inside a directory. The path can be a directory on the filesystem, or inside a JAR.
*
* @param clazz Class
* @param path Path
* @return List of files
* @throws URISyntaxException
* @throws IOException
*/
public static List<String> list(Class<?> clazz, String path) throws URISyntaxException, IOException {
return list(clazz, path, null);
}
}
package com.sismics.util;
import com.google.common.collect.Lists;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.text.MessageFormat;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* Resource utilities.
*
* @author jtremeaux
*/
public class ResourceUtil {
/**
* List files inside a directory. The path can be a directory on the filesystem, or inside a JAR.
*
* @param clazz Class
* @param path Path
* @param filter Filter
* @return List of files
* @throws URISyntaxException
* @throws IOException
*/
public static List<String> list(Class<?> clazz, String path, FilenameFilter filter) throws URISyntaxException, IOException {
// Path is a directory on the filesystem
URL dirUrl = clazz.getResource(path);
if (dirUrl != null && dirUrl.getProtocol().equals("file")) {
return Arrays.asList(new File(dirUrl.toURI()).list(filter));
}
// Path is a directory inside the same JAR as clazz
if (dirUrl == null) {
String className = clazz.getName().replace(".", "/") + ".class";
dirUrl = clazz.getClassLoader().getResource(className);
}
if (dirUrl.getProtocol().equals("jar")) {
if (path.startsWith("/")) {
path = path.substring(1);
}
if (!path.endsWith("/")) {
path = path + "/";
}
// Extract the JAR path
String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!"));
JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
Set<String> fileSet = new HashSet<String>();
try {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
// Filter according to the path
String entryName = entries.nextElement().getName();
if (!entryName.startsWith(path)) {
continue;
}
String name = entryName.substring(path.length());
if (!"".equals(name)) {
// If it is a subdirectory, just return the directory name
int checkSubdir = name.indexOf("/");
if (checkSubdir >= 0) {
name = name.substring(0, checkSubdir);
}
if (filter == null || filter.accept(null, name)) {
fileSet.add(name);
}
}
}
} finally {
jar.close();
}
return Lists.newArrayList(fileSet);
}
throw new UnsupportedOperationException(MessageFormat.format("Cannot list files for URL {0}", dirUrl));
}
/**
* List files inside a directory. The path can be a directory on the filesystem, or inside a JAR.
*
* @param clazz Class
* @param path Path
* @return List of files
* @throws URISyntaxException
* @throws IOException
*/
public static List<String> list(Class<?> clazz, String path) throws URISyntaxException, IOException {
return list(clazz, path, null);
}
}

View File

@ -1,100 +1,100 @@
package com.sismics.docs.rest;
import com.sismics.docs.rest.filter.CookieAuthenticationFilter;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.ClientResponse.Status;
import com.sun.jersey.api.client.WebResource;
import junit.framework.Assert;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
/**
* Test the app resource.
*
* @author jtremeaux
*/
public class TestAppResource extends BaseJerseyTest {
/**
* Test the API resource.
*
* @throws JSONException
*/
@Test
public void testAppResource() throws JSONException {
// Login admin
String adminAuthenticationToken = clientUtil.login("admin", "admin", false);
// Check the application info
WebResource appResource = resource().path("/app");
appResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
ClientResponse response = appResource.get(ClientResponse.class);
response = appResource.get(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
JSONObject json = response.getEntity(JSONObject.class);
String currentVersion = json.getString("current_version");
Assert.assertNotNull(currentVersion);
String minVersion = json.getString("min_version");
Assert.assertNotNull(minVersion);
Long freeMemory = json.getLong("free_memory");
Assert.assertTrue(freeMemory > 0);
Long totalMemory = json.getLong("total_memory");
Assert.assertTrue(totalMemory > 0 && totalMemory > freeMemory);
Assert.assertEquals(0, json.getInt("document_count"));
// Rebuild Lucene index
appResource = resource().path("/app/batch/reindex");
appResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
response = appResource.post(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
// Clean storage
appResource = resource().path("/app/batch/clean_storage");
appResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
response = appResource.post(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
}
/**
* Test the log resource.
*
* @throws JSONException
*/
@Test
public void testLogResource() throws JSONException {
// Login admin
String adminAuthenticationToken = clientUtil.login("admin", "admin", false);
// Check the logs (page 1)
WebResource appResource = resource()
.path("/app/log")
.queryParam("level", "DEBUG");
ClientResponse response = appResource.get(ClientResponse.class);
appResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
response = appResource.get(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
JSONObject json = response.getEntity(JSONObject.class);
JSONArray logs = json.getJSONArray("logs");
Assert.assertTrue(logs.length() > 0);
Long date1 = logs.optJSONObject(0).optLong("date");
Long date2 = logs.optJSONObject(9).optLong("date");
Assert.assertTrue(date1 > date2);
// Check the logs (page 2)
appResource = resource()
.path("/app/log")
.queryParam("offset", "10")
.queryParam("level", "DEBUG");
response = appResource.get(ClientResponse.class);
appResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
response = appResource.get(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class);
logs = json.getJSONArray("logs");
Assert.assertTrue(logs.length() > 0);
Long date3 = logs.optJSONObject(0).optLong("date");
Long date4 = logs.optJSONObject(9).optLong("date");
Assert.assertTrue(date3 > date4);
}
package com.sismics.docs.rest;
import com.sismics.docs.rest.filter.CookieAuthenticationFilter;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.ClientResponse.Status;
import com.sun.jersey.api.client.WebResource;
import junit.framework.Assert;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
/**
* Test the app resource.
*
* @author jtremeaux
*/
public class TestAppResource extends BaseJerseyTest {
/**
* Test the API resource.
*
* @throws JSONException
*/
@Test
public void testAppResource() throws JSONException {
// Login admin
String adminAuthenticationToken = clientUtil.login("admin", "admin", false);
// Check the application info
WebResource appResource = resource().path("/app");
appResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
ClientResponse response = appResource.get(ClientResponse.class);
response = appResource.get(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
JSONObject json = response.getEntity(JSONObject.class);
String currentVersion = json.getString("current_version");
Assert.assertNotNull(currentVersion);
String minVersion = json.getString("min_version");
Assert.assertNotNull(minVersion);
Long freeMemory = json.getLong("free_memory");
Assert.assertTrue(freeMemory > 0);
Long totalMemory = json.getLong("total_memory");
Assert.assertTrue(totalMemory > 0 && totalMemory > freeMemory);
Assert.assertEquals(0, json.getInt("document_count"));
// Rebuild Lucene index
appResource = resource().path("/app/batch/reindex");
appResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
response = appResource.post(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
// Clean storage
appResource = resource().path("/app/batch/clean_storage");
appResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
response = appResource.post(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
}
/**
* Test the log resource.
*
* @throws JSONException
*/
@Test
public void testLogResource() throws JSONException {
// Login admin
String adminAuthenticationToken = clientUtil.login("admin", "admin", false);
// Check the logs (page 1)
WebResource appResource = resource()
.path("/app/log")
.queryParam("level", "DEBUG");
ClientResponse response = appResource.get(ClientResponse.class);
appResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
response = appResource.get(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
JSONObject json = response.getEntity(JSONObject.class);
JSONArray logs = json.getJSONArray("logs");
Assert.assertTrue(logs.length() > 0);
Long date1 = logs.optJSONObject(0).optLong("date");
Long date2 = logs.optJSONObject(9).optLong("date");
Assert.assertTrue(date1 > date2);
// Check the logs (page 2)
appResource = resource()
.path("/app/log")
.queryParam("offset", "10")
.queryParam("level", "DEBUG");
response = appResource.get(ClientResponse.class);
appResource.addFilter(new CookieAuthenticationFilter(adminAuthenticationToken));
response = appResource.get(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class);
logs = json.getJSONArray("logs");
Assert.assertTrue(logs.length() > 0);
Long date3 = logs.optJSONObject(0).optLong("date");
Long date4 = logs.optJSONObject(9).optLong("date");
Assert.assertTrue(date3 > date4);
}
}