Search in sources :

Example 1 with ResourceAction

use of com.manydesigns.portofino.resourceactions.ResourceAction in project Portofino by ManyDesigns.

the class PortofinoFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) {
    UriInfo uriInfo = requestContext.getUriInfo();
    if (uriInfo.getMatchedResources().isEmpty()) {
        return;
    }
    Object resource = uriInfo.getMatchedResources().get(0);
    if (resourceInfo == null || resourceInfo.getResourceClass() == null) {
        return;
    }
    if (resource.getClass() != resourceInfo.getResourceClass()) {
        throw new RuntimeException("Inconsistency: matched resource is not of the right type, " + resourceInfo.getResourceClass());
    }
    logger.debug("Setting up logging MDC");
    MDC.clear();
    HttpServletRequest request = ElementsThreadLocals.getHttpServletRequest();
    if (request != null) {
        MDC.put("req.requestURI", request.getRequestURI());
    }
    if (resource instanceof ResourceAction) {
        ResourceAction resourceAction = (ResourceAction) resource;
        logger.debug("Retrieving user");
        Object userId = resourceAction.getSecurity().getUserId();
        if (userId != null) {
            // Issue #755
            MDC.put("userId", userId.toString());
        }
        OgnlContext ognlContext = ElementsThreadLocals.getOgnlContext();
        ognlContext.put("securityUtils", resourceAction.getSecurity().getSecurityUtilsBean());
        resourceAction.prepareForExecution();
    }
    WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    SecurityFacade facade = NoSecurity.AT_ALL;
    if (context != null) {
        try {
            facade = context.getBean(SecurityFacade.class);
        } catch (NoSuchBeanDefinitionException e) {
            logger.debug("No security facade found, using no-op", e);
        }
    }
    facade.checkWebResourceIsAccessible(requestContext, resource, resourceInfo.getResourceMethod());
    if (resource instanceof ResourceAction) {
        checkResourceActionInvocation(requestContext, (ResourceAction) resource);
    }
    Method resourceMethod = resourceInfo.getResourceMethod();
    if (isAccessToBeLogged(resource, resourceMethod)) {
        accessLogger.info(requestContext.getMethod() + " " + resourceMethod.getName() + ", queryString " + request.getQueryString());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SecurityFacade(com.manydesigns.portofino.security.SecurityFacade) Method(java.lang.reflect.Method) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) UriInfo(javax.ws.rs.core.UriInfo) ResourceAction(com.manydesigns.portofino.resourceactions.ResourceAction) OgnlContext(ognl.OgnlContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 2 with ResourceAction

use of com.manydesigns.portofino.resourceactions.ResourceAction in project Portofino by ManyDesigns.

the class ActionsAction method create.

@POST
public void create(String actionClassName) throws Exception {
    String actionPath = StringUtils.join(parameters.subList(0, parameters.size() - 1), "/");
    String segment = parameters.get(parameters.size() - 1);
    ActionInstance parentActionInstance = getPageInstance(actionPath);
    ResourceAction parent = parentActionInstance.getActionBean();
    Class actionClass = codeBase.loadClass(actionClassName);
    ActionInfo info = actionRegistry.getInfo(actionClass);
    String scriptTemplate = info.scriptTemplate;
    Class<?> configurationClass = info.configurationClass;
    boolean supportsDetail = info.supportsDetail;
    String className = actionClass.getSimpleName() + "_" + RandomUtil.createRandomId();
    OgnlContext ognlContext = ElementsThreadLocals.getOgnlContext();
    ognlContext.put("generatedClassName", className);
    ognlContext.put("actionClassName", actionClassName);
    String script = OgnlTextFormat.format(scriptTemplate, parent);
    ActionDescriptor action = new ActionDescriptor();
    Object configuration = null;
    if (configurationClass != null) {
        configuration = ReflectionUtil.newInstance(configurationClass);
        if (configuration instanceof ConfigurationWithDefaults) {
            ((ConfigurationWithDefaults) configuration).setupDefaults();
        }
    }
    action.init();
    FileObject directory = parentActionInstance.getChildPageDirectory(segment);
    if (directory.exists()) {
        logger.error("Can't create actionDescriptor - directory {} exists", directory.getName().getPath());
        throw new WebApplicationException(Response.serverError().entity(ElementsThreadLocals.getText("error.creating.page.the.directory.already.exists")).build());
    }
    directory.createFolder();
    logger.debug("Creating the new child actionDescriptor in directory: {}", directory);
    ActionLogic.saveActionDescriptor(directory, action);
    if (configuration != null) {
        ActionLogic.saveConfiguration(directory, configuration);
    }
    FileObject groovyScriptFile = directory.resolveFile("action.groovy");
    groovyScriptFile.createFile();
    try (Writer w = new OutputStreamWriter(groovyScriptFile.getContent().getOutputStream())) {
        w.write(script);
    }
    if (supportsDetail) {
        FileObject detailDir = directory.resolveFile(ActionInstance.DETAIL);
        logger.debug("Creating _detail directory: {}", detailDir);
        detailDir.createFolder();
    }
    logger.info("Created action of type " + actionClassName + " in directory " + directory);
}
Also used : ActionDescriptor(com.manydesigns.portofino.actions.ActionDescriptor) ConfigurationWithDefaults(com.manydesigns.portofino.resourceactions.ConfigurationWithDefaults) ActionInfo(com.manydesigns.portofino.resourceactions.registry.ActionInfo) OgnlContext(ognl.OgnlContext) ActionInstance(com.manydesigns.portofino.resourceactions.ActionInstance) FileObject(org.apache.commons.vfs2.FileObject) OutputStreamWriter(java.io.OutputStreamWriter) FileObject(org.apache.commons.vfs2.FileObject) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) AbstractResourceAction(com.manydesigns.portofino.resourceactions.AbstractResourceAction) ResourceAction(com.manydesigns.portofino.resourceactions.ResourceAction)

Example 3 with ResourceAction

use of com.manydesigns.portofino.resourceactions.ResourceAction in project Portofino by ManyDesigns.

the class ActionsAction method getPageInstance.

public ActionInstance getPageInstance(String actionPath) {
    ResourceAction action = (ResourceAction) getResource(actionPath);
    if (action == null) {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    ActionInstance actionInstance = action.getActionInstance();
    checkPermissions(actionInstance);
    return actionInstance;
}
Also used : ActionInstance(com.manydesigns.portofino.resourceactions.ActionInstance) AbstractResourceAction(com.manydesigns.portofino.resourceactions.AbstractResourceAction) ResourceAction(com.manydesigns.portofino.resourceactions.ResourceAction)

Example 4 with ResourceAction

use of com.manydesigns.portofino.resourceactions.ResourceAction in project Portofino by ManyDesigns.

the class SettingsAction method update.

@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Form update(String jsonObject) {
    Form form = setupFormAndBean();
    FormUtil.readFromJson(form, new JSONObject(jsonObject));
    if (form.validate()) {
        logger.debug("Applying settings to model");
        try {
            Settings settings = new Settings();
            form.writeToObject(settings);
            ResourceAction action = (ResourceAction) getRoot();
            String[] loginPath = settings.loginPath.split("/");
            for (String loginPathSegment : loginPath) {
                Object subResource = action.getSubResource(loginPathSegment);
                if (!(subResource instanceof ResourceAction)) {
                    throw new WebApplicationException("Invalid login path");
                }
                action = (ResourceAction) subResource;
            }
            configuration.getProperties().setProperty(PortofinoProperties.APP_NAME, settings.appName);
            configuration.getProperties().setProperty(PortofinoProperties.APP_VERSION, settings.appVersion);
            configuration.getProperties().setProperty(PortofinoProperties.LOGIN_PATH, settings.loginPath);
            if (!settings.preloadGroovyPages || configuration.getProperties().getProperty(PortofinoProperties.PRELOAD_ACTIONS) != null) {
                configuration.getProperties().setProperty(PortofinoProperties.PRELOAD_ACTIONS, settings.preloadGroovyPages);
            }
            if (!settings.preloadGroovyClasses || configuration.getProperties().getProperty(PortofinoProperties.PRELOAD_CLASSES) != null) {
                configuration.getProperties().setProperty(PortofinoProperties.PRELOAD_CLASSES, settings.preloadGroovyClasses);
            }
            configuration.save();
            return form;
        } catch (Exception e) {
            logger.error("Configuration not saved", e);
            throw new WebApplicationException("Configuration not saved", e);
        }
    } else {
        throw new WebApplicationException(Response.serverError().entity(form).build());
    }
}
Also used : JSONObject(org.json.JSONObject) Form(com.manydesigns.elements.forms.Form) JSONObject(org.json.JSONObject) Settings(com.manydesigns.portofino.upstairs.Settings) AbstractResourceAction(com.manydesigns.portofino.resourceactions.AbstractResourceAction) ResourceAction(com.manydesigns.portofino.resourceactions.ResourceAction)

Aggregations

ResourceAction (com.manydesigns.portofino.resourceactions.ResourceAction)4 AbstractResourceAction (com.manydesigns.portofino.resourceactions.AbstractResourceAction)3 ActionInstance (com.manydesigns.portofino.resourceactions.ActionInstance)2 OgnlContext (ognl.OgnlContext)2 Form (com.manydesigns.elements.forms.Form)1 ActionDescriptor (com.manydesigns.portofino.actions.ActionDescriptor)1 ConfigurationWithDefaults (com.manydesigns.portofino.resourceactions.ConfigurationWithDefaults)1 ActionInfo (com.manydesigns.portofino.resourceactions.registry.ActionInfo)1 SecurityFacade (com.manydesigns.portofino.security.SecurityFacade)1 Settings (com.manydesigns.portofino.upstairs.Settings)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 Method (java.lang.reflect.Method)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 UriInfo (javax.ws.rs.core.UriInfo)1 FileObject (org.apache.commons.vfs2.FileObject)1 JSONObject (org.json.JSONObject)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1