OmniSearchSavedSearchServlet.java 14.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.i18n.I18n
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.apache.sling.servlets.post.HtmlResponse
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.omnisearch.impl.servlets;

import com.adobe.granite.omnisearch.api.core.OmniSearchException;
import com.adobe.granite.omnisearch.api.core.OmniSearchService;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.servlets.post.HtmlResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(immediate=1, metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/granite/omnisearch/savedsearch"})})
public class OmniSearchSavedSearchServlet
extends SlingAllMethodsServlet {
    private static final Logger log = LoggerFactory.getLogger(OmniSearchSavedSearchServlet.class);
    static final String COMMAND = ":operation";
    static final String LIST_COMMAND = "list";
    static final String DETAIL_COMMAND = "detail";
    static final String UPDATE_COMMAND = "update";
    static final String CREATE_COMMAND = "create";
    static final String DELETE_COMMAND = "delete";
    static final String LIMIT = "limit";
    static final String OFFSET = "offset";
    static final String RESOURCE_PATH = "resourcePath";
    @Reference
    private OmniSearchService omnisearchService;

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        if (this.checkRequireParameter(request, response)) {
            String location = request.getParameter("location");
            String command = request.getParameter(":operation");
            long limit = 0;
            if (request.getParameter("limit") != null) {
                limit = Long.valueOf(request.getParameter("limit"));
            }
            long offset = 0;
            if (request.getParameter("offset") != null) {
                offset = Long.valueOf(request.getParameter("offset"));
            }
            if (command.equals("list")) {
                Writer out = this.getJsonOutputWriter(response);
                try {
                    Iterator<Resource> savedSearchList = this.omnisearchService.getSavedSearches(request.getResourceResolver(), location, limit, offset);
                    out.write(this.getJsonSavedSearches(savedSearchList, request.getResourceResolver()).toString());
                }
                catch (RepositoryException re) {
                    log.error("Error while accessing resource path of saved search", (Throwable)re);
                    this.createSatusResponse(500, I18n.get((HttpServletRequest)request, (String)"Error while accessing saved search list"), "", response);
                }
                catch (JSONException je) {
                    log.error("Error while updating json object ", (Throwable)je);
                    this.createSatusResponse(500, I18n.get((HttpServletRequest)request, (String)"Error while accessing saved search list"), "", response);
                }
                catch (OmniSearchException se) {
                    log.error("Error while extracting list of saved search for location {}", (Object)location, (Object)se);
                    this.createSatusResponse(500, I18n.getVar((HttpServletRequest)request, (String)se.getMessage()), "", response);
                }
            } else if (command.equals("detail")) {
                Resource resource;
                String resourcePath = request.getParameter("resourcePath");
                if (resourcePath == null) {
                    this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Resource path can not be empty"), "", response);
                }
                if ((resource = request.getResourceResolver().getResource(resourcePath)) == null) {
                    this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Error while extracting the detail of saved search"), "", response);
                }
                ResourceResolver resolver = request.getResourceResolver();
                Writer out = this.getJsonOutputWriter(response);
                try {
                    Map<String, String> savedSearchDetail = this.omnisearchService.getSavedSearchParameters(resolver, location, resourcePath);
                    if (savedSearchDetail.isEmpty()) {
                        this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"No Saved Search exist on the given path {0}", (String)resourcePath), resourcePath, response);
                    }
                    JSONObject resultObj = this.getJsonSavedSearchDetails(savedSearchDetail);
                    Session session = (Session)resolver.adaptTo(Session.class);
                    boolean canEdit = false;
                    if (session.hasPermission(resourcePath, "set_property")) {
                        canEdit = true;
                    }
                    resultObj.put("canEdit", canEdit);
                    out.write(resultObj.toString());
                }
                catch (RepositoryException re) {
                    log.error("Error while accessing resource path of saved search", (Throwable)re);
                    this.createSatusResponse(500, I18n.get((HttpServletRequest)request, (String)"Error while access resource Path {0}", (String)resourcePath), resourcePath, response);
                }
                catch (JSONException je) {
                    log.error("Error while updating json object ", (Throwable)je);
                    this.createSatusResponse(500, I18n.get((HttpServletRequest)request, (String)"Error while access resource Path {0}", (String)resourcePath), resourcePath, response);
                }
                catch (OmniSearchException se) {
                    log.error("Error while extracting detail of saved search", (Throwable)se);
                    this.createSatusResponse(500, I18n.getVar((HttpServletRequest)request, (String)se.getMessage()), "", response);
                }
            } else {
                this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"operation {0} does not exist", (String)command), "", response);
            }
        }
    }

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        block14 : {
            if (this.checkRequireParameter(request, response)) {
                String command = request.getParameter(":operation");
                String resourcePath = request.getParameter("resourcePath");
                Resource resource = null;
                if (!command.equals("create")) {
                    if (resourcePath == null) {
                        this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Resource path can not be empty"), "", response);
                    }
                    if ((resource = request.getResourceResolver().getResource(resourcePath)) == null) {
                        this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Error while extracting the deleting of saved search"), "", response);
                        return;
                    }
                }
                if (command.equals("update") || command.equals("create")) {
                    try {
                        resource = this.omnisearchService.createOrUpdateSavedSearch(request.getResourceResolver(), request.getParameterMap());
                        if (resource != null) {
                            this.createSatusResponse(200, I18n.get((HttpServletRequest)request, (String)"Saved Search Added"), resource.getPath(), response);
                            break block14;
                        }
                        this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Error while adding the saved search"), resourcePath, response);
                    }
                    catch (OmniSearchException se) {
                        log.error("Error while creating or updating saved search", (Throwable)se);
                        this.createSatusResponse(500, I18n.getVar((HttpServletRequest)request, (String)se.getMessage()), resourcePath, response);
                    }
                } else if (command.equals("delete")) {
                    String location = request.getParameter("location");
                    try {
                        boolean result = this.omnisearchService.deleteSavedSearch(request.getResourceResolver(), location, resourcePath);
                        if (!result) break block14;
                        if (result) {
                            this.createSatusResponse(200, I18n.get((HttpServletRequest)request, (String)"Saved Search Deleted"), resourcePath, response);
                            break block14;
                        }
                        this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Error while deleting the saved search"), resourcePath, response);
                    }
                    catch (OmniSearchException se) {
                        log.error("Error while deleting saved search", (Throwable)se);
                        this.createSatusResponse(500, I18n.getVar((HttpServletRequest)request, (String)se.getMessage()), resourcePath, response);
                    }
                } else {
                    this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Operation {0} does not exist", (String)command), "", response);
                }
            }
        }
    }

    private Writer getJsonOutputWriter(SlingHttpServletResponse response) throws IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        return out;
    }

    private JSONArray getJsonSavedSearches(Iterator<Resource> savedSearchResult, ResourceResolver resolver) throws RepositoryException, JSONException {
        Session session = (Session)resolver.adaptTo(Session.class);
        JSONArray resultArray = new JSONArray();
        while (savedSearchResult.hasNext()) {
            Resource savedSearchResource = savedSearchResult.next();
            ValueMap vm = savedSearchResource.getValueMap();
            String resourceTitle = (String)vm.get("jcr:title", String.class);
            String resourcePath = savedSearchResource.getPath();
            boolean canDelete = false;
            if (session.hasPermission(resourcePath, "remove")) {
                canDelete = true;
            }
            JSONObject resourceObj = new JSONObject();
            resourceObj.put("title", (Object)resourceTitle);
            resourceObj.put("path", (Object)resourcePath);
            resourceObj.put("canDelete", canDelete);
            resultArray.put((Object)resourceObj);
        }
        return resultArray;
    }

    private JSONObject getJsonSavedSearchDetails(Map<String, String> savedSearchDetail) throws JSONException {
        JSONObject resultJsonObj = new JSONObject();
        for (String key : savedSearchDetail.keySet()) {
            resultJsonObj.put(key, (Object)savedSearchDetail.get(key));
        }
        return resultJsonObj;
    }

    private boolean checkRequireParameter(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
        if (request.getParameter("location") == null) {
            this.createSatusResponse(400, "Missing Parameter 'location'", "", response);
            return false;
        }
        if (request.getParameter(":operation") == null) {
            this.createSatusResponse(400, "Missing Parameter ':operation'", "", response);
            return false;
        }
        return true;
    }

    private void createSatusResponse(int status, String message, String path, SlingHttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        HtmlResponse htmlResponse = new HtmlResponse();
        if (path != null) {
            htmlResponse.setPath(path);
            htmlResponse.setLocation(path);
        }
        htmlResponse.setStatus(status, message);
        if (status < 400) {
            htmlResponse.setTitle("OK");
        } else {
            htmlResponse.setTitle(" Error");
        }
        htmlResponse.send((HttpServletResponse)response, true);
    }

    protected void bindOmnisearchService(OmniSearchService omniSearchService) {
        this.omnisearchService = omniSearchService;
    }

    protected void unbindOmnisearchService(OmniSearchService omniSearchService) {
        if (this.omnisearchService == omniSearchService) {
            this.omnisearchService = null;
        }
    }
}