use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus in project timbuctoo by HuygensING.
the class RdfUpload method upload.
@Consumes(MediaType.MULTIPART_FORM_DATA)
@POST
public Response upload(@FormDataParam("file") final InputStream rdfInputStream, @FormDataParam("file") final FormDataBodyPart body, @FormDataParam("fileMimeTypeOverride") final MediaType mimeTypeOverride, @FormDataParam("encoding") final String encoding, @FormDataParam("baseUri") final URI baseUri, @FormDataParam("defaultGraph") final URI defaultGraph, @HeaderParam("authorization") final String authHeader, @PathParam("userId") final String userId, @PathParam("dataSet") final String dataSetId, @QueryParam("forceCreation") boolean forceCreation, @QueryParam("async") final boolean async) throws ExecutionException, InterruptedException, LogStorageFailedException, DataStoreCreationException {
final Either<Response, Response> result = authCheck.getOrCreate(authHeader, userId, dataSetId, forceCreation).flatMap(userAndDs -> authCheck.hasAdminAccess(userAndDs.getLeft(), userAndDs.getRight())).map((Tuple<User, DataSet> userDataSetTuple) -> {
final MediaType mediaType = mimeTypeOverride == null ? body.getMediaType() : mimeTypeOverride;
final DataSet dataSet = userDataSetTuple.getRight();
ImportManager importManager = dataSet.getImportManager();
if (mediaType == null || !importManager.isRdfTypeSupported(mediaType)) {
return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity("{\"error\": \"We do not support the mediatype '" + mediaType + "'. Make sure to add the correct " + "mediatype to the file parameter. In curl you'd use `-F \"file=@<filename>;type=<mediatype>\"`. In a " + "webbrowser you probably have no way of setting the correct mimetype. So you can use a special " + "parameter " + "to override it: `formData.append(\"fileMimeTypeOverride\", \"<mimetype>\");`\"}").build();
}
if (StringUtils.isBlank(encoding)) {
return Response.status(Response.Status.BAD_REQUEST).entity("Please provide an 'encoding' parameter").build();
}
if (StringUtils.isBlank(body.getContentDisposition().getFileName())) {
return Response.status(400).entity("filename cannot be empty.").build();
}
Future<ImportStatus> promise = null;
try {
promise = importManager.addLog(baseUri == null ? dataSet.getMetadata().getBaseUri() : baseUri.toString(), defaultGraph == null ? dataSet.getMetadata().getBaseUri() : defaultGraph.toString(), body.getContentDisposition().getFileName(), rdfInputStream, Optional.of(Charset.forName(encoding)), mediaType);
} catch (LogStorageFailedException e) {
return Response.serverError().build();
}
if (!async) {
return handleImportManagerResult(promise);
}
return Response.accepted().build();
});
if (result.isLeft()) {
return result.getLeft();
} else {
return result.get();
}
}
use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus in project timbuctoo by HuygensING.
the class ImportManagerTest method addLogSavesTheLogToDisk.
@Test
public void addLogSavesTheLogToDisk() throws Exception {
File file = FileHelpers.getFileFromResource(ImportManagerTest.class, "clusius.ttl").toFile();
String name = "http://example.com/clusius.ttl";
String defaultGraph = "http://example.com/defaultGraph";
String baseUri = "http://example.com/baseUri";
Future<ImportStatus> promise = importManager.addLog(baseUri, defaultGraph, name, new FileInputStream(file), Optional.of(Charsets.UTF_8), MediaType.valueOf("text/turtle"));
ImportStatus status = promise.get();
assertThat(status.getErrorCount(), is((0)));
LogEntry logEntry = importManager.getLogEntries().get(0);
assertThat(logEntry.getBaseUri(), is(baseUri));
assertThat(logEntry.getDefaultGraph(), is(defaultGraph));
// The first character is an @. if we can read that we apparently can access the file
assertThat(fileStorage.getLog(logEntry.getLogToken().get()).getReader().read(), is(64));
}
use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus in project timbuctoo by HuygensING.
the class ImportManagerTest method callsStoresWhenANewLogIsAdded.
@Test
public void callsStoresWhenANewLogIsAdded() throws Exception {
File file = FileHelpers.getFileFromResource(ImportManagerTest.class, "clusius.ttl").toFile();
String name = "http://example.com/clusius.ttl";
String defaultGraph = "http://example.com/defaultGraph";
String baseUri = "http://example.com/baseUri";
CountingProcessor processor = new CountingProcessor();
importManager.subscribeToRdf(processor);
Future<ImportStatus> promise = importManager.addLog(baseUri, defaultGraph, name, new FileInputStream(file), Optional.of(Charsets.UTF_8), MediaType.valueOf("text/turtle"));
ImportStatus status = promise.get();
assertThat(processor.getCounter(), is(28));
assertThat(status.hasErrors(), is(false));
}
use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus in project timbuctoo by HuygensING.
the class ImportManagerTest method generateLogSavesTheLogAndCallsTheStores.
@Test
public void generateLogSavesTheLogAndCallsTheStores() throws Exception {
String defaultGraph = "http://example.com/defaultGraph";
String baseUri = "http://example.com/baseUri";
CountingProcessor processor = new CountingProcessor();
importManager.subscribeToRdf(processor);
Future<ImportStatus> promise = importManager.generateLog(baseUri, defaultGraph, new DummyRdfCreator());
ImportStatus status = promise.get();
assertThat(status.hasErrors(), is(false));
assertThat(processor.getCounter(), is(3));
LogEntry logEntry = importManager.getLogEntries().get(0);
assertThat(logEntry.getBaseUri(), is(baseUri));
assertThat(logEntry.getDefaultGraph(), is(defaultGraph));
// The first character is an < (start of a uri in nquads) if we can read that we apparently can access the file
assertThat(fileStorage.getLog(logEntry.getLogToken().get()).getReader().read(), is(60));
}
use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus in project timbuctoo by HuygensING.
the class RdfDescriptionSaverTest method setUp.
@Before
public void setUp() throws Exception {
descriptionFileName = "description.xml";
descriptionFile = new File(descriptionFileName);
logList = new LogList();
importStatus = new ImportStatus(logList);
testRdfDescriptionSaver = new RdfDescriptionSaver(descriptionFile, BASE_URI, importStatus);
testRdfDescriptionSaver.start(0);
}
Aggregations