mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-06 03:50:19 +02:00
better redirect support; a template for basic auth support
This commit is contained in:
parent
fbc20f58dd
commit
078a7fe21f
5 changed files with 29 additions and 11 deletions
|
@ -19,9 +19,8 @@
|
|||
|
||||
package org.geometerplus.android.fbreader.network;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.ArrayList;
|
||||
import java.util.*;
|
||||
import java.net.*;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
@ -41,6 +40,18 @@ class NetworkView {
|
|||
public static NetworkView Instance() {
|
||||
if (ourInstance == null) {
|
||||
ourInstance = new NetworkView();
|
||||
Authenticator.setDefault(new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
System.err.println("getPasswordAuthentication");
|
||||
System.err.println(getRequestingSite().getHostName());
|
||||
System.err.println(getRequestingPrompt());
|
||||
System.err.println(getRequestingProtocol());
|
||||
final String username = "geometer";
|
||||
final String password = "XXXXXXXX";
|
||||
return new PasswordAuthentication(username, password.toCharArray());
|
||||
}
|
||||
});
|
||||
}
|
||||
return ourInstance;
|
||||
}
|
||||
|
|
|
@ -117,7 +117,7 @@ public class OPDSCustomLink extends OPDSNetworkLink implements ICustomNetworkLin
|
|||
ZLNetworkManager.Instance().perform(new ZLNetworkRequest(getUrl(UrlInfo.Type.Catalog)) {
|
||||
@Override
|
||||
public void handleStream(URLConnection connection, InputStream inputStream) throws IOException, ZLNetworkException {
|
||||
final CatalogInfoReader info = new CatalogInfoReader(URL, OPDSCustomLink.this, opensearchDescriptionURLs);
|
||||
final CatalogInfoReader info = new CatalogInfoReader(getURL(), OPDSCustomLink.this, opensearchDescriptionURLs);
|
||||
new OPDSXMLReader(info).read(inputStream);
|
||||
|
||||
if (!info.FeedStarted) {
|
||||
|
@ -146,7 +146,7 @@ public class OPDSCustomLink extends OPDSNetworkLink implements ICustomNetworkLin
|
|||
requests.add(new ZLNetworkRequest(url) {
|
||||
@Override
|
||||
public void handleStream(URLConnection connection, InputStream inputStream) throws IOException, ZLNetworkException {
|
||||
new OpenSearchXMLReader(URL, descriptions).read(inputStream);
|
||||
new OpenSearchXMLReader(getURL(), descriptions).read(inputStream);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -93,7 +93,7 @@ public class OPDSNetworkLink extends AbstractNetworkLink {
|
|||
}
|
||||
|
||||
new OPDSXMLReader(
|
||||
new NetworkOPDSFeedReader(URL, result)
|
||||
new NetworkOPDSFeedReader(getURL(), result)
|
||||
).read(inputStream);
|
||||
|
||||
if (result.Listener.confirmInterrupt()) {
|
||||
|
|
|
@ -86,15 +86,15 @@ public class ZLNetworkManager {
|
|||
//httpConnection.setRequestProperty("Connection", "Close");
|
||||
httpConnection.setRequestProperty("User-Agent", ZLNetworkUtil.getUserAgent());
|
||||
httpConnection.setRequestProperty("Accept-Language", Locale.getDefault().getLanguage());
|
||||
httpConnection.setAllowUserInteraction(false);
|
||||
httpConnection.setAllowUserInteraction(true);
|
||||
if (httpConnection instanceof HttpsURLConnection) {
|
||||
HttpsURLConnection httpsConnection = (HttpsURLConnection)httpConnection;
|
||||
|
||||
final ArrayList<TrustManager> managers = new ArrayList<TrustManager>();
|
||||
collectStandardTrustManagers(managers);
|
||||
if (request.SSLCertificate != null) {
|
||||
managers.add(createZLTrustManager(request.SSLCertificate));
|
||||
}
|
||||
collectStandardTrustManagers(managers);
|
||||
|
||||
try {
|
||||
SSLContext context = SSLContext.getInstance("TLS");
|
||||
|
@ -113,7 +113,7 @@ public class ZLNetworkManager {
|
|||
HttpURLConnection httpConnection = null;
|
||||
int response = -1;
|
||||
final int retryLimit = 3;
|
||||
for (int retryCounter = 0; retryCounter < retryLimit && response == -1; ++retryCounter) {
|
||||
for (int retryCounter = 0; retryCounter < retryLimit && (response == -1 || response == 302); ++retryCounter) {
|
||||
final URLConnection connection = new URL(request.URL).openConnection();
|
||||
if (!(connection instanceof HttpURLConnection)) {
|
||||
throw new ZLNetworkException(ZLNetworkException.ERROR_UNSUPPORTED_PROTOCOL);
|
||||
|
@ -145,6 +145,9 @@ public class ZLNetworkManager {
|
|||
httpConnection.connect();
|
||||
}
|
||||
response = httpConnection.getResponseCode();
|
||||
if (response == 302) {
|
||||
request.URL = httpConnection.getHeaderField("Location");
|
||||
}
|
||||
}
|
||||
|
||||
InputStream stream = null;
|
||||
|
@ -167,7 +170,7 @@ public class ZLNetworkManager {
|
|||
} else {
|
||||
if (response == HttpURLConnection.HTTP_UNAUTHORIZED) {
|
||||
throw new ZLNetworkException(ZLNetworkException.ERROR_AUTHENTICATION_FAILED);
|
||||
} else if (response != HttpURLConnection.HTTP_OK) {
|
||||
} else if (response >= 400) {
|
||||
throw new ZLNetworkException(true, httpConnection.getResponseMessage());
|
||||
} else {
|
||||
throw new ZLNetworkException(ZLNetworkException.ERROR_SOMETHING_WRONG, ZLNetworkUtil.hostFromUrl(request.URL));
|
||||
|
|
|
@ -24,7 +24,7 @@ import java.io.IOException;
|
|||
import java.net.URLConnection;
|
||||
|
||||
public abstract class ZLNetworkRequest {
|
||||
public final String URL;
|
||||
String URL;
|
||||
public final String SSLCertificate;
|
||||
public final String PostData;
|
||||
|
||||
|
@ -32,6 +32,10 @@ public abstract class ZLNetworkRequest {
|
|||
this(url, null, null);
|
||||
}
|
||||
|
||||
public String getURL() {
|
||||
return URL;
|
||||
}
|
||||
|
||||
protected ZLNetworkRequest(String url, String sslCertificate, String postData) {
|
||||
URL = url;
|
||||
SSLCertificate = sslCertificate;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue