You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2019/02/13 16:02:22 UTC

[httpcomponents-core] 03/03: Added HttpEntityTemplate

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch immutable-httpentities
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit 9153a9ca6550a1b054138790f2c95490bb74e252
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Wed Feb 13 17:01:43 2019 +0100

    Added HttpEntityTemplate
---
 .../core5/http/io/entity/HttpContentProducer.java  |  44 ---------
 .../core5/http/io/entity/HttpEntityTemplate.java   | 101 +++++++++++++++++++++
 2 files changed, 101 insertions(+), 44 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/HttpContentProducer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/HttpContentProducer.java
deleted file mode 100644
index 742426b..0000000
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/HttpContentProducer.java
+++ /dev/null
@@ -1,44 +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.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-
-package org.apache.hc.core5.http.io.entity;
-
-import java.io.IOException;
-import java.io.OutputStream;
-
-/**
- * An abstract entity content producer.
- *<p>Content producers are expected to be able to produce their
- * content multiple times</p>
- *
- * @since 5.0
- */
-public interface HttpContentProducer {
-
-    void writeTo(OutputStream outStream) throws IOException;
-
-}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/HttpEntityTemplate.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/HttpEntityTemplate.java
new file mode 100644
index 0000000..db4c94b
--- /dev/null
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/entity/HttpEntityTemplate.java
@@ -0,0 +1,101 @@
+/*
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http.io.entity;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.hc.core5.annotation.Contract;
+import org.apache.hc.core5.annotation.ThreadingBehavior;
+import org.apache.hc.core5.function.IOCallback;
+import org.apache.hc.core5.http.ContentType;
+
+/**
+ * A generic non-repeatable entity template that delegates its content generation to {@link IOCallback}.
+ *
+ * @since 5.0
+ */
+@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
+public final class HttpEntityTemplate extends AbstractHttpEntity {
+
+    private final long contentLength;
+    private final IOCallback<OutputStream> contentCallback;
+
+    public HttpEntityTemplate(
+            final long contentLength,
+            final ContentType contentType,
+            final String contentEncoding,
+            final IOCallback<OutputStream> contentCallback) {
+        super(contentType, contentEncoding, false);
+        this.contentLength = contentLength;
+        this.contentCallback = contentCallback;
+    }
+
+    public HttpEntityTemplate(
+            final long contentLength,
+            final ContentType contentType,
+            final IOCallback<OutputStream> contentCallback) {
+        this(contentLength, contentType, null, contentCallback);
+    }
+
+    public HttpEntityTemplate(final ContentType contentType, final IOCallback<OutputStream> contentCallback) {
+        this(-1, contentType, null, contentCallback);
+    }
+
+    @Override
+    public long getContentLength() {
+        return contentLength;
+    }
+
+    @Override
+    public InputStream getContent() throws UnsupportedOperationException {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public void writeTo(final OutputStream outStream) throws IOException {
+        contentCallback.execute(outStream);
+        outStream.flush();
+    }
+
+    @Override
+    public final boolean isRepeatable() {
+        return false;
+    }
+
+    @Override
+    public final boolean isStreaming() {
+        return false;
+    }
+
+    @Override
+    public final void close() throws IOException {
+    }
+
+}