use of org.eclipse.sirius.components.graphql.api.UploadFile in project sirius-components by eclipse-sirius.
the class GraphQLController method getVariables.
/**
* Creates the map of variables for the evaluation of the GraphQL mutation by adding the necessary content from the
* multipart file into the variables.
*
* @param graphQLPayload
* The GraphQL payload used to find the regular variables
* @param jsonNode
* The mapping between the GraphQL variables and the content of the multipart file
* @param file
* The multipart file containing the raw content of the file to upload
* @return The variables to use to evaluate the request or an empty optional in case of error
*/
private Optional<Map<String, Object>> getVariables(GraphQLPayload graphQLPayload, JsonNode jsonNode, MultipartFile file) {
Optional<Map<String, Object>> optionalVariables = Optional.empty();
String variableName = jsonNode.get(FIRST_UPLOADED_FILE).asText();
if (variableName.equals(MULTIPART_VARIABLES_FILE)) {
Map<String, Object> variables = new HashMap<>(graphQLPayload.getVariables());
Map<Object, Object> inputMap = new HashMap<>();
Object input = variables.get(INPUT_ARGUMENT);
if (input instanceof Map<?, ?>) {
Map<?, ?> rawInputMap = (Map<?, ?>) input;
rawInputMap.entrySet().stream().forEach(entry -> inputMap.put(entry.getKey(), entry.getValue()));
}
if (file != null) {
try {
InputStream inputStream = file.getInputStream();
inputMap.put(VARIABLE_FILE, new UploadFile(file.getOriginalFilename(), inputStream));
variables.put(INPUT_ARGUMENT, inputMap);
optionalVariables = Optional.of(variables);
} catch (IOException exception) {
this.logger.warn(exception.getMessage(), exception);
}
} else {
// $NON-NLS-1$
this.logger.warn("Missing multipart file");
}
}
return optionalVariables;
}
use of org.eclipse.sirius.components.graphql.api.UploadFile in project sirius-web by eclipse-sirius.
the class ProjectImportService method importProject.
/**
* Returns {@link UploadProjectSuccessPayload} if the project import has been successful, {@link ErrorPayload}
* otherwise.
*
* <p>
* Unzip the given {@link UploadFile}, then creates a project with the name of the root directory in the zip file,
* then use {@link ProjectImporter} to create documents and representations. If the project has not been imported,
* it disposes the {@link IEditingContextEventProcessor} used to create documents and representations then delete
* the created project in order to keep the server in the same state before the project upload attempt.
* </p>
*
* @param inputId
* The identifier of the input which has triggered the upload
* @param file
* the file to upload
* @return {@link UploadProjectSuccessPayload} whether the project import has been successful, {@link ErrorPayload}
* otherwise
*/
@Override
public IPayload importProject(UUID inputId, UploadFile file) {
IPayload payload = new ErrorPayload(inputId, this.messageService.unexpectedError());
ProjectUnzipper unzipper = new ProjectUnzipper(file.getInputStream(), this.objectMapper);
Optional<UnzippedProject> optionalUnzippedProject = unzipper.unzipProject();
if (optionalUnzippedProject.isEmpty()) {
return new ErrorPayload(inputId, this.messageService.unexpectedError());
}
UnzippedProject unzippedProject = optionalUnzippedProject.get();
ProjectManifest manifest = unzippedProject.getProjectManifest();
String projectName = unzippedProject.getProjectName();
CreateProjectInput createProjectInput = new CreateProjectInput(inputId, projectName, Visibility.PRIVATE);
IPayload createProjectPayload = this.projectService.createProject(createProjectInput);
if (createProjectPayload instanceof CreateProjectSuccessPayload) {
Project project = ((CreateProjectSuccessPayload) createProjectPayload).getProject();
Optional<IEditingContextEventProcessor> optionalEditingContextEventProcessor = this.editingContextEventProcessorRegistry.getOrCreateEditingContextEventProcessor(project.getId().toString());
if (optionalEditingContextEventProcessor.isPresent()) {
IEditingContextEventProcessor editingContextEventProcessor = optionalEditingContextEventProcessor.get();
Map<String, UploadFile> documents = unzippedProject.getDocumentIdToUploadFile();
List<RepresentationDescriptor> representations = unzippedProject.getRepresentationDescriptors();
ProjectImporter projectImporter = new ProjectImporter(project.getId().toString(), editingContextEventProcessor, documents, representations, manifest, this.idMappingRepository);
boolean hasBeenImported = projectImporter.importProject(inputId);
if (!hasBeenImported) {
this.editingContextEventProcessorRegistry.disposeEditingContextEventProcessor(project.getId().toString());
this.projectService.delete(project.getId());
} else {
payload = new UploadProjectSuccessPayload(inputId, project);
}
}
}
return payload;
}
use of org.eclipse.sirius.components.graphql.api.UploadFile in project sirius-web by eclipse-sirius.
the class MutationUploadDocumentDataFetcher method get.
@Override
public CompletableFuture<IPayload> get(DataFetchingEnvironment environment) throws Exception {
Map<Object, Object> inputArgument = environment.getArgument(MutationTypeProvider.INPUT_ARGUMENT);
// We cannot use directly UploadDocumentInput, the objectMapper cannot handle the file stream.
// @formatter:off
UUID id = Optional.of(inputArgument.get(ID)).filter(String.class::isInstance).map(String.class::cast).flatMap(new IDParser()::parse).orElse(null);
String editingContextId = Optional.of(inputArgument.get(EDITING_CONTEXT_ID)).filter(String.class::isInstance).map(String.class::cast).orElse(null);
UploadFile file = Optional.of(inputArgument.get(FILE)).filter(UploadFile.class::isInstance).map(UploadFile.class::cast).orElse(null);
// @formatter:on
UploadDocumentInput input = new UploadDocumentInput(id, editingContextId, file);
// @formatter:off
return this.editingContextEventProcessorRegistry.dispatchEvent(input.getEditingContextId(), input).defaultIfEmpty(new ErrorPayload(input.getId(), this.messageService.unexpectedError())).toFuture();
// @formatter:on
}
use of org.eclipse.sirius.components.graphql.api.UploadFile in project sirius-web by eclipse-sirius.
the class MutationUploadProjectDataFetcher method get.
@Override
public IPayload get(DataFetchingEnvironment environment) throws Exception {
Map<Object, Object> input = environment.getArgument(MutationTypeProvider.INPUT_ARGUMENT);
// @formatter:off
UUID id = Optional.of(input.get(ID)).filter(String.class::isInstance).map(String.class::cast).flatMap(new IDParser()::parse).orElse(null);
return Optional.of(input.get(FILE)).filter(UploadFile.class::isInstance).map(UploadFile.class::cast).map(uploadFile -> this.projectImportService.importProject(id, uploadFile)).orElse(new ErrorPayload(id, this.messageService.unexpectedError()));
// @formatter:on
}
use of org.eclipse.sirius.components.graphql.api.UploadFile in project sirius-web by eclipse-sirius.
the class UploadDocumentEventHandler method handle.
@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) {
this.counter.increment();
IPayload payload = new ErrorPayload(input.getId(), this.messageService.unexpectedError());
ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
if (input instanceof UploadDocumentInput) {
UploadDocumentInput uploadDocumentInput = (UploadDocumentInput) input;
String projectId = uploadDocumentInput.getEditingContextId();
UploadFile file = uploadDocumentInput.getFile();
// @formatter:off
Optional<AdapterFactoryEditingDomain> optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
// @formatter:on
String name = file.getName().trim();
if (optionalEditingDomain.isPresent()) {
AdapterFactoryEditingDomain adapterFactoryEditingDomain = optionalEditingDomain.get();
String content = this.getContent(adapterFactoryEditingDomain.getResourceSet().getPackageRegistry(), file);
var optionalDocument = this.documentService.createDocument(projectId, name, content);
if (optionalDocument.isPresent()) {
Document document = optionalDocument.get();
ResourceSet resourceSet = adapterFactoryEditingDomain.getResourceSet();
URI uri = URI.createURI(document.getId().toString());
if (resourceSet.getResource(uri, false) == null) {
ResourceSet loadingResourceSet = new ResourceSetImpl();
loadingResourceSet.setPackageRegistry(resourceSet.getPackageRegistry());
JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
loadingResourceSet.getResources().add(resource);
try (var inputStream = new ByteArrayInputStream(document.getContent().getBytes())) {
resource.load(inputStream, null);
} catch (IOException exception) {
this.logger.warn(exception.getMessage(), exception);
}
resource.eAdapters().add(new DocumentMetadataAdapter(name));
resourceSet.getResources().add(resource);
payload = new UploadDocumentSuccessPayload(input.getId(), document);
changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input);
}
}
}
}
payloadSink.tryEmitValue(payload);
changeDescriptionSink.tryEmitNext(changeDescription);
}
Aggregations