package org.mb.http;

import com.google.common.base.Charsets;
import org.mb.http.basic.Content;
import org.mb.http.basic.Request;
import org.mb.http.basic.Response;
import org.mb.http.basic.Handler;
import org.mb.http.mapping.ResponseDataMapping;
import org.mb.scripting.JSPLikeProcessor;
import org.mb.settings.Settings;
import org.mb.parsing.Parsing;
import sun.org.mozilla.javascript.internal.Scriptable;

import java.io.*;
import java.util.Map;

/**
 * Created by Dmitriy Dzhevaga on 11.09.2015.
 */
public class MainHandler implements Handler {
    private final String PARSING = "parsing";
    private final String REQUEST = "request";
    private final Settings settings;

    public MainHandler(Settings settings) {
        this.settings = settings;
    }

    @Override
    public Response handle(final Request request) {
        Parsing commonParsing = settings.getParsing();
        final Map<String, String> parsingResult = commonParsing.parse(request.getContent());

        ResponseDataMapping.ResponseData responseData = settings.getMapping().find(request, parsingResult);
        final Response response = responseData.getResponse();
        Parsing requestParsing = responseData.getParsing();

        parsingResult.putAll(requestParsing.parse(request.getContent()));

        final InputStream inputStream = response.getContent().toStream();
        return response.setContent(new Content() {
            @Override
            public void writeTo(OutputStream outputStream) throws IOException {
                try(
                    Reader reader = new InputStreamReader(inputStream, Charsets.UTF_8);
                    Writer writer = new OutputStreamWriter(outputStream, Charsets.UTF_8);
                ) {
                    JSPLikeProcessor.from(reader).
                            put(PARSING, parsingResult).
                            put(REQUEST, request).
                            compile(writer);
                }
            }

            @Override
            public InputStream toStream() {
                return response.getContent().toStream();
            }
        });
    }
}
@@ -287,24 +287,69 @@
 Processor;%0D%0A
+import org.mb.scripting.ScriptingException;%0D%0A
 import org.m
@@ -1614,45 +1614,19 @@
     
-try(%0D%0A                    Reader read
+Writer writ
 er =
@@ -1630,18 +1630,19 @@
 r = new 
-In
+Out
 putStrea
@@ -1646,17 +1646,18 @@
 ream
-Reader(in
+Writer(out
 putS
@@ -1693,39 +1693,61 @@
             
-    Writer writ
+try(%0D%0A                    Reader read
 er = new Out
@@ -1735,35 +1735,34 @@
 er reader = new 
-Out
+In
 putStreamWriter(
@@ -1750,34 +1750,33 @@
  InputStream
-Writer(out
+Reader(in
 putStream, C

Fix output stream closing in case of exception while request handling