2828import io .swagger .annotations .ApiModelProperty ;
2929
3030import javax .validation .constraints .NotNull ;
31+ import javax .ws .rs .BadRequestException ;
3132import java .util .Date ;
3233import java .util .Objects ;
3334import java .util .Optional ;
3637
3738import static com .devicehive .auth .HiveAction .NONE ;
3839
39- public class JwtUserPayloadView implements HiveEntity {
40+ public class JwtUserPayloadView < T > implements HiveEntity {
4041
4142 private static final long serialVersionUID = 9015868660504625526L ;
4243
@@ -54,7 +55,7 @@ public class JwtUserPayloadView implements HiveEntity {
5455
5556 @ JsonProperty (ACTIONS )
5657 @ SerializedName (ACTIONS )
57- private Set <String > actions ;
58+ private Set <T > actions ;
5859
5960 @ JsonProperty (NETWORK_IDS )
6061 @ SerializedName (NETWORK_IDS )
@@ -72,7 +73,7 @@ public class JwtUserPayloadView implements HiveEntity {
7273 @ SerializedName (TOKEN_TYPE )
7374 private TokenType tokenType ;
7475
75- public JwtUserPayloadView (Long userId , Set <String > actions , Set <String > networkIds ,
76+ public JwtUserPayloadView (Long userId , Set <T > actions , Set <String > networkIds ,
7677 Set <String > deviceTypeIds , Date expiration , TokenType tokenType ) {
7778 this .userId = userId ;
7879 this .actions = actions ;
@@ -90,11 +91,11 @@ public void setUserId(Long userId) {
9091 this .userId = userId ;
9192 }
9293
93- public Set <String > getActions () {
94+ public Set <T > getActions () {
9495 return actions ;
9596 }
9697
97- public void setActions (Set <String > actions ) {
98+ public void setActions (Set <T > actions ) {
9899 this .actions = actions ;
99100 }
100101
@@ -134,7 +135,13 @@ public JwtUserPayload convertTo() {
134135 Set <Integer > actionIds = Optional .ofNullable (actions )
135136 .map (value -> value .stream ()
136137 //Here the compatibility with old behavior is provided to ignore not valid actions
137- .map (action -> HiveAction .fromString (action ))
138+ .map (action -> {
139+ if (action instanceof String ) {
140+ return HiveAction .fromString ((String ) action );
141+ } else if (action instanceof Number && ((Double ) action - ((Double ) action ).intValue () == 0 )) {
142+ return HiveAction .fromId (((Double ) action ).intValue ());
143+ } else throw new BadRequestException ("Actions list should contain only Strings or Integers" );
144+ })
138145 .filter (Objects ::nonNull )
139146 .mapToInt (HiveAction ::getId )
140147 .boxed ()
@@ -148,15 +155,15 @@ public static Builder newBuilder() {
148155 return new Builder ();
149156 }
150157
151- public static class Builder {
158+ public static class Builder < T > {
152159 private Long userId ;
153- private Set <String > actions ;
160+ private Set <T > actions ;
154161 private Set <String > networkIds ;
155162 private Set <String > deviceTypeIds ;
156163 private Date expiration ;
157164 private TokenType tokenType ;
158165
159- public Builder withPublicClaims (Long userId , Set <String > actions ,
166+ public Builder withPublicClaims (Long userId , Set <T > actions ,
160167 Set <String > networkIds , Set <String > deviceTypeIds ) {
161168 this .userId = userId ;
162169 this .actions = actions ;
@@ -165,7 +172,7 @@ public Builder withPublicClaims(Long userId, Set<String> actions,
165172 return this ;
166173 }
167174
168- public Builder withPayload (JwtUserPayloadView payload ) {
175+ public Builder withPayload (JwtUserPayloadView < T > payload ) {
169176 this .userId = payload .getUserId ();
170177 this .actions = payload .getActions ();
171178 this .networkIds = payload .getNetworkIds ();
@@ -179,7 +186,7 @@ public Builder withUserId(Long userId) {
179186 return this ;
180187 }
181188
182- public Builder withActions (Set <String > actions ) {
189+ public Builder withActions (Set <T > actions ) {
183190 this .actions = actions ;
184191 return this ;
185192 }
@@ -204,8 +211,8 @@ public Builder withExpirationDate(Date expiration) {
204211 return this ;
205212 }
206213
207- public JwtUserPayloadView buildPayload () {
208- return new JwtUserPayloadView (userId , actions , networkIds , deviceTypeIds , expiration , tokenType );
214+ public JwtUserPayloadView < T > buildPayload () {
215+ return new JwtUserPayloadView < T > (userId , actions , networkIds , deviceTypeIds , expiration , tokenType );
209216 }
210217 }
211218}
0 commit comments