Search in sources :

Example 1 with SelfServiceMetaDataId

use of org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId in project winery by eclipse.

the class CsarExporter method addSelfServiceMetaData.

/**
 * Adds all self service meta data to the targetDir
 *
 * @param targetDir the directory in the CSAR where to put the content to
 * @param refMap    is used later to create the CSAR
 */
private void addSelfServiceMetaData(IRepository repository, ServiceTemplateId entryId, String targetDir, Map<RepositoryFileReference, String> refMap) throws IOException {
    final SelfServiceMetaDataId selfServiceMetaDataId = new SelfServiceMetaDataId(entryId);
    // This method is also called if the directory SELFSERVICE-Metadata exists without content and even if the directory does not exist at all,
    // but the ServiceTemplate itself exists.
    // The current assumption is that this is enough for an existence.
    // Thus, we have to take care of the case of an empty directory and add a default data.xml
    SelfServiceMetaDataUtils.ensureDataXmlExists(selfServiceMetaDataId);
    refMap.put(SelfServiceMetaDataUtils.getDataXmlRef(selfServiceMetaDataId), targetDir + "data.xml");
    // The schema says that the images have to exist
    // However, at a quick modeling, there might be no images
    // Therefore, we check for existence
    final RepositoryFileReference iconJpgRef = SelfServiceMetaDataUtils.getIconJpgRef(selfServiceMetaDataId);
    if (repository.exists(iconJpgRef)) {
        refMap.put(iconJpgRef, targetDir + "icon.jpg");
    }
    final RepositoryFileReference imageJpgRef = SelfServiceMetaDataUtils.getImageJpgRef(selfServiceMetaDataId);
    if (repository.exists(imageJpgRef)) {
        refMap.put(imageJpgRef, targetDir + "image.jpg");
    }
    Application application = SelfServiceMetaDataUtils.getApplication(selfServiceMetaDataId);
    // clear CSAR name as this may change.
    application.setCsarName(null);
    // hack for the OpenTOSCA container to display something
    application.setVersion("1.0");
    List<String> authors = application.getAuthors();
    if (authors.isEmpty()) {
        authors.add("Winery");
    }
    // make the patches to data.xml permanent
    try {
        BackendUtils.persist(application, SelfServiceMetaDataUtils.getDataXmlRef(selfServiceMetaDataId), MediaTypes.MEDIATYPE_TEXT_XML);
    } catch (IOException e) {
        LOGGER.error("Could not persist patches to data.xml", e);
    }
    Options options = application.getOptions();
    if (options != null) {
        SelfServiceMetaDataId id = new SelfServiceMetaDataId(entryId);
        for (ApplicationOption option : options.getOption()) {
            String url = option.getIconUrl();
            if (Util.isRelativeURI(url)) {
                putRefIntoRefMap(targetDir, refMap, repository, id, url);
            }
            url = option.getPlanInputMessageUrl();
            if (Util.isRelativeURI(url)) {
                putRefIntoRefMap(targetDir, refMap, repository, id, url);
            }
        }
    }
}
Also used : Options(org.eclipse.winery.model.selfservice.Application.Options) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) ApplicationOption(org.eclipse.winery.model.selfservice.ApplicationOption) SelfServiceMetaDataId(org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId) Application(org.eclipse.winery.model.selfservice.Application)

Example 2 with SelfServiceMetaDataId

use of org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId 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 3 with SelfServiceMetaDataId

use of org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId in project winery by eclipse.

the class CsarExporter method addSelfServiceMetaData.

private void addSelfServiceMetaData(IRepository repository, ServiceTemplateId serviceTemplateId, Map<RepositoryFileReference, String> refMap) throws IOException {
    SelfServiceMetaDataId id = new SelfServiceMetaDataId(serviceTemplateId);
    // We add the selfservice information regardless of the existance. - i.e., no "if (repository.exists(id)) {"
    // This ensures that the name of the application is
    // add everything in the root of the CSAR
    String targetDir = Constants.DIRNAME_SELF_SERVICE_METADATA + "/";
    addSelfServiceMetaData(repository, serviceTemplateId, targetDir, refMap);
}
Also used : SelfServiceMetaDataId(org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId)

