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();
}
}
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();
}
}
Aggregations