[AAF-21] Initial code import
[aaf/cadi.git] / client / src / main / java / com / att / cadi / dme2 / DEClient.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\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
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \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
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.dme2;\r
25 \r
26 import java.io.ByteArrayOutputStream;\r
27 import java.io.IOException;\r
28 import java.net.URI;\r
29 \r
30 import javax.servlet.http.HttpServletResponse;\r
31 \r
32 import com.att.aft.dme2.api.DME2Client;\r
33 import com.att.aft.dme2.api.DME2Exception;\r
34 import com.att.aft.dme2.api.DME2Manager;\r
35 import com.att.aft.dme2.handler.DME2RestfulHandler;\r
36 import com.att.aft.dme2.handler.DME2RestfulHandler.ResponseInfo;\r
37 import com.att.cadi.CadiException;\r
38 import com.att.cadi.SecuritySetter;\r
39 import com.att.cadi.client.EClient;\r
40 import com.att.cadi.client.Future;\r
41 import com.att.cadi.client.Rcli;\r
42 import com.att.inno.env.APIException;\r
43 import com.att.inno.env.Data;\r
44 import com.att.rosetta.env.RosettaDF;\r
45 \r
46 public class DEClient implements EClient<DME2Client> {\r
47         private DME2Client client;\r
48         private DME2RestfulHandler replyHandler;\r
49         private EClient.Transfer payload;\r
50         private boolean isProxy;\r
51         private SecuritySetter<DME2Client> ss;\r
52         \r
53         public DEClient(DME2Manager manager, SecuritySetter<DME2Client> ss, URI uri, long timeout) throws DME2Exception, CadiException {\r
54                 client = new DME2Client(manager,uri,timeout);\r
55                 client.setAllowAllHttpReturnCodes(true);\r
56                 this.ss = ss;\r
57                 ss.setSecurity(client);\r
58                 replyHandler = new DME2RestfulHandler(Rcli.BLANK);\r
59                 client.setReplyHandler(replyHandler);\r
60         }\r
61 \r
62         @Override\r
63         public void setMethod(String meth) {\r
64                 client.setMethod(meth);\r
65         }\r
66 \r
67         /**\r
68          * DME2 can't handle having QueryParams on the URL line, but it is the most natural way, so...\r
69          * \r
70          * Also, DME2 can't handle "/proxy" as part of Context in the main URI line, so we add it when we see authz-gw to "isProxy"\r
71          */\r
72         public void setPathInfo(String pathinfo) {\r
73                 int qp = pathinfo.indexOf('?');\r
74                 if(qp<0) {\r
75                         client.setContext(isProxy?("/proxy"+pathinfo):pathinfo);\r
76                 } else {\r
77                         client.setContext(isProxy?("/proxy"+pathinfo.substring(0,qp)):pathinfo.substring(0,qp));\r
78                         client.setQueryParams(pathinfo.substring(qp+1));\r
79                 }\r
80         }\r
81 \r
82         @Override\r
83         public void setPayload(EClient.Transfer transfer) {\r
84                 payload = transfer;\r
85         }\r
86 \r
87         @Override\r
88         public void addHeader(String tag, String value) {\r
89                 client.addHeader(tag, value);\r
90         }\r
91 \r
92 \r
93         @Override\r
94         public void setQueryParams(String q) {\r
95                 client.setQueryParams(q);\r
96         }\r
97 \r
98         @Override\r
99         public void setFragment(String f) {\r
100                 // DME2 does not implement this\r
101         }\r
102 \r
103         @Override\r
104         public void send() throws APIException {\r
105                 try {\r
106                         if(payload!=null) {\r
107                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
108                                 payload.transfer(baos);\r
109                                 client.setPayload(new String(baos.toByteArray()));\r
110                         } else {\r
111                                 client.setPayload("");\r
112                         }\r
113                         client.send();\r
114                 } catch (DME2Exception e) {\r
115                         throw new APIException(e);\r
116                 } catch (IOException e) {\r
117                         throw new APIException(e);\r
118                 }\r
119         }\r
120 \r
121         \r
122         public class DFuture<T> extends Future<T> {\r
123                 protected final DME2RestfulHandler reply;\r
124                 protected ResponseInfo info;\r
125                 \r
126                 public DFuture(DME2RestfulHandler reply) {\r
127                         this.reply = reply;\r
128                 }\r
129                 \r
130                 protected boolean evalInfo() throws APIException{\r
131                         //return info.getCode()==200;\r
132                         return true;\r
133                 };\r
134                 \r
135                 public final boolean get(int timeout) throws CadiException {\r
136                         try {\r
137                                 info = reply.getResponse(timeout);\r
138                                 ss.setLastResponse(info.getCode());\r
139                                 return evalInfo();\r
140                         } catch (Exception e) {\r
141                                 throw new CadiException(e);\r
142                         }\r
143                 }\r
144 \r
145                 @Override\r
146                 public int code() {\r
147                         return info.getCode();\r
148                 }\r
149 \r
150                 @Override\r
151                 public String body() {\r
152                         return info.getBody();\r
153                 }\r
154 \r
155                 @Override\r
156                 public String header(String tag) {\r
157                         return info.header(tag);\r
158                 }\r
159 \r
160         }\r
161 \r
162         @Override\r
163         public <T> Future<T> futureCreate(Class<T> t) {\r
164                 return new DFuture<T>(replyHandler) {\r
165                         public boolean evalInfo() throws APIException {\r
166                                 \r
167                                 return info.getCode()==201;\r
168                         }\r
169                 };\r
170         }\r
171         \r
172 \r
173         @Override\r
174         public Future<String> futureReadString() {\r
175                 return new DFuture<String>(replyHandler) {\r
176                         public boolean evalInfo() throws APIException {\r
177                                 if(info.getCode()==200) {\r
178                                         value = info.getBody();\r
179                                         return true;\r
180                                 }\r
181                                 return false;\r
182                         }\r
183                 };\r
184         }\r
185         \r
186         @Override\r
187         public<T> Future<T> futureRead(final RosettaDF<T> df, final Data.TYPE type) {\r
188                 return new DFuture<T>(replyHandler) {\r
189                         public boolean evalInfo() throws APIException {\r
190                                 if(info.getCode()==200) {\r
191                                         value = df.newData().in(type).load(info.getBody()).asObject();\r
192                                         return true;\r
193                                 }\r
194                                 return false;\r
195                         }\r
196                 };\r
197         }\r
198 \r
199         @Override\r
200         public <T> Future<T> future(final T t) {\r
201                 return new DFuture<T>(replyHandler) {\r
202                         public boolean evalInfo() {\r
203                                 if(info.getCode()==200) {\r
204                                         value = t;\r
205                                         return true;\r
206                                 }\r
207                                 return false;\r
208                         }\r
209                 };\r
210         }\r
211 \r
212         @Override\r
213         public Future<Void> future(HttpServletResponse resp,int expected) throws APIException {\r
214                 // TODO Auto-generated method stub\r
215                 return null;\r
216         }\r
217 \r
218         public void setProxy(boolean isProxy) {\r
219                 this.isProxy=isProxy;\r
220         }\r
221 \r
222         \r
223 }\r