Example usage for java.util.zip GZIPInputStream GZIPInputStream

List of usage examples for java.util.zip GZIPInputStream GZIPInputStream

Introduction

In this page you can find the example usage for java.util.zip GZIPInputStream GZIPInputStream.

Prototype

public GZIPInputStream(InputStream in) throws IOException 

Source Link

Document

Creates a new input stream with a default buffer size.

Usage

From source file:net.acesinc.convergentui.content.ContentService.java

public String getDownstreamResponse() {
    RequestContext context = RequestContext.getCurrentContext();
    // there is no body to send
    if (context.getResponseBody() == null && context.getResponseDataStream() == null) {
        return null;
    }/*from   w  ww .j  a  va  2 s .co m*/

    String body = null;
    if (RequestContext.getCurrentContext().getResponseBody() != null) {
        body = RequestContext.getCurrentContext().getResponseBody();
    } else {
        try {
            HttpServletResponse servletResponse = context.getResponse();
            servletResponse.setCharacterEncoding("UTF-8");
            InputStream is = null;

            boolean isGzipRequested = false;
            final String requestEncoding = context.getRequest().getHeader(ZuulHeaders.ACCEPT_ENCODING);
            if (requestEncoding != null && requestEncoding.equals("gzip")) {
                isGzipRequested = true;
            }
            is = context.getResponseDataStream();
            InputStream inputStream = is;
            if (is != null) {
                if (context.sendZuulResponse()) {
                    // if origin response is gzipped, and client has not requested gzip,
                    // decompress stream
                    // before sending to client
                    // else, stream gzip directly to client
                    if (context.getResponseGZipped() && !isGzipRequested) {
                        try {
                            inputStream = new GZIPInputStream(is);
                        } catch (java.util.zip.ZipException ex) {
                            System.out.println("gzip expected but not " + "received assuming unencoded response"
                                    + RequestContext.getCurrentContext().getRequest().getRequestURL()
                                            .toString());
                            inputStream = is;
                        }
                    } else if (context.getResponseGZipped() && isGzipRequested) {
                        servletResponse.setHeader(ZuulHeaders.CONTENT_ENCODING, "gzip");
                    }
                    body = IOUtils.toString(inputStream, "utf8");
                }
            }
        } catch (IOException ioe) {
            log.warn("IOException getting output stream", ioe);
        }
    }
    return body;
}

From source file:dashboard.ImportCDN.java

