Search in sources :

Example 16 with RepositoryFileReference

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

the class AbstractRepository method getMimeType.

/**
 * This is a simple implementation using the information put by
 * setMimeType(RepositoryFileReference ref) or determining the mime type
 * using Utils.getMimeType. If the latter is done, the mime type is
 * persisted using setMimeType
 */
@Override
public String getMimeType(RepositoryFileReference ref) throws IOException {
    RepositoryFileReference mimeFileRef = this.getMimeFileRef(ref);
    String mimeType;
    if (this.exists(mimeFileRef)) {
        InputStream is = this.newInputStream(mimeFileRef);
        mimeType = IOUtils.toString(is, "UTF-8");
        is.close();
    } else {
        // repository has been manipulated manually,
        // create mimetype information
        MediaType mediaType;
        try (InputStream is = this.newInputStream(ref);
            BufferedInputStream bis = new BufferedInputStream(is)) {
            mediaType = BackendUtils.getMimeType(bis, ref.getFileName());
        }
        if (mediaType != null) {
            // successful execution
            this.setMimeType(ref, mediaType);
            mimeType = mediaType.toString();
        } else {
            AbstractRepository.LOGGER.debug("Could not determine mimetype");
            mimeType = null;
        }
    }
    return mimeType;
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) MediaType(org.apache.tika.mime.MediaType)

Example 17 with RepositoryFileReference

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

the class AbstractRepository method setMimeType.

/**
 * Stores the mime type of the given file reference in a separate file
 * <p>
 * This method calls putContentToFile(), where the filename is appended with
 * Constants.SUFFIX_MIMETYPE and a null mime type. The latter indicates that
 * no "normal" file is stored.
 *
 * @param ref       the file reference
 * @param mediaType the mimeType
 */
protected void setMimeType(RepositoryFileReference ref, MediaType mediaType) throws IOException {
    RepositoryFileReference mimeFileRef = this.getMimeFileRef(ref);
    this.putContentToFile(mimeFileRef, mediaType.toString(), null);
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference)

Example 18 with RepositoryFileReference

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

the class AbstractRepository method getDefinitions.

@Override
public Definitions getDefinitions(DefinitionsChildId id) {
    RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(id);
    if (!exists(ref)) {
        return BackendUtils.createWrapperDefinitionsAndInitialEmptyElement(this, id);
    }
    try {
        InputStream is = RepositoryFactory.getRepository().newInputStream(ref);
        Unmarshaller u = JAXBSupport.createUnmarshaller();
        return (Definitions) u.unmarshal(is);
    } catch (Exception e) {
        LOGGER.error("Could not read content from file " + ref, e);
        throw new IllegalStateException(e);
    }
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Definitions(org.eclipse.winery.model.tosca.Definitions) Unmarshaller(javax.xml.bind.Unmarshaller) IOException(java.io.IOException)

Example 19 with RepositoryFileReference

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

the class WriterUtils method storeTypes.

public static void storeTypes(Path path, String namespace, String id) {
    LOGGER.debug("Store type: {}", id);
    try {
        MediaType mediaType = MediaTypes.MEDIATYPE_XSD;
        TImport.Builder builder = new TImport.Builder(Namespaces.XML_NS);
        builder.setNamespace(namespace);
        builder.setLocation(id + ".xsd");
        GenericImportId rid = new XSDImportId(namespace, id, false);
        TDefinitions definitions = BackendUtils.createWrapperDefinitions(rid);
        definitions.getImport().add(builder.build());
        CsarImporter.storeDefinitions(rid, definitions);
        RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(rid);
        List<File> files = Files.list(path).filter(Files::isRegularFile).map(Path::toFile).collect(Collectors.toList());
        for (File file : files) {
            BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file));
            RepositoryFileReference fileRef = new RepositoryFileReference(ref.getParent(), file.getName());
            RepositoryFactory.getRepository().putContentToFile(fileRef, stream, mediaType);
        }
    } catch (IllegalArgumentException | IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : XSDImportId(org.eclipse.winery.common.ids.definitions.imports.XSDImportId) TImport(org.eclipse.winery.model.tosca.TImport) GenericImportId(org.eclipse.winery.common.ids.definitions.imports.GenericImportId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) MediaType(org.apache.tika.mime.MediaType) Files(java.nio.file.Files) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions)

Example 20 with RepositoryFileReference

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

the class GenericImportResource method getFile.

@GET
@Path("{filename}")
public Response getFile(@PathParam("filename") @NonNull final String encodedFileName) {
    Objects.requireNonNull(encodedFileName);
    final Optional<TImport> theImport = ImportUtils.getTheImport((GenericImportId) id);
    if (!theImport.isPresent()) {
        return Response.status(Status.NOT_FOUND).build();
    }
    @Nullable final String location = theImport.get().getLocation();
    @NonNull String fileName = Util.URLdecode(encodedFileName);
    if (!fileName.equals(location)) {
        LOGGER.debug("Filename mismatch %s vs %s", fileName, location);
        return Response.status(Status.NOT_FOUND).build();
    }
    RepositoryFileReference ref = new RepositoryFileReference(this.id, location);
    return RestUtils.returnRepoPath(ref, null);
}
Also used : RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) NonNull(org.eclipse.jdt.annotation.NonNull) TImport(org.eclipse.winery.model.tosca.TImport) Nullable(org.eclipse.jdt.annotation.Nullable) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

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