Search in sources :

Example 1 with NoResultsException

use of org.structr.rest.exception.NoResultsException in project structr by structr.

the class Resource method doDelete.

public RestMethodResult doDelete() throws FrameworkException {
    final App app = StructrApp.getInstance(securityContext);
    Iterable<GraphObject> results = null;
    // catch 204, DELETE must return 200 if resource is empty
    try (final Tx tx = app.tx(false, false, false)) {
        results = doGet(null, false, NodeFactory.DEFAULT_PAGE_SIZE, NodeFactory.DEFAULT_PAGE).getResults();
        tx.success();
    } catch (final NoResultsException nre) {
        results = null;
    }
    if (results != null) {
        app.command(BulkDeleteCommand.class).bulkDelete(results.iterator());
    }
    return new RestMethodResult(HttpServletResponse.SC_OK);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NoResultsException(org.structr.rest.exception.NoResultsException) Tx(org.structr.core.graph.Tx) BulkDeleteCommand(org.structr.core.graph.BulkDeleteCommand) GraphObject(org.structr.core.GraphObject) RestMethodResult(org.structr.rest.RestMethodResult)

Example 2 with NoResultsException

use of org.structr.rest.exception.NoResultsException in project structr by structr.

the class ResourceHelper method parsePath.

// ~--- methods --------------------------------------------------------
/**
 * Parse the request path and match with possible resource patterns
 *
 * @param securityContext
 * @param request
 * @param resourceMap
 * @param propertyView
 * @return resourceChain
 * @throws FrameworkException
 */
public static List<Resource> parsePath(final SecurityContext securityContext, final HttpServletRequest request, final Map<Pattern, Class<? extends Resource>> resourceMap, final Value<String> propertyView) throws FrameworkException {
    final String path = request.getPathInfo();
    // intercept empty path and send 204 No Content
    if (StringUtils.isBlank(path)) {
        throw new NoResultsException("No content");
    }
    // 1.: split request path into URI parts
    final String[] pathParts = path.split("[/]+");
    // 2.: create container for resource constraints
    final Set<String> propertyViews = Services.getInstance().getConfigurationProvider().getPropertyViews();
    final List<Resource> resourceChain = new ArrayList<>(pathParts.length);
    // 3.: try to assign resource constraints for each URI part
    for (int i = 0; i < pathParts.length; i++) {
        // eliminate empty strings
        final String part = pathParts[i].trim();
        if (part.length() > 0) {
            boolean found = false;
            // check views first
            if (propertyViews.contains(part)) {
                Resource resource = new ViewFilterResource();
                resource.checkAndConfigure(part, securityContext, request);
                resource.configurePropertyView(propertyView);
                resourceChain.add(resource);
                // mark this part as successfully parsed
                found = true;
            } else {
                // look for matching pattern
                for (Map.Entry<Pattern, Class<? extends Resource>> entry : resourceMap.entrySet()) {
                    Pattern pattern = entry.getKey();
                    Matcher matcher = pattern.matcher(pathParts[i]);
                    if (matcher.matches()) {
                        Class<? extends Resource> type = entry.getValue();
                        Resource resource = null;
                        try {
                            // instantiate resource constraint
                            resource = type.newInstance();
                        } catch (Throwable t) {
                            logger.warn("Error instantiating resource class", t);
                        }
                        if (resource != null) {
                            // set security context
                            resource.setSecurityContext(securityContext);
                            if (resource.checkAndConfigure(part, securityContext, request)) {
                                logger.debug("{} matched, adding resource of type {} for part {}", new Object[] { matcher.pattern(), type.getName(), part });
                                // allow constraint to modify context
                                resource.configurePropertyView(propertyView);
                                // add constraint and go on
                                resourceChain.add(resource);
                                found = true;
                                // first match wins, so choose priority wisely ;)
                                break;
                            }
                        }
                    }
                }
            }
            if (!found) {
                throw new NotFoundException("Cannot resolve URL path");
            }
        }
    }
    return resourceChain;
}
Also used : NoResultsException(org.structr.rest.exception.NoResultsException) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) TransformationResource(org.structr.rest.resource.TransformationResource) ViewFilterResource(org.structr.rest.resource.ViewFilterResource) Resource(org.structr.rest.resource.Resource) ArrayList(java.util.ArrayList) NotFoundException(org.structr.rest.exception.NotFoundException) ViewFilterResource(org.structr.rest.resource.ViewFilterResource) Map(java.util.Map)

Aggregations

NoResultsException (org.structr.rest.exception.NoResultsException)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 GraphObject (org.structr.core.GraphObject)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 BulkDeleteCommand (org.structr.core.graph.BulkDeleteCommand)1 Tx (org.structr.core.graph.Tx)1 RestMethodResult (org.structr.rest.RestMethodResult)1 NotFoundException (org.structr.rest.exception.NotFoundException)1 Resource (org.structr.rest.resource.Resource)1 TransformationResource (org.structr.rest.resource.TransformationResource)1 ViewFilterResource (org.structr.rest.resource.ViewFilterResource)1