REST: Add newline to response when using pretty flag

closes #3748
relates to #3422
This commit is contained in:
Shay Banon 2013-09-20 14:55:21 +02:00
parent 359d14ddc5
commit c3ecd6fd1b
6 changed files with 31 additions and 1 deletions

View File

@ -109,6 +109,11 @@ public final class XContentBuilder implements BytesStream {
return this;
}
public XContentBuilder lfAtEnd() {
generator.usePrintLineFeedAtEnd();
return this;
}
public XContentBuilder humanReadable(boolean humanReadable) {
this.humanReadable = humanReadable;
return this;

View File

@ -34,6 +34,8 @@ public interface XContentGenerator {
void usePrettyPrint();
void usePrintLineFeedAtEnd();
void writeStartArray() throws IOException;
void writeEndArray() throws IOException;

View File

@ -20,6 +20,7 @@
package org.elasticsearch.common.xcontent.json;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.io.SerializedString;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.xcontent.*;
@ -34,6 +35,7 @@ import java.io.OutputStream;
public class JsonXContentGenerator implements XContentGenerator {
protected final JsonGenerator generator;
private boolean writeLineFeedAtEnd;
public JsonXContentGenerator(JsonGenerator generator) {
this.generator = generator;
@ -49,6 +51,11 @@ public class JsonXContentGenerator implements XContentGenerator {
generator.useDefaultPrettyPrinter();
}
@Override
public void usePrintLineFeedAtEnd() {
writeLineFeedAtEnd = true;
}
@Override
public void writeStartArray() throws IOException {
generator.writeStartArray();
@ -320,6 +327,12 @@ public class JsonXContentGenerator implements XContentGenerator {
@Override
public void close() throws IOException {
if (writeLineFeedAtEnd) {
flush();
generator.writeRaw(LF);
}
generator.close();
}
private static final SerializedString LF = new SerializedString("\n");
}

View File

@ -43,6 +43,11 @@ public class SmileXContentGenerator extends JsonXContentGenerator {
return XContentType.SMILE;
}
@Override
public void usePrintLineFeedAtEnd() {
// nothing here
}
@Override
public void writeRawField(String fieldName, InputStream content, OutputStream bos) throws IOException {
writeFieldName(fieldName);

View File

@ -43,6 +43,11 @@ public class YamlXContentGenerator extends JsonXContentGenerator {
return XContentType.YAML;
}
@Override
public void usePrintLineFeedAtEnd() {
// nothing here
}
@Override
public void writeRawField(String fieldName, InputStream content, OutputStream bos) throws IOException {
writeFieldName(fieldName);

View File

@ -55,7 +55,7 @@ public class RestXContentBuilder {
}
XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(contentType), new BytesStreamOutput());
if (request.paramAsBoolean("pretty", false)) {
builder.prettyPrint();
builder.prettyPrint().lfAtEnd();
}
builder.humanReadable(request.paramAsBoolean("human", builder.humanReadable()));