ImporterResource.java 7.59 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.io.IOUtils
 *  org.apache.http.HttpEntity
 *  org.apache.http.StatusLine
 *  org.apache.http.client.methods.CloseableHttpResponse
 *  org.apache.http.client.methods.HttpGet
 *  org.apache.http.client.methods.HttpUriRequest
 *  org.apache.http.impl.client.CloseableHttpClient
 */
package com.day.cq.wcm.siteimporter.internal.resource;

import com.day.cq.wcm.siteimporter.ImporterContext;
import com.day.cq.wcm.siteimporter.internal.resource.ImporterResourceVisitor;
import com.day.cq.wcm.siteimporter.internal.resource.ReferenceLocation;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;

public abstract class ImporterResource {
    protected URL location;
    protected String content;
    protected String contentType;
    protected String downloadLocation;
    protected ArrayList<ReferenceLocation> references;
    protected ImporterContext ctx;
    public static final LineProcessor NULL_LINE_PROCESSOR = new LineProcessor(){

        @Override
        public String onLine(String line) {
            return line;
        }
    };

    public ImporterResource(URL location, ImporterContext ctx) {
        this.location = location;
        this.downloadLocation = "";
        this.content = "";
        this.ctx = ctx;
    }

    public ImporterContext getContext() {
        return this.ctx;
    }

    public boolean isDownloaded() {
        return !"".equals(this.downloadLocation);
    }

    public String getDownloadLocation() {
        return this.downloadLocation;
    }

    public void setDownloadLocation(String downloadLocation) {
        this.downloadLocation = downloadLocation;
    }

    public URL getLocation() {
        return this.location;
    }

    public String getMimeType() throws IOException {
        String mimeType = this.getContentType();
        if (mimeType.contains(";")) {
            for (String s : mimeType.split(";")) {
                if (s.contains("=")) continue;
                mimeType = s.trim();
                break;
            }
        }
        return mimeType;
    }

    public String getCharset() throws IOException {
        String charset = "UTF-8";
        String contentType = this.getContentType();
        if (contentType.contains("charset=")) {
            for (String s : contentType.split(";")) {
                if (!s.contains("charset=")) continue;
                charset = s.trim().split("=")[1];
                break;
            }
        }
        return charset;
    }

    public String getBasePath() {
        return this.getLocation().getPath();
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public InputStream getDownloadStream() throws IOException {
        String fragment;
        HttpGet get = null;
        CloseableHttpClient client = this.getContext().getHttpClient();
        try {
            StringBuilder url = new StringBuilder(this.location.toExternalForm());
            fragment = "";
            int anchorIndex = url.indexOf("#");
            if (anchorIndex > 0) {
                fragment = url.substring(anchorIndex);
                url.delete(anchorIndex, url.length());
            }
            URL buildUrl = new URL(url.toString());
            URI uri = null;
            try {
                uri = new URI(buildUrl.getProtocol(), buildUrl.getUserInfo(), buildUrl.getHost(), buildUrl.getPort(), buildUrl.getPath(), buildUrl.getQuery(), buildUrl.getRef());
            }
            catch (URISyntaxException e) {
                InputStream inputStream = null;
                if (get != null) {
                    get.releaseConnection();
                }
                return inputStream;
            }
            String encodedUrl = uri.toASCIIString() + fragment;
            get = new HttpGet(encodedUrl);
            CloseableHttpResponse httpResp = client.execute((HttpUriRequest)get);
            if (httpResp != null && httpResp.getStatusLine().getStatusCode() == 200) {
                byte[] bytes = IOUtils.toByteArray((InputStream)httpResp.getEntity().getContent());
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
                return byteArrayInputStream;
            }
            InputStream bytes = null;
            return bytes;
        }
        catch (IllegalArgumentException e) {
            fragment = null;
            return fragment;
        }
        finally {
            if (get != null) {
                get.releaseConnection();
            }
        }
    }

    public abstract ArrayList<ReferenceLocation> getExternalReferences();

    public String getContent() throws IOException {
        return this.getContent(NULL_LINE_PROCESSOR);
    }

    public String getContent(LineProcessor lineProcessor) throws IOException {
        if (!"".equals(this.content)) {
            return this.content;
        }
        byte[] byteArray = new byte[1024];
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream is = this.getDownloadStream();
        if (is != null) {
            int count;
            while ((count = is.read(byteArray, 0, byteArray.length)) > 0) {
                out.write(byteArray, 0, count);
            }
            is.close();
        }
        return this.encodeContent(out);
    }

    String encodeContent(ByteArrayOutputStream out) throws IOException {
        String charset = this.getCharset();
        this.content = charset != null ? new String(out.toByteArray(), charset) : new String(out.toByteArray());
        return this.content;
    }

    public String reload() throws IOException {
        return this.reload(NULL_LINE_PROCESSOR);
    }

    public String reload(LineProcessor lineProcessor) throws IOException {
        this.content = "";
        this.contentType = null;
        return this.getContent(lineProcessor);
    }

    public URL toAbsoluteReference(String original) throws MalformedURLException {
        if (original.startsWith("http://") || original.startsWith("https://")) {
            return new URL(original);
        }
        URL u = new URL(new URL(this.location, this.getBasePath()), original);
        return u;
    }

    public abstract Object triggerAction(ImporterResourceVisitor var1);

    String getContentType() throws IOException {
        if (this.contentType == null) {
            InputStream is;
            URLConnection connection = this.location.openConnection();
            this.contentType = connection.getContentType();
            if (this.contentType == null) {
                this.contentType = URLConnection.guessContentTypeFromName(this.location.getPath());
            }
            if (this.contentType == null && (is = this.getDownloadStream()) != null) {
                this.contentType = URLConnection.guessContentTypeFromStream(is);
            }
            if (this.contentType == null) {
                this.contentType = "application/octet-stream";
            }
        }
        return this.contentType;
    }

    public static interface LineProcessor {
        public String onLine(String var1);
    }

}