- 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(),
- 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)) {
- return true;
- } else {
- extractError();
- return false;
- }
- } catch (IOException | APIException e) {
- throw new CadiException(e);
- } finally {
- close();
- }
- }
-
- private void extractError() {
- InputStream is = huc.getErrorStream();
- try {
- if(is==null) {
- is = huc.getInputStream();
- }
- if(is!=null) {
- errContent = new StringBuilder();
- int c;
- while((c=is.read())>=0) {
- errContent.append((char)c);
- }
- }
- } catch (IOException e) {
- exception = e;
- }
- }
-
- // Typically only used by Read
- public StringBuilder inputStreamToString(InputStream is) {
- // Avoids Carriage returns, and is reasonably efficient, given
- // the buffer reads.
- try {
- StringBuilder sb = new StringBuilder();
- Reader rdr = new InputStreamReader(is);
- try {
- char[] buf = new char[256];
- int read;
- while ((read = rdr.read(buf)) >= 0) {
- sb.append(buf, 0, read);
- }
- } finally {
- rdr.close();
- }
- return sb;
- } catch (IOException e) {
- exception = e;
- 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();
- }
- }
- }
+ public int timeout() {
+ return connectTimeout;
+ }
+
+ protected HttpURLConnection getConnection(URI uri, StringBuilder pi) throws IOException, URISyntaxException {
+ URL url = new URI(
+ 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)) {
+ return true;
+ } else {
+ extractError();
+ return false;
+ }
+ } catch (IOException | APIException e) {
+ throw new CadiException(e);
+ } finally {
+ close();
+ }
+ }
+
+ private void extractError() {
+ InputStream is = huc.getErrorStream();
+ try {
+ if (is==null) {
+ is = huc.getInputStream();
+ }
+ if (is!=null) {
+ errContent = new StringBuilder();
+ int c;
+ while ((c=is.read())>=0) {
+ errContent.append((char)c);
+ }
+ }
+ } catch (IOException e) {
+ exception = e;
+ }
+ }
+
+ // Typically only used by Read
+ public StringBuilder inputStreamToString(InputStream is) {
+ // Avoids Carriage returns, and is reasonably efficient, given
+ // the buffer reads.
+ try {
+ StringBuilder sb = new StringBuilder();
+ Reader rdr = new InputStreamReader(is);
+ try {
+ char[] buf = new char[256];
+ int read;
+ while ((read = rdr.read(buf)) >= 0) {
+ sb.append(buf, 0, read);
+ }
+ } finally {
+ rdr.close();
+ }
+ return sb;
+ } catch (IOException e) {
+ exception = e;
+ 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();
+ }
+ }
+ }