Skip to content

Commit

Permalink
Revert all commits since fdc28de, there are major issues with HttpUrl…
Browse files Browse the repository at this point in the history
…Connection that makes it unuseable
  • Loading branch information
matburt committed Jan 19, 2011
1 parent e56f41a commit 9cfeb08
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 195 deletions.
2 changes: 1 addition & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<string name="error_file_permissions">Permissions error on file %s</string>
<string name="error_file_read">An error occurred while reading from file %s</string>
<string name="error_file_write">An error occurred while writing to file %s</string>
<string name="error_invalid_url">URL Is Invalid: %s</string>
<string name="error_invalid_url">URL Contains Invalid Characters: %s</string>
<string name="error_synchronizer_type_unknown">Unknown Synchronizer Method %s"</string>
<string name="error_local_storage_method_unknown">Local storage method %s unknown</string>
<string name="error_unsupported_encoding">Encountered unsupported encoding on file %s</string>
Expand Down
2 changes: 1 addition & 1 deletion src/com/matburt/mobileorg/MobileOrgActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ public void run() {
}
};
syncThread.start();

syncDialog = ProgressDialog.show(this, "",getString(R.string.sync_wait), true);
}

Expand Down
104 changes: 0 additions & 104 deletions src/com/matburt/mobileorg/SSLHandler.java

This file was deleted.

179 changes: 90 additions & 89 deletions src/com/matburt/mobileorg/WebDAVSynchronizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,28 @@
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.BufferedInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.lang.IllegalArgumentException;

import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;

import android.app.Activity;
import android.content.Context;
Expand All @@ -51,21 +60,9 @@ public class WebDAVSynchronizer extends Synchronizer
parentContext.getApplicationContext());
}

public void signOn(String mUser, String mPass) {
SSLHandler.gi().enable();
final String tUser = mUser;
final String tPass = mPass;
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(tUser, tPass.toCharArray());
}});
}

public void push() throws NotFoundException, ReportableError {
String urlActual = this.getRootUrl() + "mobileorg.org";
String storageMode = this.appSettings.getString("storageMode", "");
this.signOn(this.appSettings.getString("webUser", ""),
this.appSettings.getString("webPass", ""));
BufferedReader reader = null;
String fileContents = "";
this.pushedStageFile = false;
Expand Down Expand Up @@ -117,7 +114,10 @@ else if (storageMode.equals("sdcard")) {
e);
}

this.appendUrlFile(urlActual, fileContents);
DefaultHttpClient httpC = this.createConnection(
this.appSettings.getString("webUser", ""),
this.appSettings.getString("webPass", ""));
this.appendUrlFile(urlActual, httpC, fileContents);

