0357fa74b34af54521f66b2bf798177f4dbda853
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / authz / impl / AuthzResource.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * *\r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * *\r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package org.onap.dmaap.datarouter.authz.impl;\r
26 \r
27 import java.util.regex.Matcher;\r
28 import java.util.regex.Pattern;\r
29 \r
30 /** Internal representation of an authorization resource (the entity to which access is being requested).  Consists\r
31  * of a type and an identifier.   The constructor takes the request URI from an HTTP request and checks it against\r
32  * patterns for the the different resource types.  In DR R1, there are four resource types:\r
33  * <ul>\r
34  * <li>the feeds collection resource, the target of POST requests to create a new feed and GET requests to list\r
35  * the existing feeds.  This is the root resource for the DR provisioning system, and it has no explicit id.\r
36  * </li>\r
37  * <li>a feed resource, the target of GET, PUT, and DELETE requests used to manage an existing feed.  Each feed\r
38  * has a unique feed ID.\r
39  * </li>\r
40  * <li>a subscription collection resource, the target of POST requests to create a new subscription and GET requests\r
41  * to list the subscriptions for a feed.  Each feed has a subscription collection, and the ID associated with a\r
42  * subscription collection is the ID of the feed.\r
43  * </li>\r
44  * <li>a subscription resource, the target of GET, PUT, and DELETE requests used to manage an existing subscription.\r
45  * Each subscription has a unique subscription ID.\r
46  * </li>\r
47  *\r
48  * @author J. F. Lucas\r
49  *\r
50  */\r
51 public class AuthzResource {\r
52     private ResourceType type = null;\r
53     private String id = "";\r
54 \r
55     /* Construct an AuthzResource by matching a request URI against the various patterns */\r
56     public AuthzResource(String rURI) {\r
57         if (rURI != null) {\r
58             for (ResourceType t : ResourceType.values()) {\r
59                 Matcher m = t.getPattern().matcher(rURI);\r
60                 if (m.find(0)) {\r
61                     this.type = t;\r
62                     if (m.group("id") != null) {\r
63                         this.id = m.group("id");\r
64                     }\r
65                     break;\r
66                 }\r
67             }\r
68         }\r
69     }\r
70 \r
71     public ResourceType getType() {\r
72         return this.type;\r
73     }\r
74 \r
75     public String getId() {\r
76         return this.id;\r
77     }\r
78 \r
79     /* Enumeration that helps turn a request URI into something more useful for\r
80      * authorization purposes by given a type name and a pattern for determining if the URI\r
81      * represents that resource type.\r
82      * Highly dependent on the URL scheme, could be parameterized.\r
83      */\r
84     public enum ResourceType {\r
85         FEEDS_COLLECTION("((://[^/]+/)|(^/))(?<id>)$"),\r
86         SUBS_COLLECTION ("((://[^/]+/)|(^/{0,1}))subscribe/(?<id>[^/]+)$"),\r
87         FEED("((://[^/]+/)|(^/{0,1}))feed/(?<id>[^/]+)$"),\r
88         SUB("((://[^/]+/)|(^/{0,1}))subs/(?<id>[^/]+)$");\r
89 \r
90         private Pattern uriPattern;\r
91 \r
92         private ResourceType(String patternString) {\r
93             this.uriPattern = Pattern.compile(patternString);\r
94         }\r
95 \r
96         Pattern getPattern() {\r
97             return this.uriPattern;\r
98         }\r
99     }\r
100 }\r