Search in sources :

Example 1 with InputStreamWrapper

use of org.jbei.ice.lib.entry.sequence.InputStreamWrapper in project ice by JBEI.

the class RemoteEntriesAsCSV method writeLocalEntries.

protected void writeLocalEntries(List<Long> entries, List<EntryFieldLabel> fields, CSVWriter writer, ZipOutputStream zos) {
    if (entries == null)
        return;
    SequenceDAO sequenceDAO = DAOFactory.getSequenceDAO();
    Configuration configuration = DAOFactory.getConfigurationDAO().get(ConfigurationKey.URI_PREFIX);
    String thisUrl = configuration == null ? "" : configuration.getValue();
    for (Long id : entries) {
        Entry entry = DAOFactory.getEntryDAO().get(id);
        String[] line = new String[fields.size() + 4];
        line[0] = thisUrl;
        line[1] = entry.getCreationTime().toString();
        line[2] = entry.getPartNumber();
        int i = 2;
        for (EntryFieldLabel field : fields) {
            line[i + 1] = EntryUtil.entryFieldToValue(entry, field);
            i += 1;
        }
        // write sequence to zip file
        long entryId = entry.getId();
        if (sequenceDAO.hasSequence(entryId)) {
            String name = entry.getPartNumber() + ".gb";
            try {
                Sequence sequence = sequenceDAO.getByEntry(entry);
                line[i + 1] = name;
                GenbankFormatter genbankFormatter = new GenbankFormatter(name);
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                genbankFormatter.format(sequence, byteStream);
                ByteArrayInputStream inputStream = new ByteArrayInputStream(byteStream.toByteArray());
                InputStreamWrapper wrapper = new InputStreamWrapper(inputStream, name);
                putZipEntry(wrapper, zos);
            } catch (Exception e) {
                line[i + 1] = "";
            }
        } else {
            line[i + 1] = "";
        }
        writer.writeNext(line);
    }
}
Also used : FeaturedDNASequence(org.jbei.ice.lib.dto.FeaturedDNASequence) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel) GenbankFormatter(org.jbei.ice.lib.entry.sequence.composers.formatters.GenbankFormatter) ZipEntry(java.util.zip.ZipEntry) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) SequenceDAO(org.jbei.ice.storage.hibernate.dao.SequenceDAO)

Example 2 with InputStreamWrapper

use of org.jbei.ice.lib.entry.sequence.InputStreamWrapper in project ice by JBEI.

the class FileResource method downloadSequence.

