/*
 *
 *  * Copyright 2013 the original author or authors.
 *  *
 *  * 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 leap.oauth2.server.endpoint.token;

import leap.core.BeanFactory;
import leap.core.annotation.Inject;
import leap.lang.Out;
import leap.lang.intercepting.State;
import leap.lang.logging.Log;
import leap.lang.logging.LogFactory;
import leap.oauth2.server.OAuth2Params;
import leap.oauth2.server.token.AuthzAccessToken;
import leap.web.Request;
import leap.web.Response;

/**
 * @author kael.
 */
public class DefaultGrantTokenManager implements GrantTokenManager {
    private static final Log log = LogFactory.get(DefaultGrantTokenManager.class);
    
    protected @Inject BeanFactory factory;
    protected @Inject GrantTokenInterceptor[] interceptors;
    
    @Override
    public GrantTypeHandler getHandler(String grantType) {
        return factory.tryGetBean(GrantTypeHandler.class, grantType);
    }

    @Override
    public AuthzAccessToken grantAccessToken(Request request, Response response, OAuth2Params params, GrantTypeHandler handler) throws Throwable {
        Out<AuthzAccessToken> out = new Out<>();
        try {
            for(GrantTokenInterceptor interceptor:interceptors){
                if(State.isIntercepted(interceptor.beforeGrantTypeHandle(request,response,params,handler,out))){
                    return out.get();
                }
            }
            handler.handleRequest(request,response,params,accessToken -> out.set(accessToken));
            for(GrantTokenInterceptor interceptor:interceptors){
                if(State.isIntercepted(interceptor.afterGrantTypeHandle(request,response,params,handler,out))){
                    return out.get();
                }
            }
            return out.get();
        }finally {
            for(GrantTokenInterceptor interceptor : interceptors) {
                try {
                    interceptor.grantTypeHandleComplete(request,response,params,handler,out);
                }catch (Exception e){
                    log.warn("complete grant type handle fail for class {}, error message: ",interceptor.getClass().getName(),e);
                    log.warn(e);
                }
            }
        }
    }
}

@@ -1691,32 +1691,120 @@
 ;%0A        try %7B%0A
+            out.set(grantToken(request, response, params, handler));%0A        %7Dfinally %7B%0A
             for(
@@ -1828,33 +1828,35 @@
 ptor interceptor
-:
+ : 
 interceptors)%7B%0A 
@@ -1844,32 +1844,33 @@
  : interceptors)
+ 
 %7B%0A              
@@ -1871,39 +1871,42 @@
         
-if(State.isIntercepted(
+try %7B%0A                    
 intercep
@@ -1913,15 +1913,9 @@
 tor.
-beforeG
+g
 rant
@@ -1916,32 +1916,40 @@
 .grantTypeHandle
+Complete
 (request,respons
@@ -1973,47 +1973,208 @@
 out)
-))%7B%0A                    return out.get(
+;%0A                %7Dcatch (Exception e)%7B%0A                    log.warn(%22complete grant type handle fail for class %7B%7D, error message: %22,interceptor.getClass().getName(),e);%0A                    log.warn(e
 );%0A 
@@ -2216,42 +2216,101 @@
     
+%7D%0A
     
-handler.handle
+    return out.get();%0A    %7D%0A%0A    protected AuthzAccessToken grantToken(
 Request
-(
+ 
 request,
 resp
@@ -2309,66 +2309,142 @@
 est,
-response,params,accessToken -%3E out.set(accessToken));%0A    
+ Response response, OAuth2Params params, GrantTypeHandler handler) throws Throwable%7B%0A        Out%3CAuthzAccessToken%3E out = new Out%3C%3E();%0A
     
@@ -2512,20 +2512,16 @@
         
-    
 if(State
@@ -2551,13 +2551,14 @@
 tor.
-after
+before
 Gran
@@ -2617,36 +2617,32 @@
                 
-    
 return out.get()
@@ -2651,34 +2651,26 @@
             
-    %7D%0A    
+%7D%0A
         %7D%0A  
@@ -2679,53 +2679,92 @@
     
-    return
+handler.handleRequest(request,response,params,accessToken -%3E
  out.
-get();%0A        %7Dfinally %7B%0A    
+set(accessToken));%0A
     
@@ -2796,35 +2796,33 @@
 ptor interceptor
- : 
+:
 interceptors) %7B%0A
@@ -2810,33 +2810,32 @@
 or:interceptors)
- 
 %7B%0A              
@@ -2836,38 +2836,31 @@
     
-    try %7B%0A                    
+if(State.isIntercepted(
 inte
@@ -2859,33 +2859,38 @@
 ted(interceptor.
-g
+afterG
 rantTypeHandleCo
@@ -2883,32 +2883,24 @@
 ntTypeHandle
-Complete
 (request,res
@@ -2928,257 +2928,101 @@
 out)
-;%0A                %7Dcatch (Exception e)%7B%0A                    log.warn(%22complete grant type handle fail for class %7B%7D, error message: %22,interceptor.getClass().getName(),e);%0A                    log.warn(e);%0A                %7D%0A            %7D%0A   
+))%7B%0A                return out.get();%0A            %7D%0A        %7D%0A        return out.get();%0A
     
- 
 %7D%0A
-    %7D
 %0A%7D%0A

fix access token grant