AbstractRequest.java 5.65 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.RepositoryException
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.io.IOUtils
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.servlets.post.Modification
 *  org.apache.sling.servlets.post.ModificationType
 *  org.apache.sling.servlets.post.PostResponse
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.rest.impl.servlet;

import com.adobe.granite.rest.RequestException;
import com.adobe.granite.rest.impl.servlet.RequestHandler;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.apache.sling.servlets.post.PostResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractRequest
implements RequestHandler {
    protected Logger log;

    public AbstractRequest() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void handle(SlingHttpServletRequest request, PostResponse response) {
        try {
            String path = this.getResourcePath(request);
            response.setPath(path);
            response.setLocation(this.externalizePath(request, path));
            path = ResourceUtil.getParent((String)path);
            if (path != null) {
                response.setParentLocation(this.externalizePath(request, path));
            }
            ArrayList<Modification> changes = new ArrayList<Modification>();
            this.doHandle(request, response, changes);
            for (Modification change : changes) {
                switch (change.getType()) {
                    case MODIFY: {
                        response.onModified(change.getSource());
                        break;
                    }
                    case DELETE: {
                        response.onDeleted(change.getSource());
                        break;
                    }
                    case CREATE: {
                        response.onCreated(change.getSource());
                        break;
                    }
                    case COPY: {
                        response.onCopied(change.getSource(), change.getDestination());
                        break;
                    }
                    case MOVE: {
                        response.onMoved(change.getSource(), change.getDestination());
                        break;
                    }
                }
            }
            if (this.isCommitRequired(request)) {
                request.getResourceResolver().commit();
            }
        }
        catch (UnsupportedOperationException e) {
            this.log.error("Exception during request processing: {}", (Object)e.getMessage());
            response.setStatus(403, e.getMessage());
        }
        catch (RequestException e) {
            this.log.error("Exception during request processing: {}", (Object)e.getMessage());
            response.setStatus(e.getStatusCode(), e.getMessage());
        }
        catch (Exception e) {
            this.log.error("Exception during request processing.", (Throwable)e);
            response.setError((Throwable)e);
        }
        finally {
            try {
                if (this.isCommitRequired(request)) {
                    request.getResourceResolver().revert();
                }
            }
            catch (RepositoryException e) {
                this.log.warn("RepositoryException in finally block: {}", (Object)e.getMessage(), (Object)e);
            }
        }
    }

    protected abstract void doHandle(SlingHttpServletRequest var1, PostResponse var2, List<Modification> var3) throws UnsupportedOperationException, RequestException, Exception;

    protected String getResourcePath(SlingHttpServletRequest request) {
        return request.getResource().getPath();
    }

    protected boolean isCommitRequired(SlingHttpServletRequest request) throws RepositoryException {
        return request.getResourceResolver().hasChanges();
    }

    protected final String externalizePath(SlingHttpServletRequest request, String path) {
        StringBuilder ret = new StringBuilder();
        ret.append(request.getResourceResolver().map((HttpServletRequest)request, path));
        ret.append(".json");
        return ret.toString();
    }

    protected boolean isMultipartRequest(SlingHttpServletRequest request) {
        return "multipart/form-data".equals(this.getContentType(request));
    }

    protected String readRequestBodyAsString(InputStream inputStream, String encoding) throws IOException {
        StringWriter writer = new StringWriter();
        IOUtils.copy((InputStream)inputStream, (Writer)writer, (String)encoding);
        return writer.toString();
    }

    protected String getContentType(SlingHttpServletRequest request) {
        if (request.getContentType() == null) {
            return null;
        }
        String[] parts = request.getContentType().split(";");
        return parts[0].trim();
    }

}