Authz unit test and code cleanup
[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  * <li>the feeds collection resource, the target of POST requests to create a new feed and GET requests to list\r
34  * the existing feeds.  This is the root resource for the DR provisioning system, and it has no explicit id.\r
35  * </li>\r
36  * <li>a feed resource, the target of GET, PUT, and DELETE requests used to manage an existing feed.  Each feed\r
37  * has a unique feed ID.\r
38  * </li>\r
39  * <li>a subscription collection resource, the target of POST requests to create a new subscription and GET requests\r
40  * to list the subscriptions for a feed.  Each feed has a subscription collection, and the ID associated with a\r
41  * subscription collection is the ID of the feed.\r
42  * </li>\r
43  * <li>a subscription resource, the target of GET, PUT, and DELETE requests used to manage an existing subscription.\r
44  * Each subscription has a unique subscription ID.\r
45  * </li>\r
46  *\r
47  * @author J. F. Lucas\r
48  *\r
49  */\r
50 public class AuthzResource {\r
51     private ResourceType type = null;\r
52     private String id = "";\r
53 \r
54     /* Construct an AuthzResource by matching a request URI against the various patterns */\r
55     AuthzResource(String requestUri) {\r
56         if (requestUri != null) {\r
57             for (ResourceType t : ResourceType.values()) {\r
58                 Matcher m = t.getPattern().matcher(requestUri);\r
59                 if (m.find(0)) {\r
60                     this.type = t;\r
61                     if (m.group("id") != null) {\r
62                         this.id = m.group("id");\r
63                     }\r
64                     break;\r
65                 }\r
66             }\r
67         }\r
68     }\r
69 \r
70     public ResourceType getType() {\r
71         return this.type;\r
72     }\r
73 \r
74     public String getId() {\r
75         return this.id;\r
76     }\r
77 \r
78     /* Enumeration that helps turn a request URI into something more useful for\r
79      * authorization purposes by given a type name and a pattern for determining if the URI\r
80      * represents that resource type.\r
81      * Highly dependent on the URL scheme, could be parameterized.\r
82      */\r
83     public enum ResourceType {\r
84         FEEDS_COLLECTION("((://[^/]+/)|(^/))(?<id>)$"),\r
85         SUBS_COLLECTION("((://[^/]+/)|(^/{0,1}))subscribe/(?<id>[^/]+)$"),\r
86         FEED("((://[^/]+/)|(^/{0,1}))feed/(?<id>[^/]+)$"),\r
87         SUB("((://[^/]+/)|(^/{0,1}))subs/(?<id>[^/]+)$");\r
88 \r
89         private Pattern uriPattern;\r
90 \r
91         ResourceType(String patternString) {\r
92             this.uriPattern = Pattern.compile(patternString);\r
93         }\r
94 \r
95         Pattern getPattern() {\r
96             return this.uriPattern;\r
97         }\r
98     }\r
99 }\r