/******************************************************************************* * ============LICENSE_START==================================================== * * org.onap.aaf * * =========================================================================== * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. * * =========================================================================== * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * * ============LICENSE_END==================================================== * * * * ECOMP is a trademark and service mark of AT&T Intellectual Property. * * ******************************************************************************/ package org.onap.aaf.cadi.dme2; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URI; import javax.servlet.http.HttpServletResponse; import org.onap.aaf.cadi.CadiException; import org.onap.aaf.cadi.SecuritySetter; import org.onap.aaf.cadi.client.EClient; import org.onap.aaf.cadi.client.Future; import org.onap.aaf.cadi.client.Rcli; import com.att.aft.dme2.api.DME2Client; import com.att.aft.dme2.api.DME2Exception; import com.att.aft.dme2.api.DME2Manager; import com.att.aft.dme2.handler.DME2RestfulHandler; import com.att.aft.dme2.handler.DME2RestfulHandler.ResponseInfo; import org.onap.aaf.inno.env.APIException; import org.onap.aaf.inno.env.Data; import org.onap.aaf.rosetta.env.RosettaDF; public class DEClient implements EClient { private DME2Client client; private DME2RestfulHandler replyHandler; private EClient.Transfer payload; private boolean isProxy; private SecuritySetter ss; public DEClient(DME2Manager manager, SecuritySetter ss, URI uri, long timeout) throws DME2Exception, CadiException { client = new DME2Client(manager,uri,timeout); client.setAllowAllHttpReturnCodes(true); this.ss = ss; ss.setSecurity(client); replyHandler = new DME2RestfulHandler(Rcli.BLANK); client.setReplyHandler(replyHandler); } @Override public void setMethod(String meth) { client.setMethod(meth); } /** * DME2 can't handle having QueryParams on the URL line, but it is the most natural way, so... * * 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" */ public void setPathInfo(String pathinfo) { int qp = pathinfo.indexOf('?'); if(qp<0) { client.setContext(isProxy?("/proxy"+pathinfo):pathinfo); } else { client.setContext(isProxy?("/proxy"+pathinfo.substring(0,qp)):pathinfo.substring(0,qp)); client.setQueryParams(pathinfo.substring(qp+1)); } } @Override public void setPayload(EClient.Transfer transfer) { payload = transfer; } @Override public void addHeader(String tag, String value) { client.addHeader(tag, value); } @Override public void setQueryParams(String q) { client.setQueryParams(q); } @Override public void setFragment(String f) { // DME2 does not implement this } @Override public void send() throws APIException { try { if(payload!=null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); payload.transfer(baos); client.setPayload(new String(baos.toByteArray())); } else { client.setPayload(""); } client.send(); } catch (DME2Exception e) { throw new APIException(e); } catch (IOException e) { throw new APIException(e); } } public class DFuture extends Future { protected final DME2RestfulHandler reply; protected ResponseInfo info; public DFuture(DME2RestfulHandler reply) { this.reply = reply; } protected boolean evalInfo() throws APIException{ //return info.getCode()==200; return true; }; public final boolean get(int timeout) throws CadiException { try { info = reply.getResponse(timeout); ss.setLastResponse(info.getCode()); return evalInfo(); } catch (Exception e) { throw new CadiException(e); } } @Override public int code() { return info.getCode(); } @Override public String body() { return info.getBody(); } @Override public String header(String tag) { return info.header(tag); } } @Override public Future futureCreate(Class t) { return new DFuture(replyHandler) { public boolean evalInfo() throws APIException { return info.getCode()==201; } }; } @Override public Future futureReadString() { return new DFuture(replyHandler) { public boolean evalInfo() throws APIException { if(info.getCode()==200) { value = info.getBody(); return true; } return false; } }; } @Override public Future futureRead(final RosettaDF df, final Data.TYPE type) { return new DFuture(replyHandler) { public boolean evalInfo() throws APIException { if(info.getCode()==200) { value = df.newData().in(type).load(info.getBody()).asObject(); return true; } return false; } }; } @Override public Future future(final T t) { return new DFuture(replyHandler) { public boolean evalInfo() { if(info.getCode()==200) { value = t; return true; } return false; } }; } @Override public Future future(HttpServletResponse resp,int expected) throws APIException { // TODO Auto-generated method stub return null; } public void setProxy(boolean isProxy) { this.isProxy=isProxy; } }