LoopbackConfig.java 2.69 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.commons.Externalizer
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.ServiceReference
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dam.s7imaging.impl.gfx;

import com.day.cq.commons.Externalizer;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoopbackConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoopbackConfig.class);
    private static final String EXTERNALIZER_SELF = "self";

    public static URL createLoopbackUrl(ComponentContext ctx, Externalizer externalizer) {
        try {
            String selfDomain = StringUtils.stripEnd((String)externalizer.externalLink(null, "self", ""), (String)"/");
            try {
                return new URL(selfDomain);
            }
            catch (MalformedURLException e) {
                LOGGER.warn("unable to configure loopback url, self domain is malformed: " + selfDomain, (Throwable)e);
            }
        }
        catch (IllegalArgumentException e) {
            // empty catch block
        }
        ServiceReference ref = ctx.getBundleContext().getServiceReference("org.osgi.service.http.HttpService");
        if (ref == null) {
            LOGGER.warn("service org.osgi.service.http.HttpService not found");
            return null;
        }
        String[] endPoints = PropertiesUtil.toStringArray((Object)ref.getProperty("osgi.http.service.endpoints"), (String[])new String[0]);
        if (endPoints.length <= 0) {
            LOGGER.warn("service org.osgi.service.http.HttpService does not expose any endpoints");
            return null;
        }
        try {
            URL endPoint = new URL(endPoints[0]);
            int port = endPoint.getPort();
            String path = StringUtils.stripEnd((String)endPoint.getPath(), (String)"/");
            return new URL("http", "localhost", port, path);
        }
        catch (MalformedURLException e) {
            LOGGER.warn("unable to configure loopback url, endpoint is malformed: " + endPoints[0], (Throwable)e);
            return null;
        }
    }

    private LoopbackConfig() {
    }
}