/** * Writes the artifact content to the given working path. * @param workPath * @param content * @throws SrampArchiveException */ private void writeContent(File workPath, InputStream content) throws SrampArchiveException { OutputStream outStream = null; try { outStream = new FileOutputStream(workPath); IOUtils.copy(content, outStream); } catch (Throwable t) { throw new SrampArchiveException(Messages.i18n.format("ERROR_WRITING_CONTENT"), t); //$NON-NLS-1$ } finally { IOUtils.closeQuietly(content); IOUtils.closeQuietly(outStream); } }
/** * Creates a new, empty S-RAMP archive. * @throws SrampArchiveException */ public SrampArchive() throws SrampArchiveException { workDir = null; this.originalFile = null; try { workDir = createWorkDir(); } catch (IOException e) { if (workDir != null && workDir.exists()) { try { FileUtils.deleteDirectory(workDir); } catch (IOException e1) { } } throw new SrampArchiveException(Messages.i18n.format("FAILED_TO_CREATE_WORK_DIR"), e); //$NON-NLS-1$ } }
/** * Adds an entry to the S-RAMP archive. This method will close the content * {@link InputStream}. * @param path the path in the archive (usually just the name of the artifact) * @param metaData the artifact meta-data * @param content the entry content (or null if a meta-data only entry) * @throws SrampArchiveException */ public void addEntry(String path, BaseArtifactType metaData, InputStream content) throws SrampArchiveException { if (path == null) throw new SrampArchiveException(Messages.i18n.format("INVALID_ENTRY_PATH")); //$NON-NLS-1$ if (metaData == null) throw new SrampArchiveException(Messages.i18n.format("MISSING_META_DATA")); //$NON-NLS-1$ File metaDataFile = new File(this.workDir, path + ".atom"); //$NON-NLS-1$ File contentFile = new File(this.workDir, path); if (metaDataFile.exists()) throw new SrampArchiveException(Messages.i18n.format("ARCHIVE_ALREADY_EXISTS")); //$NON-NLS-1$ // Create any required parent directories metaDataFile.getParentFile().mkdirs(); if (content != null) writeContent(contentFile, content); try { SrampArchiveJaxbUtils.writeMetaData(metaDataFile, metaData); } catch (JAXBException e) { throw new SrampArchiveException(e); } }
/** * Creates an S-RAMP archive from an existing archive file. * @param file * @throws SrampArchiveException */ public SrampArchive(File file) throws SrampArchiveException { this(); this.originalFile = file; this.shouldDeleteOriginalFile = false; try { ArchiveUtils.unpackToWorkDir(this.originalFile, this.workDir); } catch (IOException e) { if (this.workDir != null) { try { FileUtils.deleteDirectory(this.workDir); } catch (IOException e1) { } } throw new SrampArchiveException(Messages.i18n.format("FAILED_TO_UNPACK_ARCHIVE_TO_WORK_DIR"), e); //$NON-NLS-1$ } }
/** * Creates an S-RAMP archive from an {@link InputStream}. This will consume and close the * {@link InputStream}, creating a temporary local file that will be used as the basis for * the archive input. * @param input * @throws SrampArchiveException */ public SrampArchive(InputStream input) throws SrampArchiveException { this(); this.originalFile = null; this.shouldDeleteOriginalFile = true; try { this.originalFile = File.createTempFile("s-ramp-archive", ".zip"); //$NON-NLS-1$ //$NON-NLS-2$ copyZipStream(input, this.originalFile); ArchiveUtils.unpackToWorkDir(this.originalFile, this.workDir); } catch (IOException e) { if (this.workDir != null) { try { FileUtils.deleteDirectory(this.workDir); } catch (IOException e1) { } } if (this.originalFile != null && this.originalFile.exists()) { this.originalFile.delete(); } throw new SrampArchiveException(Messages.i18n.format("FAILED_TO_UNPACK_ARCHIVE_TO_WORK_DIR"), e); //$NON-NLS-1$ } }
throw new SrampArchiveException(Messages.i18n.format("ERROR_PACKING_ARCHIVE"), t); //$NON-NLS-1$
/** * Updates an existing entry in the S-RAMP archive. This method will close the content * {@link InputStream}. * @param entry the archive entry (or null if just udpating the content) * @param content the entry content (or null if just updating meta data) * @throws SrampArchiveException */ public void updateEntry(SrampArchiveEntry entry, InputStream content) throws SrampArchiveException { if (entry.getPath() == null) throw new SrampArchiveException(Messages.i18n.format("INVALID_ENTRY_PATH")); //$NON-NLS-1$ File contentFile = new File(this.workDir, entry.getPath()); File metaDataFile = new File(this.workDir, entry.getPath() + ".atom"); //$NON-NLS-1$ if (content != null) writeContent(contentFile, content); if (entry.getMetaData() != null) { try { SrampArchiveJaxbUtils.writeMetaData(metaDataFile, entry.getMetaData()); } catch (JAXBException e) { throw new SrampArchiveException(e); } } }