use of java.util.zip.InflaterInputStream in project openhab1-addons by openhab.
the class AbstractRequest method executeUrl.
/**
* Executes the given <code>url</code> with the given <code>httpMethod</code>. In the case of httpMethods that do
* not support automatic redirection, manually handle the HTTP temporary redirect (307) and retry with the new URL.
*
* @param httpMethod
* the HTTP method to use
* @param url
* the url to execute (in milliseconds)
* @param contentString
* the content to be sent to the given <code>url</code> or <code>null</code> if no content should be
* sent.
* @param contentType
* the content type of the given <code>contentString</code>
* @return the response body or <code>NULL</code> when the request went wrong
*/
protected final String executeUrl(final String httpMethod, final String url, final String contentString, final String contentType) {
HttpClient client = new HttpClient();
HttpMethod method = HttpUtil.createHttpMethod(httpMethod, url);
method.getParams().setSoTimeout(httpRequestTimeout);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
for (String httpHeaderKey : HTTP_HEADERS.stringPropertyNames()) {
method.addRequestHeader(new Header(httpHeaderKey, HTTP_HEADERS.getProperty(httpHeaderKey)));
}
// add content if a valid method is given ...
if (method instanceof EntityEnclosingMethod && contentString != null) {
EntityEnclosingMethod eeMethod = (EntityEnclosingMethod) method;
InputStream content = new ByteArrayInputStream(contentString.getBytes());
eeMethod.setRequestEntity(new InputStreamRequestEntity(content, contentType));
}
if (logger.isDebugEnabled()) {
try {
logger.trace("About to execute '" + method.getURI().toString() + "'");
} catch (URIException e) {
logger.trace(e.getMessage());
}
}
try {
int statusCode = client.executeMethod(method);
if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_ACCEPTED) {
// perfectly fine but we cannot expect any answer...
return null;
}
// Manually handle 307 redirects with a little tail recursion
if (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT) {
Header[] headers = method.getResponseHeaders("Location");
String newUrl = headers[headers.length - 1].getValue();
return executeUrl(httpMethod, newUrl, contentString, contentType);
}
if (statusCode != HttpStatus.SC_OK) {
logger.warn("Method failed: " + method.getStatusLine());
}
InputStream tmpResponseStream = method.getResponseBodyAsStream();
Header encodingHeader = method.getResponseHeader("Content-Encoding");
if (encodingHeader != null) {
for (HeaderElement ehElem : encodingHeader.getElements()) {
if (ehElem.toString().matches(".*gzip.*")) {
tmpResponseStream = new GZIPInputStream(tmpResponseStream);
logger.trace("GZipped InputStream from {}", url);
} else if (ehElem.toString().matches(".*deflate.*")) {
tmpResponseStream = new InflaterInputStream(tmpResponseStream);
logger.trace("Deflated InputStream from {}", url);
}
}
}
String responseBody = IOUtils.toString(tmpResponseStream);
if (!responseBody.isEmpty()) {
logger.trace(responseBody);
}
return responseBody;
} catch (HttpException he) {
logger.error("Fatal protocol violation: {}", he.toString());
} catch (IOException ioe) {
logger.error("Fatal transport error: {}", ioe.toString());
} finally {
method.releaseConnection();
}
return null;
}
use of java.util.zip.InflaterInputStream in project nutz by nutzam.
the class Sender method createResponse.
protected Response createResponse(Map<String, String> reHeaders) throws IOException {
Response rep = null;
if (reHeaders != null) {
rep = new Response(conn, reHeaders);
if (rep.isOK()) {
InputStream is1 = conn.getInputStream();
InputStream is2 = null;
String encoding = conn.getContentEncoding();
// 如果采用了压缩,则需要处理否则都是乱码
if (encoding != null && encoding.contains("gzip")) {
is2 = new GZIPInputStream(is1);
} else if (encoding != null && encoding.contains("deflate")) {
is2 = new InflaterInputStream(is1, new Inflater(true));
} else {
is2 = is1;
}
BufferedInputStream is = new BufferedInputStream(is2);
rep.setStream(is);
} else {
try {
rep.setStream(conn.getInputStream());
} catch (IOException e) {
try {
rep.setStream(conn.getErrorStream());
} catch (Exception e1) {
rep.setStream(new VoidInputStream());
}
}
}
}
if (this.interceptor != null)
this.interceptor.afterResponse(request, conn, rep);
return rep;
}
use of java.util.zip.InflaterInputStream in project robovm by robovm.
the class SpdyReader method newNameValueBlockStream.
private DataInputStream newNameValueBlockStream() {
// Limit the inflater input stream to only those bytes in the Name/Value block.
final InputStream throttleStream = new InputStream() {
@Override
public int read() throws IOException {
return Util.readSingleByte(this);
}
@Override
public int read(byte[] buffer, int offset, int byteCount) throws IOException {
byteCount = Math.min(byteCount, compressedLimit);
int consumed = in.read(buffer, offset, byteCount);
compressedLimit -= consumed;
return consumed;
}
@Override
public void close() throws IOException {
in.close();
}
};
// Subclass inflater to install a dictionary when it's needed.
Inflater inflater = new Inflater() {
@Override
public int inflate(byte[] buffer, int offset, int count) throws DataFormatException {
int result = super.inflate(buffer, offset, count);
if (result == 0 && needsDictionary()) {
setDictionary(DICTIONARY);
result = super.inflate(buffer, offset, count);
}
return result;
}
};
return new DataInputStream(new InflaterInputStream(throttleStream, inflater));
}
use of java.util.zip.InflaterInputStream in project robovm by robovm.
the class FilterInputStreamNullSourceTest method testInflaterInputStream.
public void testInflaterInputStream() throws IOException {
try {
new InflaterInputStream(null);
fail();
} catch (NullPointerException expected) {
}
try {
new InflaterInputStream(null, new Inflater());
fail();
} catch (NullPointerException expected) {
}
try {
new InflaterInputStream(null, new Inflater(), 1024);
fail();
} catch (NullPointerException expected) {
}
}
use of java.util.zip.InflaterInputStream in project robovm by robovm.
the class DeflaterOutputStreamTest method testSyncFlushDeflater.
/**
* Confirm that a DeflaterOutputStream constructed with Deflater
* with flushParm == SYNC_FLUSH does not need to to be flushed.
*
* http://b/4005091
*/
public void testSyncFlushDeflater() throws Exception {
Deflater def = new Deflater();
Field f = def.getClass().getDeclaredField("flushParm");
f.setAccessible(true);
f.setInt(def, Deflater.SYNC_FLUSH);
final int deflaterBufferSize = 512;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(baos, def, deflaterBufferSize);
// make output buffer large enough that even if compressed it
// won't all fit within the deflaterBufferSize.
final int outputBufferSize = 128 * deflaterBufferSize;
byte[] output = new byte[outputBufferSize];
for (int i = 0; i < output.length; i++) {
output[i] = (byte) i;
}
dos.write(output);
byte[] compressed = baos.toByteArray();
// this main reason for this assert is to make sure that the
// compressed byte count is larger than the
// deflaterBufferSize. However, when the original bug exists,
// it will also fail because the compressed length will be
// exactly the length of the deflaterBufferSize.
assertTrue("compressed=" + compressed.length + " but deflaterBufferSize=" + deflaterBufferSize, compressed.length > deflaterBufferSize);
// assert that we returned data matches the input exactly.
ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
InflaterInputStream iis = new InflaterInputStream(bais);
byte[] input = new byte[output.length];
int total = 0;
while (true) {
int n = iis.read(input, total, input.length - total);
if (n == -1) {
break;
}
total += n;
if (total == input.length) {
try {
iis.read();
fail();
} catch (EOFException expected) {
break;
}
}
}
assertEquals(output.length, total);
assertTrue(Arrays.equals(input, output));
// ensure Deflater.finish has not been called at any point
// during the test, since that would lead to the results being
// flushed even without SYNC_FLUSH being used
assertFalse(def.finished());
// Quieten CloseGuard.
def.end();
iis.close();
}
Aggregations