1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package org.apache.hc.core5.http.io.entity; 29 30 import java.io.ByteArrayInputStream; 31 import java.io.ByteArrayOutputStream; 32 import java.io.IOException; 33 import java.io.InputStream; 34 import java.io.OutputStream; 35 36 import org.apache.hc.core5.annotation.Contract; 37 import org.apache.hc.core5.annotation.ThreadingBehavior; 38 import org.apache.hc.core5.http.ContentType; 39 import org.apache.hc.core5.io.IOCallback; 40 import org.apache.hc.core5.util.Args; 41 42 /** 43 * Entity that delegates the process of content generation to a {@link IOCallback} 44 * with {@link OutputStream} as output sink. 45 * <p> 46 * This class contains {@link ThreadingBehavior#IMMUTABLE_CONDITIONAL immutable attributes} but subclasses may contain 47 * additional immutable or mutable attributes. 48 * </p> 49 * 50 * @since 4.0 51 */ 52 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL) 53 public final class EntityTemplate extends AbstractHttpEntity { 54 55 private final long contentLength; 56 private final IOCallback<OutputStream> callback; 57 58 /** 59 * Constructs a new instance with the given attributes kept as immutable. 60 * <p> 61 * The new instance: 62 * </p> 63 * <ul> 64 * <li>is not chunked.</li> 65 * </ul> 66 * 67 * @param contentLength The value for the {@code Content-Length} header for the size of the message body, in bytes. 68 * @param contentType The content-type, may be null. 69 * @param contentEncoding The content encoding string, may be null. 70 * @param callback A consumer that write the message body to an output stream. 71 */ 72 public EntityTemplate( 73 final long contentLength, final ContentType contentType, final String contentEncoding, 74 final IOCallback<OutputStream> callback) { 75 super(contentType, contentEncoding); 76 this.contentLength = contentLength; 77 this.callback = Args.notNull(callback, "I/O callback"); 78 } 79 80 @Override 81 public long getContentLength() { 82 return contentLength; 83 } 84 85 @Override 86 public InputStream getContent() throws IOException { 87 final ByteArrayOutputStream buf = new ByteArrayOutputStream(); 88 writeTo(buf); 89 return new ByteArrayInputStream(buf.toByteArray()); 90 } 91 92 /** 93 * {@inheritDoc} 94 * <p> 95 * This implementation always returns {@code true}. 96 * </p> 97 */ 98 @Override 99 public boolean isRepeatable() { 100 return true; 101 } 102 103 @Override 104 public void writeTo(final OutputStream outStream) throws IOException { 105 Args.notNull(outStream, "Output stream"); 106 this.callback.execute(outStream); 107 } 108 109 /** 110 * {@inheritDoc} 111 * <p> 112 * This implementation always returns {@code false}. 113 * </p> 114 */ 115 @Override 116 public boolean isStreaming() { 117 return false; 118 } 119 120 /** 121 * {@inheritDoc} 122 * <p> 123 * This implementation is a no-op. 124 * </p> 125 */ 126 @Override 127 public void close() throws IOException { 128 } 129 130 }