View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.authorization.xacml.manager.endpoint;
23  
24  import static org.opencastproject.util.Jsons.Obj;
25  import static org.opencastproject.util.Jsons.Val;
26  import static org.opencastproject.util.Jsons.arr;
27  import static org.opencastproject.util.Jsons.obj;
28  import static org.opencastproject.util.Jsons.p;
29  import static org.opencastproject.util.data.Monadics.mlist;
30  
31  import org.opencastproject.authorization.xacml.manager.api.ManagedAcl;
32  import org.opencastproject.security.api.AccessControlEntry;
33  import org.opencastproject.security.api.AccessControlList;
34  import org.opencastproject.util.data.Function;
35  
36  /** Converter functions from business objects to JSON structures. */
37  public final class JsonConv {
38  
39    public static final String KEY_ID = "id";
40    public static final String KEY_NAME = "name";
41    public static final String KEY_ORGANIZATION_ID = "organizationId";
42    public static final String KEY_ACL = "acl";
43    public static final String KEY_ACE = "ace";
44    public static final String KEY_ROLE = "role";
45    public static final String KEY_ACTION = "action";
46    public static final String KEY_ALLOW = "allow";
47  
48    private JsonConv() {
49    }
50  
51    public static Obj digest(ManagedAcl acl) {
52      return obj(p(KEY_ID, acl.getId()),
53                 p(KEY_NAME, acl.getName()));
54    }
55  
56    public static Obj full(ManagedAcl acl) {
57      return obj(p(KEY_ID, acl.getId()),
58                 p(KEY_NAME, acl.getName()),
59                 p(KEY_ORGANIZATION_ID, acl.getOrganizationId()),
60                 p(KEY_ACL, full(acl.getAcl())));
61    }
62  
63    public static final Function<ManagedAcl, Val> fullManagedAcl = new Function<ManagedAcl, Val>() {
64      @Override public Val apply(ManagedAcl acl) {
65        return full(acl);
66      }
67    };
68  
69    public static Obj full(AccessControlList acl) {
70      return obj(p(KEY_ACE, arr(mlist(acl.getEntries()).map(fullAccessControlEntry))));
71    }
72  
73    public static Obj full(AccessControlEntry ace) {
74      return obj(p(KEY_ROLE, ace.getRole()),
75                 p(KEY_ACTION, ace.getAction()),
76                 p(KEY_ALLOW, ace.isAllow()));
77    }
78  
79    public static final Function<AccessControlEntry, Val> fullAccessControlEntry
80        = new Function<AccessControlEntry, Val>() {
81          @Override
82          public Val apply(AccessControlEntry ace) {
83            return full(ace);
84          }
85        };
86  }