Search in sources :

Example 21 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class XSDImportResource method getXSD.

@GET
@ApiOperation(value = "May be used by the modeler to generate an XML editor based on the XML schema")
// we cannot use "MimeTypes.MIMETYPE_XSD" here as the latter is "text/xml" and org.eclipse.winery.repository.resources.AbstractComponentInstanceResource.getDefinitionsAsResponse() also produces text/xml
@Produces("text/xsd")
public Response getXSD() {
    final Optional<String> location = ImportUtils.getLocation((GenericImportId) id);
    if (!location.isPresent()) {
        return Response.status(Status.NOT_FOUND).build();
    }
    RepositoryFileReference ref = new RepositoryFileReference(this.id, location.get());
    return RestUtils.returnRepoPath(ref, null);
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 22 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class PlanFileResource method onPutFile.

@PUT
@Consumes({ MediaType.MULTIPART_FORM_DATA })
@ApiOperation(value = "Resource currently works for BPMN4TOSCA plans only")
public // @formatter:off
Response onPutFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("file") FormDataBodyPart body) {
    // @formatter:on
    String fileName = fileDetail.getFileName();
    RepositoryFileReference ref = new RepositoryFileReference(this.planId, fileName);
    RepositoryFileReference oldRef = this.getFileRef();
    boolean persistanceNecessary;
    if (ref.equals(oldRef)) {
        // nothing todo, file will be replaced
        persistanceNecessary = false;
    } else {
        // new filename sent
        RestUtils.delete(oldRef);
        PlansResource.setPlanModelReference(this.plan, this.planId, fileName);
        persistanceNecessary = true;
    }
    // Really store it
    try {
        RepositoryFactory.getRepository().putContentToFile(ref, uploadedInputStream, org.apache.tika.mime.MediaType.parse(body.getMediaType().toString()));
    } catch (IOException e1) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Could not store plan. " + e1.getMessage()).build();
    }
    if (persistanceNecessary) {
        return RestUtils.persist(this.res);
    } else {
        return Response.noContent().build();
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) IOException(java.io.IOException) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT)

Example 23 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class PlansResource method saveFile.

private Response saveFile(TPlan tPlan, InputStream uploadedInputStream, FormDataContentDisposition fileDetail, FormDataBodyPart body) {
    boolean bpmn4toscaMode = Namespaces.URI_BPMN4TOSCA_20.equals(tPlan.getPlanLanguage());
    if (uploadedInputStream != null || bpmn4toscaMode) {
        // Determine Id
        PlansId plansId = new PlansId((ServiceTemplateId) ((ServiceTemplateResource) this.res).getId());
        PlanId planId = new PlanId(plansId, new XmlId(tPlan.getId(), false));
        // Ensure overwriting
        if (RepositoryFactory.getRepository().exists(planId)) {
            try {
                RepositoryFactory.getRepository().forceDelete(planId);
                // Quick hack to remove the deleted plan from the plans element
                ((ServiceTemplateResource) this.res).synchronizeReferences();
            } catch (IOException e) {
                return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
            }
        }
        String fileName;
        if (bpmn4toscaMode) {
            fileName = tPlan.getId() + Constants.SUFFIX_BPMN4TOSCA;
            RepositoryFileReference ref = new RepositoryFileReference(planId, fileName);
            // Errors are ignored in the following call
            RestUtils.putContentToFile(ref, "{}", MediaTypes.MEDIATYPE_APPLICATION_JSON);
        } else {
            // We use the filename also as local file name. Alternatively, we could use the xml id
            // With URL encoding, this should not be an issue
            fileName = Util.URLencode(fileDetail.getFileName());
            // Really store it
            RepositoryFileReference ref = new RepositoryFileReference(planId, fileName);
            // Errors are ignored in the following call
            RestUtils.putContentToFile(ref, uploadedInputStream, body.getMediaType());
        }
        PlansResource.setPlanModelReference(tPlan, planId, fileName);
    }
    return RestUtils.persist(this.res);
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) PlanId(org.eclipse.winery.common.ids.elements.PlanId) ServiceTemplateResource(org.eclipse.winery.repository.rest.resources.servicetemplates.ServiceTemplateResource) XmlId(org.eclipse.winery.common.ids.XmlId) IOException(java.io.IOException) PlansId(org.eclipse.winery.common.ids.elements.PlansId)

Example 24 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class OptionsResource method onPost.

