use of com.twinsoft.convertigo.engine.admin.services.at.ServiceDefinition in project convertigo by convertigo.
the class AdminServlet method doRequest.
private void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
boolean show_error = false;
try {
show_error = !EnginePropertiesManager.getProperty(PropertyName.HIDING_ERROR_INFORMATION).equals("true");
} catch (Exception e) {
Engine.logAdmin.debug("Failed to retrieve property HIDING_ERROR_INFORMATION: " + e.getClass() + " (" + e.getMessage() + ")");
}
try {
String serviceName = "";
String isAdmin = "";
try {
response.addHeader("Expires", "-1");
response.addHeader("Pragma", "no-cache");
request.setCharacterEncoding("UTF-8");
String requestURL = request.getRequestURL().toString();
int i = requestURL.lastIndexOf('/');
isAdmin = requestURL.substring(0, i).endsWith("/admin/services") ? "admin " : "";
serviceName = requestURL.substring(i + 1);
if (serviceName != null && !serviceName.equals("logs.Get")) {
Engine.logAdmin.info("Service name: " + serviceName);
}
String myPackage = this.getClass().getPackage().getName();
Class<?> serviceClass = Class.forName(myPackage + ".services." + serviceName);
// Check for authentication and roles
ServiceDefinition serviceDefinition = serviceClass.getAnnotation(ServiceDefinition.class);
if (serviceDefinition == null)
throw new IllegalArgumentException("The service '" + serviceName + "' has no service definition!");
if (Engine.isCloudMode()) {
boolean cloud_forbidden = serviceDefinition.cloud_forbidden();
Engine.logAdmin.debug("Is service forbidden for Cloud ? : " + cloud_forbidden);
if (cloud_forbidden) {
throw new EngineException("The service '" + serviceName + "' cannot be acceded on Cloud.");
}
}
if (isAdmin.isEmpty() && serviceDefinition.admin()) {
throw new ClassNotFoundException();
}
String corsOrigin = HttpUtils.applyCorsHeaders(request, response);
if (corsOrigin != null) {
Engine.logAdmin.trace("Add CORS header for: " + corsOrigin);
}
boolean needsAuthentication = !AuthenticatedSessionManager.hasRole(serviceDefinition.roles(), Role.ANONYMOUS);
Engine.logAdmin.debug("Needs authentication: " + needsAuthentication);
if (needsAuthentication) {
Engine.authenticatedSessionManager.checkRoles(request.getSession(false), serviceDefinition.roles());
}
Service service = (Service) serviceClass.getConstructor().newInstance();
try {
boolean xsrfAdmin = EnginePropertiesManager.getPropertyAsBoolean(PropertyName.XSRF_ADMIN);
if (xsrfAdmin) {
if (!serviceDefinition.allow_cors() || EnginePropertiesManager.getPropertyAsBoolean(PropertyName.XSRF_API)) {
HttpUtils.checkXSRF(request, response);
}
}
} catch (IllegalStateException e) {
Engine.logAdmin.warn("Cannot retrieve properties for XSRF, Engine probably stopped.");
}
service.run(serviceName, request, response);
} catch (ClassNotFoundException e) {
String message = "Unknown " + isAdmin + "service '" + serviceName + "'";
Engine.logAdmin.error(message);
if (show_error) {
ServiceUtils.handleError(message, request, response);
}
} catch (NoClassDefFoundError e) {
String message = "Unknown " + isAdmin + "service '" + serviceName + "'";
Engine.logAdmin.error(message);
if (show_error) {
ServiceUtils.handleError(message, request, response);
}
} catch (AuthenticationException e) {
String authMessage = e.getMessage();
Engine.logAdmin.warn(authMessage);
if (show_error) {
ServiceUtils.handleError(authMessage, request, response);
}
} catch (Exception e) {
Engine.logAdmin.error("Unable to execute the service '" + serviceName + "'", e);
if (show_error) {
ServiceUtils.handleError(e, request, response);
}
} finally {
response.flushBuffer();
}
} catch (Throwable e) {
if (show_error) {
throw new ServletException(e);
}
}
}
use of com.twinsoft.convertigo.engine.admin.services.at.ServiceDefinition in project convertigo by convertigo.
the class JSonService method run.
public void run(String serviceName, HttpServletRequest request, HttpServletResponse response) throws ServiceException {
try {
response.setContentType(MimeType.JavascriptDeprecated.value());
response.setCharacterEncoding("UTF-8");
JSONObject json = new JSONObject();
getServiceResult(request, json);
ServiceDefinition serviceDefinition = getClass().getAnnotation(ServiceDefinition.class);
if (serviceDefinition.allow_cors()) {
ServletUtils.applyCustomHeaders(request, response);
}
String sJSON = json.toString();
response.getWriter().write(sJSON);
Engine.logAdmin.debug("JSON generated:\n" + sJSON);
} catch (Throwable t) {
ServiceUtils.handleError(t, request, response);
}
}
Aggregations