Mage.Stats WS module for getting server stats in json format

This commit is contained in:
magenoxx 2014-08-27 03:38:43 +04:00
parent 78c0d76088
commit 71614becc2
43 changed files with 1587 additions and 0 deletions

View file

@ -0,0 +1,8 @@
package com.xmage.ws.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/api")
public class XMageStatsAPIApplication extends Application {
}

View file

@ -0,0 +1,34 @@
package com.xmage.ws.rest.services;
import com.xmage.ws.resource.XMageStatsResource;
import com.xmage.ws.resource.Resource;
import com.xmage.ws.rest.services.base.AbstractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
/**
*
* @author noxx
*/
@Path("/xmage/stats")
@Produces("application/json;charset=utf-8")
public class XMageStatsService extends AbstractService {
static final Logger logger = LoggerFactory.getLogger(XMageStatsService.class);
@GET
@Path("/getAll")
public Response getAllStats() {
logger.trace("getAllStats");
Resource resource = new XMageStatsResource().getAll();
return responseWithError(resource);
}
}

View file

@ -0,0 +1,73 @@
package com.xmage.ws.rest.services.base;
import com.xmage.ws.json.ResponseBuilder;
import com.xmage.ws.model.DomainErrors;
import com.xmage.ws.resource.Resource;
import net.minidev.json.JSONObject;
import org.apache.sling.commons.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.core.Response;
/**
* General approach for ws requests/responses.
*
* Consists of building response object, verifying response, prettifying, execution time calculating.
*
* @author noxx
*/
public abstract class AbstractService {
private static final Logger logger = LoggerFactory.getLogger(AbstractService.class);
/**
* Create {@link Response} from {@link com.xmage.ws.resource.Resource}
*
* @param resource Resource to build response based on
* @return
*/
public final Response responseWithError(Resource resource) {
long t1 = System.currentTimeMillis();
JSONObject response = buildResponse(resource);
response = verifyResponse(response);
String json = prettifyResponse(response);
Response responseObject = Response.status(200).entity(json).build();
long t2 = System.currentTimeMillis();
logger.info("responseWithError time: " + (t2 - t1) + "ms");
return responseObject;
}
private JSONObject buildResponse(Resource resource) {
JSONObject response = null;
try {
response = ResponseBuilder.build(resource);
} catch (Exception e) {
logger.error("responseWithError: ", e);
}
return response;
}
private String prettifyResponse(JSONObject response) {
String json = response.toJSONString();
try {
json = new org.apache.sling.commons.json.JSONObject(json).toString(1);
} catch (JSONException jse) {
jse.printStackTrace();
}
return json;
}
private JSONObject verifyResponse(JSONObject response) {
if (response == null) {
logger.error("Something bad happened on response creation");
response = ResponseBuilder.build(DomainErrors.Errors.STATUS_SERVER_ERROR.getCode());
}
return response;
}
}