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