This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new d063c8d REST refactoring.
d063c8d is described below
commit d063c8d02b8006bb239d02090cd9c45e50e2286d
Author: JamesBognar <[email protected]>
AuthorDate: Fri Mar 5 15:05:02 2021 -0500
REST refactoring.
---
.../main/ConfigurablePropertyCodeGenerator.java | 3 +-
.../java/org/apache/juneau/http/HttpEntities.java | 120 ++++++++++--
.../apache/juneau/http/entity/BasicHttpEntity.java | 3 +-
.../juneau/http/entity/BasicHttpResource.java | 1 +
.../apache/juneau/http/entity/ByteArrayEntity.java | 14 +-
.../org/apache/juneau/http/entity/FileEntity.java | 12 +-
.../juneau/http/entity/InputStreamEntity.java | 19 +-
.../{InputStreamEntity.java => ReaderEntity.java} | 83 ++++----
...alizedHttpEntity.java => SerializedEntity.java} | 140 +++++++-------
.../apache/juneau/http/entity/StringEntity.java | 58 +++---
.../java/org/apache/juneau/internal/IOUtils.java | 14 ++
.../org/apache/juneau/rest/client/RestClient.java | 20 +-
.../org/apache/juneau/rest/client/RestRequest.java | 6 +-
.../apache/juneau/http/BasicHttpEntity_Test.java | 212 ---------------------
.../apache/juneau/http/BasicHttpResource_Test.java | 1 +
.../juneau/http/SerializedHttpEntity_Test.java | 17 +-
.../http/remote/Remote_CommonInterfaces_Test.java | 1 +
.../rest/client/RestClient_BasicCalls_Test.java | 4 +-
.../juneau/rest/client/RestClient_Body_Test.java | 88 ++-------
19 files changed, 335 insertions(+), 481 deletions(-)
diff --git a/juneau-all/src/java/main/ConfigurablePropertyCodeGenerator.java
b/juneau-all/src/java/main/ConfigurablePropertyCodeGenerator.java
index f79f317..40e2be1 100644
--- a/juneau-all/src/java/main/ConfigurablePropertyCodeGenerator.java
+++ b/juneau-all/src/java/main/ConfigurablePropertyCodeGenerator.java
@@ -64,7 +64,6 @@ public class ConfigurablePropertyCodeGenerator {
ArrayAssertion.class,
Assertion.class,
BasicHeader.class,
- BasicHttpEntity.class,
BasicHttpResource.class,
HttpResponseBuilder.class,
Part.class,
@@ -175,7 +174,7 @@ public class ConfigurablePropertyCodeGenerator {
RestOperationContextBuilder.class,
SeeOther.class,
SerializedHeader.class,
- SerializedHttpEntity.class,
+ SerializedEntity.class,
SerializedPart.class,
SerializerBuilder.class,
SerializerGroupBuilder.class,
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/HttpEntities.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/HttpEntities.java
index 21c588b..d3b27d2 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/HttpEntities.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/HttpEntities.java
@@ -13,10 +13,11 @@
package org.apache.juneau.http;
import java.io.*;
-import java.nio.charset.*;
+import java.util.function.*;
import org.apache.juneau.http.entity.*;
import org.apache.juneau.http.header.*;
+import org.apache.juneau.serializer.*;
/**
* Standard predefined HTTP entities.
@@ -24,28 +25,102 @@ import org.apache.juneau.http.header.*;
public class HttpEntities {
/**
- * Creates a new {@link StringEntity} object.
+ * Creates a new {@link ByteArrayEntity} object.
*
* <p>
- * Assumes {@link ContentType#TEXT_PLAIN TEXT/PLAIN} content type and
<js>"UTF-8"</js> encoding.
+ * Assumes no content type.
+ *
+ * @param content The entity content. Can be <jk>null<jk>.
+ * @return A new {@link ByteArrayEntity} object.
+ */
+ public static final ByteArrayEntity byteArrayEntity(byte[] content) {
+ return ByteArrayEntity.of(content);
+ }
+
+ /**
+ * Creates a new {@link ByteArrayEntity} object.
+ *
+ * @param content The entity content. Can be <jk>null<jk>.
+ * @param contentType The entity content type, or <jk>null</jk> if not
specified.
+ * @return A new {@link ByteArrayEntity} object.
+ */
+ public static final ByteArrayEntity byteArrayEntity(byte[] content,
ContentType contentType) {
+ return ByteArrayEntity.of(content, contentType);
+ }
+
+ /**
+ * Creates a new {@link FileEntity} object.
+ *
+ * <p>
+ * Assumes no content type.
+ *
+ * @param content The entity content. Can be <jk>null<jk>.
+ * @return A new {@link FileEntity} object.
+ */
+ public static final FileEntity fileEntity(File content) {
+ return FileEntity.of(content);
+ }
+
+ /**
+ * Creates a new {@link FileEntity} object.
+ *
+ * @param content The entity content. Can be <jk>null<jk>.
+ * @param contentType The entity content type, or <jk>null</jk> if not
specified.
+ * @return A new {@link FileEntity} object.
+ */
+ public static final FileEntity fileEntity(File content, ContentType
contentType) {
+ return FileEntity.of(content, contentType);
+ }
+
+ /**
+ * Creates a new {@link ReaderEntity} object.
*
* @param content The entity content. Can be <jk>null</jk>.
- * @return A new {@link StringEntity} object.
+ * @return A new {@link ReaderEntity} object.
*/
- public static final StringEntity stringEntity(String content) {
- return StringEntity.of(content);
+ public static final ReaderEntity readerEntity(Reader content) {
+ return ReaderEntity.of(content);
}
/**
- * Creates a new {@link StringEntity} object.
+ * Creates a new {@link ReaderEntity} object.
*
* @param content The entity content. Can be <jk>null</jk>.
- * @param contentType The entity content type, or {@link
ContentType#TEXT_PLAIN} if not specified.
- * @param charset The content character encoding, or <js>"UTF-8"</js>
if not specified.
- * @return A new {@link StringEntity} object.
+ * @param contentType The entity content type, or <jk>null</jk> if not
specified.
+ * @return A new {@link ReaderEntity} object.
+ */
+ public static final ReaderEntity readerEntity(Reader content,
ContentType contentType) {
+ return ReaderEntity.of(content, -1, contentType);
+ }
+
+ /**
+ * Creates a new {@link SerializedEntity} object.
+ *
+ * @param content
+ * The Java POJO representing the content.
+ * <br>Can be <jk>null<jk>.
+ * @param serializer
+ * The serializer to use to serialize the POJO.
+ * <br>If <jk>null</jk>, POJO will be converted to a string using
{@link Object#toString()}.
+ * @return A new {@link SerializedEntity} object.
*/
- public static final StringEntity stringEntity(String content,
ContentType contentType, Charset charset) {
- return StringEntity.of(content, contentType, charset);
+ public static final SerializedEntity serializedEntity(Object content,
Serializer serializer) {
+ return SerializedEntity.of(content, serializer);
+ }
+
+ /**
+ * Creates a new {@link SerializedEntity} object.
+ *
+ * @param content
+ * The supplier of a Java POJO representing the content.
+ * <br>Can be <jk>null<jk>.
+ * @param serializer
+ * The serializer to use to serialize the POJO.
+ * <br>If <jk>null</jk>, POJO will be converted to a string using
{@link Object#toString()}.
+ * @return A new {@link SerializedEntity} object.
+ */
+ public static final SerializedEntity serializedEntity(Supplier<?>
content, Serializer serializer) {
+ return SerializedEntity.of(content, serializer);
}
/**
@@ -72,4 +147,25 @@ public class HttpEntities {
public static final InputStreamEntity streamEntity(InputStream content,
long length, ContentType contentType) {
return InputStreamEntity.of(content, length, contentType);
}
+
+ /**
+ * Creates a new {@link StringEntity} object.
+ *
+ * @param content The entity content. Can be <jk>null</jk>.
+ * @return A new {@link StringEntity} object.
+ */
+ public static final StringEntity stringEntity(String content) {
+ return StringEntity.of(content);
+ }
+
+ /**
+ * Creates a new {@link StringEntity} object.
+ *
+ * @param content The entity content. Can be <jk>null</jk>.
+ * @param contentType The entity content type, or <jk>null</jk> if not
specified.
+ * @return A new {@link StringEntity} object.
+ */
+ public static final StringEntity stringEntity(String content,
ContentType contentType) {
+ return StringEntity.of(content, contentType);
+ }
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/BasicHttpEntity.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/BasicHttpEntity.java
index a292d17..decac35 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/BasicHttpEntity.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/BasicHttpEntity.java
@@ -40,6 +40,7 @@ import org.apache.juneau.internal.*;
*/
@FluentSetters
@BeanIgnore
+@Deprecated
public class BasicHttpEntity extends org.apache.http.entity.BasicHttpEntity {
private Object content;
private boolean cache;
@@ -338,7 +339,6 @@ public class BasicHttpEntity extends
org.apache.http.entity.BasicHttpEntity {
}
}
- @SuppressWarnings("deprecation")
@Override
public void writeTo(OutputStream os) throws IOException {
tryCache();
@@ -394,7 +394,6 @@ public class BasicHttpEntity extends
org.apache.http.entity.BasicHttpEntity {
* @return The byte array contents.
* @throws IOException If object could not be read.
*/
- @SuppressWarnings("deprecation")
protected byte[] readBytes(Object o) throws IOException {
return IOUtils.readBytes(o);
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/BasicHttpResource.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/BasicHttpResource.java
index 33ee3c6..2f41527 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/BasicHttpResource.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/BasicHttpResource.java
@@ -39,6 +39,7 @@ import org.apache.juneau.http.header.*;
* Fluent assertions.
* </ul>
*/
+@SuppressWarnings("deprecation")
@FluentSetters
public class BasicHttpResource extends BasicHttpEntity implements HttpResource
{
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/ByteArrayEntity.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/ByteArrayEntity.java
index fba9a36..bea3c52 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/ByteArrayEntity.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/ByteArrayEntity.java
@@ -25,7 +25,6 @@ import org.apache.juneau.http.header.*;
public class ByteArrayEntity extends AbstractHttpEntity {
private final byte[] content;
- private final long maxLength;
/**
* Creates a new {@link ByteArrayEntity} object.
@@ -37,31 +36,28 @@ public class ByteArrayEntity extends AbstractHttpEntity {
* @return A new {@link ByteArrayEntity} object.
*/
public static ByteArrayEntity of(byte[] content) {
- return new ByteArrayEntity(content, -1, null);
+ return new ByteArrayEntity(content, null);
}
/**
* Creates a new {@link ByteArrayEntity} object.
*
* @param content The entity content. Can be <jk>null<jk>.
- * @param maxLength The content maximum length, or <c>-1</c> to read
the entire byte array.
* @param contentType The entity content type, or <jk>null</jk> if not
specified.
* @return A new {@link ByteArrayEntity} object.
*/
- public static ByteArrayEntity of(byte[] content, long maxLength,
ContentType contentType) {
- return new ByteArrayEntity(content, maxLength, contentType);
+ public static ByteArrayEntity of(byte[] content, ContentType
contentType) {
+ return new ByteArrayEntity(content, contentType);
}
/**
* Constructor.
*
* @param content The entity content. Can be <jk>null</jk>.
- * @param maxLength The content maximum length, or <c>-1</c> to read
the entire byte array.
* @param contentType The entity content type, or <jk>null</jk> if not
specified.
*/
- public ByteArrayEntity(byte[] content, long maxLength, ContentType
contentType) {
+ public ByteArrayEntity(byte[] content, ContentType contentType) {
this.content = content == null ? new byte[0] : content;
- this.maxLength = maxLength;
setContentType(contentType);
}
@@ -99,7 +95,7 @@ public class ByteArrayEntity extends AbstractHttpEntity {
@Override
public void writeTo(OutputStream out) throws IOException {
assertArgNotNull("out", out);
- pipe(content, out, (int)maxLength);
+ out.write(content);
}
@Override /* HttpEntity */
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/FileEntity.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/FileEntity.java
index 7507ee2..d63a19b 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/FileEntity.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/FileEntity.java
@@ -27,7 +27,7 @@ import org.apache.juneau.internal.*;
public class FileEntity extends AbstractHttpEntity {
private final File content;
- private AtomicReference<byte[]> bytes = new AtomicReference<>();
+ private AtomicReference<byte[]> cache = new AtomicReference<>();
/**
* Creates a new {@link FileEntity} object.
@@ -72,7 +72,7 @@ public class FileEntity extends AbstractHttpEntity {
@Override /* AbstractHttpEntity */
public byte[] asBytes() throws IOException {
cache();
- return bytes.get();
+ return cache.get();
}
@Override /* HttpEntity */
@@ -87,16 +87,16 @@ public class FileEntity extends AbstractHttpEntity {
@Override /* HttpEntity */
public InputStream getContent() throws IOException {
- byte[] b = bytes.get();
+ byte[] b = cache.get();
return b == null ? new FileInputStream(content) : new
ByteArrayInputStream(b);
}
@Override /* AbstractHttpEntity */
public FileEntity cache() throws IOException {
- byte[] b = bytes.get();
+ byte[] b = cache.get();
if (b == null) {
b = readBytes(content);
- bytes.set(b);
+ cache.set(b);
}
return this;
}
@@ -111,7 +111,7 @@ public class FileEntity extends AbstractHttpEntity {
public void writeTo(OutputStream out) throws IOException {
assertArgNotNull("out", out);
- byte[] b = bytes.get();
+ byte[] b = cache.get();
if (b != null) {
out.write(b);
} else {
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/InputStreamEntity.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/InputStreamEntity.java
index dabbc05..8a56a18 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/InputStreamEntity.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/InputStreamEntity.java
@@ -19,7 +19,6 @@ import java.io.*;
import java.util.concurrent.atomic.*;
import org.apache.juneau.http.header.*;
-import org.apache.juneau.internal.*;
/**
* A streamed, non-repeatable entity that obtains its content from an {@link
InputStream}.
@@ -28,7 +27,7 @@ public class InputStreamEntity extends AbstractHttpEntity {
private final InputStream content;
private final long length;
- private final AtomicReference<byte[]> bytes = new AtomicReference<>();
+ private final AtomicReference<byte[]> cache = new AtomicReference<>();
/**
* Creates a new {@link InputStreamEntity} object.
@@ -63,7 +62,7 @@ public class InputStreamEntity extends AbstractHttpEntity {
* @param length The content length, or <c>-1</c> if not known.
*/
public InputStreamEntity(InputStream content, long length, ContentType
contentType) {
- this.content = content == null ? IOUtils.EMPTY_INPUT_STREAM :
content;
+ this.content = content == null ? EMPTY_INPUT_STREAM : content;
this.length = length;
setContentType(contentType);
}
@@ -76,12 +75,12 @@ public class InputStreamEntity extends AbstractHttpEntity {
@Override /* AbstractHttpEntity */
public byte[] asBytes() throws IOException {
cache();
- return bytes.get();
+ return cache.get();
}
@Override /* HttpEntity */
public boolean isRepeatable() {
- return false;
+ return cache.get() != null;
}
@Override /* HttpEntity */
@@ -91,18 +90,18 @@ public class InputStreamEntity extends AbstractHttpEntity {
@Override /* HttpEntity */
public InputStream getContent() throws IOException {
- byte[] b = bytes.get();
+ byte[] b = cache.get();
return b == null ? content : new ByteArrayInputStream(b);
}
@Override /* AbstractHttpEntity */
public InputStreamEntity cache() throws IOException {
- byte[] b = bytes.get();
+ byte[] b = cache.get();
if (b == null) {
try (InputStream is = getContent()) {
b = readBytes(is, (int)length);
}
- bytes.set(b);
+ cache.set(b);
}
return this;
}
@@ -117,7 +116,7 @@ public class InputStreamEntity extends AbstractHttpEntity {
public void writeTo(OutputStream out) throws IOException {
assertArgNotNull("out", out);
- byte[] b = bytes.get();
+ byte[] b = cache.get();
if (b != null) {
pipe(b, out, (int)length);
} else {
@@ -129,6 +128,6 @@ public class InputStreamEntity extends AbstractHttpEntity {
@Override /* HttpEntity */
public boolean isStreaming() {
- return bytes.get() == null;
+ return cache.get() == null;
}
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/InputStreamEntity.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/ReaderEntity.java
similarity index 67%
copy from
juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/InputStreamEntity.java
copy to
juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/ReaderEntity.java
index dabbc05..0ac4b37 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/InputStreamEntity.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/ReaderEntity.java
@@ -16,43 +16,45 @@ import static org.apache.juneau.assertions.Assertions.*;
import static org.apache.juneau.internal.IOUtils.*;
import java.io.*;
+import java.nio.charset.*;
import java.util.concurrent.atomic.*;
import org.apache.juneau.http.header.*;
import org.apache.juneau.internal.*;
/**
- * A streamed, non-repeatable entity that obtains its content from an {@link
InputStream}.
+ * A streamed, non-repeatable entity that obtains its content from an {@link
Reader}.
*/
-public class InputStreamEntity extends AbstractHttpEntity {
+public class ReaderEntity extends AbstractHttpEntity {
- private final InputStream content;
+ private final Reader content;
private final long length;
- private final AtomicReference<byte[]> bytes = new AtomicReference<>();
+ private Charset charset = UTF8;
+ private final AtomicReference<String> cache = new AtomicReference<>();
/**
- * Creates a new {@link InputStreamEntity} object.
+ * Creates a new {@link ReaderEntity} object.
*
* <p>
* Assumes no content type.
*
* @param content The entity content. Can be <jk>null<jk>.
- * @return A new {@link InputStreamEntity} object.
+ * @return A new {@link ReaderEntity} object.
*/
- public static InputStreamEntity of(InputStream content) {
- return new InputStreamEntity(content, -1, null);
+ public static ReaderEntity of(Reader content) {
+ return new ReaderEntity(content, -1, null);
}
/**
- * Creates a new {@link InputStreamEntity} object.
+ * Creates a new {@link ReaderEntity} object.
*
* @param content The entity content. Can be <jk>null<jk>.
* @param contentType The entity content type, or <jk>null</jk> if not
specified.
* @param length The content length, or <c>-1</c> if not known.
- * @return A new {@link InputStreamEntity} object.
+ * @return A new {@link ReaderEntity} object.
*/
- public static InputStreamEntity of(InputStream content, long length,
ContentType contentType) {
- return new InputStreamEntity(content, length, contentType);
+ public static ReaderEntity of(Reader content, long length, ContentType
contentType) {
+ return new ReaderEntity(content, length, contentType);
}
/**
@@ -62,26 +64,41 @@ public class InputStreamEntity extends AbstractHttpEntity {
* @param contentType The entity content type, or <jk>null</jk> if not
specified.
* @param length The content length, or <c>-1</c> if not known.
*/
- public InputStreamEntity(InputStream content, long length, ContentType
contentType) {
- this.content = content == null ? IOUtils.EMPTY_INPUT_STREAM :
content;
+ public ReaderEntity(Reader content, long length, ContentType
contentType) {
+ this.content = content == null ? EMPTY_READER : content;
this.length = length;
setContentType(contentType);
}
+ /**
+ * Specifies the charset to use for the output.
+ *
+ * <p>
+ * The default is <js>"UTF-8"</js>.
+ *
+ * @param value The new charset, or <js>"UTF-8"</js> if <jk>null</jk>.
+ * @return This object (for method chaining).
+ */
+ @FluentSetter
+ public ReaderEntity charset(Charset value) {
+ this.charset = value == null ? UTF8 : value;
+ return this;
+ }
+
@Override /* AbstractHttpEntity */
public String asString() throws IOException {
- return new String(asBytes(), UTF8);
+ cache();
+ return cache.get();
}
@Override /* AbstractHttpEntity */
public byte[] asBytes() throws IOException {
- cache();
- return bytes.get();
+ return asString().getBytes(UTF8);
}
@Override /* HttpEntity */
public boolean isRepeatable() {
- return false;
+ return cache.get() != null;
}
@Override /* HttpEntity */
@@ -91,18 +108,18 @@ public class InputStreamEntity extends AbstractHttpEntity {
@Override /* HttpEntity */
public InputStream getContent() throws IOException {
- byte[] b = bytes.get();
- return b == null ? content : new ByteArrayInputStream(b);
+ String s = cache.get();
+ return s == null ? new ReaderInputStream(content, charset) :
new ReaderInputStream(new StringReader(s), charset);
}
@Override /* AbstractHttpEntity */
- public InputStreamEntity cache() throws IOException {
- byte[] b = bytes.get();
- if (b == null) {
- try (InputStream is = getContent()) {
- b = readBytes(is, (int)length);
+ public ReaderEntity cache() throws IOException {
+ String s = cache.get();
+ if (s == null) {
+ try (Reader r = content) {
+ s = read(r);
}
- bytes.set(b);
+ cache.set(s);
}
return this;
}
@@ -117,18 +134,18 @@ public class InputStreamEntity extends AbstractHttpEntity
{
public void writeTo(OutputStream out) throws IOException {
assertArgNotNull("out", out);
- byte[] b = bytes.get();
- if (b != null) {
- pipe(b, out, (int)length);
+ OutputStreamWriter osw = new OutputStreamWriter(out, charset);
+ String s = cache.get();
+ if (s != null) {
+ osw.write(s);
} else {
- try (InputStream is = getContent()) {
- pipe(is, out, length);
- }
+ pipe(content, osw);
}
+ osw.flush();
}
@Override /* HttpEntity */
public boolean isStreaming() {
- return bytes.get() == null;
+ return cache.get() == null;
}
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/SerializedHttpEntity.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/SerializedEntity.java
similarity index 55%
rename from
juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/SerializedHttpEntity.java
rename to
juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/SerializedEntity.java
index 5d6dd85..950c89a 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/SerializedHttpEntity.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/SerializedEntity.java
@@ -28,30 +28,49 @@ import org.apache.juneau.serializer.*;
* HttpEntity for serializing POJOs as the body of HTTP requests.
*/
@FluentSetters
-public class SerializedHttpEntity extends BasicHttpEntity {
+public class SerializedEntity extends AbstractHttpEntity {
+ private final Supplier<?> content;
private final Serializer serializer;
private HttpPartSchema schema;
/**
- * Creator.
+ * Creates a new {@link SerializedEntity} object.
*
- * @param content The POJO to serialize. Can also be a {@link Reader}
or {@link InputStream}.
- * @param serializer The serializer to use to serialize this response.
- * @return A new {@link SerializedHttpEntity} with uninitialized
serializer and schema.
+ * @param content
+ * The Java POJO representing the content.
+ * <br>Can be <jk>null<jk>.
+ * @param serializer
+ * The serializer to use to serialize the POJO.
+ * <br>If <jk>null</jk>, POJO will be converted to a string using
{@link Object#toString()}.
+ * @return A new {@link SerializedEntity} object.
+ */
+ public static SerializedEntity of(Object content, Serializer
serializer) {
+ return new SerializedEntity(content, serializer);
+ }
+
+ /**
+ * Creates a new {@link SerializedEntity} object.
+ *
+ * @param content
+ * The supplier for a Java POJO representing the content.
+ * <br>Can be <jk>null<jk>.
+ * @param serializer
+ * The serializer to use to serialize the POJO.
+ * <br>If <jk>null</jk>, POJO will be converted to a string using
{@link Object#toString()}.
+ * @return A new {@link SerializedEntity} object.
*/
- public static SerializedHttpEntity of(Object content, Serializer
serializer) {
- return new SerializedHttpEntity(content, serializer);
+ public static SerializedEntity of(Supplier<?> content, Serializer
serializer) {
+ return new SerializedEntity(content == null ? ()->null :
content, serializer);
}
/**
- * Creator.
+ * Constructor.
*
* @param content The POJO to serialize. Can also be a {@link Reader}
or {@link InputStream}.
* @param serializer The serializer to use to serialize this response.
- * @return A new {@link SerializedHttpEntity} with uninitialized
serializer and schema.
*/
- public static SerializedHttpEntity of(Supplier<?> content, Serializer
serializer) {
- return new SerializedHttpEntity(content, serializer);
+ public SerializedEntity(Object content, Serializer serializer) {
+ this(()->content, serializer);
}
/**
@@ -60,9 +79,11 @@ public class SerializedHttpEntity extends BasicHttpEntity {
* @param content The POJO to serialize. Can also be a {@link Reader}
or {@link InputStream}.
* @param serializer The serializer to use to serialize this response.
*/
- public SerializedHttpEntity(Object content, Serializer serializer) {
- super(content, ContentType.of(serializer == null ? null :
serializer.getResponseContentType()), null);
+ public SerializedEntity(Supplier<?> content, Serializer serializer) {
+ this.content = content;
this.serializer = serializer;
+ if (serializer != null)
+
setContentType(ContentType.of(serializer.getPrimaryMediaType()));
}
/**
@@ -75,122 +96,105 @@ public class SerializedHttpEntity extends BasicHttpEntity
{
* @return This object (for method chaining).
*/
@FluentSetter
- public SerializedHttpEntity schema(HttpPartSchema value) {
+ public SerializedEntity schema(HttpPartSchema value) {
this.schema = value;
return this;
}
- @Override /* BasicHttpEntity */
+ @Override /* HttpEntity */
public void writeTo(OutputStream os) throws IOException {
- if (isSerializable()) {
- try {
- os = new NoCloseOutputStream(os);
- Object o = getRawContent();
- if (serializer == null) {
- os.write(o.toString().getBytes());
- os.close();
- } else {
- SerializerSessionArgs sArgs =
SerializerSessionArgs.create().schema(schema);
- SerializerSession session =
serializer.createSession(sArgs);
- try (Closeable c =
session.isWriterSerializer() ? new OutputStreamWriter(os, UTF8) : os) {
- session.serialize(o, c);
- }
+ try {
+ os = new NoCloseOutputStream(os);
+ Object o = content.get();
+ if (serializer == null) {
+ try (Writer w = new OutputStreamWriter(os,
UTF8)) {
+ w.write(o.toString());
+ }
+ } else {
+ SerializerSessionArgs sArgs =
SerializerSessionArgs.create().schema(schema);
+ SerializerSession session =
serializer.createSession(sArgs);
+ try (Closeable c = session.isWriterSerializer()
? new OutputStreamWriter(os, UTF8) : os) {
+ session.serialize(o, c);
}
- } catch (SerializeException e) {
- throw new BasicRuntimeException(e,
"Serialization error on request body.");
}
- } else {
- super.writeTo(os);
+ } catch (SerializeException e) {
+ throw new BasicRuntimeException(e, "Serialization error
on request body.");
}
}
@Override /* BasicHttpEntity */
public boolean isRepeatable() {
- if (isSerializable())
- return true;
- return super.isRepeatable();
+ return true;
}
@Override /* BasicHttpEntity */
public long getContentLength() {
- if (isSerializable())
- return -1;
- return super.getContentLength();
+ return -1;
}
@Override /* BasicHttpEntity */
public InputStream getContent() {
- if (isSerializable()) {
- ByteArrayOutputStream baos = new
ByteArrayOutputStream();
- try {
- writeTo(baos);
- return new
ByteArrayInputStream(baos.toByteArray());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try {
+ writeTo(baos);
+ return new ByteArrayInputStream(baos.toByteArray());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
}
- return super.getContent();
- }
-
- private boolean isSerializable() {
- Object o = getRawContent();
- return ! (o instanceof InputStream || o instanceof Reader || o
instanceof File);
}
// <FluentSetters>
- @Override /* GENERATED - BasicHttpEntity */
- public SerializedHttpEntity cache() {
- super.cache();
- return this;
- }
-
- @Override /* GENERATED - BasicHttpEntity */
- public SerializedHttpEntity cache(boolean value) {
- super.cache(value);
+ @Override /* AbstractHttpEntity */
+ public SerializedEntity cache() {
return this;
}
@Override /* GENERATED - BasicHttpEntity */
- public SerializedHttpEntity chunked() {
+ public SerializedEntity chunked() {
super.chunked();
return this;
}
@Override /* GENERATED - BasicHttpEntity */
- public SerializedHttpEntity chunked(boolean value) {
+ public SerializedEntity chunked(boolean value) {
super.chunked(value);
return this;
}
@Override /* GENERATED - BasicHttpEntity */
- public SerializedHttpEntity contentEncoding(String value) {
+ public SerializedEntity contentEncoding(String value) {
super.contentEncoding(value);
return this;
}
@Override /* GENERATED - BasicHttpEntity */
- public SerializedHttpEntity contentEncoding(Header value) {
+ public SerializedEntity contentEncoding(Header value) {
super.contentEncoding(value);
return this;
}
@Override /* GENERATED - BasicHttpEntity */
- public SerializedHttpEntity contentLength(long value) {
+ public SerializedEntity contentLength(long value) {
super.contentLength(value);
return this;
}
@Override /* GENERATED - BasicHttpEntity */
- public SerializedHttpEntity contentType(String value) {
+ public SerializedEntity contentType(String value) {
super.contentType(value);
return this;
}
@Override /* GENERATED - BasicHttpEntity */
- public SerializedHttpEntity contentType(Header value) {
+ public SerializedEntity contentType(Header value) {
super.contentType(value);
return this;
+ }
+
+ @Override
+ public boolean isStreaming() {
+ return false;
}
// </FluentSetters>
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/StringEntity.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/StringEntity.java
index 2093372..9cf5e98 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/StringEntity.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/http/entity/StringEntity.java
@@ -13,12 +13,10 @@
package org.apache.juneau.http.entity;
import static org.apache.juneau.assertions.Assertions.*;
+import static org.apache.juneau.internal.IOUtils.*;
import static org.apache.juneau.internal.StringUtils.*;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.*;
import java.nio.charset.Charset;
import java.util.concurrent.atomic.*;
@@ -34,51 +32,60 @@ import org.apache.juneau.internal.*;
public class StringEntity extends AbstractHttpEntity {
private final String content;
- private final Charset charset;
- private final AtomicReference<byte[]> bytes = new AtomicReference<>();
+ private final AtomicReference<byte[]> cache = new AtomicReference<>();
+ private Charset charset = UTF8;
/**
* Creates a new {@link StringEntity} object.
*
- * <p>
- * Assumes {@link ContentType#TEXT_PLAIN TEXT/PLAIN} content type and
<js>"UTF-8"</js> encoding.
- *
* @param content The entity content. Can be <jk>null</jk>.
* @return A new {@link StringEntity} object.
*/
public static StringEntity of(String content) {
- return new StringEntity(content, ContentType.TEXT_PLAIN,
IOUtils.UTF8);
+ return new StringEntity(content, null);
}
/**
* Creates a new {@link StringEntity} object.
*
* @param content The entity content. Can be <jk>null</jk>.
- * @param contentType The entity content type, or {@link
ContentType#TEXT_PLAIN} if not specified.
- * @param charset The content character encoding, or <js>"UTF-8"</js>
if not specified.
+ * @param contentType The entity content type, or <jk>null</jk> if not
specified.
* @return A new {@link StringEntity} object.
*/
- public static StringEntity of(String content, ContentType contentType,
Charset charset) {
- return new StringEntity(content, contentType, null);
+ public static StringEntity of(String content, ContentType contentType) {
+ return new StringEntity(content, contentType);
}
/**
* Constructor.
*
* @param content The entity content. Can be <jk>null</jk>.
- * @param contentType The entity content type, or {@link
ContentType#TEXT_PLAIN} if not specified.
- * @param charset The content character encoding, or <js>"UTF-8"</js>
if not specified.
+ * @param contentType The entity content type, or <jk>null</jk> if not
specified.
*/
- public StringEntity(String content, ContentType contentType, Charset
charset) {
+ public StringEntity(String content, ContentType contentType) {
this.content = emptyIfNull(content);
setContentType(contentType);
- this.charset = charset;
+ }
+
+ /**
+ * Specifies the charset to use for the output.
+ *
+ * <p>
+ * The default is <js>"UTF-8"</js>.
+ *
+ * @param value The new charset, or <js>"UTF-8"</js> if <jk>null</jk>.
+ * @return This object (for method chaining).
+ */
+ @FluentSetter
+ public StringEntity charset(Charset value) {
+ this.charset = value == null ? UTF8 : value;
+ return this;
}
@Override
public byte[] asBytes() throws IOException {
cache();
- return bytes.get();
+ return cache.get();
}
@Override /* AbstractHttpEntity */
@@ -110,8 +117,9 @@ public class StringEntity extends AbstractHttpEntity {
@Override /* HttpEntity */
public void writeTo(OutputStream out) throws IOException {
assertArgNotNull("out", out);
- out.write(getBytes());
- out.flush();
+ OutputStreamWriter osw = new OutputStreamWriter(out, charset);
+ osw.write(content);
+ osw.flush();
}
@Override /* HttpEntity */
@@ -120,11 +128,11 @@ public class StringEntity extends AbstractHttpEntity {
}
private byte[] getBytes() {
- byte[] b = bytes.get();
+ byte[] b = cache.get();
if (b == null) {
- synchronized(bytes) {
- b = content.getBytes(charset == null ?
IOUtils.UTF8 : charset);
- bytes.set(b);
+ synchronized(cache) {
+ b = content.getBytes(charset);
+ cache.set(b);
}
}
return b;
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/IOUtils.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/IOUtils.java
index 7842b10..1e301b1 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/IOUtils.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/IOUtils.java
@@ -34,6 +34,20 @@ public final class IOUtils {
}
};
+ /** Reusable empty reader. */
+ public static final Reader EMPTY_READER = new Reader() {
+ @Override
+ public int read() {
+ return -1; // end of stream
+ }
+ @Override
+ public int read(char[] cbuf, int off, int len) throws
IOException {
+ return -1; // end of stream
+ }
+ @Override
+ public void close() throws IOException {}
+ };
+
private static final int BUFF_SIZE = 1024;
//-----------------------------------------------------------------------------------------------------------------
diff --git
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
index 3183482..8ecb2b7 100644
---
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
+++
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
@@ -491,9 +491,9 @@ import org.apache.juneau.utils.*;
* <li class='jc'>
* {@link InputStream} - Raw contents of {@code
InputStream} will be serialized to remote resource.
* <li class='jc'>
- * {@link HttpResource}/{@link BasicHttpResource} - Raw
contents will be serialized to remote resource. Additional headers and media
type will be set on request.
+ * {@link HttpResource} - Raw contents will be serialized
to remote resource. Additional headers and media type will be set on request.
* <li class='jc'>
- * {@link HttpEntity}/{@link BasicHttpEntity} - Bypass
Juneau serialization and pass HttpEntity directly to HttpClient.
+ * {@link HttpEntity} - Bypass Juneau serialization and
pass HttpEntity directly to HttpClient.
* <li class='jc'>
* {@link PartList} - Converted to a URL-encoded FORM post.
* <li class='jc'>
@@ -2510,9 +2510,9 @@ public class RestClient extends BeanContext implements
HttpClient, Closeable, Re
* <li>{@link NameValuePair} array - URL-encoded as name
value pairs.
* <li>{@link PartList} - URL-encoded as name value pairs.
* <li>{@link Reader}/{@link InputStream}- Streamed
directly and <l>Content-Type</l> set to
<js>"application/x-www-form-urlencoded"</js>
- * <li>{@link HttpResource}/{@link BasicHttpResource} -
Raw contents will be serialized to remote resource. Additional headers and
media type will be set on request.
- * <li>{@link HttpEntity}/{@link BasicHttpEntity} - Bypass
Juneau serialization and pass HttpEntity directly to HttpClient.
- * <li>{@link Object} - Converted to a {@link
SerializedHttpEntity} using {@link UrlEncodingSerializer} to serialize.
+ * <li>{@link HttpResource} - Raw contents will be
serialized to remote resource. Additional headers and media type will be set
on request.
+ * <li>{@link HttpEntity} - Bypass Juneau serialization
and pass HttpEntity directly to HttpClient.
+ * <li>{@link Object} - Converted to a {@link
SerializedEntity} using {@link UrlEncodingSerializer} to serialize.
* <li>{@link Supplier} - A supplier of anything on this
list.
* </ul>
* @return
@@ -2543,7 +2543,7 @@ public class RestClient extends BeanContext implements
HttpClient, Closeable, Re
}
if (body instanceof Reader || body instanceof
InputStream)
return
req.contentType("application/x-www-form-urlencoded").body(body);
- return req.body(SerializedHttpEntity.of(body,
urlEncodingSerializer));
+ return req.body(SerializedEntity.of(body,
urlEncodingSerializer));
} catch (IOException e) {
throw new RestCallException(null, e, "Could not read
form post body.");
}
@@ -2620,9 +2620,9 @@ public class RestClient extends BeanContext implements
HttpClient, Closeable, Re
* <li>
* {@link InputStream} - Raw contents of {@code
InputStream} will be serialized to remote resource.
* <li>
- * {@link HttpResource}/{@link BasicHttpResource}
- Raw contents will be serialized to remote resource. Additional headers and
media type will be set on request.
+ * {@link HttpResource} - Raw contents will be
serialized to remote resource. Additional headers and media type will be set
on request.
* <li>
- * {@link HttpEntity}/{@link BasicHttpEntity} -
Bypass Juneau serialization and pass HttpEntity directly to HttpClient.
+ * {@link HttpEntity} - Bypass Juneau
serialization and pass HttpEntity directly to HttpClient.
* <li>
* {@link Object} - POJO to be converted to text
using the {@link Serializer} registered with the
* {@link RestClient}.
@@ -2804,9 +2804,9 @@ public class RestClient extends BeanContext implements
HttpClient, Closeable, Re
* <li>
* {@link InputStream} - Raw contents of {@code
InputStream} will be serialized to remote resource.
* <li>
- * {@link HttpResource}/{@link BasicHttpResource}
- Raw contents will be serialized to remote resource. Additional headers and
media type will be set on request.
+ * {@link HttpResource} - Raw contents will be
serialized to remote resource. Additional headers and media type will be set
on request.
* <li>
- * {@link HttpEntity}/{@link BasicHttpEntity} -
Bypass Juneau serialization and pass HttpEntity directly to HttpClient.
+ * {@link HttpEntity} - Bypass Juneau
serialization and pass HttpEntity directly to HttpClient.
* <li>
* {@link Object} - POJO to be converted to text
using the {@link Serializer} registered with the
* {@link RestClient}.
diff --git
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestRequest.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestRequest.java
index 5b7d958..f057b4f 100644
---
a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestRequest.java
+++
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestRequest.java
@@ -2905,11 +2905,11 @@ public class RestRequest extends BeanSession implements
HttpUriRequest, Configur
else if (input2 instanceof HttpEntity)
entity = (HttpEntity)input2;
else if (input2 instanceof Reader)
- entity =
stringEntity(read((Reader)input2), getRequestContentType(TEXT_PLAIN), null);
+ entity =
stringEntity(read((Reader)input2), getRequestContentType(TEXT_PLAIN));
else if (input2 instanceof InputStream)
entity =
streamEntity((InputStream)input2, -1,
getRequestContentType(ContentType.APPLICATION_OCTET_STREAM));
else if (serializer != null)
- entity =
SerializedHttpEntity.of(input2,
serializer).schema(requestBodySchema).contentType(contentType);
+ entity = SerializedEntity.of(input2,
serializer).schema(requestBodySchema).contentType(contentType);
else {
if (client.hasSerializers()) {
if (contentType == null)
@@ -2918,7 +2918,7 @@ public class RestRequest extends BeanSession implements
HttpUriRequest, Configur
}
if (input2 == null)
input2 = "";
- entity =
stringEntity(BeanContext.DEFAULT.getClassMetaForObject(input2).toString(input2),
getRequestContentType(TEXT_PLAIN), null);
+ entity =
stringEntity(BeanContext.DEFAULT.getClassMetaForObject(input2).toString(input2),
getRequestContentType(TEXT_PLAIN));
}
request2.setEntity(entity);
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/http/BasicHttpEntity_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/http/BasicHttpEntity_Test.java
deleted file mode 100644
index 93b0745..0000000
---
a/juneau-utest/src/test/java/org/apache/juneau/http/BasicHttpEntity_Test.java
+++ /dev/null
@@ -1,212 +0,0 @@
-//
***************************************************************************************************************************
-// * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file *
-// * distributed with this work for additional information regarding copyright
ownership. The ASF licenses this file *
-// * to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance *
-// * with the License. You may obtain a copy of the License at
*
-// *
*
-// * http://www.apache.org/licenses/LICENSE-2.0
*
-// *
*
-// * Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an *
-// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the *
-// * specific language governing permissions and limitations under the
License. *
-//
***************************************************************************************************************************
-package org.apache.juneau.http;
-
-import static org.apache.juneau.assertions.Assertions.*;
-import static org.apache.juneau.http.entity.BasicHttpEntity.*;
-import static org.junit.Assert.*;
-
-import java.io.*;
-
-import org.apache.juneau.http.entity.*;
-import org.junit.*;
-
-public class BasicHttpEntity_Test {
- @Test
- public void a01_basic() throws Exception {
- BasicHttpEntity x = of(null);
- File f = File.createTempFile("test", "txt");
-
- assertNull(x.getContentType());
- assertNull(x.getContent());
- assertNull(x.getContentEncoding());
-
- x = of("foo");
- assertStream(x.getContent()).asString().is("foo");
- assertTrue(x.isRepeatable());
- assertFalse(x.isStreaming());
-
- x = of(new StringReader("foo"));
- assertStream(x.getContent()).asString().is("foo");
- assertFalse(x.isRepeatable());
- assertTrue(x.isStreaming());
-
- x = of("foo".getBytes());
- assertStream(x.getContent()).asString().is("foo");
- assertTrue(x.isRepeatable());
- assertFalse(x.isStreaming());
-
- x = of(new ByteArrayInputStream("foo".getBytes()));
- assertStream(x.getContent()).asString().is("foo");
- assertFalse(x.isRepeatable());
- assertTrue(x.isStreaming());
-
- x = of(null);
- assertStream(x.getContent()).asString().doesNotExist();
- assertFalse(x.isRepeatable());
- assertFalse(x.isStreaming());
-
- x = of(f);
- assertStream(x.getContent()).asString().isEmpty();
- assertTrue(x.isRepeatable());
- assertFalse(x.isStreaming());
-
-
- x = of(()->"foo");
- assertStream(x.getContent()).asString().is("foo");
- assertTrue(x.isRepeatable());
- assertFalse(x.isStreaming());
-
- x = of(()->new StringReader("foo"));
- assertStream(x.getContent()).asString().is("foo");
- assertFalse(x.isRepeatable());
- assertTrue(x.isStreaming());
-
- x = of(()->"foo".getBytes());
- assertStream(x.getContent()).asString().is("foo");
- assertTrue(x.isRepeatable());
- assertFalse(x.isStreaming());
-
- x = of(()->new ByteArrayInputStream("foo".getBytes()));
- assertStream(x.getContent()).asString().is("foo");
- assertFalse(x.isRepeatable());
- assertTrue(x.isStreaming());
-
- x = of(()->null);
- assertStream(x.getContent()).asString().doesNotExist();
- assertFalse(x.isRepeatable());
- assertFalse(x.isStreaming());
-
- x = of(()->f);
- assertStream(x.getContent()).asString().isEmpty();
- assertTrue(x.isRepeatable());
- assertFalse(x.isStreaming());
-
-
- x = of("foo").cache();
- assertStream(x.getContent()).asString().is("foo");
- assertStream(x.getContent()).asString().is("foo");
- assertTrue(x.isRepeatable());
-
- x = of(new StringReader("foo")).cache();
- assertStream(x.getContent()).asString().is("foo");
- assertStream(x.getContent()).asString().is("foo");
- assertTrue(x.isRepeatable());
-
- x = of("foo".getBytes()).cache();
- assertStream(x.getContent()).asString().is("foo");
- assertStream(x.getContent()).asString().is("foo");
- assertTrue(x.isRepeatable());
-
- x = of(new ByteArrayInputStream("foo".getBytes())).cache();
- assertStream(x.getContent()).asString().is("foo");
- assertStream(x.getContent()).asString().is("foo");
- assertTrue(x.isRepeatable());
-
- x = of(null).cache();
- assertStream(x.getContent()).asString().doesNotExist();
- assertStream(x.getContent()).asString().doesNotExist();
- assertTrue(x.isRepeatable());
- x.writeTo(new ByteArrayOutputStream());
-
- x = of(f).cache();
- assertStream(x.getContent()).asString().isEmpty();
- assertStream(x.getContent()).asString().isEmpty();
- assertTrue(x.isRepeatable());
- x.writeTo(new ByteArrayOutputStream());
-
- assertLong(of("foo").getContentLength()).is(3l);
- assertLong(of("foo".getBytes()).getContentLength()).is(3l);
- assertLong(of(f).getContentLength()).is(0l);
-
- assertLong(of(new
StringReader("foo")).getContentLength()).is(-1l);
- assertLong(of(new
StringReader("foo")).contentLength(3).getContentLength()).is(3l);
-
- BasicHttpEntity x2 = new BasicHttpEntity(new
StringReader("foo")) {
- @Override
- protected byte[] readBytes(Object o) throws IOException
{
- throw new IOException("bad");
- }
- };
- x2.cache();
- assertLong(x2.getContentLength()).is(-1l);
-
- assertThrown(()->x2.writeTo(new
ByteArrayOutputStream())).contains("bad");
- assertThrown(()->x2.getContent()).contains("bad");
- }
-
- @Test
- public void a02_contentType_String() throws Exception {
- BasicHttpEntity x1 = of("foo").contentType("text/plain");
- assertString(x1.getContentType().getValue()).is("text/plain");
- BasicHttpEntity x2 = of("foo").contentType((String)null);
- assertObject(x2.getContentType()).doesNotExist();
- }
-
- @Test
- public void a03_contentEncoding_String() throws Exception {
- BasicHttpEntity x1 = of("foo").contentEncoding("identity");
- assertString(x1.getContentEncoding().getValue()).is("identity");
- BasicHttpEntity x2 = of("foo").contentEncoding((String)null);
- assertObject(x2.getContentEncoding()).doesNotExist();
- }
-
- @Test
- public void a04_asString() throws Exception {
- BasicHttpEntity x1 = of(new StringReader("foo"));
- assertString(x1.asString()).is("foo");
- BasicHttpEntity x2 = of((String)null);
- assertString(x2.asString()).doesNotExist();
- }
-
- @Test
- public void a05_asBytes() throws Exception {
- BasicHttpEntity x1 = of(new StringReader("foo"));
- assertBytes(x1.asBytes()).asSpacedHex().is("66 6F 6F");
- BasicHttpEntity x2 = of((String)null);
- assertBytes(x2.asBytes()).doesNotExist();
- }
-
- @Test
- public void a06_assertString() throws Exception {
- BasicHttpEntity x1 = of(new StringReader("foo"));
- x1.assertString().is("foo");
- BasicHttpEntity x2 = of((String)null);
- x2.assertString().doesNotExist();
- }
-
- @Test
- public void a07_assertBytes() throws Exception {
- BasicHttpEntity x1 = of(new StringReader("foo"));
- x1.assertBytes().asSpacedHex().is("66 6F 6F");
- BasicHttpEntity x2 = of((String)null);
- x2.assertBytes().doesNotExist();
- }
-
- @Test
- public void a08_chunked() throws Exception {
- BasicHttpEntity x1 = of("foo").chunked();
- assertBoolean(x1.isChunked()).isTrue();
- BasicHttpEntity x2 = of("foo");
- assertBoolean(x2.isChunked()).isFalse();
- }
-
- @Test
- public void a09_chunked_boolean() throws Exception {
- BasicHttpEntity x1 = of("foo").chunked(true);
- assertBoolean(x1.isChunked()).isTrue();
- BasicHttpEntity x2 = of("foo").chunked(false);
- assertBoolean(x2.isChunked()).isFalse();
- }
-
-}
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/http/BasicHttpResource_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/http/BasicHttpResource_Test.java
index 45cf3db..46e2dc3 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/http/BasicHttpResource_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/http/BasicHttpResource_Test.java
@@ -24,6 +24,7 @@ import org.apache.juneau.http.entity.*;
import org.apache.juneau.http.header.*;
import org.junit.*;
+@SuppressWarnings("deprecation")
public class BasicHttpResource_Test {
@Test
public void a01_basic() throws Exception {
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/http/SerializedHttpEntity_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/http/SerializedHttpEntity_Test.java
index e02dabe..d143d6c 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/http/SerializedHttpEntity_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/http/SerializedHttpEntity_Test.java
@@ -31,7 +31,7 @@ import org.apache.juneau.rest.mock.*;
import org.apache.juneau.testutils.pojos.*;
import static org.apache.juneau.assertions.Assertions.*;
-import static org.apache.juneau.http.entity.SerializedHttpEntity.*;
+import static org.apache.juneau.http.entity.SerializedEntity.*;
import org.junit.*;
@@ -76,29 +76,25 @@ public class SerializedHttpEntity_Test {
@Test
public void a05_writeTo() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
- of(new ByteArrayInputStream("foo".getBytes()),
null).writeTo(baos);
+ of("foo", null).writeTo(baos);
assertBytes(baos.toByteArray()).asString().is("foo");
}
@Test
public void a06_isRepeatable() throws Exception {
assertBoolean(of(ABean.get(),null).isRepeatable()).isTrue();
- assertBoolean(of(new
ByteArrayInputStream("foo".getBytes()),null).isRepeatable()).isFalse();
- assertBoolean(of(new File(""),null).isRepeatable()).isTrue();
- assertBoolean(of(new
StringReader("foo"),null).isRepeatable()).isFalse();
}
@Test
public void a07_getContentLength() throws Exception {
assertLong(of(ABean.get(),null).getContentLength()).is(-1);
- assertLong(of(new
StringReader("foo"),null).cache().getContentLength()).is(3);
}
@Test
public void a08_getContent() throws Exception {
- assertStream(of(new
StringReader("foo"),null).getContent()).asString().is("foo");
+ assertStream(of("foo",null).getContent()).asString().is("foo");
- SerializedHttpEntity x = new SerializedHttpEntity("foo", null) {
+ SerializedEntity x = new SerializedEntity("foo", null) {
@Override
public void writeTo(OutputStream os) throws IOException
{
throw new IOException("Bad");
@@ -118,11 +114,6 @@ public class SerializedHttpEntity_Test {
}
@Test
- public void a11_contentLength() throws Exception {
- checkHeaderClient("Content-Length").post("/",of(new
StringReader("foo"),null).contentLength(3)).run().assertBody().is("['3']");
- }
-
- @Test
public void a12_contentType() throws Exception {
checkHeaderClient("Content-Type").post("/",of(new
StringReader("foo"),null).contentType("text/foo")).run().assertBody().is("['text/foo']");
}
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/http/remote/Remote_CommonInterfaces_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/http/remote/Remote_CommonInterfaces_Test.java
index 5b4677f..1edb429 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/http/remote/Remote_CommonInterfaces_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/http/remote/Remote_CommonInterfaces_Test.java
@@ -208,6 +208,7 @@ public class Remote_CommonInterfaces_Test {
}
}
+ @SuppressWarnings("deprecation")
@Test
public void d01_httpResource() throws Exception {
D x = MockRestClient.build(D1.class).getRemote(D.class);
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/rest/client/RestClient_BasicCalls_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/rest/client/RestClient_BasicCalls_Test.java
index 9a340bd..435fb63 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/rest/client/RestClient_BasicCalls_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/rest/client/RestClient_BasicCalls_Test.java
@@ -312,8 +312,8 @@ public class RestClient_BasicCalls_Test {
/*[ 0]*/ bean,
/*[ 1]*/ parts("f","1"),
/*[ 2]*/ new NameValuePair[]{part("f","1")},
- /*[ 3]*/ stringEntity("f=1",
ContentType.APPLICATION_FORM_URLENCODED, null),
- /*[ 4]*/ stringEntity("f=1", null, null),
+ /*[ 3]*/ stringEntity("f=1",
ContentType.APPLICATION_FORM_URLENCODED),
+ /*[ 4]*/ stringEntity("f=1", null),
/*[ 5]*/ part("f","1"),
/*[ 6]*/ BasicHttpResource.of("f=1"),
/*[ 7]*/ BasicHttpResource.of("f=1"),
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/rest/client/RestClient_Body_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/rest/client/RestClient_Body_Test.java
index 59a98b3..54dcedf 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/rest/client/RestClient_Body_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/rest/client/RestClient_Body_Test.java
@@ -14,6 +14,7 @@ package org.apache.juneau.rest.client;
import static org.apache.juneau.assertions.Assertions.*;
import static org.apache.juneau.http.HttpHeaders.*;
+import static org.apache.juneau.http.HttpEntities.*;
import static org.junit.runners.MethodSorters.*;
import java.io.*;
@@ -104,8 +105,8 @@ public class RestClient_Body_Test {
}
@Test
- public void a02_BasicHttpEntity() throws Exception {
- BasicHttpEntity x1 = httpEntity("foo");
+ public void a02_StringEntity() throws Exception {
+ HttpEntity x1 = stringEntity("foo");
client().build().post("/", x1).run()
.assertHeader("X-Content-Length").asInteger().is(3)
.assertHeader("X-Content-Encoding").doesNotExist()
@@ -113,7 +114,7 @@ public class RestClient_Body_Test {
.assertHeader("X-Transfer-Encoding").doesNotExist()
;
- BasicHttpEntity x2 =
httpEntity("foo").contentType("text/plain").contentEncoding("identity");
+ HttpEntity x2 =
stringEntity("foo").contentType("text/plain").contentEncoding("identity");
client().build().post("/",x2).run()
.assertHeader("X-Content-Length").asInteger().is(3)
.assertHeader("X-Content-Encoding").is("identity")
@@ -121,7 +122,7 @@ public class RestClient_Body_Test {
.assertHeader("X-Transfer-Encoding").doesNotExist()
;
- BasicHttpEntity x3 =
httpEntity("foo").contentType(contentType("text/plain")).contentEncoding(contentEncoding("identity")).chunked();
+ HttpEntity x3 =
stringEntity("foo").contentType(contentType("text/plain")).contentEncoding(contentEncoding("identity")).chunked();
client().build().post("/",x3).run()
.assertHeader("X-Content-Length").doesNotExist() //
Missing when chunked.
.assertHeader("X-Content-Encoding").is("identity")
@@ -129,7 +130,7 @@ public class RestClient_Body_Test {
.assertHeader("X-Transfer-Encoding").is("chunked")
;
- BasicHttpEntity x4 = new BasicHttpEntity("foo",
contentType("text/plain"), contentEncoding("identity"));
+ HttpEntity x4 = stringEntity("foo",
contentType("text/plain")).contentEncoding("identity");
client().build().post("/",x4).run()
.assertHeader("X-Content-Length").asInteger().is(3)
.assertHeader("X-Content-Encoding").is("identity")
@@ -137,17 +138,17 @@ public class RestClient_Body_Test {
.assertHeader("X-Transfer-Encoding").doesNotExist()
;
- BasicHttpEntity x7 = httpEntity(new StringReader("foo"));
+ HttpEntity x7 = readerEntity(new StringReader("foo"));
client().build().post("/",x7).run().assertBody().is("foo");
- BasicHttpEntity x8 = httpEntity(new
StringReader("foo")).cache();
+ HttpEntity x8 = readerEntity(new StringReader("foo")).cache();
client().build().post("/",x8).run().assertBody().is("foo");
client().build().post("/",x8).run().assertBody().is("foo");
- BasicHttpEntity x9 = httpEntity(null);
+ HttpEntity x9 = readerEntity(null);
client().build().post("/",x9).run().assertBody().isEmpty();
- BasicHttpEntity x12 = httpEntity("foo");
+ AbstractHttpEntity x12 = stringEntity("foo");
x12.assertString().is("foo");
x12.assertBytes().asString().is("foo");
}
@@ -155,9 +156,8 @@ public class RestClient_Body_Test {
@Test
public void a03_SerializedHttpEntity() throws Exception {
Serializer js = JsonSerializer.DEFAULT;
- File f = File.createTempFile("test", "txt");
- SerializedHttpEntity x1 =
serializedHttpEntity(ABean.get(),null);
+ SerializedEntity x1 = serializedEntity(ABean.get(),null);
client().build().post("/",x1).run()
.assertHeader("X-Content-Length").doesNotExist()
.assertHeader("X-Content-Encoding").doesNotExist()
@@ -165,61 +165,21 @@ public class RestClient_Body_Test {
.assertHeader("X-Transfer-Encoding").is("chunked") //
Because content length is -1.
;
- SerializedHttpEntity x2 = serializedHttpEntity(ABean.get(),js);
+ SerializedEntity x2 = serializedEntity(ABean.get(),js);
client().build().post("/",x2).run()
.assertHeader("X-Content-Length").doesNotExist()
.assertHeader("X-Content-Encoding").doesNotExist()
.assertHeader("X-Content-Type").is("application/json")
.assertBody().asType(ABean.class).asJson().is("{a:1,b:'foo'}");
- SerializedHttpEntity x3 =
SerializedHttpEntity.of(()->ABean.get(),js);
+ SerializedEntity x3 = SerializedEntity.of(()->ABean.get(),js);
client().build().post("/",x3).run()
.assertHeader("X-Content-Length").doesNotExist()
.assertHeader("X-Content-Encoding").doesNotExist()
.assertHeader("X-Content-Type").is("application/json")
.assertBody().asType(ABean.class).asJson().is("{a:1,b:'foo'}");
- SerializedHttpEntity x4 = serializedHttpEntity(new
StringReader("{a:1,b:'foo'}"),null);
- client().build().post("/",x4).run()
- .assertHeader("X-Content-Length").doesNotExist()
- .assertHeader("X-Content-Encoding").doesNotExist()
- .assertHeader("X-Content-Type").doesNotExist()
-
.assertBody().asType(ABean.class).asJson().is("{a:1,b:'foo'}");
-
- SerializedHttpEntity x5 = serializedHttpEntity(new
ByteArrayInputStream("{a:1,b:'foo'}".getBytes()),null);
- client().build().post("/",x5).run()
- .assertHeader("X-Content-Length").doesNotExist()
- .assertHeader("X-Content-Encoding").doesNotExist()
- .assertHeader("X-Content-Type").doesNotExist()
-
.assertBody().asType(ABean.class).asJson().is("{a:1,b:'foo'}");
-
- SerializedHttpEntity x6 = serializedHttpEntity(f,null);
- client().build().post("/",x6).run()
- .assertHeader("X-Content-Length").is("0")
- .assertHeader("X-Content-Encoding").doesNotExist()
- .assertHeader("X-Content-Type").doesNotExist()
- .assertBody().asType(ABean.class).asJson().is("{a:0}");
-
- InputStream x7 = new ByteArrayInputStream("foo".getBytes()) {
- @Override
- public int read(byte[] b, int offset, int length) {
- throw new RuntimeException("bad");
- }
- };
-
- SerializedHttpEntity x8 = new SerializedHttpEntity(x7,
null).cache();
- assertThrown(()->x8.getContent()).contains("bad");
-
- SerializedHttpEntity x9 = serializedHttpEntity(new
StringReader("foo"), null);
- assertStream(x9.getContent()).asString().is("foo");
-
- SerializedHttpEntity x10 = serializedHttpEntity(new
ByteArrayInputStream("foo".getBytes()), null);
- assertStream(x10.getContent()).asString().is("foo");
-
- SerializedHttpEntity x11 = serializedHttpEntity(f, null);
- assertStream(x11.getContent()).asString().is("");
-
- SerializedHttpEntity x12 = new
SerializedHttpEntity(ABean.get(), null) {
+ SerializedEntity x12 = new SerializedEntity(ABean.get(), null) {
@Override
public void writeTo(OutputStream os) throws IOException
{
throw new IOException("bad");
@@ -227,18 +187,6 @@ public class RestClient_Body_Test {
};
assertThrown(()->x12.getContent()).contains("bad");
-
- SerializedHttpEntity x13 = serializedHttpEntity(new
StringReader("foo"), null).chunked();
- client().build().post("/",x13).run()
- .assertHeader("X-Transfer-Encoding").is("chunked");
-
- SerializedHttpEntity x14 = serializedHttpEntity(new
StringReader("foo"), null).contentEncoding("identity");
- client().build().post("/",x14).run()
- .assertHeader("X-Content-Encoding").is("identity");
-
- SerializedHttpEntity x15 = serializedHttpEntity(new
StringReader("foo"), null).contentLength(3l);
- client().build().post("/",x15).run()
- .assertHeader("X-Content-Length").is("3");
}
//------------------------------------------------------------------------------------------------------------------
@@ -249,14 +197,6 @@ public class RestClient_Body_Test {
return BasicHttpResource.of(val);
}
- private static BasicHttpEntity httpEntity(Object val) {
- return BasicHttpEntity.of(val);
- }
-
- private static SerializedHttpEntity serializedHttpEntity(Object val,
Serializer s) {
- return SerializedHttpEntity.of(val, s);
- }
-
private static RestClientBuilder client() {
return MockRestClient.create(A.class).simpleJson();
}