1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * Copyright © 2017 Amdocs
\r
7 * * ===========================================================================
\r
8 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * * you may not use this file except in compliance with the License.
\r
10 * * You may obtain a copy of the License at
\r
12 * * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * * Unless required by applicable law or agreed to in writing, software
\r
15 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * * See the License for the specific language governing permissions and
\r
18 * * limitations under the License.
\r
19 * * ============LICENSE_END====================================================
\r
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.cssa.rserv;
\r
26 import java.io.IOException;
\r
27 import java.util.List;
\r
29 import javax.servlet.ServletException;
\r
30 import javax.servlet.http.HttpServletRequest;
\r
31 import javax.servlet.http.HttpServletResponse;
\r
33 import com.att.inno.env.Env;
\r
34 import com.att.inno.env.TimeTaken;
\r
35 import com.att.inno.env.Trans;
\r
37 public class Route<TRANS extends Trans> {
\r
38 public final String auditText;
\r
39 public final HttpMethods meth;
\r
40 public final String path;
\r
42 private Match match;
\r
43 // package on purpose
\r
44 private final TypedCode<TRANS> content;
\r
45 private final boolean isGet;
\r
47 public Route(HttpMethods meth, String path) {
\r
49 auditText = meth.name() + ' ' + path;
\r
50 this.meth = meth; // Note: Using Spark def for now.
\r
51 isGet = meth.compareTo(HttpMethods.GET) == 0;
\r
52 match = new Match(path);
\r
53 content = new TypedCode<TRANS>();
\r
56 public void add(HttpCode<TRANS,?> code, String ... others) {
\r
58 content.add(code, others);
\r
61 // public void add(HttpCode<TRANS,?> code, Class<?> cls, String version, String ... others) {
\r
62 // code.match = match;
\r
63 // content.add(code, cls, version, others);
\r
66 public HttpCode<TRANS,?> getCode(TRANS trans, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
\r
67 // Type is associated with Accept for GET (since it is what is being returned
\r
68 // We associate the rest with ContentType.
\r
69 // FYI, thought about this a long time before implementing this way.
\r
71 // String special[]; // todo, expose Charset (in special) to outside
\r
73 compare = req.getHeader("Accept"); // Accept is used for read, as we want to agree on what caller is ready to handle
\r
75 compare = req.getContentType(); // Content type used to declare what data is being created, updated or deleted (might be used for key)
\r
78 Pair<String, Pair<HttpCode<TRANS, ?>, List<Pair<String, Object>>>> hl = content.prep(trans, compare);
\r
80 resp.setStatus(406); // NOT_ACCEPTABLE
\r
82 if(isGet) { // Set Content Type to expected content
\r
83 if("*".equals(hl.x) || "*/*".equals(hl.x)) {// if wild-card, then choose first kind of type
\r
84 resp.setContentType(content.first());
\r
86 resp.setContentType(hl.x);
\r
94 public Route<TRANS> matches(String method, String path) {
\r
95 return meth.name().equalsIgnoreCase(method) && match.match(path)?this:null;
\r
98 public TimeTaken start(Trans trans, String auditText, HttpCode<TRANS,?> code, String type) {
\r
99 StringBuilder sb = new StringBuilder(auditText);
\r
101 sb.append(code.desc());
\r
102 sb.append(", Content: ");
\r
104 return trans.start(sb.toString(), Env.SUB);
\r
107 // Package on purpose.. for "find/Create" routes only
\r
108 boolean resolvesTo(HttpMethods hm, String p) {
\r
109 return(path.equals(p) && hm.equals(meth));
\r
112 public String toString() {
\r
113 return auditText + ' ' + content;
\r
116 public String report(HttpCode<TRANS, ?> code) {
\r
117 StringBuilder sb = new StringBuilder();
\r
118 sb.append(auditText);
\r
120 content.relatedTo(code, sb);
\r
121 return sb.toString();
\r
124 public RouteReport api() {
\r
125 RouteReport tr = new RouteReport();
\r
134 * contentRelatedTo (For reporting) list routes that will end up at a specific Code
\r
137 public String contentRelatedTo(HttpCode<TRANS, ?> code) {
\r
138 StringBuilder sb = new StringBuilder(path);
\r
140 content.relatedTo(code, sb);
\r
141 return sb.toString();
\r