刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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;
        }
    }
}