MainServlet.java 8.88 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.license.ProductInfoService
 *  com.adobe.granite.xss.XSSAPI
 *  javax.servlet.GenericServlet
 *  javax.servlet.Servlet
 *  javax.servlet.ServletException
 *  javax.servlet.ServletOutputStream
 *  javax.servlet.ServletRequest
 *  javax.servlet.ServletResponse
 *  javax.servlet.http.HttpServletRequest
 *  javax.servlet.http.HttpServletResponse
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  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.commons.classloader.DynamicClassLoaderManager
 *  org.apache.sling.commons.mime.MimeTypeService
 *  org.apache.sling.installer.api.info.InfoProvider
 *  org.osgi.framework.Bundle
 *  org.osgi.framework.BundleContext
 *  org.osgi.service.component.ComponentContext
 */
package com.day.crx.packmgr.impl;

import com.adobe.granite.license.ProductInfoService;
import com.adobe.granite.xss.XSSAPI;
import com.day.crx.packaging.impl.SlingInstallerSupport;
import com.day.crx.packmgr.impl.AbstractServlet;
import com.day.crx.packmgr.impl.servlets.DownloadServlet;
import com.day.crx.packmgr.impl.servlets.GroupsServlet;
import com.day.crx.packmgr.impl.servlets.IndexServlet;
import com.day.crx.packmgr.impl.servlets.InitServlet;
import com.day.crx.packmgr.impl.servlets.InstallStatusServlet;
import com.day.crx.packmgr.impl.servlets.ListServlet;
import com.day.crx.packmgr.impl.servlets.ScreenshotServlet;
import com.day.crx.packmgr.impl.servlets.ServiceServlet;
import com.day.crx.packmgr.impl.servlets.ThumbnailServlet;
import com.day.crx.packmgr.impl.servlets.UpdateServlet;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.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.commons.classloader.DynamicClassLoaderManager;
import org.apache.sling.commons.mime.MimeTypeService;
import org.apache.sling.installer.api.info.InfoProvider;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;

@Component(immediate=1)
@Service(value={Servlet.class})
@Properties(value={@Property(name="alias", value={"/crx/packmgr"}), @Property(name="contextId", value={"com.day.crx.packmgr.impl.AuthHttpContext"})})
public class MainServlet
extends GenericServlet {
    private BundleContext bundleContext;
    @Reference
    private MimeTypeService mimeTypeService;
    @Reference
    private DynamicClassLoaderManager dynLoaderMgr;
    @Reference
    private ProductInfoService productInfoService;
    @Reference
    private InfoProvider infoProvider;
    @Reference
    private XSSAPI xssAPI;
    @Reference
    private SlingInstallerSupport slingInstaller;
    private final Map<String, AbstractServlet> servlets = new HashMap<String, AbstractServlet>();

    @Activate
    protected void activate(ComponentContext ctx) {
        BundleContext bc = this.bundleContext = ctx.getBundleContext();
        this.servlets.put("index", new IndexServlet(bc));
        this.servlets.put("init", new InitServlet(bc));
        this.servlets.put("list", new ListServlet(bc, this.xssAPI));
        this.servlets.put("groups", new GroupsServlet(bc));
        this.servlets.put("thumbnail", new ThumbnailServlet(bc));
        this.servlets.put("screenshot", new ScreenshotServlet(bc));
        this.servlets.put("download", new DownloadServlet(bc));
        this.servlets.put("update", new UpdateServlet(bc, this.productInfoService, this.xssAPI));
        this.servlets.put("installstatus", new InstallStatusServlet(bc, this.infoProvider));
        ServiceServlet serviceServlet = new ServiceServlet(bc, this.slingInstaller);
        serviceServlet.setHookClassLoader(this.dynLoaderMgr.getDynamicClassLoader());
        this.servlets.put("service", serviceServlet);
    }

    @Deactivate
    protected void deactivate() {
        this.servlets.clear();
        this.bundleContext = null;
    }

    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        if (!(req instanceof HttpServletRequest) || !(res instanceof HttpServletResponse)) {
            throw new ServletException("Not a http servlet request");
        }
        this.doService((HttpServletRequest)req, (HttpServletResponse)res);
    }

    private void doService(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        if (pathInfo == null || pathInfo.length() == 0) {
            pathInfo = "/";
        }
        if (pathInfo.endsWith("/")) {
            res.sendRedirect(req.getContextPath() + req.getServletPath() + pathInfo + "index.jsp");
            return;
        }
        if (!pathInfo.endsWith(".jsp")) {
            this.doResource(req, res, pathInfo);
        } else {
            int lastDot = pathInfo.lastIndexOf(46);
            String servletName = pathInfo.substring(1, lastDot);
            AbstractServlet servlet = this.servlets.get(servletName);
            if (servlet != null) {
                servlet.service(req, res);
            } else {
                res.sendError(404);
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private void doResource(HttpServletRequest req, HttpServletResponse res, String pathInfo) throws ServletException, IOException {
        URL url = this.bundleContext.getBundle().getResource("/docroot" + pathInfo);
        if (url == null) {
            res.sendError(404);
        } else {
            String type;
            MimeTypeService mts = this.mimeTypeService;
            String string = type = mts != null ? mts.getMimeType(pathInfo) : null;
            if (type != null) {
                res.setContentType(type);
            }
            InputStream is = url.openStream();
            ServletOutputStream os = res.getOutputStream();
            try {
                int l;
                byte[] buf = new byte[2048];
                while ((l = is.read(buf)) > 0) {
                    os.write(buf, 0, l);
                }
            }
            finally {
                try {
                    is.close();
                }
                catch (IOException ignore) {}
            }
        }
    }

    protected void bindMimeTypeService(MimeTypeService mimeTypeService) {
        this.mimeTypeService = mimeTypeService;
    }

    protected void unbindMimeTypeService(MimeTypeService mimeTypeService) {
        if (this.mimeTypeService == mimeTypeService) {
            this.mimeTypeService = null;
        }
    }

    protected void bindDynLoaderMgr(DynamicClassLoaderManager dynamicClassLoaderManager) {
        this.dynLoaderMgr = dynamicClassLoaderManager;
    }

    protected void unbindDynLoaderMgr(DynamicClassLoaderManager dynamicClassLoaderManager) {
        if (this.dynLoaderMgr == dynamicClassLoaderManager) {
            this.dynLoaderMgr = null;
        }
    }

    protected void bindProductInfoService(ProductInfoService productInfoService) {
        this.productInfoService = productInfoService;
    }

    protected void unbindProductInfoService(ProductInfoService productInfoService) {
        if (this.productInfoService == productInfoService) {
            this.productInfoService = null;
        }
    }

    protected void bindInfoProvider(InfoProvider infoProvider) {
        this.infoProvider = infoProvider;
    }

    protected void unbindInfoProvider(InfoProvider infoProvider) {
        if (this.infoProvider == infoProvider) {
            this.infoProvider = null;
        }
    }

    protected void bindXssAPI(XSSAPI xSSAPI) {
        this.xssAPI = xSSAPI;
    }

    protected void unbindXssAPI(XSSAPI xSSAPI) {
        if (this.xssAPI == xSSAPI) {
            this.xssAPI = null;
        }
    }

    protected void bindSlingInstaller(SlingInstallerSupport slingInstallerSupport) {
        this.slingInstaller = slingInstallerSupport;
    }

    protected void unbindSlingInstaller(SlingInstallerSupport slingInstallerSupport) {
        if (this.slingInstaller == slingInstallerSupport) {
            this.slingInstaller = null;
        }
    }
}