if (this.pushedStageFile) {
this.removeFile("mobileorg.org");
Expand All @@ -133,8 +133,6 @@ public boolean checkReady() {
public void pull() throws NotFoundException, ReportableError {
Pattern checkUrl = Pattern.compile("http.*\\.(?:org|txt)$");
String url = this.appSettings.getString("webUrl", "");
this.signOn(this.appSettings.getString("webUser", ""),
this.appSettings.getString("webPass", ""));
if (!checkUrl.matcher(url).find()) {
throw new ReportableError(
r.getString(R.string.error_bad_url, url),
Expand Down Expand Up @@ -231,9 +229,12 @@ else if (storageMode.equals("sdcard")) {
}

private String fetchOrgFile(String orgUrl) throws NotFoundException, ReportableError {
DefaultHttpClient httpC = this.createConnection(
this.appSettings.getString("webUser", ""),
this.appSettings.getString("webPass", ""));
InputStream mainFile;
try {
mainFile = this.getUrlStream(orgUrl, true);
mainFile = this.getUrlStream(orgUrl, httpC);
}
catch (IllegalArgumentException e) {
throw new ReportableError(
Expand Down Expand Up @@ -283,91 +284,91 @@ private String getRootUrl() throws NotFoundException, ReportableError {
manageUrl.getAuthority() + directoryActual;
}

private InputStream getUrlStream(String mUrl, boolean retryOnce) throws NotFoundException, ReportableError {
URL url;
try {
url = URI.create(mUrl).toURL();
} catch (Exception e) {
throw new ReportableError(r.getString(R.string.error_invalid_url, mUrl), e);
}

HttpURLConnection.setFollowRedirects(true);
HttpURLConnection huc;
try {
huc = (HttpURLConnection) url.openConnection();
} catch (Exception e) {
throw new ReportableError(r.getString(R.string.error_url_fetch_detail,
mUrl, e.getMessage()), e);
}
private DefaultHttpClient createConnection(String user, String password) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register (new Scheme ("http",
PlainSocketFactory.getSocketFactory (), 80));
SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
schemeRegistry.register (new Scheme ("https",
sslSocketFactory, 443));
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager (
params, schemeRegistry);

UsernamePasswordCredentials bCred = new UsernamePasswordCredentials(user, password);
BasicCredentialsProvider cProvider = new BasicCredentialsProvider();
cProvider.setCredentials(AuthScope.ANY, bCred);

params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
httpClient.setParams(params);

DefaultHttpClient nHttpClient = new DefaultHttpClient(cm, params);
nHttpClient.setCredentialsProvider(cProvider);
return nHttpClient;
}

private InputStream getUrlStream(String url, DefaultHttpClient httpClient) throws NotFoundException, ReportableError {
try {
huc.connect();

int status = huc.getResponseCode();
if (retryOnce && status == -1) {
return getUrlStream(mUrl, false);
}
if (status != 200) {
throw new ReportableError(r.getString(R.string.error_url_fetch_detail,
mUrl, "Response code is: " + status), null);
HttpResponse res = httpClient.execute(new HttpGet(url));

StatusLine status = res.getStatusLine();
if (status.getStatusCode() == 404) {
return null;
}
return new BufferedInputStream(huc.getInputStream());
} catch (Exception e) {
throw new ReportableError(r.getString(R.string.error_url_fetch_detail,
mUrl, e.getMessage()), e);
}
}

private void putUrlFile(String mUrl, String content) throws NotFoundException, ReportableError {
URL url;

try {
url = URI.create(mUrl).toURL();
} catch (Exception e) {
throw new ReportableError(r.getString(R.string.error_invalid_url, mUrl), e);
if (status.getStatusCode() < 200 || status.getStatusCode() > 299) {
throw new ReportableError(
r.getString(R.string.error_url_fetch_detail,
url,
status.getReasonPhrase()),
null);
}

return res.getEntity().getContent();
}

HttpURLConnection huc;
try {
huc = (HttpURLConnection) url.openConnection();
} catch (Exception e) {
throw new ReportableError(r.getString(R.string.error_url_put_detail, mUrl, e.getMessage()), e);
catch (IOException e) {
Log.e(LT, e.toString());
Log.w(LT, "Failed to get URL");
return null; //handle exception
}
}

Log.i(LT, "Writing content to " + mUrl + ": " + content);
OutputStreamWriter out;
try {
huc.setDoOutput(true);
huc.setRequestMethod("PUT");
huc.setRequestProperty("Expect", "100-Continue");
huc.connect();
out = new OutputStreamWriter(huc.getOutputStream());
out.write(content);
out.flush();
out.close();
} catch (Exception e) {
throw new ReportableError(r.getString(R.string.error_url_put_detail, mUrl, e.getMessage()), e);
}
private void putUrlFile(String url,
DefaultHttpClient httpClient,
String content) throws NotFoundException, ReportableError {
try {
int respCode = huc.getResponseCode();
if (respCode >= 400) {
HttpPut httpPut = new HttpPut(url);
httpPut.setEntity(new StringEntity(content, "UTF-8"));
HttpResponse response = httpClient.execute(httpPut);
StatusLine statResp = response.getStatusLine();
if (statResp.getStatusCode() >= 400) {
this.pushedStageFile = false;
}
else {
} else {
this.pushedStageFile = true;
}
Log.i(LT, "Code: " + huc.getResponseCode() + " " + huc.getResponseMessage());

httpClient.getConnectionManager().shutdown();
}
catch (UnsupportedEncodingException e) {
throw new ReportableError(
r.getString(R.string.error_unsupported_encoding, "mobileorg.org"),
e);
}
catch (IOException e) {
Log.e(LT, "IO Exception attempting to put file");
throw new ReportableError(
r.getString(R.string.error_url_put, url),
e);
}
}

private void appendUrlFile(String url,
String content) throws NotFoundException, ReportableError {
DefaultHttpClient httpClient,
String content) throws NotFoundException, ReportableError {
String originalContent = this.fetchOrgFile(url);
String newContent = originalContent + '\n' + content;
this.putUrlFile(url, newContent);
this.putUrlFile(url, httpClient, newContent);
}

private String ReadInputStream(InputStream in) throws IOException {
Expand Down

0 comments on commit 9cfeb08

Please sign in to comment.