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

View File

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