Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a method to call a remote SPARQL end point, but without having to use Jena. #66

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions features/net.bioclipse.core_feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ The Eclipse Public License accompanies this distribution, and is available at ht
<import plugin="org.eclipse.equinox.p2.metadata"/>
<import plugin="org.eclipse.equinox.p2.operations"/>
<import plugin="org.openscience.cdk_bundle"/>
<import plugin="org.apache.httpcomponents.httpclient"/>
<import plugin="org.apache.httpcomponents.httpcore"/>
</requires>

<plugin
Expand Down
4 changes: 3 additions & 1 deletion plugins/net.bioclipse.business/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Require-Bundle: org.eclipse.ui,
net.bioclipse.core,
net.bioclipse.ui,
ch.qos.logback.core;bundle-version="[1.0.0,2.0.0)",
ch.qos.logback.classic;bundle-version="[1.0.0,2.0.0)"
ch.qos.logback.classic;bundle-version="[1.0.0,2.0.0)",
org.apache.httpcomponents.httpclient,
org.apache.httpcomponents.httpcore
Bundle-ActivationPolicy: lazy
Import-Package: org.apache.log4j,
org.slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
package net.bioclipse.business;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
Expand All @@ -25,12 +27,18 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.bioclipse.core.business.BioclipseException;
import net.bioclipse.managers.business.IBioclipseManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
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.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
Expand All @@ -42,6 +50,8 @@
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.FileAppender;
import net.bioclipse.core.business.BioclipseException;
import net.bioclipse.managers.business.IBioclipseManager;

public class BioclipsePlatformManager implements IBioclipseManager {

Expand Down Expand Up @@ -171,6 +181,51 @@ public IFile downloadAsFile(String url, String mimeType, IFile target,
return target;
}

public byte[] sparqlRemote(
String serviceURL,
String sparqlQueryString, IProgressMonitor monitor)
throws BioclipseException {
if (monitor == null)
monitor = new NullProgressMonitor();

monitor.beginTask("Sparqling the remote service..", 100);

// use Apache for doing the SPARQL query
DefaultHttpClient httpclient = new DefaultHttpClient();

// Set credentials on the client
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("query", sparqlQueryString));
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost(serviceURL);
httppost.setEntity(entity);
monitor.worked(20);
HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode != 200) throw new BioclipseException(
"Expected HTTP 200, but got a " + statusCode + ": " + statusLine.getReasonPhrase()
);
monitor.worked(60);

HttpEntity responseEntity = response.getEntity();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
responseEntity.writeTo(buffer);
buffer.flush();
monitor.worked(20);
return buffer.toByteArray();
} catch (UnsupportedEncodingException exception) {
throw new BioclipseException(
"Error while creating the SPARQL query: " + exception.getMessage(), exception
);
} catch (IOException exception) {
throw new BioclipseException(
"Error while processing the SPARQL endpoint feedback: " + exception.getMessage(), exception
);
}
}


public void openURL(URL url) throws BioclipseException {
IWorkbenchBrowserSupport browserSupport =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* *****************************************************************************
* Copyright (c) 2009 Egon Willighagen <[email protected]>
/* Copyright (c) 2009,2018 Egon Willighagen <[email protected]>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contact: http://www.bioclipse.net/
******************************************************************************/
*/
package net.bioclipse.business;

import org.eclipse.core.resources.IFile;
Expand Down Expand Up @@ -106,4 +105,11 @@ public void requireVersion( String lowerVersionBound,
public String fullPath( String file );

public String fullPath( IFile file );

@PublishedMethod(
params = "String url, String SPARQL",
methodSummary = "Queries a remote SPARQL end point without parsing the SPARQL first."
)
public byte[] sparqlRemote(String url, String SPARQL)
throws BioclipseException;
}