|
|
@@ -73,7 +73,7 @@ public class CHttpHandler implements URIHandler {
|
|
|
|
|
|
@Override
|
|
|
public Charset charset(URIHandle ustream) throws IOException {
|
|
|
- return getContentCharset(connect(ustream, "HEAD").getContentType());
|
|
|
+ return getContentCharset(head(ustream).getContentType());
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -81,7 +81,7 @@ public class CHttpHandler implements URIHandler {
|
|
|
public Set<URIFlags> flags(URIHandle ustream) throws IOException {
|
|
|
Set<URIFlags> re = URIFlags.collect(URIFlags.FILE);
|
|
|
try {
|
|
|
- HttpURLConnection c = connect(ustream, "HEAD");
|
|
|
+ HttpURLConnection c = head(ustream);
|
|
|
re.add(URIFlags.READABLE);
|
|
|
if(2 == (c.getResponseCode() / 100)) {
|
|
|
re.add(URIFlags.EXISTS);
|
|
|
@@ -94,6 +94,9 @@ public class CHttpHandler implements URIHandler {
|
|
|
if(c.getContentLengthLong() >= 0) {
|
|
|
re.add(URIFlags.SIZE);
|
|
|
}
|
|
|
+ if(StringTraits.contains(c.getHeaderField("Accept-Ranges"), "bytes")) {
|
|
|
+ re.add(URIFlags.SEEK);
|
|
|
+ }
|
|
|
} catch(IOException cause) {
|
|
|
// do nothing
|
|
|
}
|
|
|
@@ -103,14 +106,18 @@ public class CHttpHandler implements URIHandler {
|
|
|
@Override
|
|
|
public Instant time(URIHandle ustream, URITime ut) throws IOException {
|
|
|
if(ut == URITime.MODIFIED) {
|
|
|
- return new Date(connect(ustream, "HEAD").getHeaderFieldDate("Last-Modified", 0)).toInstant();
|
|
|
+ HttpURLConnection c = head(ustream);
|
|
|
+ if(null == c.getHeaderField("Last-Modified")) {
|
|
|
+ return Instant.ofEpochMilli(c.getDate());
|
|
|
+ }
|
|
|
+ return new Date(c.getHeaderFieldDate("Last-Modified", 0)).toInstant();
|
|
|
}
|
|
|
throw new UnsupportedOperationException(HTTP_NS);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public long size(URIHandle ustream) throws IOException {
|
|
|
- return connect(ustream, "HEAD").getContentLengthLong();
|
|
|
+ return head(ustream).getContentLengthLong();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -129,25 +136,39 @@ public class CHttpHandler implements URIHandler {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public InputStream istream(URIHandle ustream, long offset, long size) throws IOException {
|
|
|
- // @todo (assira #1) CFileHandler: istream(offset)
|
|
|
- throw new UnsupportedOperationException("Not supported yet.");
|
|
|
+ public InputStream istream(URIHandle ustream, long begin, long end) throws IOException {
|
|
|
+ HttpURLConnection c = connect(ustream);
|
|
|
+ c.setRequestMethod("GET");
|
|
|
+ c.setRequestProperty("Range", "bytes=" + begin + "-" + (end));
|
|
|
+ if (c.getResponseCode() != HttpURLConnection.HTTP_PARTIAL) {
|
|
|
+ // @todo (assira #1) CFileHandler: istream(offset)
|
|
|
+ throw new UnsupportedOperationException("HTTP " + c.getResponseCode());
|
|
|
+ } else {
|
|
|
+ return c.getInputStream();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public OutputStream ostream(URIHandle ustream) throws IOException {
|
|
|
- return connect(ustream, "PUT").getOutputStream();
|
|
|
+ HttpURLConnection c = connect(ustream);
|
|
|
+ c.setRequestMethod("PUT");
|
|
|
+ c.connect();
|
|
|
+ return c.getOutputStream();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Reader reader(URIHandle ustream) throws IOException {
|
|
|
- HttpURLConnection c = connect(ustream, "GET");
|
|
|
+ HttpURLConnection c = connect(ustream);
|
|
|
+ c.setRequestMethod("GET");
|
|
|
+ c.connect();
|
|
|
return new InputStreamReader(c.getInputStream(), getContentCharset(c.getContentType()));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Writer writer(URIHandle ustream) throws IOException {
|
|
|
- HttpURLConnection c = connect(ustream, "PUT");
|
|
|
+ HttpURLConnection c = connect(ustream);
|
|
|
+ c.setRequestMethod("PUT");
|
|
|
+ c.connect();
|
|
|
return new OutputStreamWriter(c.getOutputStream(), getContentCharset(c.getContentType()));
|
|
|
}
|
|
|
|
|
|
@@ -175,13 +196,17 @@ public class CHttpHandler implements URIHandler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private HttpURLConnection connect(URIHandle ustream, String method) throws IOException {
|
|
|
+ private HttpURLConnection head(URIHandle ustream) throws IOException {
|
|
|
HttpURLConnection c = (HttpURLConnection)((CHttpHandle)ustream).url.openConnection();
|
|
|
- c.setRequestMethod(method);
|
|
|
+ c.setRequestMethod("HEAD");
|
|
|
c.connect();
|
|
|
return c;
|
|
|
}
|
|
|
|
|
|
+ private HttpURLConnection connect(URIHandle ustream) throws IOException {
|
|
|
+ return (HttpURLConnection)((CHttpHandle)ustream).uri.toURL().openConnection();
|
|
|
+ }
|
|
|
+
|
|
|
private static Charset getContentCharset(String type) {
|
|
|
if(null == type) {
|
|
|
return Charsets.UTF8;
|