package com.changhong.epc.zuul.filter.version; import com.changhong.epc.constter.base.Context; import com.changhong.epc.rely.api.epc.tenant.NormApi; import com.changhong.epc.rely.api.tool.FilterTool; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import com.iemsoft.framework.cloud.core.tools.SpringUtil; import com.iemsoft.framework.cloud.zuul.tool.RequestContextTool; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.http.HttpServletRequestWrapper; import com.netflix.zuul.http.ServletInputStreamWrapper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.util.StreamUtils; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.util.*; import java.util.stream.Collectors; @Slf4j public class NormVersion implements Version{ @Override public String getUpdateUrl() { return "epc/epc-tenant/tenant/normType/insert"; } @Override public String getUpdateKey() { return getUpdateKey(RequestContextTool.getCurrentContext().getRequest()); } protected String getUpdateKey(HttpServletRequest request) { FilterTool.FILTER_TYPE.get(FilterTool.FilterType.PRIVATE).filter(request); Integer cId = getCid(request); String key = String.format("version_norm_%s_%s_%s" , Context.getTenantId() , Context.getCompanyId() , cId); return key; } private Integer getCid(HttpServletRequest request) { RequestContext context = RequestContextTool.getCurrentContext(); try (InputStream in = context.getRequest().getInputStream()) { String body = StreamUtils.copyToString(in, Charset.forName("UTF-8")); final byte[] reqBodyBytes = body.getBytes(); context.setRequest(new HttpServletRequestWrapper(context.getRequest()) { @Override public ServletInputStream getInputStream(){ return new ServletInputStreamWrapper(reqBodyBytes); } @Override public int getContentLength() { return reqBodyBytes.length; } @Override public long getContentLengthLong() { return reqBodyBytes.length; } }); Map bodyObj = JSONTool.toObj(body, Map.class); if(ObjectUtil.empty(bodyObj) || ObjectUtil.empty(bodyObj.get("suppStanderExtend"))){ return null; } List> suppStanderExtend = (List>)bodyObj.get("suppStanderExtend"); return NumberUtils.createInteger(Objects.toString(suppStanderExtend.get(0).get("ctId"), "0")); } catch (IOException e) { return null; } } @Override public String getCacheKey(HttpServletRequest request, String key) { String cacheKey = String.format("version_norm_%s_%s_%s" , Context.getTenantId() , Context.getCompanyId() , key); return cacheKey; } @Override public Set getKeys() { return SpringUtil.getBean(NormApi.class).getAllNormCostType() .stream() .map(costType -> Objects.toString(costType.getId(), "")).collect(Collectors.toSet()); } }