List of usage examples for javax.mail.util SharedFileInputStream SharedFileInputStream
public SharedFileInputStream(String file) throws IOException
SharedFileInputStream
for the named file From source file:se.inera.axel.shs.broker.messagestore.internal.FileMessageStoreService.java
private ShsMessage loadOriginalMessage(ShsMessageEntry entry) { if (!exists(entry)) { return null; }//from w w w .ja v a 2s . c o m File file = getFile(entry.getId()); try { return shsMessageMarshaller.unmarshal(new BufferedInputStream(new SharedFileInputStream(file))); } catch (IOException e) { // TODO decide which exception to throw throw new RuntimeException(e); } }
From source file:com.zimbra.cs.mime.Mime.java
public static void main(String[] args) throws MessagingException, IOException { String s = URLDecoder//from w w w. jav a2s . c o m .decode("Zimbra%20日本語化の考慮点.txt", "utf-8"); System.out.println(s); System.out.println(expandNumericCharacterReferences( "Zimbra%20日本語化の考慮点.txt@&;&#;&#x;&#༤7")); MimeMessage mm = new FixedMimeMessage(JMSession.getSession(), new SharedFileInputStream("C:\\Temp\\mail\\24245")); InputStream is = new RawContentMultipartDataSource(mm, new ContentType(mm.getContentType())) .getInputStream(); int num; byte buf[] = new byte[1024]; while ((num = is.read(buf)) != -1) { System.out.write(buf, 0, num); } }
From source file:org.apache.james.core.MimeMessageInputStreamSource.java
/** * Get an input stream to retrieve the data stored in the temporary file * * @return a <code>BufferedInputStream</code> containing the data */// www .j ava 2 s . c om public synchronized InputStream getInputStream() throws IOException { InputStream in; if (out.isInMemory()) { in = new SharedByteArrayInputStream(out.getData()); } else { in = new SharedFileInputStream(out.getFile()); } streams.add(in); return in; }
From source file:org.apache.james.mailbox.maildir.mail.model.MaildirMessage.java
/** * Parse message if needed//from ww w . j ava2s. c o m */ private synchronized void parseMessage() { if (parsed) return; SharedFileInputStream tmpMsgIn = null; try { tmpMsgIn = new SharedFileInputStream(messageName.getFile()); bodyStartOctet = bodyStartOctet(tmpMsgIn); // Disable line length... This should be handled by the smtp server // component and not the parser itself // https://issues.apache.org/jira/browse/IMAP-122 MimeConfig config = MimeConfig.custom().setMaxLineLen(-1).build(); final MimeTokenStream parser = new MimeTokenStream(config, new DefaultBodyDescriptorBuilder()); parser.setRecursionMode(RecursionMode.M_NO_RECURSE); parser.parse(tmpMsgIn.newStream(0, -1)); EntityState next = parser.next(); while (next != EntityState.T_BODY && next != EntityState.T_END_OF_STREAM && next != EntityState.T_START_MULTIPART) { next = parser.next(); } final MaximalBodyDescriptor descriptor = (MaximalBodyDescriptor) parser.getBodyDescriptor(); final String mediaType; final String mediaTypeFromHeader = descriptor.getMediaType(); final String subType; if (mediaTypeFromHeader == null) { mediaType = "text"; subType = "plain"; } else { mediaType = mediaTypeFromHeader; subType = descriptor.getSubType(); } propertyBuilder.setMediaType(mediaType); propertyBuilder.setSubType(subType); propertyBuilder.setContentID(descriptor.getContentId()); propertyBuilder.setContentDescription(descriptor.getContentDescription()); propertyBuilder.setContentLocation(descriptor.getContentLocation()); propertyBuilder.setContentMD5(descriptor.getContentMD5Raw()); propertyBuilder.setContentTransferEncoding(descriptor.getTransferEncoding()); propertyBuilder.setContentLanguage(descriptor.getContentLanguage()); propertyBuilder.setContentDispositionType(descriptor.getContentDispositionType()); propertyBuilder.setContentDispositionParameters(descriptor.getContentDispositionParameters()); propertyBuilder.setContentTypeParameters(descriptor.getContentTypeParameters()); // Add missing types final String codeset = descriptor.getCharset(); if (codeset == null) { if ("TEXT".equalsIgnoreCase(mediaType)) { propertyBuilder.setCharset("us-ascii"); } } else { propertyBuilder.setCharset(codeset); } final String boundary = descriptor.getBoundary(); if (boundary != null) { propertyBuilder.setBoundary(boundary); } if ("text".equalsIgnoreCase(mediaType)) { long lines = -1; final CountingInputStream bodyStream = new CountingInputStream(parser.getInputStream()); try { bodyStream.readAll(); lines = bodyStream.getLineCount(); } finally { IOUtils.closeQuietly(bodyStream); } next = parser.next(); if (next == EntityState.T_EPILOGUE) { final CountingInputStream epilogueStream = new CountingInputStream(parser.getInputStream()); try { epilogueStream.readAll(); lines += epilogueStream.getLineCount(); } finally { IOUtils.closeQuietly(epilogueStream); } } propertyBuilder.setTextualLineCount(lines); } } catch (IOException e) { // has successfully been parsen when appending, shouldn't give any // problems } catch (MimeException e) { // has successfully been parsen when appending, shouldn't give any // problems } finally { if (tmpMsgIn != null) { try { tmpMsgIn.close(); } catch (IOException e) { // ignore on close } } parsed = true; } }
From source file:org.apache.james.mailbox.store.StoreMessageManager.java
/** * @see org.apache.james.mailbox.MessageManager#appendMessage(java.io.InputStream, * java.util.Date, org.apache.james.mailbox.MailboxSession, boolean, * javax.mail.Flags)/*from ww w . j ava 2 s . c om*/ */ public long appendMessage(InputStream msgIn, Date internalDate, final MailboxSession mailboxSession, boolean isRecent, Flags flagsToBeSet) throws MailboxException { File file = null; TeeInputStream tmpMsgIn = null; BodyOffsetInputStream bIn = null; FileOutputStream out = null; SharedFileInputStream contentIn = null; if (!isWriteable(mailboxSession)) { throw new ReadOnlyException(getMailboxPath(), mailboxSession.getPathDelimiter()); } try { // Create a temporary file and copy the message to it. We will work // with the file as // source for the InputStream file = File.createTempFile("imap", ".msg"); out = new FileOutputStream(file); tmpMsgIn = new TeeInputStream(msgIn, out); bIn = new BodyOffsetInputStream(tmpMsgIn); // Disable line length... This should be handled by the smtp server // component and not the parser itself // https://issues.apache.org/jira/browse/IMAP-122 MimeConfig config = MimeConfig.custom().setMaxLineLen(-1).setMaxHeaderLen(-1).build(); final MimeTokenStream parser = new MimeTokenStream(config, new DefaultBodyDescriptorBuilder()); parser.setRecursionMode(RecursionMode.M_NO_RECURSE); parser.parse(bIn); final HeaderImpl header = new HeaderImpl(); EntityState next = parser.next(); while (next != EntityState.T_BODY && next != EntityState.T_END_OF_STREAM && next != EntityState.T_START_MULTIPART) { if (next == EntityState.T_FIELD) { header.addField(parser.getField()); } next = parser.next(); } final MaximalBodyDescriptor descriptor = (MaximalBodyDescriptor) parser.getBodyDescriptor(); final PropertyBuilder propertyBuilder = new PropertyBuilder(); final String mediaType; final String mediaTypeFromHeader = descriptor.getMediaType(); final String subType; if (mediaTypeFromHeader == null) { mediaType = "text"; subType = "plain"; } else { mediaType = mediaTypeFromHeader; subType = descriptor.getSubType(); } propertyBuilder.setMediaType(mediaType); propertyBuilder.setSubType(subType); propertyBuilder.setContentID(descriptor.getContentId()); propertyBuilder.setContentDescription(descriptor.getContentDescription()); propertyBuilder.setContentLocation(descriptor.getContentLocation()); propertyBuilder.setContentMD5(descriptor.getContentMD5Raw()); propertyBuilder.setContentTransferEncoding(descriptor.getTransferEncoding()); propertyBuilder.setContentLanguage(descriptor.getContentLanguage()); propertyBuilder.setContentDispositionType(descriptor.getContentDispositionType()); propertyBuilder.setContentDispositionParameters(descriptor.getContentDispositionParameters()); propertyBuilder.setContentTypeParameters(descriptor.getContentTypeParameters()); // Add missing types final String codeset = descriptor.getCharset(); if (codeset == null) { if ("TEXT".equalsIgnoreCase(mediaType)) { propertyBuilder.setCharset("us-ascii"); } } else { propertyBuilder.setCharset(codeset); } final String boundary = descriptor.getBoundary(); if (boundary != null) { propertyBuilder.setBoundary(boundary); } if ("text".equalsIgnoreCase(mediaType)) { final CountingInputStream bodyStream = new CountingInputStream(parser.getInputStream()); bodyStream.readAll(); long lines = bodyStream.getLineCount(); bodyStream.close(); next = parser.next(); if (next == EntityState.T_EPILOGUE) { final CountingInputStream epilogueStream = new CountingInputStream(parser.getInputStream()); epilogueStream.readAll(); lines += epilogueStream.getLineCount(); epilogueStream.close(); } propertyBuilder.setTextualLineCount(lines); } final Flags flags; if (flagsToBeSet == null) { flags = new Flags(); } else { flags = flagsToBeSet; // Check if we need to trim the flags trimFlags(flags, mailboxSession); } if (isRecent) { flags.add(Flags.Flag.RECENT); } if (internalDate == null) { internalDate = new Date(); } byte[] discard = new byte[4096]; while (tmpMsgIn.read(discard) != -1) { // consume the rest of the stream so everything get copied to // the file now // via the TeeInputStream } int bodyStartOctet = (int) bIn.getBodyStartOffset(); if (bodyStartOctet == -1) { bodyStartOctet = 0; } contentIn = new SharedFileInputStream(file); final int size = (int) file.length(); final List<MessageAttachment> attachments = extractAttachments(contentIn); final MailboxMessage message = createMessage(internalDate, size, bodyStartOctet, contentIn, flags, propertyBuilder, attachments); new QuotaChecker(quotaManager, quotaRootResolver, mailbox).tryAddition(1, size); return locker.executeWithLock(mailboxSession, getMailboxPath(), new MailboxPathLocker.LockAwareExecution<Long>() { @Override public Long execute() throws MailboxException { MessageMetaData data = appendMessageToStore(message, attachments, mailboxSession); SortedMap<Long, MessageMetaData> uids = new TreeMap<Long, MessageMetaData>(); uids.put(data.getUid(), data); dispatcher.added(mailboxSession, uids, getMailboxEntity()); return data.getUid(); } }, true); } catch (IOException e) { throw new MailboxException("Unable to parse message", e); } catch (MimeException e) { throw new MailboxException("Unable to parse message", e); } finally { IOUtils.closeQuietly(bIn); IOUtils.closeQuietly(tmpMsgIn); IOUtils.closeQuietly(out); IOUtils.closeQuietly(contentIn); // delete the temporary file if one was specified if (file != null) { if (!file.delete()) { // Don't throw an IOException. The message could be appended // and the temporary file // will be deleted hopefully some day } } } }
From source file:org.apache.james.server.core.MimeMessageInputStreamSource.java
/** * Get an input stream to retrieve the data stored in the temporary file * * @return a <code>BufferedInputStream</code> containing the data *//*from w ww. j a v a 2 s . c o m*/ @Override public synchronized InputStream getInputStream() throws IOException { InputStream in; if (out.isInMemory()) { in = new SharedByteArrayInputStream(out.getData()); } else { in = new SharedFileInputStream(out.getFile()); } streams.add(in); return in; }
From source file:org.eclipse.ecr.automation.server.jaxrs.io.MultiPartFormRequestReader.java
public ExecutionRequest readFrom(Class<ExecutionRequest> arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> headers, InputStream in) throws IOException, WebApplicationException { ExecutionRequest req = null;//from w w w . ja v a2 s. c o m try { List<String> ctypes = headers.get("Content-Type"); String ctype = ctypes.get(0); // we need to copy first the stream into a file otherwise it may // happen that // javax.mail fail to receive some parts - I am not sure why - // perhaps the stream is no more available when javax.mail need it? File tmp = File.createTempFile("nx-automation-mp-upload-", ".tmp"); FileUtils.copyToFile(in, tmp); // get the input from the saved file in = new SharedFileInputStream(tmp); try { MimeMultipart mp = new MimeMultipart(new InputStreamDataSource(in, ctype)); BodyPart part = mp.getBodyPart(0); // use content ids InputStream pin = part.getInputStream(); req = JsonRequestReader.readRequest(pin, headers); int cnt = mp.getCount(); if (cnt == 2) { // a blob req.setInput(readBlob(request, mp.getBodyPart(1))); } else if (cnt > 2) { // a blob list BlobList blobs = new BlobList(); for (int i = 1; i < cnt; i++) { blobs.add(readBlob(request, mp.getBodyPart(i))); } req.setInput(blobs); } else { log.error("Not all parts received."); for (int i = 0; i < cnt; i++) { log.error("Received parts: " + mp.getBodyPart(i).getHeader("Content-ID")[0] + " -> " + mp.getBodyPart(i).getContentType()); } throw ExceptionHandler.newException( new IllegalStateException("Received only " + cnt + " part in a multipart request")); } } finally { try { in.close(); } catch (IOException e) { // do nothing } tmp.delete(); } } catch (Throwable e) { throw ExceptionHandler.newException("Failed to parse multipart request", e); } return req; }
From source file:se.inera.axel.shs.camel.ShsMessageTypeConverter.java
@Converter public static ShsMessage fileToShsMessage(File file) throws Exception { log.trace("Converting File to ShsMessage"); return inputStreamToShsMessage(new SharedFileInputStream(file)); }