1 package cn.home1.oss.lib.common.crypto;
2
3 import static org.apache.commons.lang3.StringUtils.isBlank;
4 import static org.apache.commons.lang3.StringUtils.isNotBlank;
5
6 import com.fasterxml.jackson.annotation.JsonInclude;
7
8 import lombok.EqualsAndHashCode;
9 import lombok.Getter;
10 import lombok.NoArgsConstructor;
11 import lombok.Setter;
12 import lombok.extern.slf4j.Slf4j;
13
14 import org.codehaus.jackson.map.annotate.JsonSerialize;
15
16 import javax.xml.bind.annotation.XmlAccessType;
17 import javax.xml.bind.annotation.XmlAccessorType;
18 import javax.xml.bind.annotation.XmlRootElement;
19
20
21
22
23 @XmlRootElement(name = "key")
24 @XmlAccessorType(XmlAccessType.FIELD)
25 @JsonInclude(JsonInclude.Include.NON_EMPTY)
26 @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)
27 @EqualsAndHashCode
28 @NoArgsConstructor
29 @Setter
30 @Getter
31 @Slf4j
32 public class KeyExpression {
33
34 private static final String KEY_SPEC_NOT_PRESENT = "key spec not present, use our keygen to generate valid keys";
35
36 private String spec;
37 private String value;
38
39 KeyExpression(final String spec, final String value) {
40 if (isBlank(spec)) {
41 log.error(KEY_SPEC_NOT_PRESENT);
42 throw new IllegalArgumentException(KEY_SPEC_NOT_PRESENT);
43 }
44 this.spec = spec;
45 this.value = value;
46 }
47
48 public KeyExpression(final String expression) {
49 if (isNotBlank(expression)) {
50 final int index = expression.indexOf(CryptoConstants.COLON);
51 if (index <= 0) {
52 log.error(KEY_SPEC_NOT_PRESENT);
53 throw new IllegalArgumentException(KEY_SPEC_NOT_PRESENT);
54 }
55 this.spec = expression.substring(0, index);
56 this.value = expression.substring(index + 1);
57 }
58 }
59
60 @Override
61 public String toString() {
62 return "" + this.spec + CryptoConstants.COLON + this.value;
63 }
64
65 public Boolean isPresent() {
66 return isNotBlank(this.spec) && isNotBlank(this.value);
67 }
68 }