-
Notifications
You must be signed in to change notification settings - Fork 160
/
AccountParentPathXmlAdapter.java
72 lines (62 loc) · 2.78 KB
/
AccountParentPathXmlAdapter.java
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
/*******************************************************************************
* Copyright (c) 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.account.xml;
import com.google.common.base.Strings;
import org.eclipse.kapua.KapuaIllegalArgumentException;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.id.KapuaIdFactory;
import org.eclipse.kapua.service.account.Account;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* {@link Account#getParentAccountPath()} {@link XmlAdapter}.
*
* @since 1.6.0
*/
public class AccountParentPathXmlAdapter extends XmlAdapter<String, String> {
private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
private static final KapuaIdFactory KAPUA_ID_FACTORY = LOCATOR.getFactory(KapuaIdFactory.class);
@Override
public String marshal(String parentAccountPathLong) {
if (Strings.isNullOrEmpty(parentAccountPathLong)) {
return null;
} else {
String[] parentAccountPathLongTokens = parentAccountPathLong.substring(1).split("/");
List<String> parentAccountPathBase64List =
Arrays.stream(parentAccountPathLongTokens)
.map(p -> KAPUA_ID_FACTORY.newKapuaId(new BigInteger(p)).toCompactId())
.collect(Collectors.toList());
return "/" + String.join("/", parentAccountPathBase64List);
}
}
@Override
public String unmarshal(String parentAccountPathBase64) throws KapuaIllegalArgumentException {
if (Strings.isNullOrEmpty(parentAccountPathBase64)) {
return null;
} else {
String[] parentAccountPathBase64Tokens = parentAccountPathBase64.substring(1).split("/");
try {
List<String> parentAccountPathLongList =
Arrays.stream(parentAccountPathBase64Tokens)
.map(p -> KAPUA_ID_FACTORY.newKapuaId(p).toStringId())
.collect(Collectors.toList());
return "/" + String.join("/", parentAccountPathLongList);
} catch (IllegalArgumentException e) {
throw new KapuaIllegalArgumentException("account.parentAccountPath", parentAccountPathBase64);
}
}
}
}