* 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.
/**
* 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)
@Override
public void send() throws APIException {
- try {
- // Build URL from given URI plus current Settings
- if(uri.getPath()==null) {
- throw new APIException("Invalid URL entered for HClient");
- }
- StringBuilder pi=null;
- if(pathinfo!=null) { // additional pathinfo
- pi = new StringBuilder(uri.getPath());
- if(!pathinfo.startsWith("/")) {
- pi.append('/');
- }
- pi.append(pathinfo);
+ // Build URL from given URI plus current Settings
+ if (uri.getPath()==null) {
+ throw new APIException("Invalid URL entered for HClient");
+ }
+ StringBuilder pi=null;
+ if (pathinfo!=null) { // additional pathinfo
+ pi = new StringBuilder(uri.getPath());
+ if (!pathinfo.startsWith("/")) {
+ pi.append('/');
}
- URI sendURI = new URI(
+ pi.append(pathinfo);
+ }
+ URI sendURI = null;
+ try {
+ sendURI = new URI(
uri.getScheme(),
- uri.getUserInfo(),
- uri.getHost(),
- uri.getPort(),
+ uri.getAuthority(),
pi==null?uri.getPath():pi.toString(),
query==null?uri.getQuery():query,
fragment==null?uri.getFragment():fragment
);
huc = getConnection(sendURI, pi);
huc.setRequestMethod(meth);
- if(ss!=null) {
- ss.setSecurity(huc);
+ if (ss!=null) {
+ ss.setSecurity(huc);
}
if (headers != null)
- for (Header d : headers) {
+ for (Header d : headers) {
huc.addRequestProperty(d.tag, d.value);
}
huc.setDoInput(true);
transfer.transfer(huc.getOutputStream());
}
// TODO other settings? There's a bunch here.
+ } catch (APIException e) {
+ throw e;
} catch (Exception e) {
- throw new APIException(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) {
+ if (headers!=null) {
headers.clear();
}
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.getUserInfo(),
- uri.getHost(),
- uri.getPort(),
- pi==null?uri.getPath():pi.toString(),
+ uri.getScheme(),
+ uri.getAuthority(),
+ 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 {
huc.setReadTimeout(timeout);
respCode = huc.getResponseCode();
ss.setLastResponse(respCode);
- if(evalInfo(huc)) {
+ if (evalInfo(huc)) {
return true;
} else {
extractError();
close();
}
}
-
+
private void extractError() {
InputStream is = huc.getErrorStream();
try {
- if(is==null) {
+ if (is==null) {
is = huc.getInputStream();
}
- if(is!=null) {
+ if (is!=null) {
errContent = new StringBuilder();
int c;
- while((c=is.read())>=0) {
+ while ((c=is.read())>=0) {
errContent.append((char)c);
}
}
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) {
+ if (huc!=null) {
huc.disconnect();
}
}
int read;
InputStream is;
OutputStream os = resp.getOutputStream();
- if(respCode==expected) {
+ if (respCode==expected) {
is = huc.getInputStream();
// reuse Buffers
Pooled<byte[]> pbuff = Rcli.buffPool.get();
- try {
- while((read=is.read(pbuff.content))>=0) {
+ try {
+ while ((read=is.read(pbuff.content))>=0) {
os.write(pbuff.content,0,read);
}
} finally {
return true;
} else {
is = huc.getErrorStream();
- if(is==null) {
+ if (is==null) {
is = huc.getInputStream();
}
- if(is!=null) {
+ if (is!=null) {
errContent = new StringBuilder();
Pooled<byte[]> pbuff = Rcli.buffPool.get();
- try {
- while((read=is.read(pbuff.content))>=0) {
+ try {
+ while ((read=is.read(pbuff.content))>=0) {
os.write(pbuff.content,0,read);
}
} finally {
this.tag = t;
this.value = v;
}
-
+
public String toString() {
return tag + '=' + value;
}
}
-
+
public String toString() {
return "HttpURLConnection Client configured to " + uri.toString();
}