Example 4 with SelfServiceMetaDataId

use of org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId in project winery by eclipse.

the class CsarImporter method importSelfServiceMetaData.

/**
 * Imports a self-service meta data description (if available)
 * <p>
 * The first service template in the provided entry definitions is taken
 */
private void importSelfServiceMetaData(final TOSCAMetaFile tmf, final Path rootPath, Path entryDefinitions, final List<String> errors) {
    final Path selfServiceDir = rootPath.resolve(Constants.DIRNAME_SELF_SERVICE_METADATA);
    if (!Files.exists(selfServiceDir)) {
        CsarImporter.LOGGER.debug("Self-service Portal directory does not exist in CSAR");
        return;
    }
    if (!Files.exists(entryDefinitions)) {
        CsarImporter.LOGGER.debug("Entry definitions does not exist.");
        return;
    }
    Unmarshaller um = JAXBSupport.createUnmarshaller();
    TDefinitions defs;
    try {
        defs = (TDefinitions) um.unmarshal(entryDefinitions.toFile());
    } catch (JAXBException e) {
        errors.add("Could not unmarshal definitions " + entryDefinitions.getFileName() + " " + e.getMessage());
        return;
    } catch (ClassCastException e) {
        errors.add("Definitions " + entryDefinitions.getFileName() + " is not a TDefinitions " + e.getMessage());
        return;
    }
    final int cutLength = selfServiceDir.toString().length() + 1;
    Iterator<TExtensibleElements> iterator = defs.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().iterator();
    boolean found = false;
    TExtensibleElements next = null;
    while (iterator.hasNext() && !found) {
        next = iterator.next();
        if (next instanceof TServiceTemplate) {
            found = true;
        }
    }
    if (found) {
        TServiceTemplate serviceTemplate = (TServiceTemplate) next;
        String namespace = serviceTemplate.getTargetNamespace();
        if (namespace == null) {
            namespace = defs.getTargetNamespace();
        }
        ServiceTemplateId stId = new ServiceTemplateId(namespace, serviceTemplate.getId(), false);
        final SelfServiceMetaDataId id = new SelfServiceMetaDataId(stId);
        try {
            Files.walkFileTree(selfServiceDir, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                    String name = file.toString().substring(cutLength);
                    // check: if name contains "/", this could lead to exceptions
                    RepositoryFileReference ref = new RepositoryFileReference(id, name);
                    if (name.equals("data.xml")) {
                        // We quickly replace it via String replacement instead of XSLT
                        try {
                            String oldContent = org.apache.commons.io.FileUtils.readFileToString(file.toFile(), "UTF-8");
                            String newContent = oldContent.replace("http://opentosca.org/self-service", "http://www.eclipse.org/winery/model/selfservice");
                            newContent = newContent.replace(":application", ":Application");
                            if (!oldContent.equals(newContent)) {
                                // we replaced something -> write new content to old file
                                org.apache.commons.io.FileUtils.writeStringToFile(file.toFile(), newContent, "UTF-8");
                            }
                        } catch (IOException e) {
                            CsarImporter.LOGGER.debug("Could not replace content in data.xml", e);
                        }
                    }
                    importFile(file, ref, tmf, rootPath, errors);
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            CsarImporter.LOGGER.debug(e.getMessage(), e);
            errors.add("Self-service Meta Data: " + e.getMessage());
        }
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) SelfServiceMetaDataId(org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) Unmarshaller(javax.xml.bind.Unmarshaller) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

SelfServiceMetaDataId (org.eclipse.winery.repository.datatypes.ids.elements.SelfServiceMetaDataId)4 RepositoryFileReference (org.eclipse.winery.common.RepositoryFileReference)3 ApplicationOption (org.eclipse.winery.model.selfservice.ApplicationOption)2 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 Application (org.eclipse.winery.model.selfservice.Application)1 Options (org.eclipse.winery.model.selfservice.Application.Options)1