20
20
21
21
package com .cognifide .apm .main .permissions ;
22
22
23
- import com .google .common .collect .ImmutableList ;
24
- import com .google .common .collect .ImmutableMap ;
25
23
import com .google .common .collect .ImmutableSet ;
26
24
import java .util .Collections ;
27
25
import java .util .HashMap ;
28
26
import java .util .List ;
29
27
import java .util .Map ;
28
+ import java .util .Optional ;
30
29
import java .util .Set ;
31
30
import javax .jcr .PropertyType ;
32
31
import javax .jcr .Value ;
@@ -40,12 +39,14 @@ public class Restrictions {
40
39
41
40
private static final String REP_GLOB_PROPERTY = "rep:glob" ;
42
41
43
- private static final String REP_NTNAMES_PROPERTY = "rep:ntNames " ;
42
+ private static final String REP_GLOBS_PROPERTY = "rep:globs " ;
44
43
45
- private static final String REP_ITEMNAMES_PROPERTY = "rep:itemNames " ;
44
+ private static final String REP_NT_NAMES_PROPERTY = "rep:ntNames " ;
46
45
47
- private static final Set <String > MULTIVALUE_REP_PROPERTIES = ImmutableSet .of (
48
- REP_NTNAMES_PROPERTY , REP_ITEMNAMES_PROPERTY , "rep:prefixes" , "rep:current" , "rep:globs" ,
46
+ private static final String REP_ITEM_NAMES_PROPERTY = "rep:itemNames" ;
47
+
48
+ private static final Set <String > MULTI_VALUE_REP_PROPERTIES = ImmutableSet .of (
49
+ REP_NT_NAMES_PROPERTY , REP_ITEM_NAMES_PROPERTY , REP_GLOBS_PROPERTY , "rep:prefixes" , "rep:current" ,
49
50
"rep:subtrees" , "sling:resourceTypes" , "sling:resourceTypesWithDescendants"
50
51
);
51
52
@@ -59,29 +60,23 @@ public class Restrictions {
59
60
60
61
public Restrictions (String glob , List <String > ntNames , List <String > itemNames , Map <String , Object > customRestrictions ) {
61
62
this .glob = glob ;
62
- this .ntNames = notNullCopy (ntNames );
63
- this .itemNames = notNullCopy (itemNames );
64
- this .customRestrictions = notNullCopy (customRestrictions );
65
- }
66
-
67
- private List <String > notNullCopy (List <String > strings ) {
68
- return strings != null ? ImmutableList .copyOf (strings ) : Collections .emptyList ();
69
- }
70
-
71
- private Map <String , Object > notNullCopy (Map <String , Object > items ) {
72
- return items != null ? ImmutableMap .copyOf (items ) : Collections .emptyMap ();
63
+ this .ntNames = ntNames ;
64
+ this .itemNames = itemNames ;
65
+ this .customRestrictions = Optional .ofNullable (customRestrictions )
66
+ .orElse (Collections .emptyMap ());
73
67
}
74
68
75
69
public Map <String , Value > getSingleValueRestrictions (ValueFactory valueFactory ) throws ValueFormatException {
76
70
Map <String , Value > result = new HashMap <>();
77
71
addRestriction (valueFactory , result , REP_GLOB_PROPERTY , glob );
78
72
for (Map .Entry <String , Object > entry : customRestrictions .entrySet ()) {
79
- if (!isMultivalue (entry )) {
73
+ if (!isMultiValue (entry )) {
80
74
String value ;
81
75
if (entry .getValue () instanceof String ) {
82
76
value = (String ) entry .getValue ();
83
77
} else {
84
- value = ((List <String >) entry .getValue ()).get (0 );
78
+ List <String > values = (List <String >) entry .getValue ();
79
+ value = values .isEmpty () ? "" : values .get (0 );
85
80
}
86
81
addRestriction (valueFactory , result , entry .getKey (), value );
87
82
}
@@ -90,12 +85,16 @@ public Map<String, Value> getSingleValueRestrictions(ValueFactory valueFactory)
90
85
}
91
86
92
87
private void addRestriction (ValueFactory valueFactory , Map <String , Value > result , String key , String value ) throws ValueFormatException {
93
- if (StringUtils .isNotBlank (value )) {
94
- if (REP_GLOB_PROPERTY .equals (key )) {
95
- result .put (key , normalizeGlob (valueFactory ));
96
- } else {
97
- result .put (key , valueFactory .createValue (value , PropertyType .NAME ));
98
- }
88
+ if (value != null ) {
89
+ result .put (key , createValue (valueFactory , key , value ));
90
+ }
91
+ }
92
+
93
+ private Value createValue (ValueFactory valueFactory , String key , String value ) throws ValueFormatException {
94
+ if (StringUtils .equalsAny (key , REP_GLOB_PROPERTY , REP_GLOBS_PROPERTY )) {
95
+ return normalizeGlob (valueFactory );
96
+ } else {
97
+ return valueFactory .createValue (value , determinePropertyType (key ));
99
98
}
100
99
}
101
100
@@ -108,13 +107,14 @@ private Value normalizeGlob(ValueFactory valueFactory) {
108
107
109
108
public Map <String , Value []> getMultiValueRestrictions (ValueFactory valueFactory ) throws ValueFormatException {
110
109
Map <String , Value []> result = new HashMap <>();
111
- addRestrictions (valueFactory , result , REP_NTNAMES_PROPERTY , ntNames );
112
- addRestrictions (valueFactory , result , REP_ITEMNAMES_PROPERTY , itemNames );
110
+ addRestrictions (valueFactory , result , REP_NT_NAMES_PROPERTY , ntNames );
111
+ addRestrictions (valueFactory , result , REP_ITEM_NAMES_PROPERTY , itemNames );
113
112
for (Map .Entry <String , Object > entry : customRestrictions .entrySet ()) {
114
- if (isMultivalue (entry )) {
113
+ if (isMultiValue (entry )) {
115
114
List <String > values ;
116
115
if (entry .getValue () instanceof String ) {
117
- values = Collections .singletonList ((String ) entry .getValue ());
116
+ String value = (String ) entry .getValue ();
117
+ values = value .isEmpty () ? Collections .emptyList () : Collections .singletonList (value );
118
118
} else {
119
119
values = (List <String >) entry .getValue ();
120
120
}
@@ -125,24 +125,32 @@ public Map<String, Value[]> getMultiValueRestrictions(ValueFactory valueFactory)
125
125
}
126
126
127
127
private void addRestrictions (ValueFactory valueFactory , Map <String , Value []> result , String key , List <String > names ) throws ValueFormatException {
128
- if (names != null && ! names . isEmpty () ) {
129
- result .put (key , createRestrictions (valueFactory , names ));
128
+ if (names != null ) {
129
+ result .put (key , createRestrictions (valueFactory , key , names ));
130
130
}
131
131
}
132
132
133
- private Value [] createRestrictions (ValueFactory valueFactory , List <String > names ) throws ValueFormatException {
133
+ private Value [] createRestrictions (ValueFactory valueFactory , String key , List <String > names ) throws ValueFormatException {
134
134
Value [] values = new Value [names .size ()];
135
135
for (int index = 0 ; index < names .size (); index ++) {
136
- values [index ] = valueFactory . createValue (names .get (index ), PropertyType . NAME );
136
+ values [index ] = createValue (valueFactory , key , names .get (index ));
137
137
}
138
138
return values ;
139
139
}
140
140
141
- private boolean isMultivalue (Map .Entry <String , Object > entry ) {
141
+ private int determinePropertyType (String key ) {
142
+ if (StringUtils .equalsAny (key , REP_NT_NAMES_PROPERTY , REP_ITEM_NAMES_PROPERTY )) {
143
+ return PropertyType .NAME ;
144
+ } else {
145
+ return PropertyType .STRING ;
146
+ }
147
+ }
148
+
149
+ private boolean isMultiValue (Map .Entry <String , Object > entry ) {
142
150
boolean result ;
143
151
if (REP_GLOB_PROPERTY .equals (entry .getKey ())) {
144
152
result = false ;
145
- } else if (MULTIVALUE_REP_PROPERTIES .contains (entry .getKey ())) {
153
+ } else if (MULTI_VALUE_REP_PROPERTIES .contains (entry .getKey ())) {
146
154
result = true ;
147
155
} else {
148
156
result = entry .getValue () instanceof List ;
0 commit comments