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);
}
}
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());
}
}
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;
}
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);
}
}
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());
}
Aggregations