001package org.xbib.elasticsearch.http;
002
003import org.elasticsearch.common.io.stream.StreamInput;
004import org.elasticsearch.common.io.stream.StreamOutput;
005import org.elasticsearch.common.io.stream.Streamable;
006import org.elasticsearch.common.transport.BoundTransportAddress;
007import org.elasticsearch.common.unit.ByteSizeValue;
008import org.elasticsearch.common.xcontent.ToXContent;
009import org.elasticsearch.common.xcontent.XContentBuilder;
010import org.elasticsearch.common.xcontent.XContentBuilderString;
011
012import java.io.IOException;
013import java.io.Serializable;
014
015
016public class HttpInfo implements Streamable, Serializable, ToXContent {
017
018    private BoundTransportAddress address;
019    private long maxContentLength;
020
021    HttpInfo() {
022    }
023
024    public HttpInfo(BoundTransportAddress address, long maxContentLength) {
025        this.address = address;
026        this.maxContentLength = maxContentLength;
027    }
028
029    static final class Fields {
030        static final XContentBuilderString HTTP = new XContentBuilderString("http");
031        static final XContentBuilderString BOUND_ADDRESS = new XContentBuilderString("bound_address");
032        static final XContentBuilderString PUBLISH_ADDRESS = new XContentBuilderString("publish_address");
033        static final XContentBuilderString MAX_CONTENT_LENGTH = new XContentBuilderString("max_content_length");
034        static final XContentBuilderString MAX_CONTENT_LENGTH_IN_BYTES = new XContentBuilderString("max_content_length_in_bytes");
035    }
036
037    @Override
038    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
039        builder.startObject(Fields.HTTP);
040        builder.field(Fields.BOUND_ADDRESS, address.boundAddress().toString());
041        builder.field(Fields.PUBLISH_ADDRESS, address.publishAddress().toString());
042        builder.byteSizeField(Fields.MAX_CONTENT_LENGTH_IN_BYTES, Fields.MAX_CONTENT_LENGTH, maxContentLength);
043        builder.endObject();
044        return builder;
045    }
046
047    public static HttpInfo readHttpInfo(StreamInput in) throws IOException {
048        HttpInfo info = new HttpInfo();
049        info.readFrom(in);
050        return info;
051    }
052
053    @Override
054    public void readFrom(StreamInput in) throws IOException {
055        address = BoundTransportAddress.readBoundTransportAddress(in);
056        maxContentLength = in.readLong();
057    }
058
059    @Override
060    public void writeTo(StreamOutput out) throws IOException {
061        address.writeTo(out);
062        out.writeLong(maxContentLength);
063    }
064
065    public BoundTransportAddress address() {
066        return address;
067    }
068
069    public BoundTransportAddress getAddress() {
070        return address();
071    }
072
073    public ByteSizeValue maxContentLength() {
074        return new ByteSizeValue(maxContentLength);
075    }
076
077    public ByteSizeValue getMaxContentLength() {
078        return maxContentLength();
079    }
080}