Skip to content

Commit 8568b36

Browse files
Merge branch 'master' into message-output-fix
2 parents 9596c1d + 2b50475 commit 8568b36

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ dependencies {
7474
compile group: 'com.squareup.okhttp3', name: 'okhttp-urlconnection', version: '3.11.0'
7575
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
7676
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.5'
77+
compile group: 'commons-io', name: 'commons-io', version: '2.6'
7778
compile group: 'org.glassfish.jersey.bundles.repackaged', name: 'jersey-jsr166e', version: '2.25.1'
7879
testCompile group: 'simple-jndi', name: 'simple-jndi', version: '0.11.4.1'
7980

core/src/main/java/com/ibm/watson/developer_cloud/http/InputStreamRequestBody.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@
1212
*/
1313
package com.ibm.watson.developer_cloud.http;
1414

15+
import java.io.ByteArrayInputStream;
16+
import java.io.ByteArrayOutputStream;
1517
import java.io.IOException;
1618
import java.io.InputStream;
1719

20+
import com.ibm.watson.developer_cloud.util.HttpLogging;
1821
import okhttp3.MediaType;
1922
import okhttp3.RequestBody;
2023
import okhttp3.internal.Util;
24+
import okhttp3.logging.HttpLoggingInterceptor;
2125
import okio.BufferedSink;
2226
import okio.Okio;
2327
import okio.Source;
28+
import org.apache.commons.io.IOUtils;
2429

2530
/**
2631
* RequestBody that takes an {@link InputStream}.
@@ -30,6 +35,7 @@ public class InputStreamRequestBody extends RequestBody {
3035

3136
private InputStream inputStream;
3237
private MediaType mediaType;
38+
private byte[] bytes;
3339

3440
/**
3541
* Creates the @link {@link RequestBody} from an @link {@link InputStream}.
@@ -45,6 +51,19 @@ public static RequestBody create(final MediaType mediaType, final InputStream in
4551
private InputStreamRequestBody(InputStream inputStream, MediaType mediaType) {
4652
this.inputStream = inputStream;
4753
this.mediaType = mediaType;
54+
55+
// if we're logging everything, we'll need to store the bytes to reuse later
56+
if (HttpLogging.getLoggingInterceptor().getLevel().equals(HttpLoggingInterceptor.Level.BODY)) {
57+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
58+
59+
try {
60+
IOUtils.copy(inputStream, outputStream);
61+
} catch (IOException e) {
62+
e.printStackTrace();
63+
}
64+
65+
this.bytes = outputStream.toByteArray();
66+
}
4867
}
4968

5069
/*
@@ -65,8 +84,13 @@ public MediaType contentType() {
6584
@Override
6685
public void writeTo(BufferedSink sink) throws IOException {
6786
Source source = null;
87+
6888
try {
69-
source = Okio.source(inputStream);
89+
if (bytes != null) {
90+
source = Okio.source(new ByteArrayInputStream(bytes));
91+
} else {
92+
source = Okio.source(inputStream);
93+
}
7094
sink.writeAll(source);
7195
} finally {
7296
Util.closeQuietly(source);

0 commit comments

Comments
 (0)