Search in sources :

Example 1 with RemoteFile

use of nl.knaw.huygens.timbuctoo.remote.rs.download.RemoteFile 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

Charsets (com.google.common.base.Charsets)1 Sets (com.google.common.collect.Sets)1 URI (java.net.URI)1 Iterator (java.util.Iterator)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Either (javaslang.control.Either)1 HeaderParam (javax.ws.rs.HeaderParam)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 QueryParam (javax.ws.rs.QueryParam)1 MediaType (javax.ws.rs.core.MediaType)1 Response (javax.ws.rs.core.Response)1 RemoteFile (nl.knaw.huygens.timbuctoo.remote.rs.download.RemoteFile)1 ResourceSyncFileLoader (nl.knaw.huygens.timbuctoo.remote.rs.download.ResourceSyncFileLoader)1 ImportManager (nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager)1 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)1 DataStoreCreationException (nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException)1 AuthCheck (nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.auth.AuthCheck)1