UrlUtil.java 5.62 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.resourceresolverhelper.ResourceResolverHelper
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.datamanager.impl;

import com.adobe.aemds.datamanager.impl.CRXUrlStreamHandler;
import com.adobe.aemds.datamanager.impl.RepositoryUrlStreamHandler;
import com.adobe.aemds.datamanager.impl.ServiceContainer;
import com.adobe.granite.resourceresolverhelper.ResourceResolverHelper;
import com.adobe.service.DataManagerOperations;
import com.adobe.service.FileDataBuffer;
import com.adobe.service.InvalidSourceException;
import com.adobe.service.ResolverCallbackHelper;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.concurrent.Callable;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UrlUtil {
    private static final Logger logger = LoggerFactory.getLogger(UrlUtil.class);
    private static final URLStreamHandler crxHandler = new CRXUrlStreamHandler();
    private static final URLStreamHandler repositoryHandler = new RepositoryUrlStreamHandler();

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private static void copyFile(InputStream is, String filePath) throws IOException {
        int BUFFER_SIZE = 16384;
        byte[] buffer = new byte[BUFFER_SIZE];
        BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(filePath), BUFFER_SIZE);
        try {
            int len;
            while ((len = is.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
            Object var7_6 = null;
        }
        catch (Throwable var6_8) {
            Object var7_7 = null;
            outStream.close();
            throw var6_8;
        }
        outStream.close();
        {
        }
    }

    private static ResourceResolver getResourceResolver(byte[] context) throws Exception {
        String ticketVal = new String(context);
        return ResolverCallbackHelper.getResolver(ticketVal);
    }

    public static URL toURL(String url) throws IOException {
        int pidx = url.indexOf(58);
        if (pidx == -1) {
            throw new MalformedURLException("No protocol specified in URL: " + url);
        }
        String p = url.substring(0, pidx);
        URLStreamHandler handler = p.equals(CRXUrlStreamHandler.CRX_PROTOCOL) ? crxHandler : (p.equals(RepositoryUrlStreamHandler.REPOSITORY_PROTOCOL) ? repositoryHandler : null);
        return new URL(null, url, handler);
    }

    private static InputStream doOpenUrlStream(String url, StringBuffer ct) throws IOException {
        URL u = UrlUtil.toURL(url);
        URLConnection uc = u.openConnection();
        uc.connect();
        ct.append(uc.getContentType());
        return uc.getInputStream();
    }

    private static InputStream openUrlStream(final String urlStr, byte[] invocationContext, final StringBuffer contentType) throws Exception {
        if (invocationContext != null) {
            ResourceResolver rr = UrlUtil.getResourceResolver(invocationContext);
            InputStream is = (InputStream)ServiceContainer.resourceResolverHelper.callWith(rr, (Callable)new Callable<InputStream>(){

                @Override
                public InputStream call() throws Exception {
                    return UrlUtil.doOpenUrlStream(urlStr, contentType);
                }
            });
            return is;
        }
        return UrlUtil.doOpenUrlStream(urlStr, contentType);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    static FileDataBuffer createFileDataBufferFromUrl(String urlStr, DataManagerOperations dm) throws InvalidSourceException {
        if (urlStr == null) {
            throw new InvalidSourceException("Null URL arg in createFileDataBufferFromUrl");
        }
        try {
            FileDataBuffer fileDataBuffer;
            String filePath = dm.getTempFileName(true);
            StringBuffer contentType = new StringBuffer();
            InputStream is = UrlUtil.openUrlStream(urlStr, dm.getInvocationContext(), contentType);
            try {
                UrlUtil.copyFile(is, filePath);
                FileDataBuffer db = dm.createFileDataBuffer(filePath);
                db.setContentType(contentType.toString());
                fileDataBuffer = db;
                Object var8_8 = null;
                if (is == null) return fileDataBuffer;
            }
            catch (Throwable var7_10) {
                Object var8_9 = null;
                if (is != null) {
                    is.close();
                }
                throw var7_10;
            }
            is.close();
            return fileDataBuffer;
        }
        catch (Throwable e) {
            logger.error("Error creating FileDataBuffer from URL " + urlStr, e);
            throw new InvalidSourceException(UrlUtil.getMessage(e, urlStr, "CREATE_FILE_DATA_BUFFER"));
        }
    }

    static String getMessage(Throwable t, String urlStr, String operation) {
        return "Error of type '" + t.getClass().getName() + "' thrown on attempting operation '" + operation + "' on URL '" + urlStr + "'. Message = [" + t.getMessage() + "]";
    }

}