EntityReader.java 1 KB
package com.mobithink.tracesdk.rest.client;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

import org.apache.http.HttpEntity;
import org.apache.http.entity.HttpEntityWrapper;

/**
 * Read entity content to the target type.
 */
public abstract class EntityReader<T> {

	public static final String ENCODING_GZIP = "gzip";

	public abstract T read(HttpEntity entity);
	
	protected String debug;

	public String getDebug() {
		return this.debug;
	}

	public static class GzipDecompressingEntity extends HttpEntityWrapper {

		public GzipDecompressingEntity(final HttpEntity entity) {
			super(entity);
		}

		@Override
		public InputStream getContent() throws IOException, IllegalStateException {
			// the wrapped entity's getContent() decides about repeatability
			InputStream wrappedin = wrappedEntity.getContent();
			return new GZIPInputStream(wrappedin);
		}

		@Override
		public long getContentLength() {
			// length of ungzipped content is not known
			return -1;
		}
	}
}