SearchPromoteServiceImpl.java 8.56 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.httpclient.HttpClient
 *  org.apache.commons.httpclient.HttpMethod
 *  org.apache.commons.httpclient.methods.GetMethod
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.osgi.OsgiUtil
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.searchpromote.impl;

import com.day.cq.searchpromote.SearchPromoteException;
import com.day.cq.searchpromote.SearchPromoteService;
import com.day.cq.searchpromote.impl.DecompressingMethod;
import com.day.cq.searchpromote.impl.SearchPromoteEnvironment;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.Dictionary;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, immediate=1, label="AEM 6 - Search&Promote Integration Configuration", description="Configuration options for Search&Promote integration")
@Service
public class SearchPromoteServiceImpl
implements SearchPromoteService {
    private final Logger log;
    @Property(value={"https://searchandpromote.omniture.com/px/"}, label="Remote server URI", description="Server URI for template XML requests")
    private static final String SERVER_URI = "cq.searchpromote.configuration.server.uri";
    private static final String DEFAULT_SP_ENVIRONMENT = "stage";
    @Property(value={"stage"}, label="Search&Promote Environment", description="The Search&Promote environment environment to use (stage/live)")
    private static final String SP_ENVIRONMENT = "cq.searchpromote.configuration.environment";
    private SearchPromoteEnvironment environment;
    private String stagedParam;
    private URI serverUri;

    public SearchPromoteServiceImpl() {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.stagedParam = "";
    }

    @Override
    public URI getServerURI() {
        return this.serverUri;
    }

    @Override
    public String getSearchForm(String memberid, String accountnumber) throws SearchPromoteException {
        return this.getSearchForm(memberid, accountnumber, SearchPromoteEnvironment.STAGE);
    }

    @Override
    public String getSearchForm(String memberId, String accountNumber, SearchPromoteEnvironment environment) throws SearchPromoteException {
        String staged = environment.equals((Object)SearchPromoteEnvironment.STAGE) ? "&sp_staged=1" : "";
        return this.executeMethod(new MethodWrapper(new DecompressingMethod((HttpMethod)new GetMethod(this.serverUri.toString() + "/searchform/?sp_id=" + memberId + "-" + accountNumber + "&sp_fn=xml" + staged))){

            @Override
            public String process(int status) throws SearchPromoteException, IOException {
                switch (status) {
                    case 200: {
                        return this.getMethod().getResponseBodyAsString();
                    }
                    case 404: {
                        throw new SearchPromoteException("Credentials invalid");
                    }
                }
                throw new SearchPromoteException("Unexpected status received: " + status);
            }
        });
    }

    @Override
    public String getFacetList(String memberid, String accountnumber) throws SearchPromoteException {
        return this.executeMethod(new MethodWrapper(new DecompressingMethod((HttpMethod)new GetMethod(this.serverUri.toString() + "/design/?sp_id=" + memberid + "-" + accountnumber + "&sp_fn=facets" + this.stagedParam))){

            @Override
            public String process(int status) throws SearchPromoteException, IOException {
                return this.getMethod().getResponseBodyAsString();
            }
        });
    }

    private String executeMethod(MethodWrapper wrapper) throws SearchPromoteException {
        HttpClient client = new HttpClient();
        try {
            int status = client.executeMethod(wrapper.getMethod());
            String string = wrapper.process(status);
            return string;
        }
        catch (UnknownHostException e) {
            this.log.error(e.getMessage(), (Throwable)e);
            throw new SearchPromoteException("Unknown host: " + e.getMessage(), e);
        }
        catch (IOException e) {
            this.log.error(e.getMessage(), (Throwable)e);
            throw new SearchPromoteException(e.getMessage(), e);
        }
        finally {
            wrapper.getMethod().releaseConnection();
        }
    }

    @Activate
    protected void activate(ComponentContext componentContext) {
        this.log.debug("Starting SearchPromoteConfiguration...");
        Dictionary properties = componentContext.getProperties();
        String uri = OsgiUtil.toString(properties.get("cq.searchpromote.configuration.server.uri"), (String)"");
        if (uri.endsWith("/")) {
            uri = uri.substring(0, uri.lastIndexOf("/"));
        }
        try {
            this.serverUri = new URI(uri);
        }
        catch (URISyntaxException e) {
            this.log.error(e.getMessage(), (Throwable)e);
        }
        this.environment = SearchPromoteEnvironment.valueOf(OsgiUtil.toString(properties.get("cq.searchpromote.configuration.environment"), (String)"stage").toUpperCase());
        this.log.debug("Using environment {}", (Object)this.environment);
        if (this.environment.equals((Object)SearchPromoteEnvironment.STAGE)) {
            this.stagedParam = "&sp_staged=1";
        }
        this.log.debug("...SearchPromoteConfiguration started");
    }

    @Deactivate
    protected void deactivate(ComponentContext componentContext) {
        this.log.debug("Stopping SearchPromoteConfiguration...");
        this.log.debug("...SearchPromoteConfiguration stopped");
    }

    @Override
    public String getBannerAreaList(String memberid, String accountnumber) throws SearchPromoteException {
        return this.executeMethod(new MethodWrapper(new DecompressingMethod((HttpMethod)new GetMethod(this.serverUri + "/design/?sp_id=" + memberid + "-" + accountnumber + "&sp_fn=bannerareas" + this.stagedParam))){

            @Override
            public String process(int status) throws SearchPromoteException, IOException {
                return this.getMethod().getResponseBodyAsString();
            }
        });
    }

    @Override
    public String callRemoteIndex(String accountNumber, String remotePassword, boolean full) throws SearchPromoteException {
        String operation = null;
        operation = StringUtils.isNotEmpty((CharSequence)this.stagedParam) ? (full ? "full_staged_index" : "incremental_staged_index") : (full ? "full_index" : "incremental_index");
        StringBuilder uri = new StringBuilder(this.stripToRoot(this.serverUri.toString()));
        uri.append("/search/cgiindex.tk?sp_a=").append(accountNumber).append("&sp_password=").append(remotePassword).append("&sp_operation=").append(operation);
        this.log.debug("Calling remote index from {}", (Object)uri.toString());
        return this.executeMethod(new MethodWrapper(new DecompressingMethod((HttpMethod)new GetMethod(uri.toString()))){

            @Override
            public String process(int status) throws SearchPromoteException, IOException {
                return this.getMethod().getResponseBodyAsString();
            }
        });
    }

    private String stripToRoot(String url) {
        if (url.indexOf(47) != url.lastIndexOf(47)) {
            return url.substring(0, url.lastIndexOf(47));
        }
        return url;
    }

    private abstract class MethodWrapper {
        final HttpMethod method;

        public MethodWrapper(HttpMethod method) {
            this.method = method;
        }

        public HttpMethod getMethod() {
            return this.method;
        }

        public abstract String process(int var1) throws SearchPromoteException, IOException;
    }

}