AbstractUrlConnection.java 2.76 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.aemds.datamanager.impl;

import com.adobe.aemds.datamanager.impl.UrlUtil;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

abstract class AbstractUrlConnection
extends URLConnection {
    protected String contentType;
    protected String contentLengthStr;

    AbstractUrlConnection(URL url) {
        super(url);
    }

    protected String getResourcePath(String protocol) throws Exception {
        String pre = protocol + ":";
        String urlStr = this.url.toExternalForm();
        if (!urlStr.startsWith(pre)) {
            throw new MalformedURLException("URL " + urlStr + " is not of the form '" + pre + "*'");
        }
        int endIdx = (urlStr = urlStr.substring(pre.length())).indexOf(63);
        return endIdx == -1 ? urlStr : urlStr.substring(0, endIdx);
    }

    protected abstract void doConnect() throws Exception;

    protected abstract String doGetContentType() throws Exception;

    protected abstract long doGetContentLength() throws Exception;

    public void connect() throws IOException {
        if (!this.connected) {
            try {
                this.doConnect();
                this.contentType = this.doGetContentType();
                this.contentLengthStr = String.valueOf(this.doGetContentLength());
            }
            catch (IOException e) {
                throw e;
            }
            catch (Exception e) {
                throw new IOException(UrlUtil.getMessage(e, this.url.toExternalForm(), "CONNECT"), e);
            }
            this.connected = true;
        }
    }

    public String getContentType() {
        try {
            this.connect();
        }
        catch (IOException e) {
            throw new RuntimeException(UrlUtil.getMessage(e, this.url.toExternalForm(), "CONNECT"), e);
        }
        return this.contentType;
    }

    protected abstract InputStream doGetInputStream() throws Exception;

    public InputStream getInputStream() throws IOException {
        this.connect();
        try {
            return this.doGetInputStream();
        }
        catch (IOException e) {
            throw e;
        }
        catch (Exception e) {
            throw new IOException(UrlUtil.getMessage(e, this.url.toExternalForm(), "OPEN_STREAM"), e);
        }
    }

    public String getHeaderField(String name) {
        if ("content-length".equals(name)) {
            try {
                this.connect();
            }
            catch (IOException e) {
                throw new RuntimeException(UrlUtil.getMessage(e, this.url.toExternalForm(), "CONNECT"), e);
            }
            return this.contentLengthStr;
        }
        return super.getHeaderField(name);
    }
}