Frost 1 vuosi sitten
vanhempi
commit
5e52ceb5ee

+ 33 - 0
src/main/java/com/wj/exception/SSLUtil.java

@@ -0,0 +1,33 @@
+package com.wj.exception;
+
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import java.security.cert.X509Certificate;
+
+public class SSLUtil {
+    public static void ignoreSSLVerification() throws CustomXmlParseException {
+        try {
+            TrustManager[] trustAllCertificates = new TrustManager[]{
+                    new X509TrustManager() {
+                        public X509Certificate[] getAcceptedIssuers() {
+                            return null;
+                        }
+
+                        public void checkClientTrusted(X509Certificate[] certs, String authType) {
+                        }
+
+                        public void checkServerTrusted(X509Certificate[] certs, String authType) {
+                        }
+                    }
+            };
+
+            SSLContext sslContext = SSLContext.getInstance("TLS");
+            sslContext.init(null, trustAllCertificates, new java.security.SecureRandom());
+            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
+        } catch (Exception e) {
+            throw new CustomXmlParseException(e);
+        }
+    }
+}

+ 66 - 69
src/main/java/com/wj/service/impl/XmlProcessor.java

@@ -1,7 +1,6 @@
 package com.wj.service.impl;
 
 import com.wj.DTO.RecordingInfo;
-import com.wj.config.PropertiesConfig;
 import com.wj.exception.CustomXmlParseException;
 import lombok.Getter;
 import org.springframework.stereotype.Service;
