Skip to content

Commit c16ea20

Browse files
gab1onectrueden
authored andcommitted
Initial implementation of new HTTPHandle/Location
Uses okhttp3 for the heavy lifting. The StreamHandle interfaces will move to SciJava Common once they are mature enough. Signed-off-by: Curtis Rueden <[email protected]>
0 parents  commit c16ea20

13 files changed

+1500
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/target/
2+
/.settings/
3+
.classpath
4+
.project
5+
resources/testfile

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SciJava I/O: HTTP
2+
3+
Adds support for HTTP locations and handles to SciJava.
4+
5+
Based on [square/okhttp](https://github.com/square/okhttp)

pom.xml

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.scijava</groupId>
8+
<artifactId>pom-scijava</artifactId>
9+
<version>16.2.0</version>
10+
<relativePath />
11+
</parent>
12+
13+
<artifactId>scijava-io-http</artifactId>
14+
<version>0.1.0-SNAPSHOT</version>
15+
16+
<name>SciJava I/O: HTTP</name>
17+
<description>Handles for remote HTTP(S)</description>
18+
<url>https://github.com/scijava/scijava-handles-http</url>
19+
<inceptionYear>2017</inceptionYear>
20+
<organization>
21+
<name>SciJava</name>
22+
<url>http://www.scijava.org/</url>
23+
</organization>
24+
<licenses>
25+
<license>
26+
<name>Simplified BSD License</name>
27+
<distribution>repo</distribution>
28+
</license>
29+
</licenses>
30+
31+
<developers>
32+
<developer>
33+
<id>gab1one</id>
34+
<name>Gabriel Einsdorf</name>
35+
<url>http://imagej.net/User:gab1one</url>
36+
<roles>
37+
<role>founder</role>
38+
<role>lead</role>
39+
<role>developer</role>
40+
<role>debugger</role>
41+
<role>reviewer</role>
42+
<role>support</role>
43+
<role>maintainer</role>
44+
</roles>
45+
</developer>
46+
<developer>
47+
<id>ctrueden</id>
48+
<name>Curtis Rueden</name>
49+
<url>https://imagej.net/User:Rueden</url>
50+
<roles>
51+
<role>founder</role>
52+
<role>lead</role>
53+
<role>developer</role>
54+
<role>debugger</role>
55+
<role>reviewer</role>
56+
<role>support</role>
57+
<role>maintainer</role>
58+
</roles>
59+
</developer>
60+
</developers>
61+
<contributors>
62+
<!--
63+
NB: Need at least one element to override the parent.
64+
See: https://issues.apache.org/jira/browse/MNG-5220
65+
-->
66+
<contributor>
67+
<name>None</name>
68+
</contributor>
69+
</contributors>
70+
71+
<mailingLists>
72+
<mailingList>
73+
<name>SciJava</name>
74+
<subscribe>https://groups.google.com/group/scijava</subscribe>
75+
<unsubscribe>https://groups.google.com/group/scijava</unsubscribe>
76+
<post>[email protected]</post>
77+
<archive>https://groups.google.com/group/scijava</archive>
78+
</mailingList>
79+
</mailingLists>
80+
81+
<scm>
82+
<connection>scm:git:git://github.com/scijava/scijava-io-http</connection>
83+
<developerConnection>scm:git:[email protected]:scijava/scijava-io-http</developerConnection>
84+
<tag>HEAD</tag>
85+
<url>https://github.com/scijava/scijava-io-http</url>
86+
</scm>
87+
<issueManagement>
88+
<system>GitHub Issues</system>
89+
<url>http://github.com/scijava/scijava-io-http/issues</url>
90+
</issueManagement>
91+
<ciManagement>
92+
<system>Travis CI</system>
93+
<url>https://travis-ci.org/scijava/scijava-io-http</url>
94+
</ciManagement>
95+
96+
<properties>
97+
<package-name>org.scijava.io.http</package-name>
98+
<license.licenseName>bsd_2</license.licenseName>
99+
<license.copyrightOwners>University of Konstanz and Board of Regents
100+
of the University of Wisconsin-Madison.</license.copyrightOwners>
101+
102+
<scijava-common.version>2.65.0</scijava-common.version>
103+
<okhttp.version>3.6.0</okhttp.version>
104+
</properties>
105+
106+
<repositories>
107+
<repository>
108+
<id>imagej.public</id>
109+
<url>http://maven.imagej.net/content/groups/public</url>
110+
</repository>
111+
</repositories>
112+
113+
<dependencies>
114+
<!-- SciJava dependencies -->
115+
<dependency>
116+
<groupId>org.scijava</groupId>
117+
<artifactId>scijava-common</artifactId>
118+
</dependency>
119+
120+
121+
<!-- Third-party dependencies -->
122+
<dependency>
123+
<groupId>com.squareup.okhttp3</groupId>
124+
<artifactId>okhttp</artifactId>
125+
<version>${okhttp.version}</version>
126+
</dependency>
127+
128+
<!-- Test scope dependencies -->
129+
<dependency>
130+
<groupId>junit</groupId>
131+
<artifactId>junit</artifactId>
132+
<scope>test</scope>
133+
</dependency>
134+
<dependency>
135+
<groupId>org.scijava</groupId>
136+
<artifactId>scijava-common</artifactId>
137+
<classifier>tests</classifier>
138+
<scope>test</scope>
139+
</dependency>
140+
<dependency>
141+
<groupId>org.eclipse.jetty</groupId>
142+
<artifactId>jetty-server</artifactId>
143+
<scope>test</scope>
144+
</dependency>
145+
<dependency>
146+
<groupId>org.eclipse.jetty</groupId>
147+
<artifactId>jetty-servlet</artifactId>
148+
<scope>test</scope>
149+
</dependency>
150+
</dependencies>
151+
</project>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io.handle;
33+
34+
import java.io.IOException;
35+
36+
import org.scijava.io.location.Location;
37+
38+
public abstract class AbstractSeekableStreamHandle<L extends Location> extends
39+
AbstractStreamHandle<L> implements SeekableStreamHandle<L>
40+
{
41+
42+
private long jumpCuttoff = 10000;
43+
44+
@Override
45+
public void seek(final long pos) throws IOException {
46+
47+
// how much and which direction we have to jump
48+
final long delta = pos - offset();
49+
50+
if (delta == 0) {
51+
return;
52+
// nothing to do
53+
}
54+
else if (delta > 0) {
55+
// offset position is "downstream"
56+
57+
// try to reconnect instead of linearly reading large chunks
58+
if (recreatePossible() && delta > jumpCuttoff) {
59+
recreateStreamFromPos(pos);
60+
}
61+
else {
62+
jump(delta);
63+
}
64+
65+
}
66+
else { // delta < 0
67+
// need to recreate the stream
68+
if (recreatePossible()) {
69+
recreateStreamFromPos(pos);
70+
}
71+
else {
72+
resetStream();
73+
jump(pos);
74+
}
75+
}
76+
setOffset(pos);
77+
}
78+
79+
/**
80+
* Recreates the internal input stream available through {@link #in()}, so
81+
* that it starts from the specified position.
82+
*
83+
* @param pos
84+
* @throws IOException
85+
*/
86+
protected abstract void recreateStreamFromPos(long pos) throws IOException;
87+
88+
/**
89+
* In some implementations of this class, the ability to recreate the stream
90+
* depends on external factors (e.g. server support). This influences a
91+
*
92+
* @return if recreate is actually possible.
93+
* @throws IOException
94+
*/
95+
protected abstract boolean recreatePossible() throws IOException;
96+
97+
/**
98+
* Sets the jumpCutoff, the maximum of bytes which are read from the stream
99+
* when seeking forward, any larger number will result in a call to
100+
* {@link #recreateStreamFromPos(long)}.
101+
*/
102+
protected void setJumpCuttoff(long jumpCuttoff) {
103+
this.jumpCuttoff = jumpCuttoff;
104+
}
105+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io.handle;
33+
34+
import org.scijava.io.location.Location;
35+
36+
/**
37+
* Abstract base class for {@link StreamHandle} implementations.
38+
*
39+
* @author Curtis Rueden
40+
* @author Melissa Linkert
41+
*/
42+
public abstract class AbstractStreamHandle<L extends Location> extends
43+
AbstractDataHandle<L> implements StreamHandle<L>
44+
{
45+
46+
// -- Fields --
47+
48+
/** Current position within the stream(s). */
49+
private long offset;
50+
51+
// -- StreamHandle methods --
52+
53+
@Override
54+
public void setOffset(final long offset) {
55+
this.offset = offset;
56+
}
57+
58+
// -- DataHandle methods --
59+
60+
@Override
61+
public long offset() {
62+
return offset;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)