• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / pit-service / #473

29 Jan 2025 02:18PM UTC coverage: 76.81%. First build
#473

Pull #271

github

web-flow
Merge 61c449f25 into 4c17b004d
Pull Request #271: Separate resolver from the configured system

33 of 35 new or added lines in 4 files covered. (94.29%)

944 of 1229 relevant lines covered (76.81%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/src/main/java/edu/kit/datamanager/pit/resolver/Resolver.java
1
package edu.kit.datamanager.pit.resolver;
2

3
import edu.kit.datamanager.pit.common.ExternalServiceException;
4
import edu.kit.datamanager.pit.common.PidNotFoundException;
5
import edu.kit.datamanager.pit.domain.PIDRecord;
6
import edu.kit.datamanager.pit.pidsystem.impl.HandleProtocolAdapter;
7
import edu.kit.datamanager.pit.pitservice.ITypingService;
8
import net.handle.api.HSAdapter;
9
import net.handle.api.HSAdapterFactory;
10
import net.handle.hdllib.HandleException;
11
import net.handle.hdllib.HandleValue;
12

13
import java.util.Arrays;
14
import java.util.Collection;
15
import java.util.stream.Collectors;
16

17
/**
18
 * Universal resolver. Will not only resolve PIDs from the configured PID system,
19
 * but also from other external systems, to which it has read-only access to.
20
 * <p>
21
 * Currently implemented read-only systems:
22
 * <p>
23
 * - Handle System
24
 */
25
public class Resolver {
26
    /**
27
     * The configured system to which we usually have write access.
28
     * Only used if the prefix of the PID matches the prefix of this system.
29
     */
30
    private final ITypingService identifierSystem;
31

32
    /**
33
     * The client to the Handle System, used in read-only mode.
34
     * Used as a fallback, if the prefix is not the one of the configured system.
35
     */
36
    private final HSAdapter client = HSAdapterFactory.newInstance();
1✔
37
    private static final String SERVICE_NAME_HANDLE = "Handle System (read-only access)";
38

39
    public Resolver(ITypingService identifierSystem) {
1✔
40
        this.identifierSystem = identifierSystem;
1✔
41
    }
1✔
42

43
    /**
44
     * Resolves a PID to a PIDRecord.
45
     * <p>
46
     * Takes advantage of administrative access to the configured PID system, if possible.
47
     * Otherwise, falls back to read-only access to external systems.
48
     *
49
     * @param pid the PID to resolve.
50
     * @return the PIDRecord associated with the PID.
51
     * @throws PidNotFoundException if the PID could not be found in any system.
52
     * @throws ExternalServiceException if there was an error with the communication to an external system.
53
     */
54
    public PIDRecord resolve(String pid) throws PidNotFoundException, ExternalServiceException {
55
        String prefix = Arrays.stream(
1✔
56
                pid.split("/", 2)
1✔
57
        )
58
                .findFirst()
1✔
59
                .orElseThrow(() -> new PidNotFoundException(pid, "Could not find prefix in PID."))
1✔
60
                + "/"; // needed because the prefix is always followed by a slash
61
        boolean isInConfiguredIdentifierSystem = this.identifierSystem != null && this.identifierSystem.getPrefix()
1✔
62
                .map(prefix::equals)
1✔
63
                .orElse(false);
1✔
64
        if (isInConfiguredIdentifierSystem) {
1✔
65
            return this.identifierSystem.queryPid(pid);
1✔
66
        } else {
67
            try {
68
                Collection<HandleValue> recordProperties = Arrays.stream(this.client.resolveHandle(pid, null, null))
1✔
69
                        .filter(value -> !HandleProtocolAdapter.isHandleInternalValue(value))
1✔
70
                        .collect(Collectors.toList());
1✔
71
                return new PIDRecord(recordProperties).withPID(pid);
1✔
72
            } catch (HandleException e) {
1✔
73
                int code = e.getCode();
1✔
74
                boolean isExistingPid = code == HandleException.HANDLE_DOES_NOT_EXIST;
1✔
75
                boolean missingPrefixHost = false;
1✔
76
                if (e.getCause() instanceof HandleException inner) {
1✔
77
                    int innerCode = inner.getCode();
1✔
78
                    missingPrefixHost = innerCode == HandleException.SERVICE_NOT_FOUND
1✔
79
                            || innerCode == HandleException.HANDLE_DOES_NOT_EXIST;
80
                }
81
                if (isExistingPid || missingPrefixHost) {
1✔
82
                    throw new PidNotFoundException(pid, e);
1✔
83
                } else {
NEW
84
                    throw new ExternalServiceException(SERVICE_NAME_HANDLE, e);
×
85
                }
86
            }
87
        }
88
    }
89
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc