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

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

29 Jan 2025 04:42PM UTC coverage: 77.85%. First build
#474

Pull #271

github

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

83 of 116 new or added lines in 6 files covered. (71.55%)

956 of 1228 relevant lines covered (77.85%)

0.78 hits per line

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

33.33
/src/main/java/edu/kit/datamanager/pit/pidsystem/impl/handle/HandleBehavior.java
1
package edu.kit.datamanager.pit.pidsystem.impl.handle;
2

3
import edu.kit.datamanager.pit.domain.PIDRecord;
4
import edu.kit.datamanager.pit.domain.PIDRecordEntry;
5
import net.handle.hdllib.Common;
6
import net.handle.hdllib.HandleValue;
7

8
import java.nio.charset.StandardCharsets;
9
import java.util.*;
10

11
/**
12
 * This class defines helper functions and constants which define special
13
 * behavior in the Typed PID Maker context.
14
 * <p>
15
 * This has the purpose of reuse, e.g. in the resolver module, but also
16
 * separating the authentication and PID logic from certain behavior aspects.
17
 * <p>
18
 * In later aspects, this may be extended to implement an interface and use the
19
 * strategy pattern in order to make the behavior configurable.
20
 */
21
public class HandleBehavior {
22

23
    /**
24
     * A list of type codes which are considered "internal" or "handle-native"
25
     * and should not be exposed. Use `isHandleInternalValue` to check if a
26
     * value should be filtered out.
27
     */
28
    private static final byte[][][] BLACKLIST_NONTYPE_LISTS = {
1✔
29
            Common.SITE_INFO_AND_SERVICE_HANDLE_INCL_PREFIX_TYPES,
30
            Common.DERIVED_PREFIX_SITE_AND_SERVICE_HANDLE_TYPES,
31
            Common.SERVICE_HANDLE_TYPES,
32
            Common.LOCATION_AND_ADMIN_TYPES,
33
            Common.SECRET_KEY_TYPES,
34
            Common.PUBLIC_KEY_TYPES,
35
            // Common.STD_TYPES, // not using because of URL and EMAIL
36
            {
37
                    // URL and EMAIL might contain valuable information and can be considered
38
                    // non-technical.
39
                    // Common.STD_TYPE_URL,
40
                    // Common.STD_TYPE_EMAIL,
41
                    Common.STD_TYPE_HSADMIN,
42
                    Common.STD_TYPE_HSALIAS,
43
                    Common.STD_TYPE_HSSITE,
44
                    Common.STD_TYPE_HSSITE6,
45
                    Common.STD_TYPE_HSSERV,
46
                    Common.STD_TYPE_HSSECKEY,
47
                    Common.STD_TYPE_HSPUBKEY,
48
                    Common.STD_TYPE_HSVALLIST,
49
            }
50
    };
51

52
    /**
53
     * This class is not meant to be instantiated.
54
     */
55
    private HandleBehavior() {}
56

57
    /**
58
     * Checks if a given value is considered an "internal" or "handle-native" value.
59
     * <p>
60
     * This may be used to filter out administrative information from a PID record.
61
     *
62
     * @param v the value to check.
63
     * @return true, if the value is considered "handle-native".
64
     */
65
    public static boolean isHandleInternalValue(HandleValue v) {
66
        boolean isInternalValue = false;
1✔
67
        for (byte[][] typeList : BLACKLIST_NONTYPE_LISTS) {
1✔
68
            for (byte[] typeCode : typeList) {
1✔
69
                isInternalValue = isInternalValue || Arrays.equals(v.getType(), typeCode);
1✔
70
            }
71
        }
72
        return isInternalValue;
1✔
73
    }
74

75
    /**
76
     * Convert a `PIDRecord` instance to an array of `HandleValue`s. It is the
77
     * inverse method to `pidRecordFrom`.
78
     *
79
     * @param pidRecord the record containing values to convert / extract.
80
     * @param toMerge   an optional list to merge the result with.
81
     * @return HandleValues containing the same key-value pairs as the given record,
82
     *         but e.g. without the name.
83
     */
84
    public static ArrayList<HandleValue> handleValuesFrom(
85
            final PIDRecord pidRecord,
86
            final Optional<List<HandleValue>> toMerge)
87
    {
NEW
88
        ArrayList<Integer> skippingIndices = new ArrayList<>();
×
NEW
89
        ArrayList<HandleValue> result = new ArrayList<>();
×
NEW
90
        if (toMerge.isPresent()) {
×
NEW
91
            for (HandleValue v : toMerge.get()) {
×
NEW
92
                result.add(v);
×
NEW
93
                skippingIndices.add(v.getIndex());
×
NEW
94
            }
×
95
        }
NEW
96
        HandleIndex index = new HandleIndex().skipping(skippingIndices);
×
NEW
97
        Map<String, List<PIDRecordEntry>> entries = pidRecord.getEntries();
×
98

NEW
99
        for (Map.Entry<String, List<PIDRecordEntry>> entry : entries.entrySet()) {
×
NEW
100
            for (PIDRecordEntry val : entry.getValue()) {
×
NEW
101
                String key = val.getKey();
×
NEW
102
                HandleValue hv = new HandleValue();
×
NEW
103
                int i = index.nextIndex();
×
NEW
104
                hv.setIndex(i);
×
NEW
105
                hv.setType(key.getBytes(StandardCharsets.UTF_8));
×
NEW
106
                hv.setData(val.getValue().getBytes(StandardCharsets.UTF_8));
×
NEW
107
                result.add(hv);
×
NEW
108
            }
×
NEW
109
        }
×
NEW
110
        assert result.size() >= pidRecord.getEntries().size();
×
NEW
111
        return result;
×
112
    }
113

114
    public static PIDRecord recordFrom(final Collection<HandleValue> values) {
115
        PIDRecord record = new PIDRecord();
1✔
116
        values.forEach(v -> record.addEntry(
1✔
117
                v.getTypeAsString(),
1✔
118
                v.getDataAsString())
1✔
119
        );
120
        return record;
1✔
121
    }
122
}
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