Example usage for com.google.common.net MediaType subtype

List of usage examples for com.google.common.net MediaType subtype

Introduction

In this page you can find the example usage for com.google.common.net MediaType subtype.

Prototype

String subtype

To view the source code for com.google.common.net MediaType subtype.

Click Source Link

Usage

From source file:com.mastfrog.netty.http.client.RequestBuilder.java

@Override
public HttpRequestBuilder setBody(Object o, MediaType contentType) throws IOException {
    if (o instanceof CharSequence) {
        CharSequence seq = (CharSequence) o;
        setBody(seq.toString().getBytes(CharsetUtil.UTF_8), contentType);
    } else if (o instanceof byte[]) {
        byte[] b = (byte[]) o;
        ByteBuf buffer = alloc.buffer(b.length).writeBytes(b);
        setBody(buffer, contentType);//from w w w  .  j a  va 2  s  .c  om
    } else if (o instanceof ByteBuf) {
        body = (ByteBuf) o;
        if (send100Continue) {
            addHeader(Headers.stringHeader(HttpHeaders.Names.EXPECT), HttpHeaders.Values.CONTINUE);
        }
        addHeader(Headers.CONTENT_LENGTH, (long) body.readableBytes());
        addHeader(Headers.CONTENT_TYPE, contentType);
    } else if (o instanceof InputStream) {
        ByteBuf buf = newByteBuf();
        try (ByteBufOutputStream out = new ByteBufOutputStream(buf)) {
            try (InputStream in = (InputStream) o) {
                Streams.copy(in, out, 1024);
            }
        }
        setBody(buf, contentType);
    } else if (o instanceof RenderedImage) {
        ByteBuf buf = newByteBuf();
        try (ByteBufOutputStream out = new ByteBufOutputStream(buf)) {
            String type = contentType.subtype();
            if ("jpeg".equals(type)) {
                type = "jpg";
            }
            ImageIO.write((RenderedImage) o, type, out);
        }
        setBody(buf, contentType);
    } else {
        try {
            setBody(new ObjectMapper().writeValueAsBytes(o), contentType);
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }
    return this;
}