package org.qfox.jestful.form;

import org.qfox.jestful.core.BeanContainer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.DynamicAttributes;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by yangchangpei on 17/8/21.
 */
public class TokenInput extends TagSupport implements DynamicAttributes {
    private static final long serialVersionUID = 8901103747024632711L;

    private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();

    @Override
    public int doStartTag() throws JspException {
        try {
            BeanContainer beanContainer = (BeanContainer) pageContext.getServletContext().getAttribute(BeanContainer.ROOT_BEAN_CONTAINER_CONTEXT_ATTRIBUTE);
            TokenManager tokenManager = beanContainer.get(TokenManager.class);
            String token = tokenManager.create();

            if (!attributes.containsKey("name")) attributes.put("name", "form-token");
            if (!attributes.containsKey("type")) attributes.put("type", "hidden");
            if (!attributes.containsKey("value")) attributes.put("value", token);

            StringBuilder sb = new StringBuilder();
            sb.append("<input");

            for (Map.Entry<String, Object> entry : attributes.entrySet()) {
                String name = entry.getKey();
                Object value = entry.getValue();
                if (value == null) continue;
                else sb.append(" ");
                sb.append(name).append("=").append("\"").append(value.toString()).append("\"");
            }

            sb.append("/>");
            pageContext.getOut().write(sb.toString());
            return SKIP_BODY;
        } catch (Exception e) {
            throw new JspException(e);
        }
    }

    @Override
    public void setDynamicAttribute(String uri, String localName, Object value) throws JspException {
        attributes.put(localName, value);
    }

    @Override
    public void release() {
        attributes.clear();
        super.release();
    }
}

@@ -858,58 +858,8 @@
 ss);
-%0A            String token = tokenManager.create();
 %0A%0A  
@@ -1105,16 +1105,32 @@
 %22, token
+Manager.create()
 );%0A%0A    

Add Anti Form Repeat Submit Supports