use of nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet in project timbuctoo by HuygensING.
the class Rml method upload.
@POST
public Response upload(final String rdfData, @PathParam("userId") final String ownerId, @PathParam("dataSetId") final String dataSetId, @HeaderParam("authorization") String authHeader) throws DataStoreCreationException, LogStorageFailedException, ExecutionException, InterruptedException {
Optional<User> user;
try {
user = userValidator.getUserFromAccessToken(authHeader);
} catch (UserValidationException e) {
LOG.error("Exception validating user", e);
return Response.status(Response.Status.UNAUTHORIZED).build();
}
if (!user.isPresent()) {
return Response.status(Response.Status.UNAUTHORIZED).build();
}
final Optional<DataSet> dataSet = dataSetRepository.getDataSet(user.get(), ownerId, dataSetId);
if (dataSet.isPresent()) {
ImportManager importManager = dataSet.get().getImportManager();
final String baseUri = dataSet.get().getMetadata().getBaseUri();
Future<ImportStatus> promise = importManager.generateLog(baseUri, baseUri, new RmlRdfCreator(baseUri, rdfData));
return handleImportManagerResult(promise);
} else {
return errorResponseHelper.dataSetNotFound(ownerId, dataSetId);
}
}
use of nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet in project timbuctoo by HuygensING.
the class TabularUpload method upload.
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@POST
public Response upload(@FormDataParam("file") final InputStream rdfInputStream, @FormDataParam("file") final FormDataBodyPart body, @FormDataParam("file") final FormDataContentDisposition fileInfo, @FormDataParam("fileMimeTypeOverride") final MediaType mimeTypeOverride, FormDataMultiPart formData, @HeaderParam("authorization") final String authHeader, @PathParam("userId") final String ownerId, @PathParam("dataSetId") final String dataSetId, @QueryParam("forceCreation") boolean forceCreation) throws DataStoreCreationException, FileStorageFailedException, ExecutionException, InterruptedException, LogStorageFailedException {
final Either<Response, Response> result = authCheck.getOrCreate(authHeader, ownerId, dataSetId, forceCreation).flatMap(userAndDs -> authCheck.hasAdminAccess(userAndDs.getLeft(), userAndDs.getRight())).map(userAndDs -> {
final MediaType mediaType = mimeTypeOverride == null ? body.getMediaType() : mimeTypeOverride;
Optional<Loader> loader = LoaderFactory.createFor(mediaType.toString(), formData.getFields().entrySet().stream().filter(entry -> entry.getValue().size() > 0).filter(entry -> entry.getValue().get(0) != null).filter(entry -> MediaTypes.typeEqual(MediaType.TEXT_PLAIN_TYPE, entry.getValue().get(0).getMediaType())).collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().get(0).getValue())));
if (!loader.isPresent()) {
return errorResponseHelper.error(400, "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>\");`");
}
final DataSet dataSet = userAndDs.getRight();
ImportManager importManager = dataSet.getImportManager();
if (StringUtils.isBlank(fileInfo.getName())) {
return Response.status(400).entity("filename cannot be empty.").build();
}
try {
String fileToken = importManager.addFile(rdfInputStream, fileInfo.getFileName(), mediaType);
Future<ImportStatus> promise = importManager.generateLog(dataSet.getMetadata().getBaseUri(), dataSet.getMetadata().getBaseUri(), new TabularRdfCreator(loader.get(), fileToken, fileInfo.getFileName()));
return handleImportManagerResult(promise);
} catch (FileStorageFailedException | LogStorageFailedException e) {
LOG.error("Tabular upload failed", e);
return Response.serverError().build();
}
});
if (result.isLeft()) {
return result.getLeft();
} else {
return result.get();
}
}
use of nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet in project timbuctoo by HuygensING.
the class TabularRdfCreator method sendQuads.
@Override
public void sendQuads(RdfSerializer saver, DataSet dataSet, Consumer<String> statusConsumer) throws LogStorageFailedException {
try (CachedFile file = dataSet.getImportManager().getFile(fileToken)) {
final RawUploadRdfSaver rawUploadRdfSaver = new RawUploadRdfSaver(dataSet.getMetadata(), file.getFile().getName(), file.getMimeType(), saver, fileName, Clock.systemUTC());
loader.loadData(Lists.newArrayList(tuple(fileName, file.getFile())), new Importer(new StateMachine<>(rawUploadRdfSaver), new ResultReporter(statusConsumer)));
} catch (Exception e) {
throw new LogStorageFailedException(e);
}
}
use of nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet in project timbuctoo by HuygensING.
the class DataSetRepository method loadDataSetsFromJson.
private void loadDataSetsFromJson() throws IOException {
Map<String, Set<DataSetMetaData>> ownerMetadataMap = dataStorage.loadDataSetMetaData();
synchronized (dataSetMap) {
for (Map.Entry<String, Set<DataSetMetaData>> entry : ownerMetadataMap.entrySet()) {
String ownerId = entry.getKey();
Set<DataSetMetaData> ownerMetaDatas = entry.getValue();
HashMap<String, DataSet> ownersSets = new HashMap<>();
dataSetMap.put(ownerId, ownersSets);
for (DataSetMetaData dataSetMetaData : ownerMetaDatas) {
String dataSetName = dataSetMetaData.getDataSetId();
try {
ownersSets.put(dataSetName, dataSet(dataSetMetaData, executorService, rdfBaseUri, dataStoreFactory, () -> onUpdated.accept(dataSetMetaData.getCombinedId()), dataStorage.getDataSetStorage(ownerId, dataSetName)));
} catch (DataStoreCreationException e) {
throw new IOException(e);
}
}
}
}
}
use of nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet in project timbuctoo by HuygensING.
the class DataSetRepository method removeDataSet.
public void removeDataSet(String ownerId, String dataSetName, User user) throws IOException, NotEnoughPermissionsException, DataSetDoesNotExistException {
try {
DataSet dataSet = dataSetMap.get(ownerId).get(dataSetName);
if (dataSet == null) {
LOG.warn("DataSet '{}' of user with id '{}' does not exist (anymore).", dataSetName, ownerId);
throw new DataSetDoesNotExistException(dataSetName, ownerId);
}
String combinedId = dataSet.getMetadata().getCombinedId();
if (!permissionFetcher.getPermissions(user, dataSet.getMetadata()).contains(Permission.ADMIN)) {
throw new NotEnoughPermissionsException(String.format("User '%s' is not allowed to remove dataset '%s'", user.getDisplayName(), combinedId));
}
dataSet.stop();
dataSetMap.get(ownerId).remove(dataSetName);
permissionFetcher.removeAuthorizations(combinedId);
} catch (PermissionFetchingException e) {
throw new IOException(e);
}
// remove folder
dataStorage.getDataSetStorage(ownerId, dataSetName).clear();
}
Aggregations