Skip to content

Commit

Permalink
Fix Protobuf support - HTTP headers already written
Browse files Browse the repository at this point in the history
Prior to this commit, the `ProtobufHttpMessageConverter` would call
`outputMessage.getBody()` at the beginning of the `writeInternal`
method, thus writing HTTP headers. Since this method is trying to write
"x-protobuf" headers after that, protobuf support wasn't working
properly for the default "x-protobuf" media type.

Thanks Toshiaki Maki for the repro project!

Also fixed:
* improve `MockHttpOutputMessage` behavior to reproduce the read-only
state of HTTP headers once they've been written.

Issue: SPR-12229
  • Loading branch information
bclozel committed Sep 22, 2014
1 parent 2989fe4 commit b9348bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,18 @@ protected void writeInternal(Message message, HttpOutputMessage outputMessage)

MediaType contentType = outputMessage.getHeaders().getContentType();
Charset charset = getCharset(contentType);
OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(), charset);

if (MediaType.TEXT_HTML.isCompatibleWith(contentType)) {
HtmlFormat.print(message, writer);
HtmlFormat.print(message, new OutputStreamWriter(outputMessage.getBody(), charset));
}
else if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
JsonFormat.print(message, writer);
JsonFormat.print(message, new OutputStreamWriter(outputMessage.getBody(), charset));
}
else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
TextFormat.print(message, writer);
TextFormat.print(message, new OutputStreamWriter(outputMessage.getBody(), charset));
}
else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
XmlFormat.print(message, writer);
XmlFormat.print(message, new OutputStreamWriter(outputMessage.getBody(), charset));
}
else if (PROTOBUF.isCompatibleWith(contentType)) {
setProtoHeader(outputMessage, message);
Expand All @@ -191,6 +190,8 @@ private Charset getCharset(MediaType contentType) {
/**
* Set the "X-Protobuf-*" HTTP headers when responding with a message of
* content type "application/x-protobuf"
* <p><b>Note:</b> <code>outputMessage.getBody()</code> should not have been called
* before because it writes HTTP headers (making them read only).</p>
*/
private void setProtoHeader(HttpOutputMessage response, Message message) {
response.getHeaders().set(X_PROTOBUF_SCHEMA_HEADER, message.getDescriptorForType().getFile().getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,17 +32,21 @@ public class MockHttpOutputMessage implements HttpOutputMessage {

private final ByteArrayOutputStream body = spy(new ByteArrayOutputStream());

private boolean headersWritten = false;

@Override
public HttpHeaders getHeaders() {
return headers;
return (this.headersWritten ? HttpHeaders.readOnlyHttpHeaders(this.headers) : this.headers);
}

@Override
public OutputStream getBody() throws IOException {
this.headersWritten = true;
return body;
}

public byte[] getBodyAsBytes() {
this.headersWritten = true;
return body.toByteArray();
}

Expand Down

0 comments on commit b9348bb

Please sign in to comment.