@GET
@Path("{partId}/sequence/{type}")
public Response downloadSequence(@PathParam("partId") final String partId, @PathParam("type") final String downloadType, @DefaultValue("-1") @QueryParam("remoteId") long remoteId, @QueryParam("sid") String sid) {
    if (StringUtils.isEmpty(sessionId))
        sessionId = sid;
    final String userId = getUserId(sessionId);
    if (remoteId != -1) {
        RemoteSequence sequence = new RemoteSequence(remoteId, Long.decode(partId));
        final InputStreamWrapper wrapper = sequence.get(downloadType);
        StreamingOutput stream = output -> IOUtils.copy(wrapper.getInputStream(), output);
        return addHeaders(Response.ok(stream), wrapper.getName());
    } else {
        InputStreamWrapper wrapper = new PartSequence(userId, partId).toFile(SequenceFormat.fromString(downloadType), true);
        StreamingOutput stream = output -> IOUtils.copy(wrapper.getInputStream(), output);
        return addHeaders(Response.ok(stream), wrapper.getName());
    }
}
Also used : RemoteSequence(org.jbei.ice.lib.net.RemoteSequence) AttachmentInfo(org.jbei.ice.lib.dto.entry.AttachmentInfo) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) ConfigurationSettings(org.jbei.ice.lib.config.ConfigurationSettings) Sequences(org.jbei.ice.lib.entry.sequence.Sequences) PartSequence(org.jbei.ice.lib.entry.sequence.PartSequence) Setting(org.jbei.ice.lib.dto.Setting) TraceSequences(org.jbei.ice.lib.entry.sequence.analysis.TraceSequences) ConfigurationKey(org.jbei.ice.lib.dto.ConfigurationKey) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Entries(org.jbei.ice.lib.entry.Entries) MediaType(javax.ws.rs.core.MediaType) DAOFactory(org.jbei.ice.storage.DAOFactory) Logger(org.jbei.ice.lib.common.logging.Logger) ByteArrayInputStream(java.io.ByteArrayInputStream) FileBulkUpload(org.jbei.ice.lib.bulkupload.FileBulkUpload) SequenceInfo(org.jbei.ice.lib.dto.entry.SequenceInfo) SequenceFormat(org.jbei.ice.lib.entry.sequence.SequenceFormat) EntryType(org.jbei.ice.lib.dto.entry.EntryType) Utils(org.jbei.ice.lib.utils.Utils) EntrySelection(org.jbei.ice.lib.entry.EntrySelection) Files(java.nio.file.Files) FileUtils(org.apache.commons.io.FileUtils) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) Collectors(java.util.stream.Collectors) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) Attachments(org.jbei.ice.lib.entry.attachment.Attachments) RemoteEntries(org.jbei.ice.lib.net.RemoteEntries) FormDataParam(org.glassfish.jersey.media.multipart.FormDataParam) List(java.util.List) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) Paths(java.nio.file.Paths) ShotgunSequenceDAO(org.jbei.ice.storage.hibernate.dao.ShotgunSequenceDAO) TraceSequence(org.jbei.ice.storage.model.TraceSequence) EntriesAsCSV(org.jbei.ice.lib.entry.EntriesAsCSV) InvalidFormatParserException(org.jbei.ice.lib.parsers.InvalidFormatParserException) ShotgunSequence(org.jbei.ice.storage.model.ShotgunSequence) InputStream(java.io.InputStream) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) PartSequence(org.jbei.ice.lib.entry.sequence.PartSequence) StreamingOutput(javax.ws.rs.core.StreamingOutput) RemoteSequence(org.jbei.ice.lib.net.RemoteSequence)

Example 3 with InputStreamWrapper

use of org.jbei.ice.lib.entry.sequence.InputStreamWrapper in project ice by JBEI.

the class EntriesAsCSV method customize.

public ByteArrayOutputStream customize(EntrySelection selection, SequenceFormat format, boolean onePerFolder) throws IOException {
    Entries retriever = new Entries(this.userId);
    this.entries = retriever.getEntriesFromSelectionContext(selection);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (ZipOutputStream zos = new ZipOutputStream(baos)) {
        for (long entryId : this.entries) {
            // get the entry
            Entry entry = DAOFactory.getEntryDAO().get(entryId);
            if (entry == null) {
                // write to csv file
                Logger.error("ERROR : no entry " + entryId);
                continue;
            }
            if (!sequenceDAO.hasSequence(entry.getId())) {
                continue;
            }
            // get the sequence
            InputStreamWrapper wrapper = new PartSequence(userId, Long.toString(entryId)).toFile(format, onePerFolder);
            if (wrapper == null) {
                Logger.error("ERROR : no sequence " + entryId);
                continue;
            }
            if (onePerFolder)
                wrapper.setName(entry.getPartNumber() + File.separatorChar + wrapper.getName());
            else
                wrapper.setName(wrapper.getName());
            putZipEntry(wrapper, zos);
        }
        this.includeSequences = false;
        writeList(selection.getFields().toArray(new EntryFieldLabel[0]));
        // write the csv file to zip file
        FileInputStream fis = new FileInputStream(csvPath.toFile());
        InputStreamWrapper wrapper = new InputStreamWrapper(fis, "entries.csv");
        putZipEntry(wrapper, zos);
    }
    return baos;
}
Also used : Entry(org.jbei.ice.storage.model.Entry) ZipEntry(java.util.zip.ZipEntry) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) ZipOutputStream(java.util.zip.ZipOutputStream) PartSequence(org.jbei.ice.lib.entry.sequence.PartSequence) EntryFieldLabel(org.jbei.ice.lib.dto.entry.EntryFieldLabel)