public static int readAmazonLogs(int n) throws Exception {
    if (n < 1)
        return 0;
    int eventsNumber = 0;
    int begin = 0;
    int zips = 0;
    int postFailures = 0;
    int deletedZips = 0;
    int mixpanelStatus = 0;
    String line = null;/*from   w ww.ja  v  a  2 s .  c o m*/

    // Log files Bucket
    AWSCredentials credentials = new BasicAWSCredentials(AWS_USER, AWS_PASS);
    AmazonS3Client s3Client = new AmazonS3Client(credentials);
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName);

    // Set MARKER - from which Log File to start reading
    // listObjectsRequest.setMarker("E2DXXJR0N8BXOK.2013-03-18-10.ICK6IvaY.gz");

    BufferedReader br = null;

    ObjectListing objectListing = s3Client.listObjects(listObjectsRequest);
    ObjectListing nextObjectListing = objectListing;
    zips = 0;
    Boolean more = true;
    if (objectListing == null)
        more = false;

    while (more) {
        // Reads 1000 files
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            // Handle  ZIP files        

            try { // Open and send to mixpanel events of one ZIP file  
                String key = objectSummary.getKey();

                S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, key));
                // Extract ZIP and read Object to reader
                br = new BufferedReader(new InputStreamReader(new GZIPInputStream(object.getObjectContent())));
                zips++;

                // Read the lines from the unzipped file, break it and send to Mixpanel
                while ((line = br.readLine()) != null) {
                    if (line.startsWith("#"))
                        continue;
                    if (line.trim().equals(""))
                        continue;
                    String[] values = line.split("\\s");

                    String eventTime = values[0] + " " + values[1];
                    String ip = values[4];
                    String method = values[5];
                    String fileName = values[7];
                    String statusCode = values[8];
                    String userAgent = values[10];
                    String fName = fileName;

                    if (fileName.contains("gigaspaces-")) {
                        begin = fileName.lastIndexOf("gigaspaces-") + 11;
                        fName = fileName.substring(begin, fileName.length());
                    }

                    eventsNumber++;
                    System.out.println(eventsNumber + ": " + eventTime + " " + ip);
                    // Track the event in Mixpanel (using the POST import)
                    mixpanelStatus = postCDNEventToMixpanel(ip, "Cloudfront CDN", eventTime, method, fileName,
                            fName, userAgent, statusCode);

                    // If failed  
                    if (mixpanelStatus != 1) {
                        postFailures++;
                        System.out.println("   >>> POST event to Mixpanel Failed!!  " + postFailures);

                    }
                }
                // while on ZIP file lines

                if (mixpanelStatus == 1 & DELETE_PROCESSED_LOGS.equals("YES")) {
                    // Delete the CDN log ZIP file
                    s3Client.deleteObject(bucketName, key);
                    System.out.println("============ Deleted Zip " + zips + " ============");
                    deletedZips++;
                } else
                    System.out.println("============ Zip " + zips + " (not deleted) ============");
            } catch (IOException e) {

                e.printStackTrace();
                return eventsNumber;
            } finally {
                if (br != null) {
                    br.close();
                }

                if (eventsNumber >= n) {
                    System.out.println("\n>>> " + eventsNumber + " events in " + zips + " Zip files.");
                    System.out.println("\n>>> " + deletedZips + " Zip files deleted.");
                    System.out.println("\n>>> " + postFailures + " post Failures\n");
                    return eventsNumber;
                }
            }

        }
        // for (continue to next ZIP file

        // If there are more ZIP files, read next batch of 1000
        if (objectListing.isTruncated()) {
            nextObjectListing = s3Client.listNextBatchOfObjects(objectListing);
            objectListing = nextObjectListing;
        } else
            more = false; // no more files

    } // while next objectListing

    System.out.println("\n>>> " + eventsNumber + " events in " + zips + " Zip files.");
    System.out.println("\n>>> " + deletedZips + " Zip files deleted.");
    System.out.println("\n>>> " + postFailures + " post Failures\n");
    return eventsNumber;
}

From source file:de.codecentric.boot.admin.zuul.filters.post.SendResponseFilter.java

