package com.lutao.cloud.common.xss.core;

import cn.hutool.core.util.StrUtil;
import com.lutao.cloud.common.xss.utils.XssUtil;
import com.lutao.cloud.common.xss.config.CloudXssProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;

/**
 * 表单 xss 处理
 *

 */
@ControllerAdvice
@RequiredArgsConstructor
public class FormXssClean{

	private final CloudXssProperties properties;

	private final XssCleaner xssCleaner;

	@InitBinder
	public void initBinder(WebDataBinder binder){
		// 处理前端传来的表单字符串
		binder.registerCustomEditor(String.class, new StringPropertiesEditor(xssCleaner, properties));
	}

	@Slf4j
	@RequiredArgsConstructor
	public static class StringPropertiesEditor extends PropertyEditorSupport{

		private final XssCleaner xssCleaner;

		private final CloudXssProperties properties;

		@Override
		public String getAsText(){
			Object value = getValue();
			return value != null ? value.toString() : StrUtil.EMPTY;
		}

		@Override
		public void setAsText(String text) throws IllegalArgumentException{
			if (text == null){
				setValue(null);
			}
			else if (XssHolder.isEnabled()){
				String value = xssCleaner.clean(XssUtil.trim(text, properties.isTrimText()));
				setValue(value);
				log.debug("Request parameter value:{} cleaned up by mica-xss, current value is:{}.", text, value);
			}
			else{
				setValue(XssUtil.trim(text, properties.isTrimText()));
			}
		}

	}

}
