package de.butterworks.awscommons.lambdaweb;

import com.google.gson.JsonObject;
import de.butterworks.awscommons.lambdaweb.actions.AbstractApiAction;
import de.butterworks.awscommons.lambdaweb.exceptions.ExceptionHandler;
import de.butterworks.awscommons.lambdaweb.integration.IntegrationRequestBody;
import de.butterworks.awscommons.lambdaweb.integration.UserInfo;
import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

public abstract class AbstractLambdaRouter {

    public void doHandle(final InputStream inStream, final OutputStream outStream) throws Exception {

        try {
            final JsonObject inputJson = SerializationUtil.parseAsJsonElement(IOUtils.toString(inStream, Charset.defaultCharset())).getAsJsonObject();

            final AbstractApiAction apiAction = instantiateAction(inputJson.getAsJsonPrimitive("action").getAsString());

            final UserInfo userInfo = new UserInfo(inputJson.getAsJsonPrimitive("uid").getAsString(), inputJson.getAsJsonPrimitive("groups").getAsString());

            final AbstractApiResponse responseObject = apiAction.getType() != null ?
                    apiAction.handleGeneric((IntegrationRequestBody) SerializationUtil.fromJson(inputJson.getAsJsonObject("body"), apiAction.getType()), userInfo) :
                    apiAction.handleGeneric(null, userInfo);

            if (responseObject != null) {
                IOUtils.write(responseObject.toJson(), outStream, Charset.defaultCharset());
            }
        } catch (final Exception e) {
            ExceptionHandler.processException(e);
        }
    }

    protected abstract AbstractApiAction instantiateAction(final String className);
}

@@ -488,16 +488,55 @@
 Charset;
+%0Aimport java.util.zip.GZIPOutputStream;
 %0A%0Apublic
@@ -1474,24 +1474,115 @@
  != null) %7B%0A
+                final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outStream);%0A
             
@@ -1624,17 +1624,24 @@
 Json(), 
-o
+gzipOutp
 utStream
@@ -1646,32 +1646,58 @@
 am, 
-Charset.defaultCharset()
+%22UTF-8%22);%0A                gzipOutputStream.finish(
 );%0A 

Add LambdaRouter Gzip support