导航菜单

页面标题

页面副标题

Rilo v2.0.21 - DefaultResourceRetriever.java 源代码

正在查看: Rilo v2.0.21 应用的 DefaultResourceRetriever.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.nimbusds.jose.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import net.jcip.annotations.ThreadSafe;

@ThreadSafe
public class DefaultResourceRetriever extends AbstractRestrictedResourceRetriever implements RestrictedResourceRetriever {
    private boolean disconnectAfterUse;
    private Proxy proxy;
    private final SSLSocketFactory sslSocketFactory;

    public DefaultResourceRetriever() {
        this(0, 0);
    }

    public DefaultResourceRetriever(int i, int i2) {
        this(i, i2, 0);
    }

    public DefaultResourceRetriever(int i, int i2, int i3) {
        this(i, i2, i3, true);
    }

    public DefaultResourceRetriever(int i, int i2, int i3, boolean z) {
        this(i, i2, i3, z, null);
    }

    public DefaultResourceRetriever(int i, int i2, int i3, boolean z, SSLSocketFactory sSLSocketFactory) {
        super(i, i2, i3);
        this.disconnectAfterUse = z;
        this.sslSocketFactory = sSLSocketFactory;
    }

    public boolean disconnectsAfterUse() {
        return this.disconnectAfterUse;
    }

    public void setDisconnectsAfterUse(boolean z) {
        this.disconnectAfterUse = z;
    }

    public Proxy getProxy() {
        return this.proxy;
    }

    public void setProxy(Proxy proxy) {
        this.proxy = proxy;
    }

    @Override
    public Resource retrieveResource(URL url) throws IOException {
        HttpURLConnection httpURLConnection = null;
        try {
            try {
                HttpURLConnection openConnection = openConnection(url);
                openConnection.setConnectTimeout(getConnectTimeout());
                openConnection.setReadTimeout(getReadTimeout());
                SSLSocketFactory sSLSocketFactory = this.sslSocketFactory;
                if (sSLSocketFactory != null && (openConnection instanceof HttpsURLConnection)) {
                    ((HttpsURLConnection) openConnection).setSSLSocketFactory(sSLSocketFactory);
                }
                if (getHeaders() != null && !getHeaders().isEmpty()) {
                    for (Map.Entry<String, List<String>> entry : getHeaders().entrySet()) {
                        Iterator<String> it = entry.getValue().iterator();
                        while (it.hasNext()) {
                            openConnection.addRequestProperty(entry.getKey(), it.next());
                        }
                    }
                }
                InputStream inputStream = getInputStream(openConnection, getSizeLimit());
                try {
                    String readInputStreamToString = IOUtils.readInputStreamToString(inputStream, StandardCharset.UTF_8);
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    int responseCode = openConnection.getResponseCode();
                    String responseMessage = openConnection.getResponseMessage();
                    if (responseCode > 299 || responseCode < 200) {
                        throw new IOException("HTTP " + responseCode + ": " + responseMessage);
                    }
                    Resource resource = new Resource(readInputStreamToString, openConnection.getContentType());
                    if (this.disconnectAfterUse && openConnection != null) {
                        openConnection.disconnect();
                    }
                    return resource;
                } catch (Throwable th) {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (Throwable th2) {
                            th.addSuppressed(th2);
                        }
                    }
                    throw th;
                }
            } catch (ClassCastException e) {
                throw new IOException("Couldn't open HTTP(S) connection: " + e.getMessage(), e);
            }
        } catch (Throwable th3) {
            if (this.disconnectAfterUse && 0 != 0) {
                httpURLConnection.disconnect();
            }
            throw th3;
        }
    }

    protected HttpURLConnection openConnection(URL url) throws IOException {
        Proxy proxy = this.proxy;
        if (proxy != null) {
            return (HttpURLConnection) url.openConnection(proxy);
        }
        return (HttpURLConnection) url.openConnection();
    }

    private InputStream getInputStream(HttpURLConnection httpURLConnection, int i) throws IOException {
        InputStream inputStream = httpURLConnection.getInputStream();
        return i > 0 ? new BoundedInputStream(inputStream, getSizeLimit()) : inputStream;
    }
}