package com.sptg.common.tag.processor;

import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.context.WebEngineContext;
import org.thymeleaf.engine.AttributeName;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor;
import org.thymeleaf.templatemode.TemplateMode;

import javax.servlet.http.HttpServletRequest;

/**
 * title: CustomSessionIdTagProcessor
 * package: com.sptg.common.tag.processor
 * description: 自定义测试标签
 *
 *                  
 *                 
 */
public class CustomSessionIdTagProcessor extends AbstractStandardExpressionAttributeTagProcessor {

    private static final int PRECEDENCE = 1000;
    private static final String ATTR_NAME = "sessionId";


    public CustomSessionIdTagProcessor(final String dialectPrefix) {
        super(TemplateMode.HTML, dialectPrefix, ATTR_NAME, PRECEDENCE, false, false);
    }

    @Override
    protected void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName, String attributeValue, Object expressionResult, IElementTagStructureHandler structureHandler) {
        if (context instanceof WebEngineContext){
            WebEngineContext webEngineContext = (WebEngineContext)context;
            //  获取request对象
            HttpServletRequest request = webEngineContext.getRequest();
            String sessionId = request.getRequestedSessionId();

            structureHandler.setBody(ATTR_NAME + ":" + sessionId, false);
        }
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}
