2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.client;
|
2017-11-18 08:27:25 +01:00
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* This file is a part of Fedilab
|
2017-11-18 08:27:25 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-11-18 08:27:25 +01:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
2017-11-18 08:27:25 +01:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2018-12-29 19:21:39 +01:00
|
|
|
|
2019-05-24 15:13:28 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2017-11-18 09:30:58 +01:00
|
|
|
import android.content.Context;
|
2017-11-18 11:25:04 +01:00
|
|
|
import android.content.SharedPreferences;
|
2017-11-18 09:30:58 +01:00
|
|
|
import android.os.Build;
|
2017-11-20 16:06:31 +01:00
|
|
|
import android.text.Html;
|
|
|
|
import android.text.SpannableString;
|
2018-09-05 10:19:07 +02:00
|
|
|
|
2019-03-24 16:38:12 +01:00
|
|
|
import com.google.gson.JsonObject;
|
2018-12-29 19:21:39 +01:00
|
|
|
|
2019-07-16 15:25:23 +02:00
|
|
|
import net.gotev.uploadservice.MultipartUploadRequest;
|
|
|
|
import net.gotev.uploadservice.ServerResponse;
|
|
|
|
import net.gotev.uploadservice.UploadInfo;
|
|
|
|
import net.gotev.uploadservice.UploadNotificationConfig;
|
|
|
|
import net.gotev.uploadservice.UploadStatusDelegate;
|
|
|
|
|
|
|
|
import org.apache.poi.util.IOUtils;
|
2017-11-18 09:44:54 +01:00
|
|
|
import org.json.JSONObject;
|
2018-12-29 19:21:39 +01:00
|
|
|
|
2017-11-18 09:30:58 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
2017-11-18 08:27:25 +01:00
|
|
|
import java.io.IOException;
|
2017-11-18 09:30:58 +01:00
|
|
|
import java.io.InputStream;
|
2018-04-22 18:02:00 +02:00
|
|
|
import java.io.OutputStream;
|
2018-01-19 18:59:30 +01:00
|
|
|
import java.net.Authenticator;
|
2017-11-18 12:22:41 +01:00
|
|
|
import java.net.HttpURLConnection;
|
2018-01-19 18:59:30 +01:00
|
|
|
import java.net.InetSocketAddress;
|
2019-07-16 15:25:23 +02:00
|
|
|
import java.net.MalformedURLException;
|
2018-01-19 18:59:30 +01:00
|
|
|
import java.net.PasswordAuthentication;
|
|
|
|
import java.net.Proxy;
|
2017-11-18 08:27:25 +01:00
|
|
|
import java.net.URL;
|
2019-11-15 16:32:25 +01:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2017-11-18 08:27:25 +01:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2019-11-27 15:05:54 +01:00
|
|
|
import java.util.Objects;
|
2018-08-17 18:55:38 +02:00
|
|
|
import java.util.Scanner;
|
2019-11-27 15:05:54 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-11-18 08:27:25 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2018-12-29 19:21:39 +01:00
|
|
|
|
2019-05-24 15:13:28 +02:00
|
|
|
import javax.net.ssl.HostnameVerifier;
|
2017-11-18 08:27:25 +01:00
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
2019-05-24 15:13:28 +02:00
|
|
|
import javax.net.ssl.SSLSession;
|
2018-12-29 19:21:39 +01:00
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.R;
|
2020-01-18 18:23:09 +01:00
|
|
|
import app.fedilab.android.activities.MainActivity;
|
2019-10-12 16:12:23 +02:00
|
|
|
import app.fedilab.android.activities.SlideMediaActivity;
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.activities.TootActivity;
|
|
|
|
import app.fedilab.android.client.Entities.Error;
|
|
|
|
import app.fedilab.android.helper.FileNameCleaner;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.interfaces.OnDownloadInterface;
|
2019-12-02 18:53:33 +01:00
|
|
|
import okhttp3.Cache;
|
2019-11-27 15:05:54 +01:00
|
|
|
import okhttp3.HttpUrl;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.Response;
|
2017-11-18 09:30:58 +01:00
|
|
|
|
2019-11-29 15:26:49 +01:00
|
|
|
import static app.fedilab.android.helper.Helper.urlPattern;
|
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 17/11/2017.
|
|
|
|
* Manage http queries
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class HttpsConnection {
|
|
|
|
|
|
|
|
|
|
|
|
private HttpsURLConnection httpsURLConnection;
|
2018-01-24 15:56:33 +01:00
|
|
|
private HttpURLConnection httpURLConnection;
|
2017-11-18 08:27:25 +01:00
|
|
|
private String since_id, max_id;
|
2017-11-18 09:30:58 +01:00
|
|
|
private Context context;
|
2017-11-22 07:51:12 +01:00
|
|
|
private int CHUNK_SIZE = 4096;
|
2018-01-19 18:59:30 +01:00
|
|
|
private SharedPreferences sharedpreferences;
|
|
|
|
private Proxy proxy;
|
2019-05-22 19:13:21 +02:00
|
|
|
private String instance;
|
2019-11-29 18:02:49 +01:00
|
|
|
private String USER_AGENT;
|
2019-12-02 18:53:33 +01:00
|
|
|
private int cacheSize = 30*1024*1024;
|
2017-11-18 09:30:58 +01:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
public HttpsConnection(Context context, String instance) {
|
|
|
|
this.instance = instance;
|
2017-11-18 09:30:58 +01:00
|
|
|
this.context = context;
|
2018-01-19 18:59:30 +01:00
|
|
|
sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
boolean proxyEnabled = sharedpreferences.getBoolean(Helper.SET_PROXY_ENABLED, false);
|
2018-01-20 09:46:28 +01:00
|
|
|
int type = sharedpreferences.getInt(Helper.SET_PROXY_TYPE, 0);
|
2018-01-19 18:59:30 +01:00
|
|
|
proxy = null;
|
2019-11-29 18:02:49 +01:00
|
|
|
|
|
|
|
USER_AGENT = sharedpreferences.getString(Helper.SET_CUSTOM_USER_AGENT, Helper.USER_AGENT);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (proxyEnabled) {
|
2018-08-30 17:48:32 +02:00
|
|
|
try {
|
|
|
|
String host = sharedpreferences.getString(Helper.SET_PROXY_HOST, "127.0.0.1");
|
|
|
|
int port = sharedpreferences.getInt(Helper.SET_PROXY_PORT, 8118);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (type == 0)
|
2018-08-30 17:48:32 +02:00
|
|
|
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
|
|
|
|
else
|
|
|
|
proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(host, port));
|
|
|
|
final String login = sharedpreferences.getString(Helper.SET_PROXY_LOGIN, null);
|
|
|
|
final String pwd = sharedpreferences.getString(Helper.SET_PROXY_PASSWORD, null);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (login != null) {
|
2018-08-30 17:48:32 +02:00
|
|
|
Authenticator authenticator = new Authenticator() {
|
|
|
|
public PasswordAuthentication getPasswordAuthentication() {
|
|
|
|
assert pwd != null;
|
|
|
|
return (new PasswordAuthentication(login,
|
|
|
|
pwd.toCharArray()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Authenticator.setDefault(authenticator);
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
2018-08-30 17:48:32 +02:00
|
|
|
proxy = null;
|
2018-01-19 18:59:30 +01:00
|
|
|
}
|
2018-08-30 17:48:32 +02:00
|
|
|
|
2018-01-19 18:59:30 +01:00
|
|
|
}
|
2019-05-24 15:13:28 +02:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
if (instance != null && instance.endsWith(".onion")) {
|
2019-05-24 15:13:28 +02:00
|
|
|
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
|
|
|
|
@SuppressLint("BadHostnameVerifier")
|
|
|
|
@Override
|
|
|
|
public boolean verify(String arg0, SSLSession arg1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-11-18 09:30:58 +01:00
|
|
|
}
|
2017-11-18 08:27:25 +01:00
|
|
|
|
2017-12-10 18:37:58 +01:00
|
|
|
|
2019-11-27 15:05:54 +01:00
|
|
|
/**
|
|
|
|
* Get calls
|
|
|
|
* @param urlConnection String url
|
|
|
|
* @param timeout int timeout
|
|
|
|
* @param paramaters HashMap<String, String> paramaters
|
|
|
|
* @param token String token
|
|
|
|
* @return String
|
|
|
|
* @throws IOException
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
* @throws KeyManagementException
|
|
|
|
* @throws HttpsConnectionException
|
|
|
|
*/
|
2017-11-18 08:27:25 +01:00
|
|
|
public String get(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
|
|
|
|
2019-11-28 07:21:28 +01:00
|
|
|
|
|
|
|
Map<String, Object> params = new LinkedHashMap<>();
|
|
|
|
if (paramaters != null) {
|
|
|
|
Iterator it = paramaters.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry pair = (Map.Entry) it.next();
|
|
|
|
params.put(pair.getKey().toString(), pair.getValue());
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StringBuilder postData = new StringBuilder();
|
2019-12-08 17:49:13 +01:00
|
|
|
URL url;
|
|
|
|
if( params.size() > 0 ) {
|
|
|
|
for (Map.Entry<String, Object> param : params.entrySet()) {
|
|
|
|
if (postData.length() != 0) postData.append('&');
|
|
|
|
postData.append(param.getKey());
|
|
|
|
postData.append('=');
|
|
|
|
postData.append(param.getValue());
|
|
|
|
}
|
|
|
|
url = new URL(urlConnection + "?" + postData);
|
|
|
|
}else{
|
|
|
|
url = new URL(urlConnection);
|
2019-11-28 07:21:28 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 15:05:54 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
2019-12-15 17:33:15 +01:00
|
|
|
Cache cache = new Cache(context.getCacheDir(), cacheSize);
|
|
|
|
OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(timeout, TimeUnit.SECONDS).cache(cache);
|
2019-11-27 15:05:54 +01:00
|
|
|
if (proxy != null) {
|
|
|
|
builder.proxy(proxy);
|
|
|
|
}
|
|
|
|
OkHttpClient client = builder.build();
|
|
|
|
Request.Builder requestBuilder = new Request.Builder()
|
|
|
|
.url(urlConnection);
|
2019-11-28 07:21:28 +01:00
|
|
|
HttpUrl.Builder httpBuider = Objects.requireNonNull(HttpUrl.parse(url.toString())).newBuilder();
|
2019-11-27 15:05:54 +01:00
|
|
|
if (token != null && !token.startsWith("Basic ")) {
|
|
|
|
requestBuilder.addHeader("Authorization", "Bearer " + token);
|
|
|
|
} else if (token != null && token.startsWith("Basic ")) {
|
|
|
|
requestBuilder.addHeader("Authorization", token);
|
|
|
|
}
|
|
|
|
Request requesthttp = requestBuilder
|
2018-10-26 16:06:31 +02:00
|
|
|
|
2019-11-27 15:05:54 +01:00
|
|
|
.url(httpBuider.build())
|
|
|
|
.build();
|
2020-01-18 18:23:09 +01:00
|
|
|
int code = 500;
|
2019-12-17 16:37:21 +01:00
|
|
|
try {
|
|
|
|
Response httpresponse = client.newCall(requesthttp).execute();
|
2019-11-27 15:05:54 +01:00
|
|
|
assert httpresponse.body() != null;
|
|
|
|
String response = httpresponse.body().string();
|
2020-01-18 18:23:09 +01:00
|
|
|
code = httpresponse.code();
|
2019-11-27 15:05:54 +01:00
|
|
|
String error = httpresponse.message();
|
|
|
|
if (code >= 200 && code < 400) {
|
2019-12-17 18:45:29 +01:00
|
|
|
if (!cache.isClosed()) {
|
|
|
|
try {
|
|
|
|
cache.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2019-11-29 07:30:11 +01:00
|
|
|
getOKHttpHeader(httpresponse.headers().toMultimap());
|
2019-11-27 15:05:54 +01:00
|
|
|
return response;
|
|
|
|
} else {
|
|
|
|
throw new HttpsConnectionException(code, error);
|
|
|
|
}
|
2019-12-17 16:37:21 +01:00
|
|
|
} catch (Exception ignored){}
|
|
|
|
finally {
|
2019-12-15 17:33:15 +01:00
|
|
|
if (!cache.isClosed()) {
|
|
|
|
try {
|
|
|
|
cache.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 15:05:54 +01:00
|
|
|
}
|
2020-01-18 18:23:09 +01:00
|
|
|
throw new HttpsConnectionException(code, context.getString(R.string.toast_error));
|
2019-11-27 15:05:54 +01:00
|
|
|
}else{
|
2019-11-28 07:21:28 +01:00
|
|
|
|
2018-01-24 16:27:52 +01:00
|
|
|
if (proxy != null)
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
|
|
|
httpsURLConnection.setConnectTimeout(timeout * 1000);
|
|
|
|
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
|
2019-11-29 18:02:49 +01:00
|
|
|
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2019-04-16 17:07:51 +02:00
|
|
|
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
httpsURLConnection.setRequestProperty("Accept", "application/json");
|
2019-05-22 19:13:21 +02:00
|
|
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
|
2019-02-02 19:47:21 +01:00
|
|
|
if (token != null && !token.startsWith("Basic "))
|
2018-01-24 16:27:52 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-02-02 19:47:21 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", token);
|
2018-01-24 16:27:52 +01:00
|
|
|
httpsURLConnection.setRequestMethod("GET");
|
|
|
|
String response;
|
|
|
|
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
|
2018-09-02 10:28:45 +02:00
|
|
|
response = converToString(httpsURLConnection.getInputStream());
|
2018-01-24 16:27:52 +01:00
|
|
|
} else {
|
2018-04-22 13:16:52 +02:00
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpsURLConnection.getErrorStream() != null) {
|
2018-08-17 18:55:38 +02:00
|
|
|
InputStream stream = httpsURLConnection.getErrorStream();
|
|
|
|
if (stream == null) {
|
|
|
|
stream = httpsURLConnection.getInputStream();
|
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
2018-01-24 16:27:52 +01:00
|
|
|
int responseCode = httpsURLConnection.getResponseCode();
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-01-24 16:27:52 +01:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
|
|
|
getSinceMaxId();
|
2018-01-13 16:59:10 +01:00
|
|
|
httpsURLConnection.getInputStream().close();
|
2018-01-24 16:27:52 +01:00
|
|
|
return response;
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-29 15:53:46 +01:00
|
|
|
/**
|
|
|
|
* Will check if the current url is redirecting
|
|
|
|
* @param urlConnection String the url to check
|
|
|
|
* @return String null|string url redirection
|
|
|
|
*/
|
2019-11-29 15:26:49 +01:00
|
|
|
public String checkUrl(String urlConnection){
|
|
|
|
URL url;
|
|
|
|
String redirect = null;
|
|
|
|
try {
|
|
|
|
url = new URL(urlConnection);
|
|
|
|
if (proxy != null)
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
|
|
|
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
|
|
|
|
httpsURLConnection.setInstanceFollowRedirects(false);
|
|
|
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
|
2019-11-29 15:53:46 +01:00
|
|
|
httpsURLConnection.setRequestMethod("HEAD");
|
2019-11-29 15:26:49 +01:00
|
|
|
if( httpsURLConnection.getResponseCode() == 301) {
|
|
|
|
Map<String, List<String>> map = httpsURLConnection.getHeaderFields();
|
|
|
|
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
|
2019-11-29 15:53:46 +01:00
|
|
|
if (entry.toString().toLowerCase().startsWith("location")) {
|
2019-11-29 15:26:49 +01:00
|
|
|
Matcher matcher = urlPattern.matcher(entry.toString());
|
|
|
|
if (matcher.find()) {
|
|
|
|
redirect = matcher.group(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-12-04 11:39:49 +01:00
|
|
|
if (redirect != null && redirect.compareTo(urlConnection)!=0){
|
|
|
|
URL redirectURL = new URL(redirect);
|
|
|
|
String host = redirectURL.getHost();
|
|
|
|
String protocol = redirectURL.getProtocol();
|
|
|
|
if( protocol == null || host == null){
|
|
|
|
redirect = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return redirect;
|
2019-11-29 15:26:49 +01:00
|
|
|
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-10 18:37:58 +01:00
|
|
|
public String get(String urlConnection) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
2019-11-27 15:05:54 +01:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
2019-12-15 17:34:34 +01:00
|
|
|
Cache cache = new Cache(context.getCacheDir(), cacheSize);
|
|
|
|
OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS).cache(cache);
|
2019-11-27 15:05:54 +01:00
|
|
|
if (proxy != null) {
|
|
|
|
builder.proxy(proxy);
|
|
|
|
}
|
2019-12-08 18:48:52 +01:00
|
|
|
if( !urlConnection.startsWith("http")){
|
|
|
|
urlConnection = "http://" + urlConnection;
|
|
|
|
}
|
2019-11-27 15:05:54 +01:00
|
|
|
OkHttpClient client = builder.build();
|
|
|
|
Request.Builder requestBuilder = new Request.Builder()
|
|
|
|
.url(urlConnection);
|
|
|
|
HttpUrl.Builder httpBuider = Objects.requireNonNull(HttpUrl.parse(urlConnection)).newBuilder();
|
|
|
|
Request requesthttp = requestBuilder
|
|
|
|
|
|
|
|
.url(httpBuider.build())
|
|
|
|
.build();
|
|
|
|
try (Response httpresponse = client.newCall(requesthttp).execute()) {
|
|
|
|
assert httpresponse.body() != null;
|
|
|
|
String response = httpresponse.body().string();
|
|
|
|
int code = httpresponse.code();
|
|
|
|
String error = httpresponse.message();
|
|
|
|
if (code >= 200 && code < 400) {
|
2019-11-29 07:30:11 +01:00
|
|
|
getOKHttpHeader(httpresponse.headers().toMultimap());
|
2019-11-27 15:05:54 +01:00
|
|
|
return response;
|
|
|
|
} else {
|
|
|
|
throw new HttpsConnectionException(code, error);
|
|
|
|
}
|
2019-12-15 17:34:34 +01:00
|
|
|
}finally {
|
|
|
|
if (!cache.isClosed()) {
|
|
|
|
try {
|
|
|
|
cache.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 15:05:54 +01:00
|
|
|
}
|
2019-12-08 17:49:13 +01:00
|
|
|
|
2019-11-27 15:05:54 +01:00
|
|
|
} else {
|
2017-12-10 18:37:58 +01:00
|
|
|
URL url = new URL(urlConnection);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (proxy != null)
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
2018-01-19 18:59:30 +01:00
|
|
|
else
|
2019-09-06 17:55:14 +02:00
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
2017-12-10 18:37:58 +01:00
|
|
|
httpsURLConnection.setConnectTimeout(30 * 1000);
|
|
|
|
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
|
2019-04-16 17:07:51 +02:00
|
|
|
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
httpsURLConnection.setRequestProperty("Accept", "application/json");
|
2017-12-10 18:37:58 +01:00
|
|
|
httpsURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
|
2019-05-22 19:13:21 +02:00
|
|
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
|
2017-12-10 18:37:58 +01:00
|
|
|
httpsURLConnection.setRequestMethod("GET");
|
2018-01-13 16:59:10 +01:00
|
|
|
String response;
|
2017-12-10 18:37:58 +01:00
|
|
|
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
|
2018-01-13 16:59:10 +01:00
|
|
|
getSinceMaxId();
|
2018-09-02 10:28:45 +02:00
|
|
|
response = converToString(httpsURLConnection.getInputStream());
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-04-22 13:16:52 +02:00
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpsURLConnection.getErrorStream() != null) {
|
2018-08-17 18:55:38 +02:00
|
|
|
InputStream stream = httpsURLConnection.getErrorStream();
|
|
|
|
if (stream == null) {
|
|
|
|
stream = httpsURLConnection.getInputStream();
|
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
2018-01-13 16:59:10 +01:00
|
|
|
int responseCode = httpsURLConnection.getResponseCode();
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-01-13 16:59:10 +01:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
2017-12-10 18:37:58 +01:00
|
|
|
}
|
2018-01-13 16:59:10 +01:00
|
|
|
getSinceMaxId();
|
|
|
|
httpsURLConnection.getInputStream().close();
|
|
|
|
return response;
|
2017-12-10 18:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
public String post(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (urlConnection.startsWith("https://")) {
|
2018-01-24 15:56:33 +01:00
|
|
|
URL url = new URL(urlConnection);
|
|
|
|
Map<String, Object> params = new LinkedHashMap<>();
|
|
|
|
if (paramaters != null) {
|
|
|
|
Iterator it = paramaters.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry pair = (Map.Entry) it.next();
|
|
|
|
params.put(pair.getKey().toString(), pair.getValue());
|
|
|
|
it.remove();
|
|
|
|
}
|
2017-11-18 14:10:53 +01:00
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
StringBuilder postData = new StringBuilder();
|
|
|
|
for (Map.Entry<String, Object> param : params.entrySet()) {
|
|
|
|
if (postData.length() != 0) postData.append('&');
|
|
|
|
postData.append(param.getKey());
|
|
|
|
postData.append('=');
|
2019-11-15 16:32:25 +01:00
|
|
|
postData.append(param.getValue());
|
2018-01-24 15:56:33 +01:00
|
|
|
}
|
2019-11-15 16:32:25 +01:00
|
|
|
byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8);
|
2018-01-24 15:56:33 +01:00
|
|
|
if (proxy != null)
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.setConnectTimeout(timeout * 1000);
|
|
|
|
httpsURLConnection.setDoOutput(true);
|
2019-05-22 19:13:21 +02:00
|
|
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.setRequestMethod("POST");
|
2019-02-03 18:29:34 +01:00
|
|
|
if (token != null && !token.startsWith("Basic "))
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-02-03 18:29:34 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", token);
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
2017-11-18 14:10:53 +01:00
|
|
|
|
|
|
|
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.getOutputStream().write(postDataBytes);
|
|
|
|
String response;
|
|
|
|
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
|
|
|
|
getSinceMaxId();
|
2018-09-02 10:28:45 +02:00
|
|
|
response = converToString(httpsURLConnection.getInputStream());
|
2018-01-24 15:56:33 +01:00
|
|
|
} else {
|
2018-04-22 13:16:52 +02:00
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpsURLConnection.getErrorStream() != null) {
|
2018-08-17 18:55:38 +02:00
|
|
|
InputStream stream = httpsURLConnection.getErrorStream();
|
|
|
|
if (stream == null) {
|
|
|
|
stream = httpsURLConnection.getInputStream();
|
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
int responseCode = httpsURLConnection.getResponseCode();
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
2018-01-13 16:59:10 +01:00
|
|
|
getSinceMaxId();
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2018-01-24 15:56:33 +01:00
|
|
|
return response;
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-01-24 15:56:33 +01:00
|
|
|
URL url = new URL(urlConnection);
|
|
|
|
Map<String, Object> params = new LinkedHashMap<>();
|
|
|
|
if (paramaters != null) {
|
|
|
|
Iterator it = paramaters.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry pair = (Map.Entry) it.next();
|
|
|
|
params.put(pair.getKey().toString(), pair.getValue());
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StringBuilder postData = new StringBuilder();
|
|
|
|
for (Map.Entry<String, Object> param : params.entrySet()) {
|
|
|
|
if (postData.length() != 0) postData.append('&');
|
|
|
|
postData.append(param.getKey());
|
|
|
|
postData.append('=');
|
2019-11-15 16:32:25 +01:00
|
|
|
postData.append(param.getValue());
|
2018-01-24 15:56:33 +01:00
|
|
|
}
|
2019-11-15 16:32:25 +01:00
|
|
|
byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8);
|
2018-01-24 15:56:33 +01:00
|
|
|
|
2019-03-24 16:38:12 +01:00
|
|
|
if (proxy != null)
|
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2019-03-24 16:38:12 +01:00
|
|
|
httpURLConnection.setConnectTimeout(timeout * 1000);
|
|
|
|
httpURLConnection.setDoOutput(true);
|
|
|
|
httpURLConnection.setRequestMethod("POST");
|
|
|
|
if (token != null && !token.startsWith("Basic "))
|
|
|
|
httpURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-03-24 16:38:12 +01:00
|
|
|
httpURLConnection.setRequestProperty("Authorization", token);
|
|
|
|
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
|
|
|
|
|
|
|
httpURLConnection.getOutputStream().write(postDataBytes);
|
|
|
|
String response;
|
|
|
|
if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) {
|
|
|
|
getSinceMaxId();
|
|
|
|
response = converToString(httpURLConnection.getInputStream());
|
|
|
|
} else {
|
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpURLConnection.getErrorStream() != null) {
|
2019-03-24 16:38:12 +01:00
|
|
|
InputStream stream = httpURLConnection.getErrorStream();
|
|
|
|
if (stream == null) {
|
|
|
|
stream = httpURLConnection.getInputStream();
|
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-03-24 16:38:12 +01:00
|
|
|
}
|
|
|
|
int responseCode = httpURLConnection.getResponseCode();
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2019-03-24 16:38:12 +01:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
|
|
|
getSinceMaxId();
|
|
|
|
httpURLConnection.getInputStream().close();
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String postJson(String urlConnection, int timeout, JsonObject jsonObject, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (urlConnection.startsWith("https://")) {
|
2019-03-24 16:38:12 +01:00
|
|
|
URL url = new URL(urlConnection);
|
|
|
|
byte[] postDataBytes = new byte[0];
|
2019-11-15 16:32:25 +01:00
|
|
|
postDataBytes = jsonObject.toString().getBytes(StandardCharsets.UTF_8);
|
2019-03-24 16:38:12 +01:00
|
|
|
if (proxy != null)
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2019-03-24 16:38:12 +01:00
|
|
|
httpsURLConnection.setConnectTimeout(timeout * 1000);
|
|
|
|
httpsURLConnection.setDoOutput(true);
|
2019-05-22 19:13:21 +02:00
|
|
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
|
2019-03-24 16:38:12 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
httpsURLConnection.setRequestProperty("Accept", "application/json");
|
|
|
|
httpsURLConnection.setRequestMethod("POST");
|
|
|
|
if (token != null && !token.startsWith("Basic "))
|
|
|
|
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-03-24 16:38:12 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", token);
|
|
|
|
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
|
|
|
|
|
|
|
|
|
|
|
httpsURLConnection.getOutputStream().write(postDataBytes);
|
|
|
|
String response;
|
|
|
|
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
|
|
|
|
getSinceMaxId();
|
|
|
|
response = converToString(httpsURLConnection.getInputStream());
|
|
|
|
} else {
|
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpsURLConnection.getErrorStream() != null) {
|
2019-03-24 16:38:12 +01:00
|
|
|
InputStream stream = httpsURLConnection.getErrorStream();
|
|
|
|
if (stream == null) {
|
|
|
|
stream = httpsURLConnection.getInputStream();
|
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2019-03-24 16:38:12 +01:00
|
|
|
}
|
|
|
|
int responseCode = httpsURLConnection.getResponseCode();
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2019-03-24 16:38:12 +01:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
|
|
|
getSinceMaxId();
|
|
|
|
httpsURLConnection.getInputStream().close();
|
|
|
|
return response;
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2019-03-24 16:38:12 +01:00
|
|
|
URL url = new URL(urlConnection);
|
2019-11-15 16:32:25 +01:00
|
|
|
byte[] postDataBytes = jsonObject.toString().getBytes(StandardCharsets.UTF_8);
|
2019-03-24 16:38:12 +01:00
|
|
|
|
2018-01-24 15:56:33 +01:00
|
|
|
if (proxy != null)
|
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2018-01-24 15:56:33 +01:00
|
|
|
httpURLConnection.setConnectTimeout(timeout * 1000);
|
|
|
|
httpURLConnection.setDoOutput(true);
|
|
|
|
httpURLConnection.setRequestMethod("POST");
|
2019-02-03 18:29:34 +01:00
|
|
|
if (token != null && !token.startsWith("Basic "))
|
2018-01-24 15:56:33 +01:00
|
|
|
httpURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-02-03 18:29:34 +01:00
|
|
|
httpURLConnection.setRequestProperty("Authorization", token);
|
2018-01-24 15:56:33 +01:00
|
|
|
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
|
|
|
|
|
|
|
httpURLConnection.getOutputStream().write(postDataBytes);
|
|
|
|
String response;
|
|
|
|
if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) {
|
|
|
|
getSinceMaxId();
|
2018-09-07 07:58:20 +02:00
|
|
|
response = converToString(httpURLConnection.getInputStream());
|
2018-01-24 15:56:33 +01:00
|
|
|
} else {
|
2018-04-22 13:16:52 +02:00
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpURLConnection.getErrorStream() != null) {
|
2018-09-07 07:58:20 +02:00
|
|
|
InputStream stream = httpURLConnection.getErrorStream();
|
2018-08-17 18:55:38 +02:00
|
|
|
if (stream == null) {
|
2018-09-07 07:58:20 +02:00
|
|
|
stream = httpURLConnection.getInputStream();
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
int responseCode = httpURLConnection.getResponseCode();
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
|
|
|
getSinceMaxId();
|
|
|
|
httpURLConnection.getInputStream().close();
|
|
|
|
return response;
|
2018-01-13 16:59:10 +01:00
|
|
|
}
|
2017-11-18 08:27:25 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-29 19:21:39 +01:00
|
|
|
public String postMisskey(String urlConnection, int timeout, JSONObject paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
|
|
|
URL url = new URL(urlConnection);
|
2019-11-15 16:32:25 +01:00
|
|
|
byte[] postDataBytes = paramaters.toString().getBytes(StandardCharsets.UTF_8);
|
2018-12-29 19:21:39 +01:00
|
|
|
|
|
|
|
if (proxy != null)
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2018-12-29 19:21:39 +01:00
|
|
|
httpsURLConnection.setConnectTimeout(timeout * 1000);
|
|
|
|
httpsURLConnection.setDoOutput(true);
|
2019-05-22 19:13:21 +02:00
|
|
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
|
2018-12-29 19:21:39 +01:00
|
|
|
httpsURLConnection.setRequestMethod("POST");
|
2019-02-03 18:29:34 +01:00
|
|
|
if (token != null && !token.startsWith("Basic "))
|
2018-12-29 19:21:39 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-02-03 18:29:34 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", token);
|
2018-12-29 19:21:39 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
|
|
|
|
|
|
|
|
|
|
|
httpsURLConnection.getOutputStream().write(postDataBytes);
|
|
|
|
String response;
|
|
|
|
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
|
|
|
|
getSinceMaxId();
|
|
|
|
response = converToString(httpsURLConnection.getInputStream());
|
|
|
|
} else {
|
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpsURLConnection.getErrorStream() != null) {
|
2018-12-29 19:21:39 +01:00
|
|
|
InputStream stream = httpsURLConnection.getErrorStream();
|
|
|
|
if (stream == null) {
|
|
|
|
stream = httpsURLConnection.getInputStream();
|
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-12-29 19:21:39 +01:00
|
|
|
}
|
|
|
|
int responseCode = httpsURLConnection.getResponseCode();
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-12-29 19:21:39 +01:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
|
|
|
getSinceMaxId();
|
|
|
|
httpsURLConnection.getInputStream().close();
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-27 08:57:07 +01:00
|
|
|
/***
|
|
|
|
* Download method which works for http and https connections
|
|
|
|
* @param downloadUrl String download url
|
|
|
|
* @param listener OnDownloadInterface, listener which manages progress
|
|
|
|
*/
|
2017-11-18 12:22:41 +01:00
|
|
|
public void download(final String downloadUrl, final OnDownloadInterface listener) {
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
URL url;
|
2017-12-27 08:36:55 +01:00
|
|
|
HttpsURLConnection httpsURLConnection = null;
|
|
|
|
HttpURLConnection httpURLConnection = null;
|
|
|
|
if (downloadUrl.startsWith("https://")) {
|
|
|
|
try {
|
|
|
|
url = new URL(downloadUrl);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (proxy != null)
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
2018-01-19 18:59:30 +01:00
|
|
|
else
|
2019-09-06 17:55:14 +02:00
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2017-12-27 08:36:55 +01:00
|
|
|
int responseCode = httpsURLConnection.getResponseCode();
|
|
|
|
|
|
|
|
// always check HTTP response code first
|
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
|
String fileName = "";
|
|
|
|
String disposition = httpsURLConnection.getHeaderField("Content-Disposition");
|
|
|
|
|
|
|
|
if (disposition != null) {
|
|
|
|
// extracts file name from header field
|
|
|
|
int index = disposition.indexOf("filename=");
|
|
|
|
if (index > 0) {
|
|
|
|
fileName = disposition.substring(index + 10,
|
|
|
|
disposition.length() - 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// extracts file name from URL
|
2019-11-15 16:32:25 +01:00
|
|
|
fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1
|
|
|
|
);
|
2017-11-18 12:22:41 +01:00
|
|
|
}
|
2019-04-16 18:32:38 +02:00
|
|
|
fileName = FileNameCleaner.cleanFileName(fileName);
|
2017-12-27 08:36:55 +01:00
|
|
|
// opens input stream from the HTTP connection
|
|
|
|
InputStream inputStream = httpsURLConnection.getInputStream();
|
|
|
|
File saveDir = context.getCacheDir();
|
|
|
|
final String saveFilePath = saveDir + File.separator + fileName;
|
|
|
|
|
|
|
|
// opens an output stream to save into file
|
|
|
|
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
|
|
|
|
|
|
|
|
int bytesRead;
|
|
|
|
byte[] buffer = new byte[CHUNK_SIZE];
|
|
|
|
int contentSize = httpsURLConnection.getContentLength();
|
|
|
|
int downloadedFileSize = 0;
|
|
|
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
|
outputStream.write(buffer, 0, bytesRead);
|
|
|
|
downloadedFileSize += bytesRead;
|
2019-10-12 16:12:23 +02:00
|
|
|
if (context instanceof SlideMediaActivity) {
|
2017-12-27 08:36:55 +01:00
|
|
|
final int currentProgress = (downloadedFileSize * 100) / contentSize;
|
2019-10-12 16:12:23 +02:00
|
|
|
((SlideMediaActivity) context).runOnUiThread(new Runnable() {
|
2017-12-27 08:36:55 +01:00
|
|
|
public void run() {
|
2019-09-06 17:55:14 +02:00
|
|
|
listener.onUpdateProgress(currentProgress > 0 ? currentProgress : 101);
|
2017-12-27 08:36:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outputStream.close();
|
|
|
|
inputStream.close();
|
|
|
|
if (context instanceof TootActivity)
|
|
|
|
((TootActivity) context).runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
listener.onDownloaded(saveFilePath, downloadUrl, null);
|
|
|
|
}
|
|
|
|
});
|
2019-10-12 16:12:23 +02:00
|
|
|
if (context instanceof SlideMediaActivity)
|
|
|
|
((SlideMediaActivity) context).runOnUiThread(new Runnable() {
|
2017-12-27 08:36:55 +01:00
|
|
|
public void run() {
|
|
|
|
listener.onDownloaded(saveFilePath, downloadUrl, null);
|
|
|
|
}
|
|
|
|
});
|
2017-11-18 12:22:41 +01:00
|
|
|
} else {
|
2017-12-27 08:36:55 +01:00
|
|
|
final Error error = new Error();
|
|
|
|
error.setError(String.valueOf(responseCode));
|
|
|
|
if (context instanceof TootActivity)
|
|
|
|
((TootActivity) context).runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
listener.onDownloaded(null, downloadUrl, error);
|
|
|
|
}
|
|
|
|
});
|
2019-10-12 16:12:23 +02:00
|
|
|
if (context instanceof SlideMediaActivity)
|
|
|
|
((SlideMediaActivity) context).runOnUiThread(new Runnable() {
|
2017-11-19 16:32:00 +01:00
|
|
|
public void run() {
|
2017-12-27 08:36:55 +01:00
|
|
|
listener.onDownloaded(null, downloadUrl, error);
|
2017-11-19 16:32:00 +01:00
|
|
|
}
|
|
|
|
});
|
2017-12-27 08:36:55 +01:00
|
|
|
|
2017-11-18 12:22:41 +01:00
|
|
|
}
|
2017-12-27 08:36:55 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
url = new URL(downloadUrl);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (proxy != null)
|
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
|
2018-01-19 18:59:30 +01:00
|
|
|
else
|
2019-09-06 17:55:14 +02:00
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2017-12-27 08:36:55 +01:00
|
|
|
int responseCode = httpURLConnection.getResponseCode();
|
|
|
|
|
|
|
|
// always check HTTP response code first
|
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
|
String fileName = "";
|
|
|
|
String disposition = httpURLConnection.getHeaderField("Content-Disposition");
|
|
|
|
|
|
|
|
if (disposition != null) {
|
|
|
|
// extracts file name from header field
|
|
|
|
int index = disposition.indexOf("filename=");
|
|
|
|
if (index > 0) {
|
|
|
|
fileName = disposition.substring(index + 10,
|
|
|
|
disposition.length() - 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// extracts file name from URL
|
2019-11-15 16:32:25 +01:00
|
|
|
fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1
|
|
|
|
);
|
2017-12-27 08:36:55 +01:00
|
|
|
}
|
2019-04-16 18:32:38 +02:00
|
|
|
fileName = FileNameCleaner.cleanFileName(fileName);
|
2017-12-27 08:36:55 +01:00
|
|
|
// opens input stream from the HTTP connection
|
|
|
|
InputStream inputStream = httpURLConnection.getInputStream();
|
|
|
|
File saveDir = context.getCacheDir();
|
|
|
|
final String saveFilePath = saveDir + File.separator + fileName;
|
|
|
|
|
|
|
|
// opens an output stream to save into file
|
|
|
|
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
|
|
|
|
|
|
|
|
int bytesRead;
|
|
|
|
byte[] buffer = new byte[CHUNK_SIZE];
|
|
|
|
int contentSize = httpURLConnection.getContentLength();
|
|
|
|
int downloadedFileSize = 0;
|
|
|
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
|
outputStream.write(buffer, 0, bytesRead);
|
|
|
|
downloadedFileSize += bytesRead;
|
2019-10-12 16:12:23 +02:00
|
|
|
if (context instanceof SlideMediaActivity) {
|
2017-12-27 08:36:55 +01:00
|
|
|
final int currentProgress = (downloadedFileSize * 100) / contentSize;
|
2019-10-12 16:12:23 +02:00
|
|
|
((SlideMediaActivity) context).runOnUiThread(new Runnable() {
|
2017-12-27 08:36:55 +01:00
|
|
|
public void run() {
|
2019-09-06 17:55:14 +02:00
|
|
|
listener.onUpdateProgress(currentProgress > 0 ? currentProgress : 101);
|
2017-12-27 08:36:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outputStream.close();
|
|
|
|
inputStream.close();
|
|
|
|
if (context instanceof TootActivity)
|
|
|
|
((TootActivity) context).runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
listener.onDownloaded(saveFilePath, downloadUrl, null);
|
|
|
|
}
|
|
|
|
});
|
2019-10-12 16:12:23 +02:00
|
|
|
if (context instanceof SlideMediaActivity)
|
|
|
|
((SlideMediaActivity) context).runOnUiThread(new Runnable() {
|
2017-12-27 08:36:55 +01:00
|
|
|
public void run() {
|
|
|
|
listener.onDownloaded(saveFilePath, downloadUrl, null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
final Error error = new Error();
|
|
|
|
error.setError(String.valueOf(responseCode));
|
|
|
|
if (context instanceof TootActivity)
|
|
|
|
((TootActivity) context).runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
listener.onDownloaded(null, downloadUrl, error);
|
|
|
|
}
|
|
|
|
});
|
2019-10-12 16:12:23 +02:00
|
|
|
if (context instanceof SlideMediaActivity)
|
|
|
|
((SlideMediaActivity) context).runOnUiThread(new Runnable() {
|
2017-12-27 08:36:55 +01:00
|
|
|
public void run() {
|
|
|
|
listener.onDownloaded(null, downloadUrl, error);
|
|
|
|
}
|
|
|
|
});
|
2017-11-18 19:25:00 +01:00
|
|
|
|
2017-12-27 08:36:55 +01:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(context.getString(R.string.toast_error));
|
2017-11-18 12:22:41 +01:00
|
|
|
}
|
|
|
|
|
2017-12-27 08:36:55 +01:00
|
|
|
}
|
2017-11-18 12:22:41 +01:00
|
|
|
}
|
|
|
|
}).start();
|
2017-11-26 19:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 11:09:58 +01:00
|
|
|
public InputStream getPicture(final String downloadUrl) {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (downloadUrl.startsWith("https://")) {
|
2018-01-17 15:22:44 +01:00
|
|
|
try {
|
2018-01-24 15:56:33 +01:00
|
|
|
URL url = new URL(downloadUrl);
|
|
|
|
if (proxy != null)
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
2019-05-22 19:13:21 +02:00
|
|
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
|
2019-11-29 18:02:49 +01:00
|
|
|
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2018-01-24 15:56:33 +01:00
|
|
|
int responseCode = httpsURLConnection.getResponseCode();
|
|
|
|
// always check HTTP response code first
|
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
|
// opens input stream from the HTTP connection
|
2019-12-16 18:44:48 +01:00
|
|
|
return httpsURLConnection.getInputStream();
|
2018-01-24 15:56:33 +01:00
|
|
|
}
|
2018-01-17 15:22:44 +01:00
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-12-16 18:44:48 +01:00
|
|
|
} catch (IOException | NoSuchAlgorithmException | KeyManagementException ignored) {}
|
2018-01-24 15:56:33 +01:00
|
|
|
if (httpsURLConnection != null)
|
|
|
|
try {
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
return null;
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-01-24 15:56:33 +01:00
|
|
|
try {
|
|
|
|
URL url = new URL(downloadUrl);
|
|
|
|
if (proxy != null)
|
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2018-01-24 15:56:33 +01:00
|
|
|
int responseCode = httpURLConnection.getResponseCode();
|
|
|
|
// always check HTTP response code first
|
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
|
// opens input stream from the HTTP connection
|
|
|
|
return httpURLConnection.getInputStream();
|
|
|
|
}
|
|
|
|
httpURLConnection.getInputStream().close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
|
|
|
if (httpURLConnection != null)
|
|
|
|
try {
|
|
|
|
httpURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
return null;
|
|
|
|
}
|
2017-12-13 11:09:58 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
private void uploadMedia(String urlConnection, InputStream avatar, InputStream header, String filename) {
|
2019-07-16 15:25:23 +02:00
|
|
|
UploadNotificationConfig uploadConfig = new UploadNotificationConfig();
|
|
|
|
uploadConfig.getCompleted().autoClear = true;
|
|
|
|
File file = new File(context.getCacheDir() + "/" + filename);
|
|
|
|
OutputStream outputStream;
|
|
|
|
try {
|
|
|
|
outputStream = new FileOutputStream(file);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (avatar != null) {
|
2019-07-16 15:25:23 +02:00
|
|
|
IOUtils.copy(avatar, outputStream);
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2019-07-16 15:25:23 +02:00
|
|
|
IOUtils.copy(header, outputStream);
|
2018-04-22 18:02:00 +02:00
|
|
|
}
|
2019-07-16 15:25:23 +02:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-04-22 18:02:00 +02:00
|
|
|
|
2019-07-16 15:25:23 +02:00
|
|
|
try {
|
|
|
|
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
|
|
|
MultipartUploadRequest m = new MultipartUploadRequest(context, urlConnection)
|
|
|
|
.setMethod("PATCH");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (avatar != null) {
|
|
|
|
m.addFileToUpload(file.getPath(), "avatar");
|
|
|
|
} else {
|
|
|
|
m.addFileToUpload(file.getPath(), "header");
|
|
|
|
}
|
|
|
|
m.addParameter("name", filename)
|
2019-07-16 15:25:23 +02:00
|
|
|
.addHeader("Authorization", "Bearer " + token)
|
|
|
|
.setNotificationConfig(uploadConfig)
|
|
|
|
.setDelegate(new UploadStatusDelegate() {
|
|
|
|
@Override
|
|
|
|
public void onProgress(Context context, UploadInfo uploadInfo) {
|
|
|
|
// your code here
|
2018-04-23 13:42:21 +02:00
|
|
|
}
|
2018-05-10 09:50:19 +02:00
|
|
|
|
2019-07-16 15:25:23 +02:00
|
|
|
@Override
|
|
|
|
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse,
|
|
|
|
Exception exception) {
|
|
|
|
// your code here
|
|
|
|
file.delete();
|
|
|
|
}
|
2018-04-23 13:42:21 +02:00
|
|
|
|
2019-07-16 15:25:23 +02:00
|
|
|
@Override
|
|
|
|
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
|
|
|
|
file.delete();
|
|
|
|
}
|
2018-05-10 09:50:19 +02:00
|
|
|
|
2019-07-16 15:25:23 +02:00
|
|
|
@Override
|
|
|
|
public void onCancelled(Context context, UploadInfo uploadInfo) {
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.startUpload();
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
2018-05-10 09:50:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
@SuppressWarnings("SameParameterValue")
|
|
|
|
public String patch(String urlConnection, int timeout, HashMap<String, String> paramaters, InputStream avatar, String avatarName, InputStream header, String headerName, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
|
|
|
if (urlConnection.startsWith("https://")) {
|
2018-05-10 09:50:19 +02:00
|
|
|
URL url = new URL(urlConnection);
|
|
|
|
Map<String, Object> params = new LinkedHashMap<>();
|
|
|
|
if (paramaters != null) {
|
|
|
|
Iterator it = paramaters.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry pair = (Map.Entry) it.next();
|
|
|
|
params.put(pair.getKey().toString(), pair.getValue());
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StringBuilder postData = new StringBuilder();
|
|
|
|
for (Map.Entry<String, Object> param : params.entrySet()) {
|
|
|
|
if (postData.length() != 0) postData.append('&');
|
|
|
|
postData.append(param.getKey());
|
|
|
|
postData.append('=');
|
2019-11-15 16:32:25 +01:00
|
|
|
postData.append(param.getValue());
|
2018-05-10 09:50:19 +02:00
|
|
|
}
|
2019-11-15 16:32:25 +01:00
|
|
|
byte[] postDataBytes = (postData.toString()).getBytes(StandardCharsets.UTF_8);
|
2018-05-10 09:50:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (proxy != null)
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2018-05-10 09:50:19 +02:00
|
|
|
httpsURLConnection.setConnectTimeout(timeout * 1000);
|
2019-05-22 19:13:21 +02:00
|
|
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
|
2019-09-06 17:55:14 +02:00
|
|
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
|
2018-05-10 09:50:19 +02:00
|
|
|
httpsURLConnection.setRequestMethod("PATCH");
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-05-10 09:50:19 +02:00
|
|
|
httpsURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
|
|
|
|
httpsURLConnection.setRequestMethod("POST");
|
|
|
|
}
|
2019-02-03 18:29:34 +01:00
|
|
|
if (token != null && !token.startsWith("Basic "))
|
2018-05-10 09:50:19 +02:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-02-03 18:29:34 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", token);
|
2018-05-10 09:50:19 +02:00
|
|
|
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
|
|
|
httpsURLConnection.setDoOutput(true);
|
|
|
|
|
2019-06-30 15:24:40 +02:00
|
|
|
String response;
|
2018-05-10 09:50:19 +02:00
|
|
|
OutputStream outputStream = httpsURLConnection.getOutputStream();
|
|
|
|
outputStream.write(postDataBytes);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (avatar != null) {
|
|
|
|
uploadMedia(urlConnection, avatar, null, avatarName);
|
2019-07-16 15:25:23 +02:00
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
if (header != null) {
|
|
|
|
uploadMedia(urlConnection, null, header, headerName);
|
2019-07-16 15:25:23 +02:00
|
|
|
}
|
2018-05-10 09:50:19 +02:00
|
|
|
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
|
2019-06-30 15:24:40 +02:00
|
|
|
response = converToString(httpsURLConnection.getInputStream());
|
2018-05-10 09:50:19 +02:00
|
|
|
} else {
|
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpsURLConnection.getErrorStream() != null) {
|
2018-08-17 18:55:38 +02:00
|
|
|
InputStream stream = httpsURLConnection.getErrorStream();
|
|
|
|
if (stream == null) {
|
|
|
|
stream = httpsURLConnection.getInputStream();
|
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
2018-05-10 09:50:19 +02:00
|
|
|
int responseCode = httpsURLConnection.getResponseCode();
|
|
|
|
try {
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-05-10 09:50:19 +02:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-06-30 15:24:40 +02:00
|
|
|
return response;
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-05-10 09:50:19 +02:00
|
|
|
URL url = new URL(urlConnection);
|
|
|
|
Map<String, Object> params = new LinkedHashMap<>();
|
|
|
|
if (paramaters != null) {
|
|
|
|
Iterator it = paramaters.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry pair = (Map.Entry) it.next();
|
|
|
|
params.put(pair.getKey().toString(), pair.getValue());
|
|
|
|
it.remove();
|
2018-04-23 13:42:21 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-10 09:50:19 +02:00
|
|
|
StringBuilder postData = new StringBuilder();
|
|
|
|
for (Map.Entry<String, Object> param : params.entrySet()) {
|
|
|
|
if (postData.length() != 0) postData.append('&');
|
|
|
|
postData.append(param.getKey());
|
|
|
|
postData.append('=');
|
2019-11-15 16:32:25 +01:00
|
|
|
postData.append(param.getValue());
|
2018-05-10 09:50:19 +02:00
|
|
|
}
|
2019-11-15 16:32:25 +01:00
|
|
|
byte[] postDataBytes = (postData.toString()).getBytes(StandardCharsets.UTF_8);
|
2018-04-23 13:42:21 +02:00
|
|
|
|
2018-05-10 09:50:19 +02:00
|
|
|
if (proxy != null)
|
|
|
|
httpURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpURLConnection = (HttpsURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2018-05-10 09:50:19 +02:00
|
|
|
httpURLConnection.setConnectTimeout(timeout * 1000);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
|
2018-05-10 09:50:19 +02:00
|
|
|
httpURLConnection.setRequestMethod("PATCH");
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-05-10 09:50:19 +02:00
|
|
|
httpURLConnection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
|
|
|
|
httpURLConnection.setRequestMethod("POST");
|
|
|
|
}
|
2019-02-03 18:29:34 +01:00
|
|
|
if (token != null && !token.startsWith("Basic "))
|
2018-05-10 09:50:19 +02:00
|
|
|
httpURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-02-03 18:29:34 +01:00
|
|
|
httpURLConnection.setRequestProperty("Authorization", token);
|
2018-05-10 09:50:19 +02:00
|
|
|
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
|
|
|
httpURLConnection.setDoOutput(true);
|
|
|
|
|
|
|
|
|
|
|
|
OutputStream outputStream = httpURLConnection.getOutputStream();
|
|
|
|
outputStream.write(postDataBytes);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (avatar != null) {
|
|
|
|
uploadMedia(urlConnection, avatar, null, avatarName);
|
2019-07-16 15:25:23 +02:00
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
if (header != null) {
|
|
|
|
uploadMedia(urlConnection, null, header, headerName);
|
2019-07-16 15:25:23 +02:00
|
|
|
}
|
2019-06-30 15:24:40 +02:00
|
|
|
String response;
|
2018-04-22 18:02:00 +02:00
|
|
|
if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) {
|
2019-06-30 15:24:40 +02:00
|
|
|
response = converToString(httpsURLConnection.getInputStream());
|
2018-04-22 18:02:00 +02:00
|
|
|
} else {
|
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpURLConnection.getErrorStream() != null) {
|
2018-09-07 07:58:20 +02:00
|
|
|
InputStream stream = httpURLConnection.getErrorStream();
|
2018-08-17 18:55:38 +02:00
|
|
|
if (stream == null) {
|
2018-09-07 07:58:20 +02:00
|
|
|
stream = httpURLConnection.getInputStream();
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
2018-04-22 18:02:00 +02:00
|
|
|
int responseCode = httpURLConnection.getResponseCode();
|
2018-04-23 13:42:21 +02:00
|
|
|
try {
|
|
|
|
httpURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-04-23 13:42:21 +02:00
|
|
|
|
2018-04-22 18:02:00 +02:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
|
|
|
httpURLConnection.getInputStream().close();
|
2019-06-30 15:24:40 +02:00
|
|
|
return response;
|
2018-04-22 18:02:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
public String put(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (urlConnection.startsWith("https://")) {
|
2018-01-24 15:56:33 +01:00
|
|
|
URL url = new URL(urlConnection);
|
|
|
|
Map<String, Object> params = new LinkedHashMap<>();
|
|
|
|
if (paramaters != null) {
|
|
|
|
Iterator it = paramaters.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry pair = (Map.Entry) it.next();
|
|
|
|
params.put(pair.getKey().toString(), pair.getValue());
|
|
|
|
it.remove();
|
|
|
|
}
|
2017-11-18 14:10:53 +01:00
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
StringBuilder postData = new StringBuilder();
|
|
|
|
for (Map.Entry<String, Object> param : params.entrySet()) {
|
|
|
|
if (postData.length() != 0) postData.append('&');
|
|
|
|
postData.append(param.getKey());
|
|
|
|
postData.append('=');
|
2019-11-15 16:32:25 +01:00
|
|
|
postData.append(param.getValue());
|
2018-01-24 15:56:33 +01:00
|
|
|
}
|
2019-11-15 16:32:25 +01:00
|
|
|
byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8);
|
2017-11-18 08:27:25 +01:00
|
|
|
|
2018-01-24 15:56:33 +01:00
|
|
|
if (proxy != null)
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.setConnectTimeout(timeout * 1000);
|
2019-05-22 19:13:21 +02:00
|
|
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
|
2019-02-03 18:29:34 +01:00
|
|
|
if (token != null && !token.startsWith("Basic "))
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-02-03 18:29:34 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", token);
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
2017-11-18 14:10:53 +01:00
|
|
|
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.setRequestMethod("PUT");
|
|
|
|
httpsURLConnection.setDoInput(true);
|
|
|
|
httpsURLConnection.setDoOutput(true);
|
2017-11-18 14:10:53 +01:00
|
|
|
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.getOutputStream().write(postDataBytes);
|
|
|
|
String response;
|
|
|
|
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
|
|
|
|
getSinceMaxId();
|
2018-09-02 10:28:45 +02:00
|
|
|
response = converToString(httpsURLConnection.getInputStream());
|
2018-01-24 15:56:33 +01:00
|
|
|
} else {
|
2018-04-22 13:16:52 +02:00
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpsURLConnection.getErrorStream() != null) {
|
2018-08-17 18:55:38 +02:00
|
|
|
InputStream stream = httpsURLConnection.getErrorStream();
|
|
|
|
if (stream == null) {
|
|
|
|
stream = httpsURLConnection.getInputStream();
|
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
int responseCode = httpsURLConnection.getResponseCode();
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
2018-01-13 16:59:10 +01:00
|
|
|
getSinceMaxId();
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2018-01-24 15:56:33 +01:00
|
|
|
return response;
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-01-24 15:56:33 +01:00
|
|
|
URL url = new URL(urlConnection);
|
|
|
|
Map<String, Object> params = new LinkedHashMap<>();
|
|
|
|
if (paramaters != null) {
|
|
|
|
Iterator it = paramaters.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry pair = (Map.Entry) it.next();
|
|
|
|
params.put(pair.getKey().toString(), pair.getValue());
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StringBuilder postData = new StringBuilder();
|
|
|
|
for (Map.Entry<String, Object> param : params.entrySet()) {
|
|
|
|
if (postData.length() != 0) postData.append('&');
|
|
|
|
postData.append(param.getKey());
|
|
|
|
postData.append('=');
|
2019-11-15 16:32:25 +01:00
|
|
|
postData.append(param.getValue());
|
2018-01-24 15:56:33 +01:00
|
|
|
}
|
2019-11-15 16:32:25 +01:00
|
|
|
byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8);
|
2018-01-24 15:56:33 +01:00
|
|
|
|
|
|
|
if (proxy != null)
|
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2018-01-24 15:56:33 +01:00
|
|
|
httpURLConnection.setConnectTimeout(timeout * 1000);
|
2019-02-03 18:29:34 +01:00
|
|
|
if (token != null && !token.startsWith("Basic "))
|
2018-01-24 15:56:33 +01:00
|
|
|
httpURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-02-03 18:29:34 +01:00
|
|
|
httpURLConnection.setRequestProperty("Authorization", token);
|
2018-01-24 15:56:33 +01:00
|
|
|
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
|
|
|
|
|
|
|
httpURLConnection.setRequestMethod("PUT");
|
|
|
|
httpURLConnection.setDoInput(true);
|
|
|
|
httpURLConnection.setDoOutput(true);
|
|
|
|
|
|
|
|
httpURLConnection.getOutputStream().write(postDataBytes);
|
|
|
|
String response;
|
|
|
|
if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) {
|
|
|
|
getSinceMaxId();
|
2018-09-07 07:58:20 +02:00
|
|
|
response = converToString(httpURLConnection.getInputStream());
|
2018-01-24 15:56:33 +01:00
|
|
|
} else {
|
2018-04-22 13:16:52 +02:00
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpURLConnection.getErrorStream() != null) {
|
2018-09-07 07:58:20 +02:00
|
|
|
InputStream stream = httpURLConnection.getErrorStream();
|
2018-08-17 18:55:38 +02:00
|
|
|
if (stream == null) {
|
2018-09-07 07:58:20 +02:00
|
|
|
stream = httpURLConnection.getInputStream();
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
int responseCode = httpURLConnection.getResponseCode();
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
|
|
|
getSinceMaxId();
|
|
|
|
httpURLConnection.getInputStream().close();
|
2018-09-02 10:28:45 +02:00
|
|
|
return response;
|
2018-01-13 16:59:10 +01:00
|
|
|
}
|
2017-11-18 08:27:25 +01:00
|
|
|
|
|
|
|
}
|
2019-07-16 15:25:34 +02:00
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
public int delete(String urlConnection, int timeout, HashMap<String, String> paramaters, String token) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (urlConnection.startsWith("https://")) {
|
2018-01-24 15:56:33 +01:00
|
|
|
URL url = new URL(urlConnection);
|
|
|
|
Map<String, Object> params = new LinkedHashMap<>();
|
|
|
|
if (paramaters != null) {
|
|
|
|
Iterator it = paramaters.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry pair = (Map.Entry) it.next();
|
|
|
|
params.put(pair.getKey().toString(), pair.getValue());
|
|
|
|
it.remove();
|
|
|
|
}
|
2017-11-18 14:10:53 +01:00
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
StringBuilder postData = new StringBuilder();
|
|
|
|
for (Map.Entry<String, Object> param : params.entrySet()) {
|
|
|
|
if (postData.length() != 0) postData.append('&');
|
|
|
|
postData.append(param.getKey());
|
|
|
|
postData.append('=');
|
2019-11-15 16:32:25 +01:00
|
|
|
postData.append(param.getValue());
|
2018-01-24 15:56:33 +01:00
|
|
|
}
|
2019-11-15 16:32:25 +01:00
|
|
|
byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8);
|
2017-11-18 08:27:25 +01:00
|
|
|
|
2018-01-24 15:56:33 +01:00
|
|
|
if (proxy != null)
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection(proxy);
|
|
|
|
else
|
|
|
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpsURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2019-05-22 19:13:21 +02:00
|
|
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory(this.instance));
|
2019-02-03 18:29:34 +01:00
|
|
|
if (token != null && !token.startsWith("Basic "))
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-02-03 18:29:34 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", token);
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
httpsURLConnection.setRequestMethod("DELETE");
|
|
|
|
httpsURLConnection.setConnectTimeout(timeout * 1000);
|
|
|
|
httpsURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
2017-11-18 17:53:30 +01:00
|
|
|
|
2018-01-24 15:56:33 +01:00
|
|
|
httpsURLConnection.getOutputStream().write(postDataBytes);
|
|
|
|
if (httpsURLConnection.getResponseCode() >= 200 && httpsURLConnection.getResponseCode() < 400) {
|
|
|
|
getSinceMaxId();
|
|
|
|
httpsURLConnection.getInputStream().close();
|
|
|
|
return httpsURLConnection.getResponseCode();
|
|
|
|
} else {
|
2018-04-22 13:16:52 +02:00
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpsURLConnection.getErrorStream() != null) {
|
2018-08-17 18:55:38 +02:00
|
|
|
InputStream stream = httpsURLConnection.getErrorStream();
|
|
|
|
if (stream == null) {
|
|
|
|
stream = httpsURLConnection.getInputStream();
|
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
int responseCode = httpsURLConnection.getResponseCode();
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-01-24 15:56:33 +01:00
|
|
|
URL url = new URL(urlConnection);
|
2019-09-06 17:55:14 +02:00
|
|
|
Map<String, Object> params = new LinkedHashMap<>();
|
|
|
|
if (paramaters != null) {
|
2018-01-24 15:56:33 +01:00
|
|
|
Iterator it = paramaters.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry pair = (Map.Entry) it.next();
|
|
|
|
params.put(pair.getKey().toString(), pair.getValue());
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StringBuilder postData = new StringBuilder();
|
2019-09-06 17:55:14 +02:00
|
|
|
for (Map.Entry<String, Object> param : params.entrySet()) {
|
2018-01-24 15:56:33 +01:00
|
|
|
if (postData.length() != 0) postData.append('&');
|
|
|
|
postData.append(param.getKey());
|
|
|
|
postData.append('=');
|
2019-11-15 16:32:25 +01:00
|
|
|
postData.append(param.getValue());
|
2018-01-24 15:56:33 +01:00
|
|
|
}
|
2019-11-15 16:32:25 +01:00
|
|
|
byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8);
|
2018-01-24 15:56:33 +01:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
if (proxy != null)
|
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection(proxy);
|
2018-01-24 15:56:33 +01:00
|
|
|
else
|
2019-09-06 17:55:14 +02:00
|
|
|
httpURLConnection = (HttpURLConnection) url.openConnection();
|
2019-11-29 18:02:49 +01:00
|
|
|
httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
|
2019-02-02 19:47:21 +01:00
|
|
|
if (token != null && !token.startsWith("Basic "))
|
|
|
|
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
|
2019-09-06 17:55:14 +02:00
|
|
|
else if (token != null && token.startsWith("Basic "))
|
2019-02-02 19:47:21 +01:00
|
|
|
httpsURLConnection.setRequestProperty("Authorization", token);
|
2018-01-24 15:56:33 +01:00
|
|
|
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
|
|
httpURLConnection.setRequestMethod("DELETE");
|
|
|
|
httpURLConnection.setConnectTimeout(timeout * 1000);
|
|
|
|
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
|
|
|
|
|
|
|
|
httpURLConnection.getOutputStream().write(postDataBytes);
|
|
|
|
|
|
|
|
|
|
|
|
if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) {
|
|
|
|
getSinceMaxId();
|
|
|
|
httpURLConnection.getInputStream().close();
|
|
|
|
return httpURLConnection.getResponseCode();
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-04-22 13:16:52 +02:00
|
|
|
String error = null;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (httpURLConnection.getErrorStream() != null) {
|
2018-09-07 07:58:20 +02:00
|
|
|
InputStream stream = httpURLConnection.getErrorStream();
|
2018-08-17 18:55:38 +02:00
|
|
|
if (stream == null) {
|
2018-09-07 07:58:20 +02:00
|
|
|
stream = httpURLConnection.getInputStream();
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
|
|
|
try (Scanner scanner = new Scanner(stream)) {
|
|
|
|
scanner.useDelimiter("\\Z");
|
2019-09-06 17:55:14 +02:00
|
|
|
if (scanner.hasNext()) {
|
2019-08-17 17:10:24 +02:00
|
|
|
error = scanner.next();
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
int responseCode = httpURLConnection.getResponseCode();
|
2019-08-17 11:10:31 +02:00
|
|
|
try {
|
|
|
|
httpURLConnection.getInputStream().close();
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
throw new HttpsConnectionException(responseCode, error);
|
|
|
|
}
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getSince_id() {
|
|
|
|
return since_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMax_id() {
|
|
|
|
return max_id;
|
|
|
|
}
|
|
|
|
|
2019-11-29 07:30:11 +01:00
|
|
|
private void getOKHttpHeader(Map<String, List<String>> headers){
|
|
|
|
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
|
|
|
|
if (entry.toString().startsWith("Link") || entry.toString().startsWith("link")) {
|
|
|
|
Pattern patternMaxId = Pattern.compile("max_id=([0-9a-zA-Z]{1,}).*");
|
|
|
|
Matcher matcherMaxId = patternMaxId.matcher(entry.toString());
|
|
|
|
if (matcherMaxId.find()) {
|
|
|
|
max_id = matcherMaxId.group(1);
|
|
|
|
}
|
|
|
|
if (entry.toString().startsWith("Link")) {
|
|
|
|
Pattern patternSinceId = Pattern.compile("since_id=([0-9a-zA-Z]{1,}).*");
|
|
|
|
Matcher matcherSinceId = patternSinceId.matcher(entry.toString());
|
|
|
|
if (matcherSinceId.find()) {
|
|
|
|
since_id = matcherSinceId.group(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-12-08 17:49:13 +01:00
|
|
|
}else if (entry.toString().startsWith("Min-Id") || entry.toString().startsWith("min-id")) {
|
|
|
|
Pattern patternMaxId = Pattern.compile("min-id=\\[([0-9a-zA-Z]{1,}).*\\]");
|
|
|
|
Matcher matcherMaxId = patternMaxId.matcher(entry.toString());
|
|
|
|
if (matcherMaxId.find()) {
|
|
|
|
max_id = matcherMaxId.group(1);
|
|
|
|
}
|
2019-11-29 07:30:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
private void getSinceMaxId() {
|
|
|
|
if (Helper.getLiveInstanceWithProtocol(context) == null)
|
2018-01-14 19:03:20 +01:00
|
|
|
return;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (Helper.getLiveInstanceWithProtocol(context).startsWith("https://")) {
|
2018-01-24 15:56:33 +01:00
|
|
|
if (httpsURLConnection == null)
|
|
|
|
return;
|
|
|
|
Map<String, List<String>> map = httpsURLConnection.getHeaderFields();
|
2019-12-08 17:49:13 +01:00
|
|
|
|
2018-01-24 15:56:33 +01:00
|
|
|
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (entry.toString().startsWith("Link") || entry.toString().startsWith("link")) {
|
2019-02-18 14:00:00 +01:00
|
|
|
Pattern patternMaxId = Pattern.compile("max_id=([0-9a-zA-Z]{1,}).*");
|
2018-01-24 15:56:33 +01:00
|
|
|
Matcher matcherMaxId = patternMaxId.matcher(entry.toString());
|
|
|
|
if (matcherMaxId.find()) {
|
|
|
|
max_id = matcherMaxId.group(1);
|
|
|
|
}
|
|
|
|
if (entry.toString().startsWith("Link")) {
|
2019-02-18 14:00:00 +01:00
|
|
|
Pattern patternSinceId = Pattern.compile("since_id=([0-9a-zA-Z]{1,}).*");
|
2018-01-24 15:56:33 +01:00
|
|
|
Matcher matcherSinceId = patternSinceId.matcher(entry.toString());
|
|
|
|
if (matcherSinceId.find()) {
|
|
|
|
since_id = matcherSinceId.group(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-12-08 17:49:13 +01:00
|
|
|
}else if (entry.toString().startsWith("Min-Id") || entry.toString().startsWith("min-id")) {
|
|
|
|
Pattern patternMaxId = Pattern.compile("min-id=\\[([0-9a-zA-Z]{1,}).*\\]");
|
|
|
|
Matcher matcherMaxId = patternMaxId.matcher(entry.toString());
|
|
|
|
if (matcherMaxId.find()) {
|
|
|
|
max_id = matcherMaxId.group(1);
|
|
|
|
}
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-01-24 15:56:33 +01:00
|
|
|
if (httpURLConnection == null)
|
|
|
|
return;
|
|
|
|
Map<String, List<String>> map = httpURLConnection.getHeaderFields();
|
|
|
|
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
|
2018-08-24 17:44:36 +02:00
|
|
|
if (entry.toString().startsWith("Link") || entry.toString().startsWith("link")) {
|
2019-02-18 14:00:00 +01:00
|
|
|
Pattern patternMaxId = Pattern.compile("max_id=([0-9a-zA-Z]{1,}).*");
|
2018-01-24 15:56:33 +01:00
|
|
|
Matcher matcherMaxId = patternMaxId.matcher(entry.toString());
|
|
|
|
if (matcherMaxId.find()) {
|
|
|
|
max_id = matcherMaxId.group(1);
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2018-01-24 15:56:33 +01:00
|
|
|
if (entry.toString().startsWith("Link")) {
|
2019-02-18 14:00:00 +01:00
|
|
|
Pattern patternSinceId = Pattern.compile("since_id=([0-9a-zA-Z]{1,}).*");
|
2018-01-24 15:56:33 +01:00
|
|
|
Matcher matcherSinceId = patternSinceId.matcher(entry.toString());
|
|
|
|
if (matcherSinceId.find()) {
|
|
|
|
since_id = matcherSinceId.group(1);
|
|
|
|
}
|
2017-11-19 07:25:43 +01:00
|
|
|
|
2018-01-24 15:56:33 +01:00
|
|
|
}
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-02 10:28:45 +02:00
|
|
|
private String converToString(InputStream inputStream) throws IOException {
|
2018-09-15 14:05:21 +02:00
|
|
|
java.util.Scanner s = new java.util.Scanner(inputStream).useDelimiter("\\A");
|
|
|
|
return s.hasNext() ? s.next() : "";
|
2018-09-02 10:28:45 +02:00
|
|
|
}
|
|
|
|
|
2017-12-02 14:54:25 +01:00
|
|
|
int getActionCode() {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (Helper.getLiveInstanceWithProtocol(context).startsWith("https://")) {
|
2018-01-24 15:56:33 +01:00
|
|
|
try {
|
|
|
|
return httpsURLConnection.getResponseCode();
|
|
|
|
} catch (IOException e) {
|
|
|
|
return -1;
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-01-24 15:56:33 +01:00
|
|
|
try {
|
|
|
|
return httpURLConnection.getResponseCode();
|
|
|
|
} catch (IOException e) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-11-18 14:10:53 +01:00
|
|
|
}
|
2017-11-18 09:30:58 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
|
|
|
|
enum imageType {
|
|
|
|
AVATAR,
|
|
|
|
BANNER
|
|
|
|
}
|
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
public class HttpsConnectionException extends Exception {
|
|
|
|
|
|
|
|
private int statusCode;
|
|
|
|
private String message;
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
HttpsConnectionException(int statusCode, String message) {
|
|
|
|
this.statusCode = statusCode;
|
2017-11-20 16:06:31 +01:00
|
|
|
SpannableString spannableString;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (message != null) {
|
2018-08-24 15:47:16 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
|
|
spannableString = new SpannableString(Html.fromHtml(message, Html.FROM_HTML_MODE_LEGACY));
|
|
|
|
else
|
|
|
|
//noinspection deprecation
|
|
|
|
spannableString = new SpannableString(Html.fromHtml(message));
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2018-08-24 15:47:16 +02:00
|
|
|
spannableString = new SpannableString(context.getString(R.string.toast_error));
|
|
|
|
}
|
2017-11-20 16:06:31 +01:00
|
|
|
this.message = spannableString.toString();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getStatusCode() {
|
|
|
|
return statusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getMessage() {
|
|
|
|
return message;
|
|
|
|
}
|
2018-09-02 10:28:45 +02:00
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
|
|
|
}
|