com.taskadapter.redmineapi.internal.comm.TransportDecoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redmine-java-api Show documentation
Show all versions of redmine-java-api Show documentation
Free open-source Java API for Redmine and Chiliproject bug/task management systems.
This project was originally a part of Task Adapter application (http://www.taskadapter.com)
and then was open-sourced.
The newest version!
package com.taskadapter.redmineapi.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 com.taskadapter.redmineapi.RedmineException;
import com.taskadapter.redmineapi.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);
}
}