SearchFormServlet.java 3.86 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletException
 *  org.apache.commons.lang3.StringEscapeUtils
 *  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.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.searchpromote.impl.servlet;

import com.day.cq.searchpromote.SearchPromoteException;
import com.day.cq.searchpromote.SearchPromoteService;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.ServletException;
import org.apache.commons.lang3.StringEscapeUtils;
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.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/cq/searchpromote/searchform"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class SearchFormServlet
extends SlingAllMethodsServlet {
    private final Logger log;
    private static final String PARAM_MEMBERID = "memberid";
    private static final String PARAM_ACCOUNTNO = "accountno";
    @Reference
    private SearchPromoteService searchpromote;

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

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        String memberId = request.getParameter("memberid");
        String accountNo = request.getParameter("accountno");
        PrintWriter out = response.getWriter();
        JSONWriter writer = new JSONWriter((Writer)out);
        try {
            String searchformXml = this.searchpromote.getSearchForm(memberId, accountNo);
            if (searchformXml.endsWith("html>")) {
                this.log.warn("Search form XML could not be found for {}-{}", (Object)memberId, (Object)accountNo);
                throw new SearchPromoteException("Credentials invalid");
            }
            writer.object();
            writer.key("xml").value((Object)StringEscapeUtils.escapeXml((String)searchformXml));
            writer.endObject();
        }
        catch (Exception e) {
            this.log.error(e.getMessage(), (Throwable)e);
            try {
                writer.object();
                writer.key("error").value((Object)e.getMessage());
                writer.endObject();
            }
            catch (JSONException ignored) {
                this.log.error(ignored.getMessage(), (Throwable)ignored);
            }
        }
        finally {
            out.flush();
        }
    }

    protected void bindSearchpromote(SearchPromoteService searchPromoteService) {
        this.searchpromote = searchPromoteService;
    }

    protected void unbindSearchpromote(SearchPromoteService searchPromoteService) {
        if (this.searchpromote == searchPromoteService) {
            this.searchpromote = null;
        }
    }
}