Search in sources :

Example 6 with RepositoryFileReference

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

the class IRepository method getMimeType.

/**
 * Returns the mimetype belonging to the reference.
 *
 * @param ref the reference to the file
 * @return the mimetype as string
 * @throws IOException           if something goes wrong
 * @throws IllegalStateException if an internal error occurs, which is not an IOException
 */
default String getMimeType(RepositoryFileReference ref) throws IOException {
    RepositoryFileReference mimeFileRef = ref.setFileName(ref.getFileName() + Constants.SUFFIX_MIMETYPE);
    String mimeType;
    if (this.exists(mimeFileRef)) {
        InputStream is = this.newInputStream(mimeFileRef);
        mimeType = IOUtils.toString(is, StandardCharsets.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 {
            LOGGER.debug("Could not determine mimetype");
            mimeType = null;
        }
    }
    return mimeType;
}
Also used : RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) MediaType(org.apache.tika.mime.MediaType)

Example 7 with RepositoryFileReference

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

the class XmlRepositoryTest method subDirectoryExpandedCorrectly.

@Test
public void subDirectoryExpandedCorrectly() throws Exception {
    ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http://www.example.org", "at", false);
    ArtifactTemplateSourceDirectoryId artifactTemplateSourceDirectoryId = new ArtifactTemplateSourceDirectoryId(artifactTemplateId);
    final Path subDirectories = Paths.get("dir1", "dir2", "dir3");
    RepositoryFileReference ref = new RepositoryFileReference(artifactTemplateSourceDirectoryId, subDirectories, "test.txt");
    final IRepository repository = this.repository;
    final Path expected = Paths.get("artifacttemplates", "http%3A%2F%2Fwww.example.org", "at", "source", "dir1", "dir2", "dir3", "test.txt");
    assertEquals(expected, repository.getRepositoryRoot().relativize(repository.ref2AbsolutePath(ref)));
}
Also used : Path(java.nio.file.Path) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) ArtifactTemplateSourceDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId) IRepository(org.eclipse.winery.repository.backend.IRepository) Test(org.junit.jupiter.api.Test)

Example 8 with RepositoryFileReference

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

the class XmlRepositoryTest method getContainedFilesProducedCorrectPath.

@Test
public void getContainedFilesProducedCorrectPath() throws Exception {
    ArtifactTemplateId artifactTemplateWithFilesAndSourcesId = new ArtifactTemplateId("http://plain.winery.opentosca.org/artifacttemplates", "ArtifactTemplateWithFilesAndSources", false);
    DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(artifactTemplateWithFilesAndSourcesId);
    SortedSet<RepositoryFileReference> files = repository.getContainedFiles(fileDir);
    for (RepositoryFileReference ref : files) {
        assertFalse(ref.getSubDirectory().isPresent() && ref.getSubDirectory().get().toString().equals(""), "File " + ref.toString() + " contains empty sub directory");
    }
}
Also used : ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) ArtifactTemplateSourceDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) Test(org.junit.jupiter.api.Test)

Example 9 with RepositoryFileReference

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

the class XmlRepositoryTest method allFilesCorrectlyImported.

@Test
public void allFilesCorrectlyImported() throws Exception {
    this.setRevisionTo("5b7f106ab79a9ba137ece9167a79753dfc64ac84");
    ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http://www.example.org", "at", false);
    ArtifactTemplateSourceDirectoryId artifactTemplateSourceDirectoryId = new ArtifactTemplateSourceDirectoryId(artifactTemplateId);
    final Path subDirectories = Paths.get("dir1", "dir2", "dir3");
    Path workingDir = Files.createTempDirectory("winery");
    final Path subDir = workingDir.resolve(subDirectories);
    Files.createDirectories(subDir);
    Path tempFile = subDir.resolve("test.txt");
    Files.createFile(tempFile);
    BackendUtils.importDirectory(workingDir, this.repository, artifactTemplateSourceDirectoryId);
    RepositoryFileReference ref = new RepositoryFileReference(artifactTemplateSourceDirectoryId, subDirectories, "test.txt");
    assertTrue(repository.exists(ref));
}
Also used : Path(java.nio.file.Path) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) ArtifactTemplateSourceDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateSourceDirectoryId) Test(org.junit.jupiter.api.Test)

Example 10 with RepositoryFileReference

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

the class BackendUtilsTest method synchronizeReferencesDoesNotRemoveExistentFileAndDoesNotRemoveUrls.

@Test
public void synchronizeReferencesDoesNotRemoveExistentFileAndDoesNotRemoveUrls() throws Exception {
    ArtifactTemplateId artifactTemplateId = new ArtifactTemplateId("http://example.org", "test-artifact-template", false);
    // alternative test implementation: Use git-based repository
    // this test at hand is closer to the implementation, but easier to write
    IRepository repository = mock(IRepository.class);
    ArtifactTemplateFilesDirectoryId artifactTemplateFilesDirectoryId = new ArtifactTemplateFilesDirectoryId(artifactTemplateId);
    SortedSet<RepositoryFileReference> containedReferences = new TreeSet<>();
    RepositoryFileReference repositoryFileReference = new RepositoryFileReference(artifactTemplateId, "exists.txt");
    containedReferences.add(repositoryFileReference);
    when(repository.getContainedFiles(artifactTemplateFilesDirectoryId)).thenReturn(containedReferences);
    TArtifactTemplate artifactTemplate = createArtifactTemplateWithReferenceToAnUrlAndExistentFile();
    when(repository.getElement(artifactTemplateId)).thenReturn(artifactTemplate);
    TArtifactTemplate synchronizedArtifactTemplate = BackendUtils.synchronizeReferences(repository, artifactTemplateId);
    assertEquals(createArtifactTemplateWithReferenceToAnUrlAndExistentFile(), synchronizedArtifactTemplate);
}
Also used : ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) RepositoryFileReference(org.eclipse.winery.repository.common.RepositoryFileReference) TreeSet(java.util.TreeSet) TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) ArtifactTemplateId(org.eclipse.winery.model.ids.definitions.ArtifactTemplateId) Test(org.junit.jupiter.api.Test)

Aggregations

RepositoryFileReference (org.eclipse.winery.repository.common.RepositoryFileReference)81 IOException (java.io.IOException)33 Path (java.nio.file.Path)26 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)17 QName (javax.xml.namespace.QName)14 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)14 InputStream (java.io.InputStream)13 ArrayList (java.util.ArrayList)12 ArtifactTemplateId (org.eclipse.winery.model.ids.definitions.ArtifactTemplateId)12 IRepository (org.eclipse.winery.repository.backend.IRepository)12 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)11 PlanId (org.eclipse.winery.model.ids.elements.PlanId)10 PlansId (org.eclipse.winery.model.ids.elements.PlansId)10 XmlId (org.eclipse.winery.model.ids.XmlId)9 Test (org.junit.jupiter.api.Test)9 DefinitionsChildId (org.eclipse.winery.model.ids.definitions.DefinitionsChildId)8 MediaType (org.apache.tika.mime.MediaType)7 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)7 BufferedInputStream (java.io.BufferedInputStream)6 HashMap (java.util.HashMap)6