Repository: marmotta Updated Branches: refs/heads/develop 85e5ff3f5 -> af18d1652
added context to ImportClient.uploadDataset(), which overwrites the configuration Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/af18d165 Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/af18d165 Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/af18d165 Branch: refs/heads/develop Commit: af18d16526b2ac84d839233ea0f3baa5a13147b6 Parents: 85e5ff3 Author: Sergio Fernández <[email protected]> Authored: Mon Oct 27 09:19:35 2014 +0100 Committer: Sergio Fernández <[email protected]> Committed: Mon Oct 27 09:19:48 2014 +0100 ---------------------------------------------------------------------- .../marmotta/client/clients/ImportClient.java | 39 +++++++++++++-- .../apache/marmotta/client/util/HTTPUtil.java | 52 ++++++++++---------- 2 files changed, 60 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/marmotta/blob/af18d165/client/marmotta-client-java/src/main/java/org/apache/marmotta/client/clients/ImportClient.java ---------------------------------------------------------------------- diff --git a/client/marmotta-client-java/src/main/java/org/apache/marmotta/client/clients/ImportClient.java b/client/marmotta-client-java/src/main/java/org/apache/marmotta/client/clients/ImportClient.java index 0d5a62d..5dc8fd6 100644 --- a/client/marmotta-client-java/src/main/java/org/apache/marmotta/client/clients/ImportClient.java +++ b/client/marmotta-client-java/src/main/java/org/apache/marmotta/client/clients/ImportClient.java @@ -112,17 +112,33 @@ public class ImportClient { * * @param in InputStream to read the dataset from; will be consumed by this method * @param mimeType mime type of the input data + * @param context named graph to import in * @throws IOException * @throws MarmottaClientException */ - public void uploadDataset(final InputStream in, final String mimeType) throws IOException, MarmottaClientException { - //Preconditions.checkArgument(acceptableTypes.contains(mimeType)); + public void uploadDataset(final InputStream in, final String mimeType, final String context) throws IOException, MarmottaClientException { + HttpClient httpClient = HTTPUtil.createClient(config, context); + uploadDataset(in, mimeType, httpClient); + } + /** + * Upload/Import a dataset in the Marmotta Server. The dataset is given as an Inputstream that contains data of the + * mime type passed as argument. The mime type must be one of the acceptable types of the server. + * + * @param in InputStream to read the dataset from; will be consumed by this method + * @param mimeType mime type of the input data + * @throws IOException + * @throws MarmottaClientException + */ + public void uploadDataset(final InputStream in, final String mimeType) throws IOException, MarmottaClientException { HttpClient httpClient = HTTPUtil.createClient(config); + uploadDataset(in, mimeType, httpClient); + } + private void uploadDataset(final InputStream in, final String mimeType, HttpClient httpClient) throws IOException { HttpPost post = HTTPUtil.createPost(URL_UPLOAD_SERVICE, config); post.setHeader("Content-Type", mimeType); - + ContentProducer cp = new ContentProducer() { @Override public void writeTo(OutputStream outstream) throws IOException { @@ -130,7 +146,7 @@ public class ImportClient { } }; post.setEntity(new EntityTemplate(cp)); - + ResponseHandler<Boolean> handler = new ResponseHandler<Boolean>() { @Override public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException { @@ -157,7 +173,6 @@ public class ImportClient { } finally { post.releaseConnection(); } - } /** @@ -173,4 +188,18 @@ public class ImportClient { uploadDataset(new ByteArrayInputStream(data.getBytes("utf-8")), mimeType); } + /** + * Upload the data contained in the string using the given mime type; convenience method wrapping the generic + * InputStream-based method. + * + * @param data + * @param mimeType + * @param context + * @throws IOException + * @throws MarmottaClientException + */ + public void uploadDataset(String data, String mimeType, String context) throws IOException, MarmottaClientException { + uploadDataset(new ByteArrayInputStream(data.getBytes("utf-8")), mimeType, context); + } + } http://git-wip-us.apache.org/repos/asf/marmotta/blob/af18d165/client/marmotta-client-java/src/main/java/org/apache/marmotta/client/util/HTTPUtil.java ---------------------------------------------------------------------- diff --git a/client/marmotta-client-java/src/main/java/org/apache/marmotta/client/util/HTTPUtil.java b/client/marmotta-client-java/src/main/java/org/apache/marmotta/client/util/HTTPUtil.java index 39192d8..5f97947 100644 --- a/client/marmotta-client-java/src/main/java/org/apache/marmotta/client/util/HTTPUtil.java +++ b/client/marmotta-client-java/src/main/java/org/apache/marmotta/client/util/HTTPUtil.java @@ -17,14 +17,8 @@ */ package org.apache.marmotta.client.util; -import java.io.IOException; - import org.apache.commons.lang3.StringUtils; -import org.apache.http.Header; -import org.apache.http.HttpRequest; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.ProtocolException; +import org.apache.http.*; import org.apache.http.client.HttpClient; import org.apache.http.client.HttpRequestRetryHandler; import org.apache.http.client.methods.HttpGet; @@ -40,9 +34,11 @@ import org.apache.http.params.HttpParams; import org.apache.http.protocol.HttpContext; import org.apache.marmotta.client.ClientConfiguration; +import java.io.IOException; + /** * HTTP Utilities - * + * * @author Sebastian Schaffert * @author Sergio Fernández */ @@ -50,21 +46,25 @@ public class HTTPUtil { private static final String CONTEXT = "context"; - public static HttpClient createClient(ClientConfiguration config) { + public static HttpClient createClient(ClientConfiguration config) { + return createClient(config, config.getMarmottaContext()); + } + + public static HttpClient createClient(ClientConfiguration config, String context) { HttpParams httpParams = new BasicHttpParams(); - httpParams.setParameter(CoreProtocolPNames.USER_AGENT, "Marmotta Client Library/"+ MetaUtil.getVersion()); + httpParams.setParameter(CoreProtocolPNames.USER_AGENT, "Marmotta Client Library/" + MetaUtil.getVersion()); httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, config.getSoTimeout()); httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, config.getConnectionTimeout()); - httpParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS,true); - httpParams.setIntParameter(ClientPNames.MAX_REDIRECTS,3); + httpParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true); + httpParams.setIntParameter(ClientPNames.MAX_REDIRECTS, 3); - if (StringUtils.isNotBlank(config.getMarmottaContext())) { - httpParams.setParameter(CONTEXT, config.getMarmottaContext()); + if (StringUtils.isNotBlank(context)) { + httpParams.setParameter(CONTEXT, context); } - + DefaultHttpClient client; if (config.getConectionManager() != null) { client = new DefaultHttpClient(config.getConectionManager(), httpParams); @@ -75,17 +75,17 @@ public class HTTPUtil { client.setHttpRequestRetryHandler(new MarmottaHttpRequestRetryHandler()); return client; } - - public static HttpPost createPost(String path, ClientConfiguration config) { - String serviceUrl = config.getMarmottaUri() + path ; - - //FIXME: switch to a more elegant way, such as Jersey's UriBuilder - if (StringUtils.isNotBlank(config.getMarmottaContext())) { - serviceUrl += "?" + CONTEXT + "=" + config.getMarmottaContext(); - } + + public static HttpPost createPost(String path, ClientConfiguration config) { + String serviceUrl = config.getMarmottaUri() + path; + + //FIXME: switch to a more elegant way, such as Jersey's UriBuilder + if (StringUtils.isNotBlank(config.getMarmottaContext())) { + serviceUrl += "?" + CONTEXT + "=" + config.getMarmottaContext(); + } return new HttpPost(serviceUrl); - } + } private static class MarmottaRedirectStrategy extends DefaultRedirectStrategy { @@ -126,7 +126,7 @@ public class HTTPUtil { * unsuccessfully executed * @param context the context for the request execution * @return <code>true</code> if the method should be retried, <code>false</code> - * otherwise + * otherwise */ @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { @@ -134,5 +134,5 @@ public class HTTPUtil { } } - + }
