NodeServlet.java 4.16 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.Property
 *  javax.jcr.PropertyIterator
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.jcr.security.AccessControlException
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.osgi.framework.BundleContext
 *  org.slf4j.Logger
 */
package com.day.crx.delite.impl.servlets;

import com.day.crx.delite.impl.AbstractServlet;
import com.day.crx.delite.impl.support.RequestData;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.security.AccessControlException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;

public class NodeServlet
extends AbstractServlet {
    public NodeServlet(BundleContext bc) {
        super(bc);
    }

    protected void doService(HttpServletRequest req, HttpServletResponse res, Session session) throws ServletException, IOException {
        if (req.getMethod().equals("GET")) {
            this.doGet(req, res, session);
            return;
        }
        res.sendError(405);
    }

    private void doGet(HttpServletRequest request, HttpServletResponse res, Session session) throws ServletException, IOException {
        RequestData req = new RequestData(request);
        String id = req.getParameter("identifier");
        if (StringUtils.isBlank((CharSequence)id)) {
            res.sendError(404);
            return;
        }
        res.setContentType("application/json");
        res.setCharacterEncoding("UTF-8");
        PrintWriter pw = res.getWriter();
        JSONWriter writer = new JSONWriter((Writer)pw);
        try {
            Node node = session.getNodeByIdentifier(id);
            new Printer(writer).print(node);
        }
        catch (PathNotFoundException e) {
            res.sendError(404);
        }
        catch (AccessControlException e) {
            res.sendError(403);
        }
        catch (RepositoryException e) {
            this.logger.error("Error occur getting node by id: " + id, (Throwable)e);
            res.sendError(500);
            res.getWriter().write(e.getMessage());
        }
        catch (JSONException e) {
            this.logger.error("Error occur getting node by id: " + id, (Throwable)e);
            res.sendError(500);
            res.getWriter().write(e.getMessage());
        }
    }

    private class Printer {
        private JSONWriter writer;

        public Printer(JSONWriter writer) {
            this.writer = writer;
        }

        public void print(Node node) throws JSONException, RepositoryException {
            this.writer.object();
            this.writer.key(":path").value((Object)node.getPath());
            this.writer.key(":identifier").value((Object)node.getIdentifier());
            PropertyIterator it = node.getProperties();
            while (it.hasNext()) {
                Property p = it.nextProperty();
                if (p.getType() == 2) continue;
                if (p.isMultiple()) {
                    Value[] values = p.getValues();
                    this.writer.key(p.getName()).array();
                    for (Value v : values) {
                        this.writer.value((Object)v.getString());
                    }
                    this.writer.endArray();
                    continue;
                }
                this.writer.key(p.getName()).value((Object)p.getString());
            }
            this.writer.endObject();
        }
    }

}