Search in sources :

Example 1 with SlingPostProcessor

use of org.apache.sling.servlets.post.SlingPostProcessor in project sling by apache.

the class SlingPostServlet method doPost.

@Override
protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
    final VersioningConfiguration localVersioningConfig = createRequestVersioningConfiguration(request);
    request.setAttribute(VersioningConfiguration.class.getName(), localVersioningConfig);
    // prepare the response
    final PostResponse htmlResponse = createPostResponse(request);
    htmlResponse.setReferer(request.getHeader("referer"));
    final PostOperation operation = getSlingPostOperation(request);
    if (operation == null) {
        htmlResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid operation specified for POST request");
    } else {
        request.getRequestProgressTracker().log("Calling PostOperation: {0}", operation.getClass().getName());
        final SlingPostProcessor[] processors = this.cachedPostProcessors;
        try {
            operation.run(request, htmlResponse, processors);
        } catch (ResourceNotFoundException rnfe) {
            htmlResponse.setStatus(HttpServletResponse.SC_NOT_FOUND, rnfe.getMessage());
        } catch (final Exception exception) {
            log.warn("Exception while handling POST " + request.getResource().getPath() + " with " + operation.getClass().getName(), exception);
            htmlResponse.setError(exception);
        }
    }
    // check for redirect URL if processing succeeded
    if (htmlResponse.isSuccessful()) {
        if (redirectIfNeeded(request, htmlResponse, response)) {
            return;
        }
    }
    // create a html response and send if unsuccessful or no redirect
    htmlResponse.send(response, isSetStatus(request));
}
Also used : SlingPostProcessor(org.apache.sling.servlets.post.SlingPostProcessor) VersioningConfiguration(org.apache.sling.servlets.post.VersioningConfiguration) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) PostOperation(org.apache.sling.servlets.post.PostOperation) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException) PostResponse(org.apache.sling.servlets.post.PostResponse)

Example 2 with SlingPostProcessor

use of org.apache.sling.servlets.post.SlingPostProcessor in project sling by apache.

the class AbstractPostOperation method run.

/**
     * Prepares and finalizes the actual operation. Preparation encompasses
     * getting the absolute path of the item to operate on by calling the
     * {@link #getResourcePath(SlingHttpServletRequest)} method and setting the
     * location and parent location on the response. After the operation has
     * been done in the {@link #doRun(SlingHttpServletRequest, PostResponse, List)}
     * method the session is saved if there are unsaved modifications. In case
     * of errors, the unsaved changes in the session are rolled back.
     *
     * @param request the request to operate on
     * @param response The <code>PostResponse</code> to record execution
     *            progress.
     * @param processors The array of processors
     */
@Override
public void run(final SlingHttpServletRequest request, final PostResponse response, final SlingPostProcessor[] processors) {
    final VersioningConfiguration versionableConfiguration = getVersioningConfiguration(request);
    try {
        // calculate the paths
        String path = this.getResourcePath(request);
        response.setPath(path);
        // location
        response.setLocation(externalizePath(request, path));
        // parent location
        path = ResourceUtil.getParent(path);
        if (path != null) {
            response.setParentLocation(externalizePath(request, path));
        }
        final List<Modification> changes = new ArrayList<>();
        doRun(request, response, changes);
        // invoke processors
        if (processors != null) {
            for (SlingPostProcessor processor : processors) {
                processor.process(request, changes);
            }
        }
        // check modifications for remaining postfix and store the base path
        final Map<String, String> modificationSourcesContainingPostfix = new HashMap<>();
        final Set<String> allModificationSources = new HashSet<>(changes.size());
        for (final Modification modification : changes) {
            final String source = modification.getSource();
            if (source != null) {
                allModificationSources.add(source);
                final int atIndex = source.indexOf('@');
                if (atIndex > 0) {
                    modificationSourcesContainingPostfix.put(source.substring(0, atIndex), source);
                }
            }
        }
        // fail if any of the base paths (before the postfix) which had a postfix are contained in the modification set
        if (modificationSourcesContainingPostfix.size() > 0) {
            for (final Map.Entry<String, String> sourceToCheck : modificationSourcesContainingPostfix.entrySet()) {
                if (allModificationSources.contains(sourceToCheck.getKey())) {
                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Postfix-containing path " + sourceToCheck.getValue() + " contained in the modification list. Check configuration.");
                    return;
                }
            }
        }
        final Set<String> nodesToCheckin = new LinkedHashSet<>();
        // set changes on html response
        for (Modification change : changes) {
            switch(change.getType()) {
                case MODIFY:
                    response.onModified(change.getSource());
                    break;
                case DELETE:
                    response.onDeleted(change.getSource());
                    break;
                case MOVE:
                    response.onMoved(change.getSource(), change.getDestination());
                    break;
                case COPY:
                    response.onCopied(change.getSource(), change.getDestination());
                    break;
                case CREATE:
                    response.onCreated(change.getSource());
                    if (versionableConfiguration.isCheckinOnNewVersionableNode()) {
                        nodesToCheckin.add(change.getSource());
                    }
                    break;
                case ORDER:
                    response.onChange("ordered", change.getSource(), change.getDestination());
                    break;
                case CHECKOUT:
                    response.onChange("checkout", change.getSource());
                    nodesToCheckin.add(change.getSource());
                    break;
                case CHECKIN:
                    response.onChange("checkin", change.getSource());
                    nodesToCheckin.remove(change.getSource());
                    break;
                case RESTORE:
                    response.onChange("restore", change.getSource());
                    break;
            }
        }
        if (isResourceResolverCommitRequired(request)) {
            request.getResourceResolver().commit();
        }
        if (!isSkipCheckin(request)) {
            // now do the checkins
            for (String checkinPath : nodesToCheckin) {
                if (this.jcrSsupport.checkin(request.getResourceResolver().getResource(checkinPath))) {
                    response.onChange("checkin", checkinPath);
                }
            }
        }
    } catch (Exception e) {
        log.error("Exception during response processing.", e);
        response.setError(e);
    } finally {
        if (isResourceResolverCommitRequired(request)) {
            request.getResourceResolver().revert();
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Modification(org.apache.sling.servlets.post.Modification) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VersioningConfiguration(org.apache.sling.servlets.post.VersioningConfiguration) NoSuchElementException(java.util.NoSuchElementException) PersistenceException(org.apache.sling.api.resource.PersistenceException) SlingPostProcessor(org.apache.sling.servlets.post.SlingPostProcessor) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

SlingPostProcessor (org.apache.sling.servlets.post.SlingPostProcessor)2 VersioningConfiguration (org.apache.sling.servlets.post.VersioningConfiguration)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 ServletException (javax.servlet.ServletException)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)1 Modification (org.apache.sling.servlets.post.Modification)1 PostOperation (org.apache.sling.servlets.post.PostOperation)1 PostResponse (org.apache.sling.servlets.post.PostResponse)1