|
|
@@ -6,7 +6,6 @@
|
|
|
*/
|
|
|
package net.ranides.assira.io.uri.impl;
|
|
|
|
|
|
-import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.InputStreamReader;
|
|
|
@@ -19,18 +18,15 @@ import java.net.MalformedURLException;
|
|
|
import java.net.URI;
|
|
|
import java.net.URL;
|
|
|
import java.nio.charset.Charset;
|
|
|
-import java.nio.file.Path;
|
|
|
import java.time.Instant;
|
|
|
import java.util.Date;
|
|
|
import java.util.Set;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
-import net.ranides.assira.collection.query.CQuery;
|
|
|
import net.ranides.assira.io.IOStreams;
|
|
|
import net.ranides.assira.io.uri.URITime;
|
|
|
import net.ranides.assira.io.uri.URIFlags;
|
|
|
import net.ranides.assira.io.uri.URIHandle;
|
|
|
-import net.ranides.assira.io.uri.URIHandler;
|
|
|
import net.ranides.assira.text.Charsets;
|
|
|
import net.ranides.assira.text.StringTraits;
|
|
|
|
|
|
@@ -38,7 +34,7 @@ import net.ranides.assira.text.StringTraits;
|
|
|
*
|
|
|
* @author Ranides Atterwim <ranides@gmail.com>
|
|
|
*/
|
|
|
-public class CHttpHandler implements URIHandler {
|
|
|
+public class CHttpHandler extends CHandler {
|
|
|
|
|
|
static final CHttpHandler THAT = new CHttpHandler();
|
|
|
|
|
|
@@ -61,19 +57,9 @@ public class CHttpHandler implements URIHandler {
|
|
|
return ((CHttpHandle)ustream).uri;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public File file(URIHandle ustream) throws IOException {
|
|
|
- throw new IOException(HTTP_NS);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Path path(URIHandle ustream) throws IOException {
|
|
|
- throw new IOException(HTTP_NS);
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public Charset charset(URIHandle ustream) throws IOException {
|
|
|
- return getContentCharset(head(ustream).getContentType());
|
|
|
+ return getContentCharset(connect(ustream, "HEAD").getContentType());
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -81,7 +67,7 @@ public class CHttpHandler implements URIHandler {
|
|
|
public Set<URIFlags> flags(URIHandle ustream) throws IOException {
|
|
|
Set<URIFlags> re = URIFlags.collect(URIFlags.FILE);
|
|
|
try {
|
|
|
- HttpURLConnection c = head(ustream);
|
|
|
+ HttpURLConnection c = connect(ustream, "HEAD");
|
|
|
re.add(URIFlags.READABLE);
|
|
|
if(2 == (c.getResponseCode() / 100)) {
|
|
|
re.add(URIFlags.EXISTS);
|
|
|
@@ -106,28 +92,22 @@ public class CHttpHandler implements URIHandler {
|
|
|
@Override
|
|
|
public Instant time(URIHandle ustream, URITime ut) throws IOException {
|
|
|
if(ut == URITime.MODIFIED) {
|
|
|
- HttpURLConnection c = head(ustream);
|
|
|
+ HttpURLConnection c = connect(ustream, "HEAD");
|
|
|
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);
|
|
|
+ return super.time(ustream, ut);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public long size(URIHandle ustream) throws IOException {
|
|
|
- return head(ustream).getContentLengthLong();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public URIHandle parent(URIHandle ustream) throws IOException {
|
|
|
- throw new UnsupportedOperationException(HTTP_NS);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public CQuery<URIHandle> scan(URIHandle ustream) throws IOException {
|
|
|
- throw new UnsupportedOperationException(HTTP_NS);
|
|
|
+ HttpURLConnection c = connect(ustream, "HEAD");
|
|
|
+ if(2 != (c.getResponseCode() / 100)) {
|
|
|
+ throw new IOException(c.getResponseMessage());
|
|
|
+ }
|
|
|
+ return c.getContentLengthLong();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -137,12 +117,10 @@ public class CHttpHandler implements URIHandler {
|
|
|
|
|
|
@Override
|
|
|
public InputStream istream(URIHandle ustream, long begin, long end) throws IOException {
|
|
|
- HttpURLConnection c = connect(ustream);
|
|
|
- c.setRequestMethod("GET");
|
|
|
+ HttpURLConnection c = connect(ustream, "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());
|
|
|
+ return IOStreams.limit(c.getInputStream(), begin, end);
|
|
|
} else {
|
|
|
return c.getInputStream();
|
|
|
}
|
|
|
@@ -150,59 +128,39 @@ public class CHttpHandler implements URIHandler {
|
|
|
|
|
|
@Override
|
|
|
public OutputStream ostream(URIHandle ustream) throws IOException {
|
|
|
- HttpURLConnection c = connect(ustream);
|
|
|
- c.setRequestMethod("PUT");
|
|
|
+ HttpURLConnection c = connect(ustream, "PUT");
|
|
|
c.connect();
|
|
|
return c.getOutputStream();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Reader reader(URIHandle ustream, Charset cs) throws IOException {
|
|
|
- HttpURLConnection c = connect(ustream);
|
|
|
- c.setRequestMethod("GET");
|
|
|
+ HttpURLConnection c = connect(ustream, "GET");
|
|
|
c.connect();
|
|
|
return new InputStreamReader(c.getInputStream(), cs);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Reader reader(URIHandle ustream) throws IOException {
|
|
|
- HttpURLConnection c = connect(ustream);
|
|
|
- c.setRequestMethod("GET");
|
|
|
+ HttpURLConnection c = connect(ustream, "GET");
|
|
|
c.connect();
|
|
|
return new InputStreamReader(c.getInputStream(), getContentCharset(c.getContentType()));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Writer writer(URIHandle ustream, Charset cs) throws IOException {
|
|
|
- HttpURLConnection c = connect(ustream);
|
|
|
- c.setRequestMethod("PUT");
|
|
|
+ HttpURLConnection c = connect(ustream, "PUT");
|
|
|
c.connect();
|
|
|
return new OutputStreamWriter(c.getOutputStream(), cs);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Writer writer(URIHandle ustream) throws IOException {
|
|
|
- HttpURLConnection c = connect(ustream);
|
|
|
- c.setRequestMethod("PUT");
|
|
|
+ HttpURLConnection c = connect(ustream, "PUT");
|
|
|
c.connect();
|
|
|
return new OutputStreamWriter(c.getOutputStream(), getContentCharset(c.getContentType()));
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void delete(URIHandle ustream) throws IOException {
|
|
|
- throw new UnsupportedOperationException(HTTP_NS);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void create(URIHandle ustream) throws IOException {
|
|
|
- throw new UnsupportedOperationException(HTTP_NS);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void move(URIHandle ustream, URIHandle location) throws IOException {
|
|
|
- throw new UnsupportedOperationException(HTTP_NS);
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public void copy(URIHandle ustream, URIHandle location) throws IOException {
|
|
|
try(InputStream istream = ustream.istream()) {
|
|
|
@@ -212,17 +170,12 @@ public class CHttpHandler implements URIHandler {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private HttpURLConnection head(URIHandle ustream) throws IOException {
|
|
|
- HttpURLConnection c = (HttpURLConnection)((CHttpHandle)ustream).url.openConnection();
|
|
|
- c.setRequestMethod("HEAD");
|
|
|
- c.connect();
|
|
|
+ private HttpURLConnection connect(URIHandle ustream, String method) throws IOException {
|
|
|
+ HttpURLConnection c = (HttpURLConnection)((CHttpHandle)ustream).uri.toURL().openConnection();
|
|
|
+ c.setRequestMethod(method);
|
|
|
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;
|