Example 4 with InputStreamWrapper

use of org.jbei.ice.lib.entry.sequence.InputStreamWrapper in project ice by JBEI.

the class EntriesAsCSV method writeZip.

private void writeZip(Set<Long> sequenceSet) {
    Path tmpPath = Paths.get(Utils.getConfigValue(ConfigurationKey.TEMPORARY_DIRECTORY));
    try {
        File tmpZip = File.createTempFile("zip-", ".zip", tmpPath.toFile());
        // out
        FileOutputStream fos = new FileOutputStream(tmpZip);
        ZipOutputStream zos = new ZipOutputStream(fos);
        // get sequence formats
        for (long entryId : sequenceSet) {
            for (String format : formats) {
                InputStreamWrapper wrapper = new PartSequence(userId, Long.toString(entryId)).toFile(SequenceFormat.fromString(format), true);
                putZipEntry(wrapper, zos);
            }
        }
        // write the csv file
        FileInputStream fis = new FileInputStream(csvPath.toFile());
        InputStreamWrapper wrapper = new InputStreamWrapper(fis, "entries.csv");
        putZipEntry(wrapper, zos);
        zos.close();
        csvPath = tmpZip.toPath();
    } catch (Exception e) {
        Logger.error(e);
    }
}
Also used : Path(java.nio.file.Path) InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) ZipOutputStream(java.util.zip.ZipOutputStream) PartSequence(org.jbei.ice.lib.entry.sequence.PartSequence)

Example 5 with InputStreamWrapper

use of org.jbei.ice.lib.entry.sequence.InputStreamWrapper in project ice by JBEI.

the class Attachments method getAttachmentByFileId.

/**
 * Retrieves a previously uploaded attachment to an entry by it's unique file identifier
 * This file identifier is assigned on upload
 *
 * @return wrapper around file byte array and name
 * @throws PermissionException if the attachment referenced by the fileId exists but
 *                             the user does not have read permissions on the entry associated with the attachment
 * @throws IOException         on exception retrieving file
 */
public InputStreamWrapper getAttachmentByFileId(String userId, String fileId) throws IOException {
    Attachment attachment = dao.getByFileId(fileId);
    if (attachment == null)
        return null;
    entryAuthorization.expectRead(userId, attachment.getEntry());
    String dataDir = Utils.getConfigValue(ConfigurationKey.DATA_DIRECTORY);
    byte[] bytes = Files.readAllBytes(Paths.get(dataDir, attachmentDirName, attachment.getFileId()));
    return new InputStreamWrapper(bytes, attachment.getFileName());
}
Also used : InputStreamWrapper(org.jbei.ice.lib.entry.sequence.InputStreamWrapper) Attachment(org.jbei.ice.storage.model.Attachment)

Aggregations

InputStreamWrapper (org.jbei.ice.lib.entry.sequence.InputStreamWrapper)10 EntryFieldLabel (org.jbei.ice.lib.dto.entry.EntryFieldLabel)5 PartSequence (org.jbei.ice.lib.entry.sequence.PartSequence)4 IOException (java.io.IOException)3 FeaturedDNASequence (org.jbei.ice.lib.dto.FeaturedDNASequence)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ZipEntry (java.util.zip.ZipEntry)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 javax.ws.rs (javax.ws.rs)2 MediaType (javax.ws.rs.core.MediaType)2 Response (javax.ws.rs.core.Response)2 StreamingOutput (javax.ws.rs.core.StreamingOutput)2 FileUtils (org.apache.commons.io.FileUtils)2