package jnpf.elnService.template;
|
|
import jnpf.util.JsonUtil;
|
import jnpf.util.TicketUtil;
|
import lombok.Getter;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.StringUtils;
|
|
import java.util.Map;
|
|
@Service
|
public class TemplateAnnotationTicketService {
|
|
private static final String SCOPE = "onlyoffice:eln-template-annotate";
|
private static final String BIZ_MODULE = "eln.template";
|
private static final String BIZ_SCENE = "eln.template.annotate";
|
private static final String PLUGIN_GUID = "asc.{2D6D6CB6-F6FC-45B7-8EC7-0CC1940F1A6F}";
|
|
private final TicketParser ticketParser;
|
|
public TemplateAnnotationTicketService() {
|
this(TicketUtil::parseTicket);
|
}
|
|
public TemplateAnnotationTicketService(TicketParser ticketParser) {
|
this.ticketParser = ticketParser;
|
}
|
|
public AnnotationTicketContext parse(String ticket) {
|
if (!StringUtils.hasText(ticket)) {
|
throw new InvalidAnnotationTicketException();
|
}
|
Object raw;
|
try {
|
raw = ticketParser.parse(ticket);
|
} catch (RuntimeException e) {
|
throw new InvalidAnnotationTicketException();
|
}
|
if (!(raw instanceof String) || !StringUtils.hasText((String) raw)) {
|
throw new InvalidAnnotationTicketException();
|
}
|
Map<?, ?> payload;
|
try {
|
payload = JsonUtil.getJsonToBean((String) raw, Map.class);
|
} catch (RuntimeException e) {
|
throw new InvalidAnnotationTicketException();
|
}
|
if (payload == null) {
|
throw new InvalidAnnotationTicketException();
|
}
|
String scope = value(payload, "scope");
|
String tenantId = value(payload, "tenantId");
|
String userId = value(payload, "userId");
|
String fileId = value(payload, "fileId");
|
String templateId = 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 InvalidAnnotationTicketException();
|
}
|
return new AnnotationTicketContext(tenantId, userId, templateId, fileId);
|
}
|
|
private String value(Map<?, ?> payload, String key) {
|
Object value = payload.get(key);
|
if (!(value instanceof String) || !StringUtils.hasText((String) value)) {
|
throw new InvalidAnnotationTicketException();
|
}
|
return (String) value;
|
}
|
|
public interface TicketParser {
|
Object parse(String ticket);
|
}
|
|
public static class InvalidAnnotationTicketException extends RuntimeException {
|
private static final long serialVersionUID = 1L;
|
|
public InvalidAnnotationTicketException() {
|
super("标注 Ticket 无效");
|
}
|
|
public int getStatus() {
|
return 401;
|
}
|
}
|
|
@Getter
|
public static final class AnnotationTicketContext {
|
private final String tenantId;
|
private final String userId;
|
private final String templateId;
|
private final String fileId;
|
|
private AnnotationTicketContext(String tenantId, String userId, String templateId, String fileId) {
|
this.tenantId = tenantId;
|
this.userId = userId;
|
this.templateId = templateId;
|
this.fileId = fileId;
|
}
|
}
|
}
|