-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71ef6f8
commit 1565276
Showing
6 changed files
with
212 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/com/semantalytics/stardog/kibble/http/Get.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.semantalytics.stardog.kibble.http; | ||
|
||
import com.complexible.stardog.plan.filter.ExpressionVisitor; | ||
import com.complexible.stardog.plan.filter.expr.ValueOrError; | ||
import com.complexible.stardog.plan.filter.functions.AbstractFunction; | ||
import com.complexible.stardog.plan.filter.functions.UserDefinedFunction; | ||
import com.stardog.stark.Datatype; | ||
import com.stardog.stark.Value; | ||
import com.stardog.stark.Values; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.util.Base64; | ||
|
||
public class Get extends AbstractFunction implements UserDefinedFunction { | ||
|
||
public Get() { | ||
super(1, HttpVocabulary.get.toString()); | ||
} | ||
|
||
public Get(final Get get) { | ||
super(get); | ||
} | ||
|
||
@Override | ||
protected ValueOrError internalEvaluate(final Value... values) { | ||
|
||
final ByteArrayOutputStream baos; | ||
if (assertIRI(values[0])) { | ||
try { | ||
final URLConnection conn; | ||
conn = new URL(values[0].toString()).openConnection(); | ||
conn.setConnectTimeout(5000); | ||
conn.setReadTimeout(5000); | ||
conn.connect(); | ||
baos = new ByteArrayOutputStream(); | ||
IOUtils.copy(conn.getInputStream(), baos); | ||
return ValueOrError.General.of(Values.literal(Base64.getEncoder().encodeToString(baos.toByteArray()), Datatype.BASE64BINARY)); | ||
} catch (IOException e) { | ||
return ValueOrError.Error; | ||
} | ||
} else { | ||
return ValueOrError.Error; | ||
} | ||
} | ||
|
||
@Override | ||
public Get copy() { | ||
return new Get(this); | ||
} | ||
|
||
@Override | ||
public void accept(final ExpressionVisitor expressionVisitor) { | ||
expressionVisitor.visit(this); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/semantalytics/stardog/kibble/http/HttpVocabulary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.semantalytics.stardog.kibble.http; | ||
|
||
import com.stardog.stark.IRI; | ||
|
||
import static com.stardog.stark.Values.iri; | ||
|
||
public class HttpVocabulary { | ||
|
||
public static final String NS = "http://semantalytics.com/2017/09/ns/stardog/kibble/http/"; | ||
|
||
public static final IRI get = iri(NS + "get"); | ||
|
||
public static String sparqlPrefix(String prefixName) { | ||
return "PREFIX " + prefixName + ": <" + NS + "> "; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/test/java/com/semantalytics/stardog/kibble/http/HttpTestSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.semantalytics.stardog.kibble.http; | ||
|
||
import com.complexible.common.protocols.server.Server; | ||
import com.complexible.common.protocols.server.ServerException; | ||
import com.complexible.stardog.Stardog; | ||
import com.complexible.stardog.StardogConfiguration; | ||
import com.complexible.stardog.StardogException; | ||
import com.complexible.stardog.api.Connection; | ||
import com.complexible.stardog.api.admin.AdminConnection; | ||
import com.complexible.stardog.api.admin.AdminConnectionConfiguration; | ||
import com.google.common.io.Files; | ||
import junit.framework.TestCase; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
import org.junit.runners.Suite.SuiteClasses; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
|
||
@RunWith(Suite.class) | ||
@SuiteClasses({ | ||
TestGet.class, | ||
}) | ||
|
||
public class HttpTestSuite extends TestCase { | ||
|
||
private static Stardog STARDOG; | ||
private static Server SERVER; | ||
public static final String DB = "test"; | ||
public static final int TEST_PORT = 5888; | ||
protected Connection connection; | ||
private static final String STARDOG_LICENSE_PATH = System.getenv("STARDOG_LICENSE_PATH"); | ||
|
||
@BeforeClass | ||
public static void beforeClass() throws IOException, ServerException { | ||
|
||
try{ | ||
AdminConnectionConfiguration.toEmbeddedServer() | ||
.credentials("admin", "admin") | ||
.connect(); | ||
} catch(StardogException e) { | ||
|
||
|
||
final File TEST_HOME; | ||
|
||
TEST_HOME = Files.createTempDir(); | ||
TEST_HOME.deleteOnExit(); | ||
|
||
STARDOG = Stardog.builder() | ||
.set(StardogConfiguration.LICENSE_LOCATION, STARDOG_LICENSE_PATH) | ||
.home(TEST_HOME).create(); | ||
|
||
SERVER = STARDOG.newServer() | ||
.bind(new InetSocketAddress("localhost", TEST_PORT)) | ||
.start(); | ||
|
||
final AdminConnection adminConnection = AdminConnectionConfiguration.toEmbeddedServer() | ||
.credentials("admin", "admin") | ||
.connect(); | ||
|
||
if (adminConnection.list().contains(DB)) { | ||
adminConnection.drop(DB); | ||
} | ||
|
||
adminConnection.newDatabase(DB).create(); | ||
} | ||
} | ||
|
||
@AfterClass | ||
public static void afterClass() { | ||
if (SERVER != null) { | ||
SERVER.stop(); | ||
} | ||
if (STARDOG != null) { | ||
STARDOG.shutdown(); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/com/semantalytics/stardog/kibble/http/TestGet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.semantalytics.stardog.kibble.http; | ||
|
||
import com.semantalytics.stardog.kibble.AbstractStardogTest; | ||
import com.semantalytics.stardog.kibble.array.ArrayVocabulary; | ||
import com.stardog.stark.Literal; | ||
import com.stardog.stark.query.BindingSet; | ||
import com.stardog.stark.query.SelectQueryResult; | ||
import org.apache.commons.collections4.IteratorUtils; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
|
||
public class TestGet extends AbstractStardogTest { | ||
|
||
@Test | ||
public void testTwoArg() { | ||
|
||
final String aQuery = HttpVocabulary.sparqlPrefix("http") + | ||
"select ?result where { unnest(http:get(<http://google.com>) as ?result) }"; | ||
|
||
try(final SelectQueryResult aResult = connection.select(aQuery).execute()) { | ||
|
||
assertThat(aResult).hasNext().withFailMessage("Should have a result"); | ||
List<Integer> results = IteratorUtils.toList(aResult).stream().map(b -> b.get("result")).filter(Literal.class::isInstance).map(Literal.class::cast).map(Literal::intValue).collect(toList()); | ||
assertThat(results).containsExactly(1, 2, 5); | ||
} | ||
} | ||
|
||
@Test | ||
public void testWrongTypeFirstArg() { | ||
|
||
final String aQuery = ArrayVocabulary.sparqlPrefix("array") + | ||
"select ?result where { unnest(array:append(1, 5) as ?result) }"; | ||
|
||
try(final SelectQueryResult aResult = connection.select(aQuery).execute()) { | ||
|
||
assertThat(aResult).hasNext().withFailMessage("Should have a result"); | ||
|
||
final BindingSet aBindingSet = aResult.next(); | ||
|
||
assertThat(aBindingSet).isEmpty(); | ||
assertThat(aResult).isExhausted().withFailMessage("Should have no more results"); | ||
} | ||
} | ||
} |