Search in sources :

Example 1 with ServiceDefinition

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);
        }
    }
}
Also used : AuthenticationException(com.twinsoft.convertigo.engine.AuthenticationException) EngineException(com.twinsoft.convertigo.engine.EngineException) Service(com.twinsoft.convertigo.engine.admin.services.Service) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AuthenticationException(com.twinsoft.convertigo.engine.AuthenticationException) EngineException(com.twinsoft.convertigo.engine.EngineException) ServletException(javax.servlet.ServletException) ServiceDefinition(com.twinsoft.convertigo.engine.admin.services.at.ServiceDefinition)

Example 2 with ServiceDefinition

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);
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) ServiceDefinition(com.twinsoft.convertigo.engine.admin.services.at.ServiceDefinition)

Aggregations

ServiceDefinition (com.twinsoft.convertigo.engine.admin.services.at.ServiceDefinition)2 AuthenticationException (com.twinsoft.convertigo.engine.AuthenticationException)1 EngineException (com.twinsoft.convertigo.engine.EngineException)1 Service (com.twinsoft.convertigo.engine.admin.services.Service)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 JSONObject (org.codehaus.jettison.json.JSONObject)1