1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.cssa.rserv;
\r
25 import java.io.IOException;
\r
26 import java.util.List;
\r
28 import javax.servlet.ServletException;
\r
29 import javax.servlet.http.HttpServletRequest;
\r
30 import javax.servlet.http.HttpServletResponse;
\r
32 import org.onap.aaf.inno.env.Env;
\r
33 import org.onap.aaf.inno.env.TimeTaken;
\r
34 import org.onap.aaf.inno.env.Trans;
\r
36 public class Route<TRANS extends Trans> {
\r
37 public final String auditText;
\r
38 public final HttpMethods meth;
\r
39 public final String path;
\r
41 private Match match;
\r
42 // package on purpose
\r
43 private final TypedCode<TRANS> content;
\r
44 private final boolean isGet;
\r
46 public Route(HttpMethods meth, String path) {
\r
48 auditText = meth.name() + ' ' + path;
\r
49 this.meth = meth; // Note: Using Spark def for now.
\r
50 isGet = meth.compareTo(HttpMethods.GET) == 0;
\r
51 match = new Match(path);
\r
52 content = new TypedCode<TRANS>();
\r
55 public void add(HttpCode<TRANS,?> code, String ... others) {
\r
57 content.add(code, others);
\r
60 // public void add(HttpCode<TRANS,?> code, Class<?> cls, String version, String ... others) {
\r
61 // code.match = match;
\r
62 // content.add(code, cls, version, others);
\r
65 public HttpCode<TRANS,?> getCode(TRANS trans, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
\r
66 // Type is associated with Accept for GET (since it is what is being returned
\r
67 // We associate the rest with ContentType.
\r
68 // FYI, thought about this a long time before implementing this way.
\r
70 // String special[]; // todo, expose Charset (in special) to outside
\r
72 compare = req.getHeader("Accept"); // Accept is used for read, as we want to agree on what caller is ready to handle
\r
74 compare = req.getContentType(); // Content type used to declare what data is being created, updated or deleted (might be used for key)
\r
77 Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> hl = content.prep(trans, compare);
\r
79 resp.setStatus(406); // NOT_ACCEPTABLE
\r
81 if(isGet) { // Set Content Type to expected content
\r
82 if("*".equals(hl.x) || "*/*".equals(hl.x)) {// if wild-card, then choose first kind of type
\r
83 resp.setContentType(content.first());
\r
85 resp.setContentType(hl.x);
\r
93 public Route<TRANS> matches(String method, String path) {
\r
94 return meth.name().equalsIgnoreCase(method) && match.match(path)?this:null;
\r
97 public TimeTaken start(Trans trans, String auditText, HttpCode<TRANS,?> code, String type) {
\r
98 StringBuilder sb = new StringBuilder(auditText);
\r
100 sb.append(code.desc());
\r
101 sb.append(", Content: ");
\r
103 return trans.start(sb.toString(), Env.SUB);
\r
106 // Package on purpose.. for "find/Create" routes only
\r
107 boolean resolvesTo(HttpMethods hm, String p) {
\r
108 return(path.equals(p) && hm.equals(meth));
\r
111 public String toString() {
\r
112 return auditText + ' ' + content;
\r
115 public String report(HttpCode<TRANS, ?> code) {
\r
116 StringBuilder sb = new StringBuilder();
\r
117 sb.append(auditText);
\r
119 content.relatedTo(code, sb);
\r
120 return sb.toString();
\r
123 public RouteReport api() {
\r
124 RouteReport tr = new RouteReport();
\r
133 * contentRelatedTo (For reporting) list routes that will end up at a specific Code
\r
136 public String contentRelatedTo(HttpCode<TRANS, ?> code) {
\r
137 StringBuilder sb = new StringBuilder(path);
\r
139 content.relatedTo(code, sb);
\r
140 return sb.toString();
\r