package com.sy.service.impl; import com.alibaba.fastjson.JSON; import com.sy.model.DxpMsg; import com.sy.model.GatherInfo; import com.sy.model.X82Feedback; import com.sy.service.CommandLogService; import com.sy.service.RedisService; import com.sy.service.X82Service; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.converters.Converter; import com.thoughtworks.xstream.converters.MarshallingContext; import com.thoughtworks.xstream.converters.UnmarshallingContext; import com.thoughtworks.xstream.io.HierarchicalStreamReader; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; import com.thoughtworks.xstream.io.xml.DomDriver; import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; @Service @Slf4j public class X82ServiceImpl implements X82Service { @Autowired private RedisService redisService; @Autowired private CommandLogService commandLogService; @Override public DxpMsg analysis(String response) { XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("_-", "_"))); xstream.processAnnotations(DxpMsg.class); //对xstream对象设置默认的安全防护 XStream.setupDefaultSecurity(xstream); //对xstream对象设置默认的安全防护时,允许设置类 xstream.allowTypes(new Class[]{DxpMsg.class}); return (DxpMsg) xstream.fromXML(response); } @Override public X82Feedback x82Analysis(String xml) { XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("_-", "_"))); xstream.processAnnotations(X82Feedback.class); //对xstream对象设置默认的安全防护 XStream.setupDefaultSecurity(xstream); //对xstream对象设置默认的安全防护时,允许设置类 xstream.allowTypes(new Class[]{X82Feedback.class}); return (X82Feedback) xstream.fromXML(xml); } @Override public Boolean X82ReleaseCheck(X82Feedback var) { log.info("[X82-Result]金二验放结果-{}-{}",var.getCheckResult(),var.getOpHint()); if ("Y".equals(var.getCheckResult().trim()) || "1".equals(var.getCheckResult().trim()) || "200".equals(var.getCheckResult().trim()) ) { return true; } return false; } /** * 根据seqno 获取缓存的X21信息 * @param seqno * @return x21的gatherInfo */ @Override public GatherInfo readCacheWithSeqno(String seqno){ if (StringUtils.isNotEmpty(seqno)) { String X21_GatherInfo = redisService.get(seqno); if (StringUtils.isNotEmpty(X21_GatherInfo)){ GatherInfo gatherInfo = JSON.parseObject(X21_GatherInfo, GatherInfo.class); return gatherInfo; } } return null; } @Override public GatherInfo X82FeedBackToX21GatherInfo(X82Feedback var) { GatherInfo gatherInfo = new GatherInfo(); gatherInfo.setAreaid(var.getAreaId()); gatherInfo.setChnlno(var.getChannelNo()); gatherInfo.setSeqno(var.getSessionId()); return gatherInfo; } public static class DateConverter implements Converter { private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSXXX"; private final SimpleDateFormat dateFormat; public DateConverter() { this.dateFormat = new SimpleDateFormat(DATE_FORMAT); } @Override public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext context) { // Not used for unmarshalling } @Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { String dateString = reader.getValue(); try { return dateFormat.parse(dateString); } catch (ParseException e) { throw new IllegalArgumentException("Error parsing date string: " + dateString, e); } } @Override public boolean canConvert(Class aClass) { return aClass.equals(Date.class); } } }