edu.cornell.mannlib.ld4lindexing.triplestores.MinimalVirtuosoTripleStore.java Source code

Java tutorial

Introduction

Here is the source code for edu.cornell.mannlib.ld4lindexing.triplestores.MinimalVirtuosoTripleStore.java

Source

/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.ld4lindexing.triplestores;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.lf5.util.StreamUtils;
import org.json.JSONException;
import org.json.JSONObject;

import edu.cornell.mannlib.ld4lindexing.ProcessRunner;
import edu.cornell.mannlib.ld4lindexing.ProcessRunner.ProcessException;
import edu.cornell.mannlib.ld4lindexing.StartupException;

/**
 * TODO
 */
public class MinimalVirtuosoTripleStore implements TripleStore {
    private static final Log log = LogFactory.getLog(MinimalVirtuosoTripleStore.class);

    private final ThreadLocal<HttpClient> httpClientHolder = new ThreadLocal<HttpClient>() {
        @Override
        protected HttpClient initialValue() {
            return new DefaultHttpClient();
        }
    };
    //   private final HttpClient httpClient = new DefaultHttpClient();

    public MinimalVirtuosoTripleStore() throws StartupException {
        try {
            ProcessRunner runner = new ProcessRunner();
            runner.run("pgrep", "virtuoso-t");
            if (runner.getReturnCode() != 0) {
                throw new StartupException("Virtuoso is not running.");
            }
        } catch (ProcessException e) {
            throw new StartupException(e);
        }

    }

    /**
     * <pre>
     *     def sparql_query(sparql, format='application/sparql-results+json', &block)
     *       params = {'query' => sparql}
     *       headers = {'accept' => format}
     *       http_post("http://localhost:#{@http_port}/sparql/", block, params, headers)
     *     end
     * </pre>
     */
    @Override
    public JSONObject sparqlQuery(String query) {
        try {
            HttpPost method = new HttpPost("http://localhost:8890/sparql/");
            method.addHeader("Accept", "application/sparql-results+json");

            method.setEntity(new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair("query", query))));
            HttpResponse response = httpClientHolder.get().execute(method);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode > 399) {
                log.error("response " + statusCode + " to SPARQL query. \n");
            }
            try (InputStream in = response.getEntity().getContent()) {
                String json = new String(StreamUtils.getBytes(in));
                log.debug("SPARQL QUERY RESPONSE: " + json);
                return new JSONObject(json);
            }

        } catch (IllegalStateException | IOException | JSONException e) {
            log.error("Failure in SPARQL query", e);
            throw new RuntimeException(e);
        }
    }

}