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.cadi.dme2;
\r
26 import java.io.ByteArrayOutputStream;
\r
27 import java.io.IOException;
\r
28 import java.net.URI;
\r
30 import javax.servlet.http.HttpServletResponse;
\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
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
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
57 ss.setSecurity(client);
\r
58 replyHandler = new DME2RestfulHandler(Rcli.BLANK);
\r
59 client.setReplyHandler(replyHandler);
\r
63 public void setMethod(String meth) {
\r
64 client.setMethod(meth);
\r
68 * DME2 can't handle having QueryParams on the URL line, but it is the most natural way, so...
\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
72 public void setPathInfo(String pathinfo) {
\r
73 int qp = pathinfo.indexOf('?');
\r
75 client.setContext(isProxy?("/proxy"+pathinfo):pathinfo);
\r
77 client.setContext(isProxy?("/proxy"+pathinfo.substring(0,qp)):pathinfo.substring(0,qp));
\r
78 client.setQueryParams(pathinfo.substring(qp+1));
\r
83 public void setPayload(EClient.Transfer transfer) {
\r
88 public void addHeader(String tag, String value) {
\r
89 client.addHeader(tag, value);
\r
94 public void setQueryParams(String q) {
\r
95 client.setQueryParams(q);
\r
99 public void setFragment(String f) {
\r
100 // DME2 does not implement this
\r
104 public void send() throws APIException {
\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
111 client.setPayload("");
\r
114 } catch (DME2Exception e) {
\r
115 throw new APIException(e);
\r
116 } catch (IOException e) {
\r
117 throw new APIException(e);
\r
122 public class DFuture<T> extends Future<T> {
\r
123 protected final DME2RestfulHandler reply;
\r
124 protected ResponseInfo info;
\r
126 public DFuture(DME2RestfulHandler reply) {
\r
127 this.reply = reply;
\r
130 protected boolean evalInfo() throws APIException{
\r
131 //return info.getCode()==200;
\r
135 public final boolean get(int timeout) throws CadiException {
\r
137 info = reply.getResponse(timeout);
\r
138 ss.setLastResponse(info.getCode());
\r
140 } catch (Exception e) {
\r
141 throw new CadiException(e);
\r
146 public int code() {
\r
147 return info.getCode();
\r
151 public String body() {
\r
152 return info.getBody();
\r
156 public String header(String tag) {
\r
157 return info.header(tag);
\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
167 return info.getCode()==201;
\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
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
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
213 public Future<Void> future(HttpServletResponse resp,int expected) throws APIException {
\r
214 // TODO Auto-generated method stub
\r
218 public void setProxy(boolean isProxy) {
\r
219 this.isProxy=isProxy;
\r