All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redmine.ta.internal.comm.TransportDecoder Maven / Gradle / Ivy

Go to download

Free open-source Java API for Redmine and Chiliproject bug/task management systems.

The newest version!
package org.redmine.ta.internal.comm;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.redmine.ta.RedmineException;
import org.redmine.ta.RedmineTransportException;

/**
 * Transport encoding decoder.
 * 
 * @author maxkar
 * 
 */
final class TransportDecoder implements
		ContentHandler {

	@Override
	public BasicHttpResponse processContent(HttpResponse content)
			throws RedmineException {
		final HttpEntity entity = content.getEntity();
		final String charset = HttpUtil.getCharset(entity);
		final String encoding = HttpUtil.getEntityEncoding(entity);
		try {
			final InputStream initialStream = entity.getContent();
			return new BasicHttpResponse(content.getStatusLine()
					.getStatusCode(), decodeStream(encoding, initialStream),
					charset);
		} catch (IOException e) {
			throw new RedmineTransportException(e);
		}
	}

	/**
	 * Decodes a transport stream.
	 * 
	 * @param encoding
	 *            stream encoding.
	 * @param initialStream
	 *            initial stream.
	 * @return decoding stream.
	 * @throws IOException
	 */
	private InputStream decodeStream(String encoding, InputStream initialStream)
			throws IOException {
		if (encoding == null)
			return initialStream;
		if ("gzip".equals(encoding))
			return new GZIPInputStream(initialStream);
		if ("deflate".equals(encoding))
			return new InflaterInputStream(initialStream);
		throw new IOException("Unsupported transport encoding " + encoding);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy