Skip to content

Commit

Permalink
Add http:get function
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharywhitley committed Mar 26, 2021
1 parent 71ef6f8 commit 1565276
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

<properties>
<stardog.major.version>7</stardog.major.version>
<stardog.version>${stardog.major.version}.5.0</stardog.version>
<stardog.version>${stardog.major.version}.6.0</stardog.version>
<junit.version>4.12</junit.version>
<slf4j.version>1.7.21</slf4j.version>
<jansi.version>1.16</jansi.version>
Expand All @@ -63,6 +63,11 @@
</properties>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>failureaccess</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.complexible.stardog</groupId>
<artifactId>server</artifactId>
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/com/semantalytics/stardog/kibble/http/Get.java
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);
}
}
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 + "> ";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,5 @@ com.semantalytics.stardog.kibble.lang.ScoreFromAllWithArabicScript
com.semantalytics.stardog.kibble.lang.ScoreFromAllWithCyrillicScript
com.semantalytics.stardog.kibble.lang.ScoreFromAllWithDevanagariScript
com.semantalytics.stardog.kibble.lang.ScoreFromAllWithLatinScript



com.semantalytics.stardog.kibble.http.Get

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 src/test/java/com/semantalytics/stardog/kibble/http/TestGet.java
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");
}
}
}

0 comments on commit 1565276

Please sign in to comment.