* 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.
import java.net.URL;
import java.util.ArrayList;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLHandshakeException;
import javax.servlet.http.HttpServletResponse;
import org.onap.aaf.cadi.CadiException;
/**
* Low Level Http Client Mechanism. Chances are, you want the high level "HRcli"
* for Rosetta Object Translation
- *
+ *
* @author Jonathan
*
*/
this.uri = uri;
this.ss = ss;
this.connectTimeout = connectTimeout;
- pathinfo = query = fragment = null;
+ pathinfo = query = fragment = null;
}
@Override
public void setPayload(Transfer transfer) {
this.transfer = transfer;
}
-
+
@Override
public void addHeader(String tag, String value) {
if (headers == null)
}
pi.append(pathinfo);
}
- URI sendURI = null;
+ URI sendURI = null;
try {
sendURI = new URI(
uri.getScheme(),
huc = getConnection(sendURI, pi);
huc.setRequestMethod(meth);
if (ss!=null) {
- ss.setSecurity(huc);
+ ss.setSecurity(huc);
}
if (headers != null)
- for (Header d : headers) {
+ for (Header d : headers) {
huc.addRequestProperty(d.tag, d.value);
}
huc.setDoInput(true);
}
// TODO other settings? There's a bunch here.
} catch (APIException e) {
- throw e;
+ throw e;
} catch (Exception e) {
- if(sendURI==null) {
- throw new APIException("Cannot connect to Root URI: '" + uri.toString() + '\'',e);
- } else {
- throw new APIException("Cannot connect to '" + sendURI.toString() + "' (Root URI: '" + uri.toString() + "')",e);
- }
+ if(sendURI==null) {
+ throw new APIException("Cannot connect to Root URI: '" + uri.toString() + '\'',e);
+ } else {
+ throw new APIException("Cannot connect to '" + sendURI.toString() + "' (Root URI: '" + uri.toString() + "')",e);
+ }
} finally { // ensure all these are reset after sends
meth=pathinfo=null;
if (headers!=null) {
pathinfo = query = fragment = "";
}
}
-
+
public URI getURI() {
return uri;
}
+ public void setURI(URI uri) {
+ this.uri = uri;
+ }
+
public int timeout() {
return connectTimeout;
}
-
+
protected HttpURLConnection getConnection(URI uri, StringBuilder pi) throws IOException, URISyntaxException {
URL url = new URI(
- uri.getScheme(),
+ uri.getScheme(),
uri.getAuthority(),
- pi==null?uri.getPath():pi.toString(),
+ pi==null?uri.getPath():pi.toString(),
query,
fragment).toURL();
return (HttpURLConnection) url.openConnection();
}
-
+
public abstract class HFuture<T> extends Future<T> {
protected HttpURLConnection huc;
protected int respCode;
protected IOException exception;
protected StringBuilder errContent;
-
+
public HFuture(final HttpURLConnection huc) {
this.huc = huc;
}
-
+
protected boolean evalInfo(HttpURLConnection huc) throws APIException, IOException{
return respCode == 200;
};
-
+
@Override
public final boolean get(int timeout) throws CadiException {
try {
close();
}
}
-
+
private void extractError() {
InputStream is = huc.getErrorStream();
try {
exception = e;
}
}
-
+
// Typically only used by Read
public StringBuilder inputStreamToString(InputStream is) {
// Avoids Carriage returns, and is reasonably efficient, given
return null;
}
}
-
-
+
+
@Override
public int code() {
return respCode;
}
-
+
public HttpURLConnection huc() {
return huc;
}
-
+
public IOException exception() {
return exception;
}
-
+
@Override
public String header(String tag) {
return huc.getHeaderField(tag);
}
-
+
public void close() {
if (huc!=null) {
huc.disconnect();
is = huc.getInputStream();
// reuse Buffers
Pooled<byte[]> pbuff = Rcli.buffPool.get();
- try {
+ try {
while ((read=is.read(pbuff.content))>=0) {
os.write(pbuff.content,0,read);
}
if (is!=null) {
errContent = new StringBuilder();
Pooled<byte[]> pbuff = Rcli.buffPool.get();
- try {
+ try {
while ((read=is.read(pbuff.content))>=0) {
os.write(pbuff.content,0,read);
}
this.tag = t;
this.value = v;
}
-
+
public String toString() {
return tag + '=' + value;
}
}
-
+
public String toString() {
return "HttpURLConnection Client configured to " + uri.toString();
}