刘光辉
昨天 bb638871a7fb692d80f1b7a758f991dc0879002c
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
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;
        }
    }
}