View Javadoc
1   /**
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.swid.plugin.entry.archive;
28  
29  import gov.nist.secauto.swid.builder.resource.PathRelativizer;
30  import gov.nist.secauto.swid.plugin.entry.FileEntry;
31  
32  import org.apache.maven.artifact.Artifact;
33  import org.codehaus.plexus.archiver.ArchiveEntry;
34  import org.codehaus.plexus.components.io.fileselectors.FileInfo;
35  import org.codehaus.plexus.components.io.resources.PlexusIoFileResource;
36  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
37  
38  import java.io.File;
39  import java.io.IOException;
40  import java.io.InputStream;
41  import java.nio.file.Path;
42  import java.util.List;
43  import java.util.Objects;
44  
45  public class ArchiveFileEntry implements FileEntry {
46  
47    private final ArchiveEntry archiveEntry;
48    private final Artifact artifact;
49  
50    /**
51     * Construct a new file entry based on archive data.
52     * 
53     * @param archiveEntry
54     *          the archive metadata
55     * @param artifact
56     *          optional artifact metadata
57     */
58    public ArchiveFileEntry(ArchiveEntry archiveEntry, Artifact artifact) {
59      Objects.requireNonNull(archiveEntry, "archiveEntry");
60      this.archiveEntry = archiveEntry;
61      this.artifact = artifact;
62    }
63  
64    @Override
65    public Long getSize() {
66      PlexusIoResource resource = archiveEntry.getResource();
67  
68      File file = null;
69      if (resource instanceof PlexusIoFileResource) {
70        file = ((PlexusIoFileResource) resource).getFile();
71      }
72  
73      return (file == null) ? null : file.length();
74    }
75  
76    @Override
77    public InputStream getInputStream() throws IOException {
78      return archiveEntry.getInputStream();
79    }
80  
81    @Override
82    public String getVersion() {
83      String retval = null;
84      if (artifact != null) {
85        retval = artifact.getVersion();
86      }
87      return retval;
88    }
89  
90    @Override
91    public Path getPath() {
92      PlexusIoResource resource = archiveEntry.getResource();
93  
94      File file = null;
95      if (resource instanceof PlexusIoFileResource) {
96        file = ((PlexusIoFileResource) resource).getFile();
97      }
98  
99      return (file == null) ? null : file.toPath();
100   }
101 
102   @Override
103   public String getOutputRelativePath() {
104     return archiveEntry.getName();
105   }
106 
107   @Override
108   public List<String> getRelativePathSegements(String swidTagPath) {
109     return PathRelativizer.relativize(swidTagPath, getOutputRelativePath());
110   }
111 
112   @Override
113   public FileInfo asFileInfo() {
114     PlexusIoResource resource = archiveEntry.getResource();
115     return resource;
116   }
117 
118 }