/*
 * Copyright 2016 Bridje Framework.
 *
 * 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.bridje.web.impl;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bridje.ioc.Application;
import org.bridje.ioc.Component;
import org.bridje.ioc.Inject;
import org.bridje.ioc.InjectNext;
import org.bridje.ioc.IocContext;
import org.bridje.ioc.Priority;
import org.bridje.web.WebScope;
import org.bridje.http.HttpBridletContext;
import org.bridje.http.HttpBridlet;
import org.bridje.http.HttpException;
import org.bridje.ioc.thls.Thls;

@Component
@Priority(0)
class ScopeHttpBridlet implements HttpBridlet
{
    private static final Logger LOG = Logger.getLogger(ScopeHttpBridlet.class.getName());

    @Inject
    private IocContext<Application> appCtx;

    @InjectNext
    private HttpBridlet nextHandler;

    @Override
    public boolean handle(HttpBridletContext context) throws IOException, HttpException
    {
        try
        {
            WebScope scope = new WebScope(context);
            IocContext<WebScope> wrsCtx = appCtx.createChild(scope);
            context.set(WebScope.class, scope);
            context.set(IocContext.class, wrsCtx);
            return Thls.doAsEx2(() ->
            {
                return Thls.doAsEx2(() -> nextHandler.handle(context), WebScope.class, scope );
            }, IocContext.class, wrsCtx );
        }
        catch (IOException | HttpException e)
        {
            throw e;
        }
        catch (Exception ex)
        {
            LOG.log(Level.SEVERE, ex.getMessage(), ex);
            throw new HttpException(500, ex.getMessage(), ex);
        }
    }
    
}

@@ -1098,16 +1098,65 @@
 ls.Thls;
+%0Aimport org.bridje.ioc.thls.ThlsActionException2;
 %0A%0A@Compo
@@ -1809,123 +1809,646 @@
 Ex2(
-() -%3E%0A            %7B%0A                return Thls.doAsEx2(() -%3E nextHandler.handle(context), WebScope.class, scope );
+new ThlsActionException2%3CBoolean, IOException, HttpException%3E()%0A            %7B%0A                @Override%0A                public Boolean execute() throws IOException, HttpException%0A                %7B%0A                    return Thls.doAsEx2(new ThlsActionException2%3CBoolean, IOException, HttpException%3E()%0A                    %7B%0A                        @Override%0A                        public Boolean execute() throws IOException, HttpException%0A                        %7B%0A                            return nextHandler.handle(context);%0A                        %7D%0A                    %7D, WebScope.class, scope );%0A                %7D%0A                
 %0A   
@@ -2758,15 +2758,10 @@
 %7D%0A    %7D%0A
-    %0A
 %7D%0A

Fix to the scope bridlet as some jvm cannot infer the exceptiones.