private void writeResponse() throws Exception {
    RequestContext context = RequestContext.getCurrentContext();
    // there is no body to send
    if (context.getResponseBody() == null && context.getResponseDataStream() == null) {
        return;//w w w.  j  a v a 2 s  . co m
    }
    HttpServletResponse servletResponse = context.getResponse();
    if (servletResponse.getCharacterEncoding() == null) { // only set if not set
        servletResponse.setCharacterEncoding("UTF-8");
    }
    OutputStream outStream = servletResponse.getOutputStream();
    InputStream is = null;
    try {
        if (RequestContext.getCurrentContext().getResponseBody() != null) {
            String body = RequestContext.getCurrentContext().getResponseBody();
            writeResponse(new ByteArrayInputStream(body.getBytes(servletResponse.getCharacterEncoding())),
                    outStream);
            return;
        }
        boolean isGzipRequested = false;
        final String requestEncoding = context.getRequest().getHeader(ZuulHeaders.ACCEPT_ENCODING);

        if (requestEncoding != null && HTTPRequestUtils.getInstance().isGzipped(requestEncoding)) {
            isGzipRequested = true;
        }
        is = context.getResponseDataStream();
        InputStream inputStream = is;
        if (is != null) {
            if (context.sendZuulResponse()) {
                // if origin response is gzipped, and client has not requested gzip,
                // decompress stream
                // before sending to client
                // else, stream gzip directly to client
                if (context.getResponseGZipped() && !isGzipRequested) {
                    // If origin tell it's GZipped but the content is ZERO bytes,
                    // don't try to uncompress
                    final Long len = context.getOriginContentLength();
                    if (len == null || len > 0) {
                        try {
                            inputStream = new GZIPInputStream(is);
                        } catch (java.util.zip.ZipException ex) {
                            log.debug("gzip expected but not " + "received assuming unencoded response "
                                    + RequestContext.getCurrentContext().getRequest().getRequestURL()
                                            .toString());
                            inputStream = is;
                        }
                    } else {
                        // Already done : inputStream = is;
                    }
                } else if (context.getResponseGZipped() && isGzipRequested) {
                    servletResponse.setHeader(ZuulHeaders.CONTENT_ENCODING, "gzip");
                }
                writeResponse(inputStream, outStream);
            }
        }
    } finally {
        try {
            Object zuulResponse = RequestContext.getCurrentContext().get("zuulResponse");
            if (zuulResponse instanceof Closeable) {
                ((Closeable) zuulResponse).close();
            }
            outStream.flush();
            // The container will close the stream for us
        } catch (IOException ex) {
        }
    }
}

From source file:org.cleverbus.admin.web.msg.MessageLogParser.java

/**
 * Gets lines which corresponds with specified correlation ID from the specified log file.
 *
 * @param logFile the log file//from   w w  w  .  j a  va 2  s . c  om
 * @param correlationId the correlation ID
 * @return log lines
 * @throws IOException when error occurred during file reading
 */
private List<String> getLogLines(File logFile, String correlationId) throws IOException {
    List<String> logLines = new ArrayList<String>();

    Log.debug("Go through the following log file: " + logFile);

    int year = Calendar.getInstance().get(Calendar.YEAR);
    String[] possibleYears = new String[] { String.valueOf(year - 1), String.valueOf(year) };

    InputStream stream = null;
    try {
        if (logFile.getName().endsWith(GZIP_FILE_EXTENSION)) {
            stream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(logFile)));
        } else {
            stream = new BufferedInputStream(new FileInputStream(logFile));
        }

        LineIterator it = IOUtils.lineIterator(stream, Charset.defaultCharset());

        String requestId = null;
        boolean lastCorrectLine = false; // if previous log line belongs to requestId
        while (it.hasNext()) {
            String line = it.nextLine();

            if (requestId == null) {
                if (StringUtils.contains(line, correlationId)) {
                    logLines.add(formatLogLine(line));

                    // finds requestID
                    requestId = getRequestId(line);

                    if (requestId != null) {
                        Log.debug("correlationId (" + correlationId + ") => requestId (" + requestId + ")");
                    }
                }
            } else {
                // adds lines with requestID and lines that belongs to previous log record (e.g. XML request)
                //  it's better to check also correlationID because it's not one request ID
                //  for all repeated scheduled jobs for processing of partly failed messages

                // 2013-05-23 20:22:36,754 [MACHINE_IS_UNDEFINED, ajp-bio-8009-exec-19, /esb/ws/account/v1, ...
                // <checkCustomerCreditRequest xmlns="http://cleverbus.org/ws/AccountService-v1">
                //    <firstName>csd</firstName>
                //    <lastName>acs</lastName>
                //    <birthNumber>111111/1111</birthNumber>
                // </checkCustomerCreditRequest>

                if (StringUtils.contains(line, requestId) || (StringUtils.contains(line, correlationId))
                        || (lastCorrectLine && !StringUtils.startsWithAny(line, possibleYears))) {
                    logLines.add(formatLogLine(line));
                    lastCorrectLine = true;
                } else {
                    lastCorrectLine = false;
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(stream);
    }

    return logLines;
}