removed unused ports
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / rserv / Route.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.rserv;
23
24
25 import java.util.List;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.onap.aaf.misc.env.Env;
31 import org.onap.aaf.misc.env.TimeTaken;
32 import org.onap.aaf.misc.env.Trans;
33
34 public class Route<TRANS extends Trans> {
35     public final String auditText;
36     public final HttpMethods meth;
37     public final String path;
38
39     private Match match;
40     // package on purpose
41     private final TypedCode<TRANS> content;
42     private final boolean isContentType;
43
44     public Route(HttpMethods meth, String path) {
45         this.path = path;
46         auditText = meth.name() + ' ' + path;
47         this.meth = meth; // Note: Using Spark def for now.
48         isContentType = meth.compareTo(HttpMethods.GET) == 0 || meth.compareTo(HttpMethods.DELETE)==0;
49         match = new Match(path);
50         content = new TypedCode<TRANS>();
51     }
52
53     public void add(HttpCode<TRANS,?> code, String ... others) {
54         code.match = match;
55         content.add(code, others);
56     }
57
58     public HttpCode<TRANS,?> getCode(TRANS trans, HttpServletRequest req, HttpServletResponse resp) {
59         // Type is associated with Accept for GET (since it is what is being returned
60         // We associate the rest with ContentType.
61         // FYI, thought about this a long time before implementing this way.
62         String compare;
63         if (isContentType) {
64             compare = req.getHeader("Accept"); // Accept is used for read, as we want to agree on what caller is ready to handle
65         } else {
66             compare = req.getContentType(); // Content type used to declare what data is being created, updated or deleted (might be used for key)
67         }
68
69         Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> hl = content.prep(trans, compare);
70         if (hl==null) {
71             resp.setStatus(406); // NOT_ACCEPTABLE
72         } else {
73             if (isContentType) { // Set Content Type to expected content
74                 if ("*".equals(hl.x) || "*/*".equals(hl.x)) {// if wild-card, then choose first kind of type
75                     resp.setContentType(content.first());
76                 } else {
77                     resp.setContentType(hl.x);
78                 }
79             }
80             return hl.y.x;
81         }
82         return null;
83     }
84
85     public Route<TRANS> matches(String method, String path) {
86         return meth.name().equalsIgnoreCase(method) && match.match(path)?this:null;
87     }
88
89     public TimeTaken start(Trans trans, String auditText, HttpCode<TRANS,?> code, String type) {
90         StringBuilder sb = new StringBuilder(auditText);
91         sb.append(", ");
92         sb.append(code.desc());
93         sb.append(", Content: ");
94         sb.append(type);
95         return trans.start(sb.toString(), Env.SUB);
96     }
97
98     // Package on purpose.. for "find/Create" routes only
99     boolean resolvesTo(HttpMethods hm, String p) {
100         return(path.equals(p) && hm.equals(meth));
101     }
102
103     public String toString() {
104         return auditText + ' ' + content;
105     }
106
107     public String report(HttpCode<TRANS, ?> code) {
108         StringBuilder sb = new StringBuilder();
109         sb.append(auditText);
110         sb.append(' ');
111         content.relatedTo(code, sb);
112         return sb.toString();
113     }
114
115     public RouteReport api() {
116         RouteReport tr = new RouteReport();
117         tr.meth = meth;
118         tr.path = path;
119         content.api(tr);
120         return tr;
121     }
122
123
124     /**
125      * contentRelatedTo (For reporting) list routes that will end up at a specific Code
126      * @return
127      */
128     public String contentRelatedTo(HttpCode<TRANS, ?> code) {
129         StringBuilder sb = new StringBuilder(path);
130         sb.append(' ');
131         content.relatedTo(code, sb);
132         return sb.toString();
133     }
134 }