Skip to content

Commit

Permalink
Merge pull request getodk#2849 from xsteelej/issue_#620_PR2_add_okhttp3
Browse files Browse the repository at this point in the history
Issue getodk#620 pr2 add okhttp3
  • Loading branch information
yanokwa authored Feb 19, 2019
2 parents 72102cc + d992631 commit 1ee4a1f
Show file tree
Hide file tree
Showing 12 changed files with 504 additions and 98 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.google.gms:google-services:4.0.1'
classpath 'com.google.gms:google-services:4.0.1'
classpath 'org.ow2.asm:asm:6.0' // https://github.com/jacoco/jacoco/issues/639#issuecomment-355424756
classpath 'org.jacoco:org.jacoco.core:0.8.0'
classpath 'io.fabric.tools:gradle:1.25.4'
Expand Down Expand Up @@ -52,4 +52,5 @@ ext {
daggerVersion = '2.16'
rxLifecycleVersion = '2.2.1'
workManagerVersion = "1.0.0-alpha10"
okhttp3Version = '3.11.0'
}
7 changes: 5 additions & 2 deletions collect_app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ dependencies {
implementation "com.android.support:cardview-v7:${rootProject.supportLibraryVersion}"
implementation "com.android.support:exifinterface:${rootProject.supportLibraryVersion}"
implementation "com.android.support:multidex:1.0.3"

implementation "com.google.android.gms:play-services-analytics:16.0.1"
implementation "com.google.android.gms:play-services-auth:16.0.0"
implementation "com.google.android.gms:play-services-maps:15.0.1"
Expand All @@ -212,6 +212,9 @@ dependencies {
exclude group: 'org.apache.httpcomponents'
}

implementation "com.squareup.okhttp3:okhttp:${rootProject.okhttp3Version}"
implementation 'com.burgstaller:okhttp-digest:1.18'

implementation 'bikramsambat:bikram-sambat:1.1.0'
implementation "com.evernote:android-job:1.2.5"
implementation "com.rarepebble:colorpicker:2.3.1"
Expand Down Expand Up @@ -317,7 +320,7 @@ dependencies {
exclude group: 'com.google.code.findbugs', module: 'jsr305'
}

androidTestImplementation "com.squareup.okhttp3:mockwebserver:3.9.0"
androidTestImplementation "com.squareup.okhttp3:mockwebserver:${rootProject.okhttp3Version}"

androidTestImplementation "android.arch.work:work-testing:${rootProject.workManagerVersion}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ HttpGetResult getHttpInputStream(@NonNull String downloadUrl, @Nullable final St
throw new Exception("Invalid server URL (no hostname): " + downloadUrl);
}

return httpInterface.get(uri, contentType, webCredentialsUtils.getCredentials(uri));
return httpInterface.executeGetRequest(uri, contentType, webCredentialsUtils.getCredentials(uri));
}