@POST
@ApiOperation(value = "Adds a new option<p>TODO: @return JSON with .tableData: Array with row data for dataTable</p>")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public // @formatter:off
Response onPost(@FormDataParam("name") String name, @FormDataParam("description") String description, @FormDataParam("planServiceName") String planServiceName, @FormDataParam("planInputMessage") String planInputMessage, @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("file") FormDataBodyPart body) {
    // @formatter:on
    if (StringUtils.isEmpty(name)) {
        return Response.status(Status.BAD_REQUEST).entity("planName must be given").build();
    }
    if (StringUtils.isEmpty(description)) {
        return Response.status(Status.BAD_REQUEST).entity("description must be given").build();
    }
    if (StringUtils.isEmpty(planServiceName)) {
        return Response.status(Status.BAD_REQUEST).entity("planServiceName must be given").build();
    }
    if (StringUtils.isEmpty(planInputMessage)) {
        return Response.status(Status.BAD_REQUEST).entity("planInputMessage must be given").build();
    }
    if (uploadedInputStream == null) {
        return Response.status(Status.BAD_REQUEST).entity("file has to be provided").build();
    }
    ApplicationOption option = new ApplicationOption();
    String id = RestUtils.createXMLidAsString(name);
    String fileNamePrefix = OptionResource.getFileNamePrefix(id);
    String iconFileName = fileNamePrefix + OptionResource.ICON_JPG;
    String planInputMessageFileName = fileNamePrefix + OptionResource.PLAN_INPUT_XML;
    // create option data
    option.setId(id);
    option.setName(name);
    option.setDescription(description);
    option.setIconUrl(iconFileName);
    option.setPlanInputMessageUrl(planInputMessageFileName);
    option.setPlanServiceName(planServiceName);
    // BEGIN: store icon and planInputMessage
    SelfServiceMetaDataId ssmdId = ((SelfServicePortalResource) this.res).getId();
    RepositoryFileReference iconRef = new RepositoryFileReference(ssmdId, iconFileName);
    RestUtils.putContentToFile(iconRef, uploadedInputStream, body.getMediaType());
    RepositoryFileReference planInputMessageRef = new RepositoryFileReference(ssmdId, planInputMessageFileName);
    RestUtils.putContentToFile(planInputMessageRef, planInputMessage, MediaType.TEXT_XML_TYPE);
    // END: store icon and planInputMessage
    this.list.add(option);
    return RestUtils.persist(this.res);
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) ApplicationOption(org.eclipse.winery.model.selfservice.ApplicationOption) SelfServiceMetaDataId(org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation)

Example 25 with RepositoryFileReference

use of org.eclipse.winery.common.RepositoryFileReference in project winery by eclipse.

the class BackendUtilsTest method repositoryFileReferenceWithoutSubdirectoryCorrectlyCreated.

@Test
public void repositoryFileReferenceWithoutSubdirectoryCorrectlyCreated() {
    ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http://www.example.org", "at", false);
    ArtifactTemplateSourceDirectoryId artifactTemplateSourceDirectoryId = new ArtifactTemplateSourceDirectoryId(artifactTemplateId);
    final RepositoryFileReference repositoryFileReference = BackendUtils.getRepositoryFileReference(Paths.get("main"), Paths.get("main", "file.txt"), artifactTemplateSourceDirectoryId);
    Assert.assertEquals(artifactTemplateSourceDirectoryId, repositoryFileReference.getParent());
    Assert.assertEquals(Optional.empty(), repositoryFileReference.getSubDirectory());
    Assert.assertEquals("file.txt", repositoryFileReference.getFileName());
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) ArtifactTemplateSourceDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId) ArtifactTemplateId(org.eclipse.winery.common.ids.definitions.ArtifactTemplateId) Test(org.junit.Test)

Aggregations

RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)51 IOException (java.io.IOException)15 InputStream (java.io.InputStream)8 ArtifactTemplateId (org.eclipse.winery.common.ids.definitions.ArtifactTemplateId)8 Definitions (org.eclipse.winery.model.tosca.Definitions)8 BufferedInputStream (java.io.BufferedInputStream)6 ArtifactTemplateSourceDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId)6 Test (org.junit.Test)6 Path (java.nio.file.Path)5 JAXBException (javax.xml.bind.JAXBException)5 MediaType (org.apache.tika.mime.MediaType)5 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)5 ZipInputStream (java.util.zip.ZipInputStream)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 QName (javax.xml.namespace.QName)4 XmlId (org.eclipse.winery.common.ids.XmlId)4 GenericImportId (org.eclipse.winery.common.ids.definitions.imports.GenericImportId)4 XSDImportId (org.eclipse.winery.common.ids.definitions.imports.XSDImportId)4 PlanId (org.eclipse.winery.common.ids.elements.PlanId)4 PlansId (org.eclipse.winery.common.ids.elements.PlansId)4