package mage.client.remote; import mage.client.dialog.PreferencesDialog; import mage.remote.Connection; import mage.util.DebugUtil; import mage.utils.MageVersion; import org.apache.log4j.Logger; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Map; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.ReentrantLock; /** * Network: proxy class to set up and use network connections like URLConnection *
* It used stream logic for data access *
* For text: * - download text data by XmageURLConnection.downloadText *
* For binary data (e.g. file download): * - get stream by XmageURLConnection.downloadBinary * - save stream to file or process *
* TODO: no needs in POST requests (support only GET), but can be added later for another third party APIs
*
* @author JayDi85
*/
public class XmageURLConnection {
private static final MageVersion version = new MageVersion(XmageURLConnection.class);
private static final Logger logger = Logger.getLogger(XmageURLConnection.class);
private static final int CONNECTION_STARTING_TIMEOUT_MS = 10000;
private static final int CONNECTION_READING_TIMEOUT_MS = 60000;
private static final AtomicLong debugLastRequestTimeMs = new AtomicLong(0);
private static final ReentrantLock debugLogsWriterlock = new ReentrantLock();
static {
// add Authority Information Access (AIA) Extension support for certificates from Windows servers like gatherer website
// fix download errors like sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
System.setProperty("com.sun.security.enableAIAcaIssuers", "true");
}
final String url;
Proxy proxy = null;
HttpURLConnection connection = null;
HttpLoggingType loggingType = HttpLoggingType.ERRORS;
boolean forceGZipEncoding = false;
public XmageURLConnection(String url) {
this.url = url;
}
// example: 404 Not Found xxx
enum HttpLoggingType {
NONE,
ERRORS,
ALL
}
/**
* Add additional headers like non standard user agent, etc
*/
public void setRequestHeaders(Map
* Warning, result depends on Accept-Encoding, so use it for information only
*/
public int getContentLength() {
makeSureConnectionStarted();
return this.connection.getContentLength();
}
/**
* Get http status message from a web server like Not Found
*/
public String getResponseMessage() {
makeSureConnectionStarted();
try {
return this.connection.getResponseMessage();
} catch (IOException ignore) {
return "";
}
}
/**
* Get returned html from a web server
*/
public String getErrorResponseAsString() {
makeSureConnectionStarted();
java.util.Scanner s = new java.util.Scanner(this.connection.getErrorStream()).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
/**
* Get non-text data as stream, e.g. binary file content
*/
public InputStream getGoodResponseAsStream() throws IOException {
makeSureConnectionStarted();
return this.connection.getInputStream();
}
/**
* Get text data as string, e.g. html document
*/
public String getGoodResponseAsString() {
makeSureConnectionStarted();
StringBuffer tmp = new StringBuffer();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(this.getGoodResponseAsStream()));
String line;
while ((line = in.readLine()) != null) {
tmp.append(line);
}
} catch (IOException e) {
throw new RuntimeException("Network: can't get text data from " + this.url + " - " + e, e);
}
return String.valueOf(tmp);
}
private void makeSureConnectionStarted() {
if (!isConnected()) {
throw new IllegalArgumentException("Wrong code usage: must call startConnection first", new Throwable());
}
}
public static String downloadText(String resourceUrl) {
return downloadText(resourceUrl, null);
}
/**
* Fast download of text data
*
* @param additionalHeaders set extra headers like application/json
*
* @return downloaded text on OK 200 response or empty on any other errors
*/
public static String downloadText(String resourceUrl, Map