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 TemplateFillTicketService { private static final String SCOPE = "onlyoffice:eln-template-fill"; private static final String BIZ_MODULE = "eln.template"; private static final String BIZ_SCENE = "eln.template.fill"; private static final String PLUGIN_GUID = "asc.{7F4E8F35-5D66-47F8-A5B4-6AB3FAACF565}"; private final TicketParser ticketParser; public TemplateFillTicketService() { this(TicketUtil::parseTicket); } public TemplateFillTicketService(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 (!SCOPE.equals(scope) || !BIZ_MODULE.equals(bizModule) || !BIZ_SCENE.equals(bizScene) || !PLUGIN_GUID.equals(pluginGuid)) { throw new InvalidFillTicketException(); } return new FillTicketContext(tenantId, userId, fileId, bizDataId, pluginGuid); } 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; FillTicketContext(String tenantId, String userId, String fileId, String bizDataId, String pluginGuid) { this.tenantId = tenantId; this.userId = userId; this.fileId = fileId; this.bizDataId = bizDataId; this.pluginGuid = pluginGuid; } } }