2020
2121package com .cognifide .apm .main .permissions ;
2222
23- import com .google .common .collect .ImmutableList ;
24- import com .google .common .collect .ImmutableMap ;
2523import com .google .common .collect .ImmutableSet ;
2624import java .util .Collections ;
2725import java .util .HashMap ;
2826import java .util .List ;
2927import java .util .Map ;
28+ import java .util .Optional ;
3029import java .util .Set ;
3130import javax .jcr .PropertyType ;
3231import javax .jcr .Value ;
@@ -40,12 +39,14 @@ public class Restrictions {
4039
4140 private static final String REP_GLOB_PROPERTY = "rep:glob" ;
4241
43- private static final String REP_NTNAMES_PROPERTY = "rep:ntNames " ;
42+ private static final String REP_GLOBS_PROPERTY = "rep:globs " ;
4443
45- private static final String REP_ITEMNAMES_PROPERTY = "rep:itemNames " ;
44+ private static final String REP_NT_NAMES_PROPERTY = "rep:ntNames " ;
4645
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" ,
4950 "rep:subtrees" , "sling:resourceTypes" , "sling:resourceTypesWithDescendants"
5051 );
5152
@@ -59,29 +60,23 @@ public class Restrictions {
5960
6061 public Restrictions (String glob , List <String > ntNames , List <String > itemNames , Map <String , Object > customRestrictions ) {
6162 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 ());
7367 }
7468
7569 public Map <String , Value > getSingleValueRestrictions (ValueFactory valueFactory ) throws ValueFormatException {
7670 Map <String , Value > result = new HashMap <>();
7771 addRestriction (valueFactory , result , REP_GLOB_PROPERTY , glob );
7872 for (Map .Entry <String , Object > entry : customRestrictions .entrySet ()) {
79- if (!isMultivalue (entry )) {
73+ if (!isMultiValue (entry )) {
8074 String value ;
8175 if (entry .getValue () instanceof String ) {
8276 value = (String ) entry .getValue ();
8377 } else {
84- value = ((List <String >) entry .getValue ()).get (0 );
78+ List <String > values = (List <String >) entry .getValue ();
79+ value = values .isEmpty () ? "" : values .get (0 );
8580 }
8681 addRestriction (valueFactory , result , entry .getKey (), value );
8782 }
@@ -90,12 +85,16 @@ public Map<String, Value> getSingleValueRestrictions(ValueFactory valueFactory)
9085 }
9186
9287 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 ));
9998 }
10099 }
101100
@@ -108,13 +107,14 @@ private Value normalizeGlob(ValueFactory valueFactory) {
108107
109108 public Map <String , Value []> getMultiValueRestrictions (ValueFactory valueFactory ) throws ValueFormatException {
110109 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 );
113112 for (Map .Entry <String , Object > entry : customRestrictions .entrySet ()) {
114- if (isMultivalue (entry )) {
113+ if (isMultiValue (entry )) {
115114 List <String > values ;
116115 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 );
118118 } else {
119119 values = (List <String >) entry .getValue ();
120120 }
@@ -125,24 +125,32 @@ public Map<String, Value[]> getMultiValueRestrictions(ValueFactory valueFactory)
125125 }
126126
127127 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 ));
130130 }
131131 }
132132
133- private Value [] createRestrictions (ValueFactory valueFactory , List <String > names ) throws ValueFormatException {
133+ private Value [] createRestrictions (ValueFactory valueFactory , String key , List <String > names ) throws ValueFormatException {
134134 Value [] values = new Value [names .size ()];
135135 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 ));
137137 }
138138 return values ;
139139 }
140140
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 ) {
142150 boolean result ;
143151 if (REP_GLOB_PROPERTY .equals (entry .getKey ())) {
144152 result = false ;
145- } else if (MULTIVALUE_REP_PROPERTIES .contains (entry .getKey ())) {
153+ } else if (MULTI_VALUE_REP_PROPERTIES .contains (entry .getKey ())) {
146154 result = true ;
147155 } else {
148156 result = entry .getValue () instanceof List ;
0 commit comments