/*
 * Copyright 2014-2015 Red Hat, Inc, and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.projectodd.wunderboss.web.async;

import io.undertow.util.Headers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

public class ServletHttpChannel extends OutputStreamHttpChannel {

    public ServletHttpChannel(final HttpServletRequest request,
                              final HttpServletResponse response,
                              final OnOpen onOpen,
                              final OnError onError,
                              final OnClose onClose){
        super(onOpen, onError, onClose);
        this.response = response;
        request.startAsync();
    }

    @Override
    protected String getResponseCharset() {
        return this.response.getCharacterEncoding();
    }

    @Override
    protected void setContentLength(int length) {
        this.response.setIntHeader(Headers.CONTENT_LENGTH_STRING,
                                   length);
    }

    @Override
    protected OutputStream getOutputStream() throws Exception {
        return this.response.getOutputStream();
    }

    private final HttpServletResponse response;
}

@@ -701,16 +701,51 @@
 aders;%0A%0A
+import javax.servlet.AsyncContext;%0A
 import j
@@ -829,16 +829,44 @@
 sponse;%0A
+import java.io.IOException;%0A
 import j
@@ -1324,16 +1324,36 @@
 %0A       
+ this.asyncContext =
  request
@@ -1739,16 +1739,18 @@
  throws 
+IO
 Exceptio
@@ -1816,50 +1816,222 @@
     
-private final HttpServletResponse response
+@Override%0A    public void close() throws IOException %7B%0A        this.asyncContext.complete();%0A        super.close();%0A    %7D%0A%0A    private final HttpServletResponse response;%0A    private final AsyncContext asyncContext
 ;%0A%7D%0A

Complete the async context on close in-container.