001/*
002 * Copyright (C) 2014 Jörg Prante
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.xbib.elasticsearch.action.plugin.jdbc.state.put;
017
018import org.elasticsearch.action.support.master.AcknowledgedResponse;
019import org.elasticsearch.common.io.stream.StreamInput;
020import org.elasticsearch.common.io.stream.StreamOutput;
021import org.elasticsearch.common.xcontent.ToXContent;
022import org.elasticsearch.common.xcontent.XContentBuilder;
023
024import java.io.IOException;
025
026import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
027
028public class PutRiverStateResponse extends AcknowledgedResponse implements ToXContent {
029
030    public PutRiverStateResponse() {
031    }
032
033    public PutRiverStateResponse(boolean acknowledged) {
034        super(acknowledged);
035    }
036
037    @Override
038    public void readFrom(StreamInput in) throws IOException {
039        super.readFrom(in);
040        readAcknowledged(in);
041    }
042
043    @Override
044    public void writeTo(StreamOutput out) throws IOException {
045        super.writeTo(out);
046        writeAcknowledged(out);
047    }
048
049    @Override
050    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
051        builder.field("acknowledged", isAcknowledged());
052        return builder;
053    }
054
055    @Override
056    public String toString() {
057        try {
058            XContentBuilder builder = jsonBuilder();
059            builder.startObject();
060            builder = toXContent(builder, EMPTY_PARAMS);
061            builder.endObject();
062            return builder.string();
063        } catch (IOException e) {
064            return "";
065        }
066    }
067}