/** * @param response The response to parse * @return A HttpResponse object made by consuming the response of the * given HttpMethod. * @throws IOException when problems occur processing the body content */ @SuppressWarnings("unchecked") private HttpResponse makeResponse(Response response) throws IOException { HttpResponseBuilder builder = new HttpResponseBuilder(); Series<Parameter> headers = (Series<Parameter>) response.getAttributes().get("org.restlet.http.headers"); if (headers != null) { for (Parameter param : headers) { if (param.getName() != null) { builder.addHeader(param.getName(), param.getValue()); } } } Representation entity = Preconditions.checkNotNull(response.getEntity()); if (maxObjSize > 0 && entity != null && entity.getSize() > maxObjSize) { return HttpResponse.badrequest("Exceeded maximum number of bytes - " + maxObjSize); } return builder.setHttpStatusCode(response.getStatus().getCode()).setResponse(toByteArraySafe(entity)).create(); }
/** * @param sessionTransactionManager the SessionTransactionManager to use * @param dataStorageFacade data storage facade to use * @param arrayDao the ArrayDao to use * @param searchDao the SearchDao to use */ protected AbstractDesignFileHandler(SessionTransactionManager sessionTransactionManager, DataStorageFacade dataStorageFacade, ArrayDao arrayDao, SearchDao searchDao) { Preconditions.checkNotNull(sessionTransactionManager); Preconditions.checkNotNull(dataStorageFacade); Preconditions.checkNotNull(searchDao); Preconditions.checkNotNull(arrayDao); this.sessionTransactionManager = sessionTransactionManager; this.dataStorageFacade = dataStorageFacade; this.searchDao = searchDao; this.arrayDao = arrayDao; }
/** * This method is Safe replica version of org.apache.http.util.EntityUtils.toByteArray. * The try block embedding 'instream.read' has a corresponding catch block for 'EOFException' * (that's Ignored) and all other IOExceptions are let pass. * * @param entity * @return byte array containing the entity content. May be empty/null. * @throws IOException if an error occurs reading the input stream */ public byte[] toByteArraySafe(final Representation entity) throws IOException { if (entity == null) { return null; } InputStream instream = entity.getStream(); if (instream == null) { return new byte[] {}; } Preconditions.checkArgument(entity.getSize() < Integer.MAX_VALUE, "HTTP entity too large to be buffered in memory"); // The raw data stream (inside JDK) is read in a buffer of size '512'. The original code // org.apache.http.util.EntityUtils.toByteArray reads the unzipped data in a buffer of // 4096 byte. For any data stream that has a compression ratio lesser than 1/8, this may // result in the buffer/array overflow. Increasing the buffer size to '16384'. It's highly // unlikely to get data compression ratios lesser than 1/32 (3%). final int bufferLength = 16384; int i = (int) entity.getSize(); if (i < 0) { i = bufferLength; } ByteArrayBuffer buffer = new ByteArrayBuffer(i); try { byte[] tmp = new byte[bufferLength]; int l; while ((l = instream.read(tmp)) != -1) { buffer.append(tmp, 0, l); } } catch (EOFException eofe) { /** * Ref: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4040920 * Due to a bug in JDK ZLIB (InflaterInputStream), unexpected EOF error can occur. * In such cases, even if the input stream is finished reading, the * 'Inflater.finished()' call erroneously returns 'false' and * 'java.util.zip.InflaterInputStream.fill' throws the 'EOFException'. * So for such case, ignore the Exception in case Exception Cause is * 'Unexpected end of ZLIB input stream'. * * Also, ignore this exception in case the exception has no message * body as this is the case where {@link GZIPInputStream#readUByte} * throws EOFException with empty message. A bug has been filed with Sun * and will be mentioned here once it is accepted. */ if (instream.available() == 0 && (eofe.getMessage() == null || eofe.getMessage().equals("Unexpected end of ZLIB input stream"))) { LOG.log(Level.FINE, "EOFException: ", eofe); } else { throw eofe; } } finally { instream.close(); } return buffer.toByteArray(); }
/** * Change the global connection timeout for all fetchs. * * @param connectionTimeoutMs new connection timeout in milliseconds */ @Inject(optional = true) public void setConnectionTimeoutMs(@Named("shindig.http.client.connection-timeout-ms") int connectionTimeoutMs) { Preconditions.checkArgument(connectionTimeoutMs > 0, "connection-timeout-ms must be greater than 0"); this.connectionTimeoutMs = connectionTimeoutMs; }