package jnpf.elnService; import jnpf.util.JsonUtil; import jnpf.util.TicketUtil; import lombok.Getter; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.util.Map; @Service public class ElnRecordFillTicketService { public static final String SCOPE = "onlyoffice:eln-record-fill"; public static final String BIZ_MODULE = "eln.record"; public static final String BIZ_SCENE = "eln.record.fill"; private static final String PLUGIN_GUID = "asc.{7F4E8F35-5D66-47F8-A5B4-6AB3FAACF565}"; private final TicketParser ticketParser; public ElnRecordFillTicketService() { this(TicketUtil::parseTicket); } public ElnRecordFillTicketService(TicketParser ticketParser) { this.ticketParser = ticketParser; } public FillTicketContext parse(String ticket) { if (ObjectUtils.isEmpty(ticket)) { throw new InvalidFillTicketException(); } Object raw; try { raw = ticketParser.parse(ticket); } catch (RuntimeException e) { throw new InvalidFillTicketException(); } if (!(raw instanceof String) || ObjectUtils.isEmpty(raw)) { throw new InvalidFillTicketException(); } Map payload; try { payload = JsonUtil.getJsonToBean((String) raw, Map.class); } catch (RuntimeException e) { throw new InvalidFillTicketException(); } String scope = value(payload, "scope"); String tenantId = value(payload, "tenantId"); String userId = value(payload, "userId"); String fileId = value(payload, "fileId"); String bizDataId = value(payload, "bizDataId"); String bizModule = value(payload, "bizModule"); String bizScene = value(payload, "bizScene"); String pluginGuid = value(payload, "pluginGuid"); if (!isSupportedContext(scope, bizModule, bizScene) || !PLUGIN_GUID.equals(pluginGuid)) { throw new InvalidFillTicketException(); } return new FillTicketContext(tenantId, userId, fileId, bizDataId, pluginGuid, bizModule, bizScene); } private static boolean isSupportedContext(String scope, String bizModule, String bizScene) { return SCOPE.equals(scope) && BIZ_MODULE.equals(bizModule) && BIZ_SCENE.equals(bizScene); } private String value(Map payload, String key) { Object value = payload.get(key); if (!(value instanceof String) || ObjectUtils.isEmpty(value)) { throw new InvalidFillTicketException(); } return (String) value; } public interface TicketParser { Object parse(String ticket); } public static class InvalidFillTicketException extends RuntimeException { private static final long serialVersionUID = 1L; } @Getter public static class FillTicketContext { private final String tenantId; private final String userId; private final String fileId; private final String bizDataId; private final String pluginGuid; private final String bizModule; private final String bizScene; FillTicketContext(String tenantId, String userId, String fileId, String bizDataId, String pluginGuid, String bizModule, String bizScene) { this.tenantId = tenantId; this.userId = userId; this.fileId = fileId; this.bizDataId = bizDataId; this.pluginGuid = pluginGuid; this.bizModule = bizModule; this.bizScene = bizScene; } } }