public static String getPlainTextMimeType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.odk.collect.android.R;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.utilities.FileUtils;
import org.odk.collect.android.utilities.ResponseMessageParser;
import org.opendatakit.httpclientandroidlib.Header;
import org.opendatakit.httpclientandroidlib.HttpEntity;
import org.opendatakit.httpclientandroidlib.HttpHost;
Expand Down Expand Up @@ -150,7 +149,7 @@ public static ContentType of(String fileName) {

@Override
public @NonNull
HttpGetResult get(@NonNull URI uri, @Nullable final String contentType, @Nullable HttpCredentialsInterface credentials) throws Exception {
HttpGetResult executeGetRequest(@NonNull URI uri, @Nullable final String contentType, @Nullable HttpCredentialsInterface credentials) throws Exception {
addCredentialsForHost(uri, credentials);
clearCookieStore();

Expand Down Expand Up @@ -233,7 +232,7 @@ HttpGetResult get(@NonNull URI uri, @Nullable final String contentType, @Nullabl
}

@Override
public @NonNull HttpHeadResult head(@NonNull URI uri, @Nullable HttpCredentialsInterface credentials) throws Exception {
public @NonNull HttpHeadResult executeHeadRequest(@NonNull URI uri, @Nullable HttpCredentialsInterface credentials) throws Exception {
addCredentialsForHost(uri, credentials);
clearCookieStore();

Expand Down Expand Up @@ -294,11 +293,11 @@ HttpGetResult get(@NonNull URI uri, @Nullable final String contentType, @Nullabl
}

@Override
public @NonNull ResponseMessageParser uploadSubmissionFile(@NonNull List<File> fileList,
@NonNull File submissionFile,
@NonNull URI uri,
@Nullable HttpCredentialsInterface credentials,
@NonNull long contentLength) throws IOException {
public @NonNull HttpPostResult uploadSubmissionFile(@NonNull List<File> fileList,
@NonNull File submissionFile,
@NonNull URI uri,
@Nullable HttpCredentialsInterface credentials,
@NonNull long contentLength) throws IOException {
addCredentialsForHost(uri, credentials);
clearCookieStore();

Expand All @@ -309,7 +308,7 @@ HttpGetResult get(@NonNull URI uri, @Nullable final String contentType, @Nullabl
enablePreemptiveBasicAuth(uri.getHost());
}

ResponseMessageParser messageParser = null;
HttpPostResult postResult = null;

boolean first = true;
int fileIndex = 0;
Expand Down Expand Up @@ -383,10 +382,7 @@ HttpGetResult get(@NonNull URI uri, @Nullable final String contentType, @Nullabl
HttpEntity httpEntity = response.getEntity();
Timber.i("Response code:%d", responseCode);

messageParser = new ResponseMessageParser(
EntityUtils.toString(httpEntity),
responseCode,
response.getStatusLine().getReasonPhrase());
postResult = new HttpPostResult(EntityUtils.toString(httpEntity), responseCode, response.getStatusLine().getReasonPhrase());

discardEntityBytes(response);

Expand All @@ -395,7 +391,7 @@ HttpGetResult get(@NonNull URI uri, @Nullable final String contentType, @Nullabl
}

if (responseCode != HttpStatus.SC_CREATED && responseCode != HttpStatus.SC_ACCEPTED) {
return messageParser;
return postResult;
}

} catch (IOException e) {
Expand All @@ -415,7 +411,18 @@ HttpGetResult get(@NonNull URI uri, @Nullable final String contentType, @Nullabl
}
}

return messageParser;
return postResult;
}

/**
* HttpPostResult - This is just stubbed out for now, implemented when we move to OkHttpConnection
* @param uri of which to post
* @param credentials to use on this post request
* @return null
* @throws Exception not used
*/
public HttpPostResult executePostRequest(@NonNull URI uri, @Nullable HttpCredentialsInterface credentials) throws Exception {
return new HttpPostResult("", 0, "");
}

private void addCredentialsForHost(@NonNull URI uri, @Nullable HttpCredentialsInterface credentials) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class HttpCredentials implements HttpCredentialsInterface {
private final String password;

public HttpCredentials(String username, String password) {
this.username = username;
this.password = password;
this.username = (username == null) ? "" : username;
this.password = (password == null) ? "" : password;
}

@Override
Expand All @@ -19,4 +19,23 @@ public String getUsername() {
public String getPassword() {
return password;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}

if (super.equals(obj)) {
return true;
}

return ((HttpCredentials) obj).getUsername().equals(getUsername()) &&
((HttpCredentials) obj).getPassword().equals(getPassword());
}

@Override
public int hashCode() {
return (getUsername() + getPassword()).hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.odk.collect.android.http;

public class HttpPostResult {

private final String httpResponse;
private final int responseCode;
private final String reasonPhrase;

public HttpPostResult(String httpResponse, int responseCode, String reasonPhrase) {
this.httpResponse = httpResponse;
this.responseCode = responseCode;
this.reasonPhrase = reasonPhrase;
}

public int getResponseCode() {
return responseCode;
}

public String getReasonPhrase() {
return reasonPhrase;
}

public String getHttpResponse() {
return httpResponse;
}
}
Loading

0 comments on commit 1ee4a1f

Please sign in to comment.