Announcement Announcement Module
Collapse
No announcement yet.
Gzip requests bug Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Gzip requests bug

    Hi,

    I am trying to gzip my requests (json/jackson) to the server by
    using something like
    Code:
    requestHeaders.setContentEncoding(Collections.singletonList(ContentCodingType.GZIP));
    But I am having troubles doing that, and I think it is a spring bug.

    I.e
    those lines

    Code:
    JsonGenerator jsonGenerator =
    				this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);
    		try {
    			if (this.prefixJson) {
    				jsonGenerator.writeRaw("{} && ");
    			}
    			this.objectMapper.writeValue(jsonGenerator, o);
    		}
    convert my object into a string and write it to outputMessage.getBody();

    The problem I believe is in AbstractClientHttpRequest that returns a gzipoutputstream

    Code:
    public final OutputStream getBody() throws IOException {
    		checkExecuted();
    		OutputStream body = getBodyInternal(this.headers);
    		List<ContentCodingType> contentCodingTypes = this.getHeaders().getContentEncoding();
    		for (ContentCodingType contentCodingType : contentCodingTypes) {
    			if (contentCodingType.equals(ContentCodingType.GZIP)) {
    				return new GZIPOutputStream(body);
    			}
    		}
    		return body;
    	}
    which is never being closed (only flushed)

    in AbstractHttpMessageConverter<T>
    Code:
    public final void write(T t, MediaType contentType, HttpOutputMessage outputMessage)
    			throws IOException, HttpMessageNotWritableException {
    
    		HttpHeaders headers = outputMessage.getHeaders();
    		if (headers.getContentType() == null) {
    			if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
    				contentType = getDefaultContentType(t);
    			}
    			if (contentType != null) {
    				headers.setContentType(contentType);
    			}
    		}
    		if (headers.getContentLength() == -1) {
    			Long contentLength = getContentLength(t, contentType);
    			if (contentLength != null) {
    				headers.setContentLength(contentLength);
    			}
    		}
    		writeInternal(t, outputMessage);
    		outputMessage.getBody().flush();
    	}
    I think adding
    outputMessage.getBody().close()
    will solve the problem.

    In addition I think it's inefficient in case of gzip to create a new object in every call to getBody.

    Can you confirm, it's a bug, or I am doing something incorrectly.

  • #2
    This looks like it is a bug. I've opened a couple JIRAs to review this functionality.

    https://jira.springsource.org/browse/ANDROID-59
    https://jira.springsource.org/browse/ANDROID-60

    As a note, I'm exploring transitioning to the AndroidHttpClient (based on the apache DefaultHttpClient) which has built in support for gzip responses. This is only available in Android 2.2 and higher, though. HttpURLConnection also supports gzip responses, so I think it is worth exploring leveraging these where available. Of course, sending gzip requests to the server is a different situation.

    Comment

    Working...
    X