Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Commit 933869a

Browse files
committed
Update to new API version (see 59986d897da7404e2daabfafa4e1ebee093abe92 in api-wsdl)
Implemented additional features for classes NodeUrn and NodeUrnPrefix Added methods belongsTo() and getSuffix() to NodeUrn class Made NodeUrn case-insensitive because it always used to be like that in the past
1 parent 332e3bc commit 933869a

File tree

16 files changed

+483
-115
lines changed

16 files changed

+483
-115
lines changed

pom.xml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
<artifactId>joda-time</artifactId>
2626
<version>2.0</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>junit</groupId>
30+
<artifactId>junit</artifactId>
31+
<version>4.10</version>
32+
<scope>test</scope>
33+
</dependency>
2834
</dependencies>
2935

3036
<build>
@@ -119,7 +125,7 @@
119125
<configuration>
120126
<target>2.1</target>
121127
<bindingFiles>
122-
<bindingFile>${project.basedir}/src/main/resources/DateTime.xjb</bindingFile>
128+
<bindingFile>${project.basedir}/src/main/resources/Common.xjb</bindingFile>
123129
</bindingFiles>
124130
<wsdlFiles>
125131
<wsdlFile>${project.basedir}/src/main/resources/Controller.wsdl</wsdlFile>
@@ -139,7 +145,7 @@
139145
<configuration>
140146
<target>2.1</target>
141147
<bindingFiles>
142-
<bindingFile>${project.basedir}/src/main/resources/DateTime.xjb</bindingFile>
148+
<bindingFile>${project.basedir}/src/main/resources/Common.xjb</bindingFile>
143149
</bindingFiles>
144150
<wsdlFiles>
145151
<wsdlFile>${project.basedir}/src/main/resources/SM.wsdl</wsdlFile>
@@ -159,7 +165,7 @@
159165
<configuration>
160166
<target>2.1</target>
161167
<bindingFiles>
162-
<bindingFile>${project.basedir}/src/main/resources/DateTime.xjb</bindingFile>
168+
<bindingFile>${project.basedir}/src/main/resources/Common.xjb</bindingFile>
163169
</bindingFiles>
164170
<wsdlFiles>
165171
<wsdlFile>${project.basedir}/src/main/resources/WSN.wsdl</wsdlFile>
@@ -179,7 +185,7 @@
179185
<configuration>
180186
<target>2.1</target>
181187
<bindingFiles>
182-
<bindingFile>${project.basedir}/src/main/resources/DateTime.xjb</bindingFile>
188+
<bindingFile>${project.basedir}/src/main/resources/Common.xjb</bindingFile>
183189
</bindingFiles>
184190
<wsdlFiles>
185191
<wsdlFile>${project.basedir}/src/main/resources/RS.wsdl</wsdlFile>
@@ -199,7 +205,7 @@
199205
<configuration>
200206
<target>2.1</target>
201207
<bindingFiles>
202-
<bindingFile>${project.basedir}/src/main/resources/DateTime.xjb</bindingFile>
208+
<bindingFile>${project.basedir}/src/main/resources/Common.xjb</bindingFile>
203209
</bindingFiles>
204210
<wsdlFiles>
205211
<wsdlFile>${project.basedir}/src/main/resources/SNAA.wsdl</wsdlFile>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package eu.wisebed.api.v3.common;
2+
3+
import java.util.regex.Pattern;
4+
5+
public class NodeUrn {
6+
7+
public final static Pattern URN_PATTERN = Pattern.compile(
8+
"^urn:[a-zA-Z0-9][a-zA-Z0-9-]{0,31}:([a-zA-Z0-9()+,.:=@;$_!*'-]|%[0-9A-Fa-f]{2})+$",
9+
Pattern.CASE_INSENSITIVE
10+
);
11+
12+
private String nodeUrn;
13+
14+
public NodeUrn(final String nodeUrn) {
15+
16+
if (nodeUrn == null) {
17+
throw new NullPointerException("Parameter nodeUrn is null");
18+
}
19+
20+
if (!URN_PATTERN.matcher(nodeUrn).matches()) {
21+
throw new IllegalArgumentException("Parameter nodeUrn is not a valid URN");
22+
}
23+
24+
if (!hasHexOrDecLongUrnSuffix(nodeUrn)) {
25+
throw new IllegalArgumentException("Parameter nodeUrn must have a decimal or hexadecimal suffix");
26+
}
27+
28+
this.nodeUrn = nodeUrn.toLowerCase();
29+
}
30+
31+
@Override
32+
public boolean equals(final Object o) {
33+
34+
if (this == o) {
35+
return true;
36+
}
37+
38+
if (o == null || getClass() != o.getClass()) {
39+
return false;
40+
}
41+
42+
final NodeUrn other = (NodeUrn) o;
43+
44+
return this.nodeUrn.equals(other.nodeUrn);
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
return nodeUrn.hashCode();
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return nodeUrn;
55+
}
56+
57+
private static boolean hasHexOrDecLongUrnSuffix(final String nodeUrn) {
58+
String[] arr = nodeUrn.split(":");
59+
String suffix = arr[arr.length - 1];
60+
try {
61+
if (suffix.startsWith("0x")) {
62+
Long.parseLong(suffix.substring(2), 16);
63+
} else {
64+
Long.parseLong(suffix, 10);
65+
}
66+
return true;
67+
} catch (NumberFormatException e) {
68+
return false;
69+
}
70+
}
71+
72+
public NodeUrnPrefix getPrefix() {
73+
return new NodeUrnPrefix(nodeUrn.substring(0, nodeUrn.lastIndexOf(':') + 1));
74+
}
75+
76+
public String getSuffix() {
77+
return nodeUrn.substring(nodeUrn.lastIndexOf(':') + 1);
78+
}
79+
80+
public boolean belongsTo(final NodeUrnPrefix nodeUrnPrefix) {
81+
if (nodeUrnPrefix == null) {
82+
throw new NullPointerException("Parameter nodeUrnPrefix must not be null");
83+
}
84+
return getPrefix().equals(nodeUrnPrefix);
85+
}
86+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package eu.wisebed.api.v3.common;
2+
3+
import java.util.regex.Pattern;
4+
5+
public class NodeUrnPrefix {
6+
7+
public final static Pattern URN_PREFIX_PATTERN = Pattern.compile(
8+
"^urn:[a-zA-Z0-9][a-zA-Z0-9-]{0,31}((:([a-zA-Z0-9()+,.:=@;$_!*'-]|%[0-9A-Fa-f]{2})+):|:)$",
9+
Pattern.CASE_INSENSITIVE
10+
);
11+
12+
private String nodeUrnPrefix;
13+
14+
public NodeUrnPrefix(final String nodeUrnPrefix) {
15+
16+
if (nodeUrnPrefix == null) {
17+
throw new NullPointerException("Parameter nodeUrnPrefix is null");
18+
}
19+
20+
if (!URN_PREFIX_PATTERN.matcher(nodeUrnPrefix).matches()) {
21+
throw new IllegalArgumentException("Parameter nodeUrnPrefix is not a valid URN prefix");
22+
}
23+
24+
this.nodeUrnPrefix = nodeUrnPrefix;
25+
}
26+
27+
@Override
28+
public boolean equals(final Object o) {
29+
30+
if (this == o) {
31+
return true;
32+
}
33+
34+
if (o == null || getClass() != o.getClass()) {
35+
return false;
36+
}
37+
38+
final NodeUrnPrefix that = (NodeUrnPrefix) o;
39+
40+
return nodeUrnPrefix.equals(that.nodeUrnPrefix);
41+
}
42+
43+
@Override
44+
public int hashCode() {
45+
return nodeUrnPrefix.hashCode();
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return nodeUrnPrefix;
51+
}
52+
53+
public boolean belongsTo(NodeUrn nodeUrn) {
54+
if (nodeUrn == null) {
55+
throw new NullPointerException("Parameter nodeUrn must not be null");
56+
}
57+
return nodeUrn.getPrefix().equals(this);
58+
}
59+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package eu.wisebed.api.v3.util;
2+
3+
import eu.wisebed.api.v3.common.NodeUrn;
4+
5+
import javax.xml.bind.annotation.adapters.XmlAdapter;
6+
7+
public class NodeUrnAdapter extends XmlAdapter<String, NodeUrn> {
8+
9+
public NodeUrn unmarshal(String v) throws Exception {
10+
return new NodeUrn(v);
11+
}
12+
13+
public String marshal(NodeUrn v) throws Exception {
14+
return v.toString();
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package eu.wisebed.api.v3.util;
2+
3+
import eu.wisebed.api.v3.common.NodeUrnPrefix;
4+
5+
import javax.xml.bind.annotation.adapters.XmlAdapter;
6+
7+
public class NodeUrnPrefixAdapter extends XmlAdapter<String, NodeUrnPrefix> {
8+
9+
public NodeUrnPrefix unmarshal(String v) throws Exception {
10+
return new NodeUrnPrefix(v);
11+
}
12+
13+
public String marshal(NodeUrnPrefix v) throws Exception {
14+
return v.toString();
15+
}
16+
}

src/main/java/eu/wisebed/api/v3/util/WisebedConversionHelper.java

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@
2323

2424
package eu.wisebed.api.v3.util;
2525

26-
import java.util.Collection;
27-
import java.util.HashSet;
28-
import java.util.LinkedList;
29-
import java.util.List;
30-
import java.util.Set;
31-
32-
import javax.naming.directory.InvalidAttributesException;
33-
26+
import eu.wisebed.api.v3.common.NodeUrn;
3427
import eu.wisebed.api.v3.common.SecretAuthenticationKey;
3528
import eu.wisebed.api.v3.common.UsernameNodeUrnsMap;
3629
import eu.wisebed.api.v3.common.UsernameUrnPrefixPair;
3730

31+
import javax.naming.directory.InvalidAttributesException;
32+
import java.util.*;
33+
3834
/**
3935
* Small helper class to convert between two or more API types
4036
*/
@@ -43,9 +39,10 @@ public class WisebedConversionHelper {
4339
/**
4440
* Converts a list of secret authentication keys to a list of tuples comprising user names and
4541
* urn prefixes and returns the result.
46-
*
42+
*
4743
* @param secretAuthenticationKeys
48-
* A list of secret authentication keys
44+
* A list of secret authentication keys
45+
*
4946
* @return A list of tuples comprising user names and urn prefixes
5047
*/
5148
public static List<UsernameUrnPrefixPair> convert(final List<SecretAuthenticationKey> secretAuthenticationKeys) {
@@ -64,36 +61,42 @@ public static List<UsernameUrnPrefixPair> convert(final List<SecretAuthenticatio
6461
* result.
6562
*
6663
* @param usernameUrnPrefixPairs
67-
* A collection of tuples of user name and urn prefix. The ladder one indicates the
68-
* user's associated testbed organization.
64+
* A collection of tuples of user name and urn prefix. The ladder one indicates the
65+
* user's associated testbed organization.
6966
* @param nodeURNs
70-
* A collection of node urns
67+
* A collection of node urns
68+
*
7169
* @return A list of associations between tuples of user names and urn prefixes and node urns.
70+
*
7271
* @throws InvalidAttributesException
73-
* Thrown if two tuples of user names and urn prefixes share an urn prefix.
72+
* Thrown if two tuples of user names and urn prefixes share an urn prefix.
7473
*/
75-
public static List<UsernameNodeUrnsMap> convertToUsernameNodeUrnsMap(final Collection<UsernameUrnPrefixPair> usernameUrnPrefixPairs,
76-
final Collection<String> nodeURNs) throws InvalidAttributesException {
74+
public static List<UsernameNodeUrnsMap> convertToUsernameNodeUrnsMap(
75+
final Collection<UsernameUrnPrefixPair> usernameUrnPrefixPairs,
76+
final Collection<NodeUrn> nodeURNs) throws InvalidAttributesException {
7777

7878
/*
7979
* Check whether two tuples of user names and urn prefixes share an urn prefix
8080
*/
8181
Set<String> prefixes = new HashSet<String>();
82-
for (UsernameUrnPrefixPair unpp : usernameUrnPrefixPairs) {
83-
if (!prefixes.add(unpp.getUrnPrefix())) {
84-
throw new InvalidAttributesException("The node urn prefix '" + unpp.getUrnPrefix() + "' is associated to multiple user names.");
82+
for (UsernameUrnPrefixPair usernameUrnPrefixPair : usernameUrnPrefixPairs) {
83+
if (!prefixes.add(usernameUrnPrefixPair.getUrnPrefix().toString())) {
84+
throw new InvalidAttributesException("The node urn prefix '"
85+
+ usernameUrnPrefixPair.getUrnPrefix()
86+
+ "' is associated to multiple user names."
87+
);
8588
}
8689
}
8790

8891
List<UsernameNodeUrnsMap> mappings = new LinkedList<UsernameNodeUrnsMap>();
89-
for (UsernameUrnPrefixPair unpp : usernameUrnPrefixPairs) {
92+
for (UsernameUrnPrefixPair usernameUrnPrefixPair : usernameUrnPrefixPairs) {
9093

9194
UsernameNodeUrnsMap map = new UsernameNodeUrnsMap();
9295
mappings.add(map);
9396

94-
map.setUsername(unpp);
95-
for (String nodeUrn : nodeURNs) {
96-
if (nodeUrn.startsWith(unpp.getUrnPrefix())) {
97+
map.setUsername(usernameUrnPrefixPair);
98+
for (NodeUrn nodeUrn : nodeURNs) {
99+
if (nodeUrn.belongsTo(usernameUrnPrefixPair.getUrnPrefix())) {
97100
map.getNodeUrns().add(nodeUrn);
98101
}
99102
}
@@ -106,17 +109,20 @@ public static List<UsernameNodeUrnsMap> convertToUsernameNodeUrnsMap(final Colle
106109
* returns the result.
107110
*
108111
* @param secretAuthenticationKeys
109-
* A list of secret authentication keys
112+
* A list of secret authentication keys
110113
* @param nodeURNs
111-
* A collection of node urns
114+
* A collection of node urns
115+
*
112116
* @return A list of associations between tuples of user names and urn prefixes and node urns.
117+
*
113118
* @throws InvalidAttributesException
114-
* Thrown if the matching cannot be performed due to ambigious user data
119+
* Thrown if the matching cannot be performed due to ambiguous user data
115120
* @see WisebedConversionHelper#convert(List)
116121
* @see WisebedConversionHelper#convertToUsernameNodeUrnsMap(Collection, Collection)
117122
*/
118-
public static List<UsernameNodeUrnsMap> convertToUsernameNodeUrnsMap(final List<SecretAuthenticationKey> secretAuthenticationKeys,
119-
final Collection<String> nodeURNs) throws InvalidAttributesException {
120-
return WisebedConversionHelper.convertToUsernameNodeUrnsMap(convert(secretAuthenticationKeys), nodeURNs);
123+
public static List<UsernameNodeUrnsMap> convertToUsernameNodeUrnsMap(
124+
final List<SecretAuthenticationKey> secretAuthenticationKeys,
125+
final Collection<NodeUrn> nodeURNs) throws InvalidAttributesException {
126+
return convertToUsernameNodeUrnsMap(convert(secretAuthenticationKeys), nodeURNs);
121127
}
122128
}

src/main/resources/Common.xjb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<jxb:bindings version="2.1"
3+
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
4+
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
5+
xmlns:xs="http://www.w3.org/2001/XMLSchema">
6+
7+
<jxb:globalBindings>
8+
<xjc:javaType name="org.joda.time.DateTime"
9+
xmlType="xs:dateTime"
10+
adapter="eu.wisebed.api.v3.util.DateTimeAdapter" />
11+
<xjc:javaType name="eu.wisebed.api.v3.common.NodeUrn"
12+
xmlType="common:NodeUrn"
13+
adapter="eu.wisebed.api.v3.util.NodeUrnAdapter" xmlns:common="http://wisebed.eu/api/v3/common"/>
14+
<xjc:javaType name="eu.wisebed.api.v3.common.NodeUrnPrefix"
15+
xmlType="common:NodeUrnPrefix"
16+
adapter="eu.wisebed.api.v3.util.NodeUrnPrefixAdapter" xmlns:common="http://wisebed.eu/api/v3/common"/>
17+
</jxb:globalBindings>
18+
19+
</jxb:bindings>

0 commit comments

Comments
 (0)