@@ -11,43 +10,34 @@ import org.w3c.dom.NodeList;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
 import java.io.File;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.time.OffsetDateTime;
 import java.time.format.DateTimeFormatter;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.Map;
 
 @Getter
 @Service
 public class XmlProcessor {
-    //private static final Map<Path, RecordingInfo> xmlCache = new ConcurrentHashMap<>();
-    private static final Path cacheFilePath = Paths.get("xml_cache.txt");
-    // 定义缓存大小上限,例如1000条记录
-    private static final int MAX_CACHE_SIZE = PropertiesConfig.getXmlCache();
-    // 使用LinkedHashMap来维护一个有序的缓存,按照访问顺序排序
-    private static final Map<Path, RecordingInfo> xmlCache = new LinkedHashMap<Path, RecordingInfo>() {
-        @Override
-        protected boolean removeEldestEntry(Map.Entry<Path, RecordingInfo> eldest) {
-            return size() > MAX_CACHE_SIZE; // 当缓存大小超过上限时,自动移除最旧的条目
-        }
-    };
-    public XmlProcessor() throws CustomXmlParseException {
-        loadCache();
-    }
+//    //private static final Map<Path, RecordingInfo> xmlCache = new ConcurrentHashMap<>();
+//    private static final Path cacheFilePath = Paths.get("xml_cache.txt");
+//    // 定义缓存大小上限,例如1000条记录
+//    private static final int MAX_CACHE_SIZE = PropertiesConfig.getXmlCache();
+//    // 使用LinkedHashMap来维护一个有序的缓存,按照访问顺序排序
+//    private static final Map<Path, RecordingInfo> xmlCache = new LinkedHashMap<Path, RecordingInfo>() {
+//        @Override
+//        protected boolean removeEldestEntry(Map.Entry<Path, RecordingInfo> eldest) {
+//            return size() > MAX_CACHE_SIZE; // 当缓存大小超过上限时,自动移除最旧的条目
+//        }
+//    };
+//    public XmlProcessor() throws CustomXmlParseException {
+//        loadCache();
+//    }
 
     public static RecordingInfo parseXmlFile(File xmlFile) throws CustomXmlParseException {
         try {
-            Path xmlPath = xmlFile.toPath();
-            if (xmlCache.containsKey(xmlPath)) {
-                return xmlCache.get(xmlPath);
-            }
+//            Path xmlPath = xmlFile.toPath();
+//            if (xmlCache.containsKey(xmlPath)) {
+//                return xmlCache.get(xmlPath);
+//            }
             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
             Document doc = dBuilder.parse(xmlFile);
@@ -59,14 +49,21 @@ public class XmlProcessor {
             String fileName = getElementValue(doc, "inum");
 
             OffsetDateTime parsedEndTime = null;
+
+//            if (endTime != null) {
+//                parsedEndTime = OffsetDateTime.parse(endTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
+//            }
+//            if (parsedEndTime != null) {
+//                RecordingInfo info = new RecordingInfo(switchCallId, parsedEndTime.toString(), fileName);
+//                xmlCache.put(xmlFile.toPath(), info); // 缓存解析结果
+//                saveCache();
+//                return info;
+//            }
             if (endTime != null) {
                 parsedEndTime = OffsetDateTime.parse(endTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
             }
             if (parsedEndTime != null) {
-                RecordingInfo info = new RecordingInfo(switchCallId, parsedEndTime.toString(), fileName);
-                xmlCache.put(xmlFile.toPath(), info); // 缓存解析结果
-                saveCache();
-                return info;
+                return new RecordingInfo(switchCallId, parsedEndTime.toString(), fileName);
             }
         } catch (Exception e) {
             throw new CustomXmlParseException("解析XML文件出错: " + xmlFile.getName(), e);
@@ -101,41 +98,41 @@ public class XmlProcessor {
         return null;
     }
 
-    private void loadCache() throws CustomXmlParseException {
-        if (Files.exists(cacheFilePath)) {
-            try (BufferedReader reader = Files.newBufferedReader(cacheFilePath)) {
-                String line;
-                while ((line = reader.readLine()) != null) {
-                    String[] parts = line.split(",");
-                    Path xmlPath = Paths.get(parts[0]);
-                    RecordingInfo info = new RecordingInfo(parts[1], parts[2], parts[3]);
-                    xmlCache.put(xmlPath, info);
-                }
-            } catch (IOException e) {
-                throw new CustomXmlParseException(new RuntimeException(e));
-            }
-        }
-    }
-
-    private static void saveCache() throws CustomXmlParseException {
-        // 在保存缓存之前,确保缓存不超过上限
-        if (xmlCache.size() > MAX_CACHE_SIZE) {
-            Iterator<Map.Entry<Path, RecordingInfo>> iterator = xmlCache.entrySet().iterator();
-            while (xmlCache.size() > MAX_CACHE_SIZE && iterator.hasNext()) {
-                iterator.next();
-                iterator.remove(); // 移除最旧的条目
-            }
-        }
-        try (BufferedWriter writer = Files.newBufferedWriter(cacheFilePath)) {
-            for (Map.Entry<Path, RecordingInfo> entry : xmlCache.entrySet()) {
-                writer.write(entry.getKey().toString() + "," +
-                        entry.getValue().getSwitchCallId() + "," +
-                        entry.getValue().getEndTime() + "," +
-                        entry.getValue().getFileName());
-                writer.newLine();
-            }
-        } catch (IOException e) {
-            throw new CustomXmlParseException(e);
-        }
-    }
+//    private void loadCache() throws CustomXmlParseException {
+//        if (Files.exists(cacheFilePath)) {
+//            try (BufferedReader reader = Files.newBufferedReader(cacheFilePath)) {
+//                String line;
+//                while ((line = reader.readLine()) != null) {
+//                    String[] parts = line.split(",");
+//                    Path xmlPath = Paths.get(parts[0]);
+//                    RecordingInfo info = new RecordingInfo(parts[1], parts[2], parts[3]);
+//                    xmlCache.put(xmlPath, info);
+//                }
+//            } catch (IOException e) {
+//                throw new CustomXmlParseException(new RuntimeException(e));
+//            }
+//        }
+//    }
+//
+//    private static void saveCache() throws CustomXmlParseException {
+//        // 在保存缓存之前,确保缓存不超过上限
+//        if (xmlCache.size() > MAX_CACHE_SIZE) {
+//            Iterator<Map.Entry<Path, RecordingInfo>> iterator = xmlCache.entrySet().iterator();
+//            while (xmlCache.size() > MAX_CACHE_SIZE && iterator.hasNext()) {
+//                iterator.next();
+//                iterator.remove(); // 移除最旧的条目
+//            }
+//        }
+//        try (BufferedWriter writer = Files.newBufferedWriter(cacheFilePath)) {
+//            for (Map.Entry<Path, RecordingInfo> entry : xmlCache.entrySet()) {
+//                writer.write(entry.getKey().toString() + "," +
+//                        entry.getValue().getSwitchCallId() + "," +
+//                        entry.getValue().getEndTime() + "," +
+//                        entry.getValue().getFileName());
+//                writer.newLine();
+//            }
+//        } catch (IOException e) {
+//            throw new CustomXmlParseException(e);
+//        }
+//    }
 }