Search in sources :

Example 6 with ImportManager

use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager 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 7 with ImportManager

use of nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager in project timbuctoo by HuygensING.

the class Import method importData.

@POST
@Produces("application/json")
public Response importData(@HeaderParam("Authorization") String authorization, @QueryParam("forceCreation") boolean forceCreation, ImportData importData) throws DataStoreCreationException {
    final Either<Response, Response> responses = authCheck.getOrCreate(authorization, importData.userId, importData.dataSetId, forceCreation).flatMap(userAndDs -> authCheck.hasAdminAccess(userAndDs.getLeft(), userAndDs.getRight())).map(userAndDs -> {
        final DataSet dataSet = userAndDs.getRight();
        ImportManager importManager = dataSet.getImportManager();
        try {
            LOG.info("Loading files");
            Iterator<RemoteFile> files = resourceSyncFileLoader.loadFiles(importData.source.toString()).iterator();
            LOG.info("Found files '{}'", files.hasNext());
            ResourceSyncResport resourceSyncResport = new ResourceSyncResport();
            while (files.hasNext()) {
                RemoteFile file = files.next();
                MediaType parsedMediatype = MediaType.APPLICATION_OCTET_STREAM_TYPE;
                try {
                    parsedMediatype = MediaType.valueOf(file.getMimeType());
                } catch (IllegalArgumentException e) {
                    LOG.error("Failed to get mediatype", e);
                }
                if (importManager.isRdfTypeSupported(parsedMediatype)) {
                    resourceSyncResport.importedFiles.add(file.getUrl());
                    importManager.addLog(dataSet.getMetadata().getBaseUri(), dataSet.getMetadata().getBaseUri(), file.getUrl().substring(file.getUrl().lastIndexOf('/') + 1), file.getData(), Optional.of(Charsets.UTF_8), parsedMediatype);
                } else {
                    resourceSyncResport.ignoredFiles.add(file.getUrl());
                    importManager.addFile(file.getData(), file.getUrl(), parsedMediatype);
                }
            }
            return Response.ok(resourceSyncResport).build();
        } catch (Exception e) {
            LOG.error("Could not read files to import", e);
            return Response.serverError().build();
        }
    });
    if (responses.isLeft()) {
        return responses.getLeft();
    } else {
        return responses.get();
    }
}
Also used : Response(javax.ws.rs.core.Response) Charsets(com.google.common.base.Charsets) Either(javaslang.control.Either) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) Produces(javax.ws.rs.Produces) Iterator(java.util.Iterator) ResourceSyncFileLoader(nl.knaw.huygens.timbuctoo.remote.rs.download.ResourceSyncFileLoader) DataStoreCreationException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) Set(java.util.Set) RemoteFile(nl.knaw.huygens.timbuctoo.remote.rs.download.RemoteFile) Sets(com.google.common.collect.Sets) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Response(javax.ws.rs.core.Response) HeaderParam(javax.ws.rs.HeaderParam) Optional(java.util.Optional) URI(java.net.URI) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) AuthCheck(nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.auth.AuthCheck) ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) MediaType(javax.ws.rs.core.MediaType) RemoteFile(nl.knaw.huygens.timbuctoo.remote.rs.download.RemoteFile) DataStoreCreationException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Aggregations

ImportManager (nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager)6 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)5 POST (javax.ws.rs.POST)4 Response (javax.ws.rs.core.Response)4 ImportStatus (nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus)4 DataStoreCreationException (nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException)4 Optional (java.util.Optional)3 Either (javaslang.control.Either)3 HeaderParam (javax.ws.rs.HeaderParam)3 Path (javax.ws.rs.Path)3 QueryParam (javax.ws.rs.QueryParam)3 MediaType (javax.ws.rs.core.MediaType)3 AuthCheck (nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.auth.AuthCheck)3 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)3 InputStream (java.io.InputStream)2 URI (java.net.URI)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 Consumes (javax.ws.rs.Consumes)2 PathParam (javax.ws.rs.PathParam)2