2013-07-27 18:33:20 +02:00
|
|
|
package com.sismics.docs.rest.resource;
|
|
|
|
|
2013-08-01 13:36:15 +02:00
|
|
|
import com.sismics.docs.core.dao.jpa.DocumentDao;
|
|
|
|
import com.sismics.docs.core.dao.jpa.criteria.DocumentCriteria;
|
|
|
|
import com.sismics.docs.core.dao.jpa.dto.DocumentDto;
|
2013-07-27 18:33:20 +02:00
|
|
|
import com.sismics.docs.core.util.ConfigUtil;
|
|
|
|
import com.sismics.docs.core.util.jpa.PaginatedList;
|
|
|
|
import com.sismics.docs.core.util.jpa.PaginatedLists;
|
2013-08-01 13:36:15 +02:00
|
|
|
import com.sismics.docs.core.util.jpa.SortCriteria;
|
2013-07-27 18:33:20 +02:00
|
|
|
import com.sismics.docs.rest.constant.BaseFunction;
|
|
|
|
import com.sismics.rest.exception.ForbiddenClientException;
|
|
|
|
import com.sismics.rest.exception.ServerException;
|
|
|
|
import com.sismics.util.log4j.LogCriteria;
|
|
|
|
import com.sismics.util.log4j.LogEntry;
|
|
|
|
import com.sismics.util.log4j.MemoryAppender;
|
2013-08-13 20:08:14 +02:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.apache.log4j.Appender;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import org.codehaus.jettison.json.JSONException;
|
|
|
|
import org.codehaus.jettison.json.JSONObject;
|
|
|
|
|
|
|
|
import javax.ws.rs.GET;
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
import javax.ws.rs.Produces;
|
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.ResourceBundle;
|
2013-07-27 18:33:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* General app REST resource.
|
|
|
|
*
|
|
|
|
* @author jtremeaux
|
|
|
|
*/
|
|
|
|
@Path("/app")
|
|
|
|
public class AppResource extends BaseResource {
|
|
|
|
/**
|
|
|
|
* Return the information about the application.
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
* @throws JSONException
|
|
|
|
*/
|
|
|
|
@GET
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
2013-08-01 13:36:15 +02:00
|
|
|
public Response info() throws JSONException {
|
|
|
|
if (!authenticate()) {
|
|
|
|
throw new ForbiddenClientException();
|
|
|
|
}
|
|
|
|
|
2013-07-27 18:33:20 +02:00
|
|
|
ResourceBundle configBundle = ConfigUtil.getConfigBundle();
|
|
|
|
String currentVersion = configBundle.getString("api.current_version");
|
|
|
|
String minVersion = configBundle.getString("api.min_version");
|
|
|
|
|
|
|
|
JSONObject response = new JSONObject();
|
2013-08-01 13:36:15 +02:00
|
|
|
|
|
|
|
// Specific data
|
|
|
|
DocumentDao documentDao = new DocumentDao();
|
|
|
|
PaginatedList<DocumentDto> paginatedList = PaginatedLists.create(1, 0);
|
|
|
|
SortCriteria sortCriteria = new SortCriteria(0, true);
|
|
|
|
DocumentCriteria documentCriteria = new DocumentCriteria();
|
|
|
|
documentCriteria.setUserId(principal.getId());
|
|
|
|
documentDao.findByCriteria(paginatedList, documentCriteria, sortCriteria);
|
|
|
|
response.put("document_count", paginatedList.getResultCount());
|
|
|
|
|
|
|
|
// General data
|
2013-07-27 18:33:20 +02:00
|
|
|
response.put("current_version", currentVersion.replace("-SNAPSHOT", ""));
|
|
|
|
response.put("min_version", minVersion);
|
|
|
|
response.put("total_memory", Runtime.getRuntime().totalMemory());
|
|
|
|
response.put("free_memory", Runtime.getRuntime().freeMemory());
|
2013-08-01 13:36:15 +02:00
|
|
|
|
2013-07-27 18:33:20 +02:00
|
|
|
return Response.ok().entity(response).build();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the application logs.
|
|
|
|
*
|
|
|
|
* @param level Filter on logging level
|
|
|
|
* @param tag Filter on logger name / tag
|
|
|
|
* @param message Filter on message
|
|
|
|
* @param limit Page limit
|
|
|
|
* @param offset Page offset
|
|
|
|
* @return
|
|
|
|
* @throws JSONException
|
|
|
|
*/
|
|
|
|
@GET
|
|
|
|
@Path("log")
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
public Response log(
|
|
|
|
@QueryParam("level") String level,
|
|
|
|
@QueryParam("tag") String tag,
|
|
|
|
@QueryParam("message") String message,
|
|
|
|
@QueryParam("limit") Integer limit,
|
|
|
|
@QueryParam("offset") Integer offset) throws JSONException {
|
|
|
|
if (!authenticate()) {
|
|
|
|
throw new ForbiddenClientException();
|
|
|
|
}
|
|
|
|
checkBaseFunction(BaseFunction.ADMIN);
|
|
|
|
|
|
|
|
// Get the memory appender
|
|
|
|
Logger logger = Logger.getRootLogger();
|
|
|
|
Appender appender = logger.getAppender("MEMORY");
|
|
|
|
if (appender == null || !(appender instanceof MemoryAppender)) {
|
|
|
|
throw new ServerException("ServerError", "MEMORY appender not configured");
|
|
|
|
}
|
|
|
|
MemoryAppender memoryAppender = (MemoryAppender) appender;
|
|
|
|
|
|
|
|
// Find the logs
|
|
|
|
LogCriteria logCriteria = new LogCriteria();
|
|
|
|
logCriteria.setLevel(StringUtils.stripToNull(level));
|
|
|
|
logCriteria.setTag(StringUtils.stripToNull(tag));
|
|
|
|
logCriteria.setMessage(StringUtils.stripToNull(message));
|
|
|
|
|
|
|
|
PaginatedList<LogEntry> paginatedList = PaginatedLists.create(limit, offset);
|
|
|
|
memoryAppender.find(logCriteria, paginatedList);
|
|
|
|
JSONObject response = new JSONObject();
|
|
|
|
List<JSONObject> logs = new ArrayList<JSONObject>();
|
|
|
|
for (LogEntry logEntry : paginatedList.getResultList()) {
|
|
|
|
JSONObject log = new JSONObject();
|
|
|
|
log.put("date", logEntry.getTimestamp());
|
|
|
|
log.put("level", logEntry.getLevel());
|
|
|
|
log.put("tag", logEntry.getTag());
|
|
|
|
log.put("message", logEntry.getMessage());
|
|
|
|
logs.add(log);
|
|
|
|
}
|
|
|
|
response.put("total", paginatedList.getResultCount());
|
|
|
|
response.put("logs", logs);
|
|
|
|
|
|
|
|
return Response.ok().entity(response).build();
|
|
|
|
}
|
|
|
|
}
|