VppUnCipherPayloadHelper.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.usky.vpp.util;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.usky.common.core.exception.BusinessException;
  4. import org.springframework.util.StringUtils;
  5. /**
  6. * 运管平台 UN/DN 国密 HTTP 载荷解析(application/json 下密文常为 JSON 字符串)。
  7. */
  8. public final class VppUnCipherPayloadHelper {
  9. private VppUnCipherPayloadHelper() {
  10. }
  11. /**
  12. * 从 HTTP body 提取 Base64 密文。
  13. * <ul>
  14. * <li>JSON 字符串:{@code "MIIB..."}</li>
  15. * <li>裸 Base64:{@code MIIB...}</li>
  16. * </ul>
  17. */
  18. public static String extractCipherPayload(String body, ObjectMapper objectMapper) {
  19. if (!StringUtils.hasText(body)) {
  20. return "";
  21. }
  22. String trimmed = body.trim();
  23. if (trimmed.startsWith("\"")) {
  24. try {
  25. return objectMapper.readValue(trimmed, String.class);
  26. } catch (Exception ex) {
  27. throw new BusinessException("密文 JSON 字符串解析失败: " + ex.getMessage());
  28. }
  29. }
  30. return trimmed;
  31. }
  32. /** 将 Base64 密文包装为合法 JSON 字符串请求体。 */
  33. public static String wrapCipherPayload(String cipherBase64, ObjectMapper objectMapper) {
  34. try {
  35. return objectMapper.writeValueAsString(cipherBase64);
  36. } catch (Exception ex) {
  37. throw new BusinessException("密文 JSON 包装失败: " + ex.getMessage());
  38. }
  39. }
  40. }