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());
}
}
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);
}
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;
}
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());
}
}
Aggregations