View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.restapi.v2.access;
20  
21  import java.util.Arrays;
22  
23  import jakarta.ws.rs.HttpMethod;
24  
25  import org.mycore.access.MCRAccessManager;
26  
27  /**
28   * The REST API access permissions (read, write, delete)
29   */
30  public enum MCRRestAPIACLPermission {
31      READ(MCRAccessManager.PERMISSION_READ), WRITE(MCRAccessManager.PERMISSION_WRITE),
32      DELETE(MCRAccessManager.PERMISSION_DELETE);
33  
34      private String value;
35  
36      MCRRestAPIACLPermission(final String value) {
37          this.value = value;
38      }
39  
40      public static MCRRestAPIACLPermission resolve(final String permission) {
41          return Arrays.stream(values())
42              .filter(object -> object.value.equalsIgnoreCase(permission))
43              .findFirst()
44              .orElse(null);
45      }
46  
47      @Override
48      public String toString() {
49          return this.value;
50      }
51  
52      public static MCRRestAPIACLPermission fromMethod(final String method) {
53          switch (method) {
54          case HttpMethod.GET:
55          case HttpMethod.HEAD:
56              return READ;
57          case HttpMethod.DELETE:
58              return DELETE;
59          case HttpMethod.POST:
60          case HttpMethod.PUT:
61          case HttpMethod.PATCH:
62              return WRITE;
63          default:
64              return null;
65          }
66      }
67  }