Search in sources :

Example 11 with ImportStatus

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();
    }
}
Also used : Response(javax.ws.rs.core.Response) PathParam(javax.ws.rs.PathParam) ImportStatus(nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus) Path(javax.ws.rs.Path) Tuple(nl.knaw.huygens.timbuctoo.util.Tuple) StringUtils(org.apache.commons.lang3.StringUtils) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) MediaType(javax.ws.rs.core.MediaType) Future(java.util.concurrent.Future) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Charset(java.nio.charset.Charset) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) HeaderParam(javax.ws.rs.HeaderParam) URI(java.net.URI) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) AuthCheck(nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.auth.AuthCheck) Either(javaslang.control.Either) POST(javax.ws.rs.POST) ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) DataStoreCreationException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException) ErrorResponseHelper.handleImportManagerResult(nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.ErrorResponseHelper.handleImportManagerResult) ExecutionException(java.util.concurrent.ExecutionException) FormDataParam(org.glassfish.jersey.media.multipart.FormDataParam) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) InputStream(java.io.InputStream) ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) ImportStatus(nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus) MediaType(javax.ws.rs.core.MediaType) Tuple(nl.knaw.huygens.timbuctoo.util.Tuple) Consumes(javax.ws.rs.Consumes) POST(javax.ws.rs.POST)

Example 12 with ImportStatus

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));
}
Also used : CachedFile(nl.knaw.huygens.timbuctoo.v5.filestorage.dto.CachedFile) File(java.io.File) FileInputStream(java.io.FileInputStream) LogEntry(nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogEntry) Test(org.junit.Test)

Example 13 with ImportStatus

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));
}
Also used : CachedFile(nl.knaw.huygens.timbuctoo.v5.filestorage.dto.CachedFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 14 with ImportStatus

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));
}
Also used : LogEntry(nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogEntry) Test(org.junit.Test)

Example 15 with ImportStatus

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);
}
Also used : LogList(nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogList) ImportStatus(nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus) File(java.io.File) Before(org.junit.Before)

Aggregations

ImportStatus (nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus)8 LogStorageFailedException (nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException)6 Test (org.junit.Test)6 IOException (java.io.IOException)5 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)5 File (java.io.File)4 ImportManager (nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager)4 LogEntry (nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogEntry)4 LogList (nl.knaw.huygens.timbuctoo.v5.dataset.dto.LogList)4 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)4 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Optional (java.util.Optional)3 POST (javax.ws.rs.POST)3 MediaType (javax.ws.rs.core.MediaType)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Response (javax.ws.rs.core.Response)2