Search in sources :

Example 1 with UploadFile

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;
}
Also used : UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) HashMap(java.util.HashMap) InputStream(java.io.InputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with UploadFile

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;
}
Also used : RepresentationDescriptor(org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor) CreateProjectSuccessPayload(org.eclipse.sirius.web.services.api.projects.CreateProjectSuccessPayload) CreateProjectInput(org.eclipse.sirius.web.services.api.projects.CreateProjectInput) IPayload(org.eclipse.sirius.components.core.api.IPayload) UnzippedProject(org.eclipse.sirius.web.services.api.projects.UnzippedProject) Project(org.eclipse.sirius.web.services.api.projects.Project) UnzippedProject(org.eclipse.sirius.web.services.api.projects.UnzippedProject) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) ProjectManifest(org.eclipse.sirius.web.services.api.projects.ProjectManifest) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) UploadProjectSuccessPayload(org.eclipse.sirius.web.services.api.projects.UploadProjectSuccessPayload) IEditingContextEventProcessor(org.eclipse.sirius.components.collaborative.api.IEditingContextEventProcessor)

Example 3 with UploadFile

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
}
Also used : UploadDocumentInput(org.eclipse.sirius.web.services.api.document.UploadDocumentInput) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) UUID(java.util.UUID)

Example 4 with UploadFile

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
}
Also used : ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) IGraphQLMessageService(org.eclipse.sirius.web.graphql.messages.IGraphQLMessageService) MutationTypeProvider(org.eclipse.sirius.web.graphql.schema.MutationTypeProvider) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) IProjectImportService(org.eclipse.sirius.web.services.api.projects.IProjectImportService) UUID(java.util.UUID) Project(org.eclipse.sirius.web.services.api.projects.Project) IDataFetcherWithFieldCoordinates(org.eclipse.sirius.components.graphql.api.IDataFetcherWithFieldCoordinates) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) UploadProjectInput(org.eclipse.sirius.web.services.api.projects.UploadProjectInput) Objects(java.util.Objects) IPayload(org.eclipse.sirius.components.core.api.IPayload) Map(java.util.Map) Optional(java.util.Optional) MutationDataFetcher(org.eclipse.sirius.components.annotations.spring.graphql.MutationDataFetcher) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) UUID(java.util.UUID)

Example 5 with UploadFile

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);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) Document(org.eclipse.sirius.web.services.api.document.Document) URI(org.eclipse.emf.common.util.URI) IPayload(org.eclipse.sirius.components.core.api.IPayload) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) UploadDocumentInput(org.eclipse.sirius.web.services.api.document.UploadDocumentInput) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) UploadDocumentSuccessPayload(org.eclipse.sirius.web.services.api.document.UploadDocumentSuccessPayload) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)

Aggregations

UploadFile (org.eclipse.sirius.components.graphql.api.UploadFile)10 IPayload (org.eclipse.sirius.components.core.api.IPayload)5 UploadDocumentInput (org.eclipse.sirius.web.services.api.document.UploadDocumentInput)5 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)4 Document (org.eclipse.sirius.web.services.api.document.Document)4 Project (org.eclipse.sirius.web.services.api.projects.Project)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)3 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)3 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)3 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Objects (java.util.Objects)2 UUID (java.util.UUID)2 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)2 Profile (org.eclipse.sirius.web.services.api.accounts.Profile)2 IDocumentService (org.eclipse.sirius.web.services.api.document.IDocumentService)2