package com.northernwall.hadrian.handlers.caching;

import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import org.slf4j.LoggerFactory;

public class CachedContent {

    private final static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CachedContent.class);

    private final String resource;
    private final byte[] bytes;

    public CachedContent(String resource, InputStream inputStream, HtmlCompressor compressor) throws IOException {
        this.resource = resource;

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        byte[] buffer = new byte[50 * 1024];
        int len = inputStream.read(buffer);
        while (len != -1) {
            outputStream.write(buffer, 0, len);
            len = inputStream.read(buffer);
        }
        if (resource.endsWith(".html")) {
            int origSize = outputStream.size();
            bytes = compressor.compress(outputStream.toString()).getBytes();
            LOGGER.info("Loaded content {} into cache, {} bytes, was {} bytes", resource, bytes.length, origSize);
        } else if ((resource.startsWith("/webapp/js/") || resource.startsWith("/webcontent/js/"))
                && resource.endsWith(".js")) {
            int origSize = outputStream.size();
            bytes = compressionJS(outputStream.toString()).getBytes();
            LOGGER.info("Loaded content {} into cache, {} bytes, was {} bytes", resource, bytes.length, origSize);
        } else {
            bytes = outputStream.toByteArray();
            LOGGER.info("Loaded content {} into cache, {} bytes", resource, bytes.length);
        }
    }

    public void write(ServletOutputStream outputStream) {
        try {
            outputStream.write(bytes);
        } catch (Exception ex) {
            LOGGER.warn("Exception while writing content {}, {}", resource, ex.getMessage());
        }
        try {
            outputStream.flush();
        } catch (Exception ex) {
            LOGGER.warn("Exception while flushing content {}, {}", resource, ex.getMessage());
        }
    }

    public static String compressionJS(String origText) {
        int len = origText.length();
        StringBuilder b = new StringBuilder(len);
        char c = origText.charAt(0);
        for (int i = 1; i < len; i++) {
            char n = origText.charAt(i);
            if (c == ' ' && n == ' ') {
                c = n;
            } else if (c == '\n' && n == ' ') {
            } else if (c != ';' && n == '\n') {
            } else {
                b.append(c);
                c = n;
            }
        }
        b.append(c);
        return b.toString();
    }

}

@@ -416,16 +416,615 @@
 class);%0D
+%0A    %0D%0A    public static String compressionJS(String origText) %7B%0D%0A        int len = origText.length();%0D%0A        StringBuilder b = new StringBuilder(len);%0D%0A        char c = origText.charAt(0);%0D%0A        for (int i = 1; i %3C len; i++) %7B%0D%0A            char n = origText.charAt(i);%0D%0A            if (c == ' ' && n == ' ') %7B%0D%0A                c = n;%0D%0A            %7D else if (c == '%5Cn' && n == ' ') %7B%0D%0A            %7D else if (c != ';' && n == '%5Cn') %7B%0D%0A            %7D else %7B%0D%0A                b.append(c);%0D%0A                c = n;%0D%0A            %7D%0D%0A        %7D%0D%0A        b.append(c);%0D%0A        return b.toString();%0D%0A    %7D%0D
 %0A%0D%0A    p
@@ -2037,32 +2037,83 @@
          bytes =
+ outputStream.toByteArray();%0D%0A            //bytes =
  compressionJS(o
@@ -2902,601 +2902,8 @@
 %0D%0A%0D%0A
-    public static String compressionJS(String origText) %7B%0D%0A        int len = origText.length();%0D%0A        StringBuilder b = new StringBuilder(len);%0D%0A        char c = origText.charAt(0);%0D%0A        for (int i = 1; i %3C len; i++) %7B%0D%0A            char n = origText.charAt(i);%0D%0A            if (c == ' ' && n == ' ') %7B%0D%0A                c = n;%0D%0A            %7D else if (c == '%5Cn' && n == ' ') %7B%0D%0A            %7D else if (c != ';' && n == '%5Cn') %7B%0D%0A            %7D else %7B%0D%0A                b.append(c);%0D%0A                c = n;%0D%0A            %7D%0D%0A        %7D%0D%0A        b.append(c);%0D%0A        return b.toString();%0D%0A    %7D%0D%0A
 %0D%0A%7D%0D

Disable JS compression for now