package org.qfox.jestful.form;

import org.qfox.jestful.core.BeanContainer;
import org.qfox.jestful.core.Initialable;
import org.qfox.jestful.core.annotation.DELETE;
import org.qfox.jestful.core.annotation.GET;
import org.qfox.jestful.core.annotation.POST;
import org.qfox.jestful.core.annotation.PUT;
import org.qfox.jestful.interception.Interceptor;
import org.qfox.jestful.interception.Invocation;

import javax.servlet.ServletRequest;

/**
 * Created by yangchangpei on 17/8/23.
 */
public class TokenInterceptor implements Interceptor, Initialable {
    private String name = "form-token";
    private TokenManager tokenManager;

    @Override
    public void initialize(BeanContainer beanContainer) {
        tokenManager = beanContainer.get(TokenManager.class);
    }

    @GET("/.*")
    @POST("/.*")
    @PUT("/.*")
    @DELETE("/.*")
    @Override
    public Object intercept(Invocation invocation) throws Exception {
        boolean needed = invocation.getMapping().isAnnotationPresent(TokenValidate.class);
        if (needed) {
            ServletRequest request = (ServletRequest) invocation.getRequest();
            String token = request.getParameter(name);
            if (token == null) throw new TokenRequiredException();
            tokenManager.verify(token);
        }
        return invocation.invoke();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

@@ -567,17 +567,22 @@
  String 
-n
+tokenN
 ame = %22f
@@ -1166,17 +1166,22 @@
 rameter(
-n
+tokenN
 ame);%0A  
@@ -1359,16 +1359,21 @@
 ring get
+Token
 Name() %7B
@@ -1388,17 +1388,22 @@
  return 
-n
+tokenN
 ame;%0A   
@@ -1425,16 +1425,21 @@
 void set
+Token
 Name(Str
@@ -1442,17 +1442,22 @@
 (String 
-n
+tokenN
 ame) %7B%0A 
@@ -1472,16 +1472,26 @@
 his.
-name = n
+tokenName = tokenN
 ame;
@@ -1497,11 +1497,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Add Servlet Request Parameters Supports