Search in sources :

Example 1 with TemporaryStream

use of lucee.commons.io.TemporaryStream in project Lucee by lucee.

the class HTTPEngine4Impl method toHttpEntity.

/**
 * convert input to HTTP Entity
 *
 * @param value
 * @param mimetype
 *            not used for binary input
 * @param charset
 *            not used for binary input
 * @return
 * @throws IOException
 */
private static HttpEntity toHttpEntity(Object value, String mimetype, String charset) throws IOException {
    if (value instanceof HttpEntity)
        return (HttpEntity) value;
    // content type
    ContentType ct = HTTPEngine.toContentType(mimetype, charset);
    try {
        if (value instanceof TemporaryStream) {
            if (ct != null)
                return new TemporaryStreamHttpEntity((TemporaryStream) value, ct);
            return new TemporaryStreamHttpEntity((TemporaryStream) value, null);
        } else if (value instanceof InputStream) {
            if (ct != null)
                return new ByteArrayEntity(IOUtil.toBytes((InputStream) value), ct);
            return new ByteArrayEntity(IOUtil.toBytes((InputStream) value));
        } else if (Decision.isCastableToBinary(value, false)) {
            if (ct != null)
                return new ByteArrayEntity(Caster.toBinary(value), ct);
            return new ByteArrayEntity(Caster.toBinary(value));
        } else {
            boolean wasNull = false;
            if (ct == null) {
                wasNull = true;
                ct = ContentType.APPLICATION_OCTET_STREAM;
            }
            String str = Caster.toString(value);
            if (str.equals("<empty>")) {
                return new EmptyHttpEntity(ct);
            }
            if (wasNull && !StringUtil.isEmpty(charset, true))
                return new StringEntity(str, charset.trim());
            else
                return new StringEntity(str, ct);
        }
    } catch (Exception e) {
        throw ExceptionUtil.toIOException(e);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) EmptyHttpEntity(lucee.commons.net.http.httpclient.entity.EmptyHttpEntity) TemporaryStreamHttpEntity(lucee.commons.net.http.httpclient.entity.TemporaryStreamHttpEntity) EmptyHttpEntity(lucee.commons.net.http.httpclient.entity.EmptyHttpEntity) HttpEntity(org.apache.http.HttpEntity) ResourceHttpEntity(lucee.commons.net.http.httpclient.entity.ResourceHttpEntity) ByteArrayHttpEntity(lucee.commons.net.http.httpclient.entity.ByteArrayHttpEntity) ContentType(org.apache.http.entity.ContentType) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) InputStream(java.io.InputStream) TemporaryStreamHttpEntity(lucee.commons.net.http.httpclient.entity.TemporaryStreamHttpEntity) TemporaryStream(lucee.commons.io.TemporaryStream) PageException(lucee.runtime.exp.PageException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 TemporaryStream (lucee.commons.io.TemporaryStream)1 ByteArrayHttpEntity (lucee.commons.net.http.httpclient.entity.ByteArrayHttpEntity)1 EmptyHttpEntity (lucee.commons.net.http.httpclient.entity.EmptyHttpEntity)1 ResourceHttpEntity (lucee.commons.net.http.httpclient.entity.ResourceHttpEntity)1 TemporaryStreamHttpEntity (lucee.commons.net.http.httpclient.entity.TemporaryStreamHttpEntity)1 PageException (lucee.runtime.exp.PageException)1 HttpEntity (org.apache.http.HttpEntity)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1 ContentType (org.apache.http.entity.ContentType)1 StringEntity (org.apache.http.entity.StringEntity)1