List of usage examples for java.io PipedInputStream PipedInputStream
public PipedInputStream()
PipedInputStream
so that it is not yet #connect(java.io.PipedOutputStream) connected . From source file:org.springframework.integration.sftp.outbound.SftpServerOutboundTests.java
@Test public void testInt3047ConcurrentSharedSession() throws Exception { final Session<?> session1 = this.sessionFactory.getSession(); final Session<?> session2 = this.sessionFactory.getSession(); final PipedInputStream pipe1 = new PipedInputStream(); PipedOutputStream out1 = new PipedOutputStream(pipe1); final PipedInputStream pipe2 = new PipedInputStream(); PipedOutputStream out2 = new PipedOutputStream(pipe2); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); Executors.newSingleThreadExecutor().execute(() -> { try {//from w w w . j a v a2 s. c o m session1.write(pipe1, "foo.txt"); } catch (IOException e) { e.printStackTrace(); } latch1.countDown(); }); Executors.newSingleThreadExecutor().execute(() -> { try { session2.write(pipe2, "bar.txt"); } catch (IOException e) { e.printStackTrace(); } latch2.countDown(); }); out1.write('a'); out2.write('b'); out1.write('c'); out2.write('d'); out1.write('e'); out2.write('f'); out1.close(); out2.close(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); assertTrue(latch2.await(10, TimeUnit.SECONDS)); ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); ByteArrayOutputStream bos2 = new ByteArrayOutputStream(); session1.read("foo.txt", bos1); session2.read("bar.txt", bos2); assertEquals("ace", new String(bos1.toByteArray())); assertEquals("bdf", new String(bos2.toByteArray())); session1.remove("foo.txt"); session2.remove("bar.txt"); session1.close(); session2.close(); }
From source file:com.dtolabs.rundeck.core.execution.impl.jsch.JschNodeExecutor.java
public NodeExecutorResult executeCommand(final ExecutionContext context, final String[] command, final INodeEntry node) { if (null == node.getHostname() || null == node.extractHostname()) { return NodeExecutorResultImpl.createFailure(StepFailureReason.ConfigurationFailure, "Hostname must be set to connect to remote node '" + node.getNodename() + "'", node); }//from w w w . j av a 2s . c om final ExecutionListener listener = context.getExecutionListener(); final Project project = new Project(); AntSupport.addAntBuildListener(listener, project); boolean success = false; final ExtSSHExec sshexec; //perform jsch sssh command final NodeSSHConnectionInfo nodeAuthentication = new NodeSSHConnectionInfo(node, framework, context); final int timeout = nodeAuthentication.getSSHTimeout(); try { sshexec = SSHTaskBuilder.build(node, command, project, context.getDataContext(), nodeAuthentication, context.getLoglevel(), listener); } catch (SSHTaskBuilder.BuilderException e) { return NodeExecutorResultImpl.createFailure(StepFailureReason.ConfigurationFailure, e.getMessage(), node); } //Sudo support final ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactory() { public Thread newThread(Runnable r) { return new Thread(null, r, "SudoResponder " + node.getNodename() + ": " + System.currentTimeMillis()); } }); final Future<ResponderTask.ResponderResult> responderFuture; final SudoResponder sudoResponder = SudoResponder.create(node, framework, context); Runnable responderCleanup = null; if (sudoResponder.isSudoEnabled() && sudoResponder.matchesCommandPattern(command[0])) { final DisconnectResultHandler resultHandler = new DisconnectResultHandler(); //configure two piped i/o stream pairs, to connect to the input/output of the SSH connection final PipedInputStream responderInput = new PipedInputStream(); final PipedOutputStream responderOutput = new PipedOutputStream(); final PipedInputStream jschInput = new PipedInputStream(); //lead pipe allows connected inputstream to close and not hang the writer to this stream final PipedOutputStream jschOutput = new LeadPipeOutputStream(); try { responderInput.connect(jschOutput); jschInput.connect(responderOutput); } catch (IOException e) { return NodeExecutorResultImpl.createFailure(StepFailureReason.IOFailure, e.getMessage(), node); } //first sudo prompt responder ResponderTask responder = new ResponderTask(sudoResponder, responderInput, responderOutput, resultHandler); /** * Callable will be executed by the ExecutorService */ final Callable<ResponderTask.ResponderResult> responderResultCallable; //if 2nd responder final SudoResponder sudoResponder2 = SudoResponder.create(node, framework, context, SUDO2_OPT_PREFIX, DEFAULT_SUDO2_PASSWORD_OPTION, DEFAULT_SUDO2_COMMAND_PATTERN); if (sudoResponder2.isSudoEnabled() && sudoResponder2.matchesCommandPattern(CLIUtils.generateArgline(null, command, false))) { logger.debug("Enable second sudo responder"); sudoResponder2.setDescription("Second " + SudoResponder.DEFAULT_DESCRIPTION); sudoResponder.setDescription("First " + SudoResponder.DEFAULT_DESCRIPTION); //sequence of the first then the second sudo responder responderResultCallable = responder.createSequence(sudoResponder2); } else { responderResultCallable = responder; } //set up SSH execution sshexec.setAllocatePty(true); sshexec.setInputStream(jschInput); sshexec.setSecondaryStream(jschOutput); sshexec.setDisconnectHolder(resultHandler); responderFuture = executor.submit(responderResultCallable); //close streams after responder is finished responderCleanup = new Runnable() { public void run() { logger.debug("SudoResponder shutting down..."); try { responderInput.close(); } catch (IOException e) { e.printStackTrace(); } try { responderOutput.flush(); responderOutput.close(); } catch (IOException e) { e.printStackTrace(); } //executor pool shutdown executor.shutdownNow(); } }; executor.submit(responderCleanup); } else { responderFuture = null; } if (null != context.getExecutionListener()) { context.getExecutionListener().log(3, "Starting SSH Connection: " + nodeAuthentication.getUsername() + "@" + node.getHostname() + " (" + node.getNodename() + ")"); } String errormsg = null; FailureReason failureReason = null; try { sshexec.execute(); success = true; } catch (BuildException e) { final ExtractFailure extractJschFailure = extractFailure(e, node, timeout, framework); errormsg = extractJschFailure.getErrormsg(); failureReason = extractJschFailure.getReason(); context.getExecutionListener().log(0, errormsg); } if (null != responderCleanup) { responderCleanup.run(); } shutdownAndAwaitTermination(executor); if (null != responderFuture) { try { logger.debug("Waiting 5 seconds for responder future result"); final ResponderTask.ResponderResult result = responderFuture.get(5, TimeUnit.SECONDS); logger.debug("Responder result: " + result); if (!result.isSuccess() && !result.isInterrupted()) { context.getExecutionListener().log(0, result.getResponder().toString() + " failed: " + result.getFailureReason()); } } catch (InterruptedException e) { //ignore } catch (java.util.concurrent.ExecutionException e) { e.printStackTrace(); } catch (TimeoutException e) { //ignore } } final int resultCode = sshexec.getExitStatus(); if (success) { return NodeExecutorResultImpl.createSuccess(node); } else { return NodeExecutorResultImpl.createFailure(failureReason, errormsg, node, resultCode); } }
From source file:org.kepler.ssh.SshExec.java
/** * Execute a command on the remote machine and expect a password/passphrase * question from the command. The stream <i>streamOut</i> should be provided * to get the output and errors merged. <i>streamErr</i> is not used in this * method (it will be empty string finally). * /*w w w. ja v a 2s .c om*/ * @return exit code of command if execution succeeded, * @throws ExecTimeoutException * if the command failed because of timeout * @throws SshException * if an error occurs for the ssh connection during the command * execution Note: in this method, the SSH Channel is forcing a * pseudo-terminal allocation {see setPty(true)} to allow remote * commands to read something from their stdin (i.e. from us * here), thus, (1) remote environment is not set from * .bashrc/.cshrc and (2) stdout and stderr come back merged in * one stream. */ public int executeCmd(String command, OutputStream streamOut, OutputStream streamErr, String thirdPartyTarget) throws ExecException { int exitCode = 0; String cmd = forcedCleanUp ? cleanUpInfoCmd + command : command; openConnection(); // get the pwd to the third party (and perform authentication if not yet // done) String pwd = SshSession.getPwdToThirdParty(thirdPartyTarget); // create a piped stream to feed the input of the remote exec channel PipedInputStream pis = null; PipedOutputStream pos = null; if (pwd != null) { try { pis = new PipedInputStream(); pos = new PipedOutputStream(pis); } catch (IOException ex) { log.error("Error when creating the piped stream for password feededing: " + ex); } } // At this point we have an opened session to the remote machine. // But session may be down, so this complex trial cycle here. boolean tryagain = true; while (tryagain) { tryagain = false; InputStream in = null; try { pwd = SshSession.getPwdToThirdParty(thirdPartyTarget); ChannelExec channel = null; _streamReaderThread readerThread; synchronized (session) { channel = (ChannelExec) jschSession.openChannel("exec"); if (isDebugging) log.debug("pseudoTerminal=" + pseudoTerminal); channel.setPty(pseudoTerminal); channel.setCommand(cmd); // channel.setOutputStream(streamOut); // use rather // getInputStream and read it channel.setErrStream(streamErr); // Useless!! stderr goes // into stdout channel.setInputStream(pis); // remote command will read // from our stream in = channel.getInputStream(); // start output processing thread // it checks for timeout too readerThread = new _streamReaderThread(channel, in, streamOut, pwd, pos); readerThread.start(); channel.connect(); // command starts here but we get back // the control // this thread runs further but reading of pis by the remote // process may block it } if (isDebugging) { log.debug("Started remote execution of command: " + command); } // wait for the reader thread to finish // It will timeout at the latest if the command does not finish // 3 ways to finish: // - command terminates // - timeout // - IOException when reading the command's output or writing // the caller's output readerThread.join(); // on timeout finish here with a nice Exception if (readerThread.timeoutHappened()) { log.error("Timeout: " + timeout + "s elapsed for command " + command); // BUG?: disconnect does not kill the remote process!!! // Clean-up should be done somehow channel.disconnect(); if (forcedCleanUp) { // time for clean-up ;-) kill(readerThread.getProcessID(), true); } throw new ExecTimeoutException(command); } // if we cannot process output, still wait for the channel to be // closed // !!! This can lead to hang-up !!! while (!channel.isClosed()) { try { Thread.sleep(500); } catch (Exception e) { } } // command completed successfully if (isDebugging) log.debug("Command execution terminated, now waiting for the channel to be closed."); if (isDebugging) log.debug("Channel closed down, now get the exit status."); // this is the wrong test if the remote OS is OpenVMS, // but there doesn't seem to be a way to detect it. // Note: it must be called only when channel.isClosed() becomes // true. // Otherwise it may return -1, being not yet set to the exit // code exitCode = channel.getExitStatus(); if (isDebugging) log.debug("Exit status = " + exitCode + ". Now disconnect channel."); // Note: The jsch source suggests that when the exit code is // set, the // package disconnects the channel, so the extra call to // disconnect() // may be unnecessary. But I inherited this way and am tired of // debugging... channel.disconnect(); if (exitCode != 0 && forcedCleanUp) { // what is sure is sure ;-) kill(readerThread.getProcessID(), true); } } catch (JSchException ex) { if (ex.toString().indexOf("session is down") > -1) { log.error("Session to " + session.getUser() + "@" + session.getHost() + " is down, try connect again"); closeConnection(); openConnection(); tryagain = true; } else { throw new SshException("JSchException caught at command: " + command + "\n" + ex); } } catch (Exception e) { throw new SshException("Exception caught at command: " + command + "\n" + e); } } // end while return exitCode; }
From source file:fi.hip.sicx.jclouds.JCloudClient.java
/** * Returns OutputStream that can be used to write data to the * specified filename in the cloud. The size of the file has to be * known and given.//from w w w . j a v a 2 s . c om * * @param fileInTheCloud Name of the file in the cloud * @param datasize Size of the file * @param sco observer * @return OutputStream that can be used to write data to the file in the cloud */ public OutputStream writeData(String infileInTheCloud, int indatasize, StorageClientObserver sco) { // Save parameters this.datasize = indatasize; this.fileInTheCloud = infileInTheCloud; // Filesystem is missing this feature so this is a workaround for now if (this.provider.equals("filesystem")) { // Create temporary test file String body = "sixc_secret_data1" + infileInTheCloud; String ending = ".tmp"; try { this.fs_outfile = File.createTempFile(body, ending, null); this.fs_outstream = new FileOutputStream(this.fs_outfile); this.dos = new DataOutputStream(this.fs_outstream); // This is needed to know the number of bytes // written } catch (IOException e) { e.printStackTrace(); } return this.dos; } // Lets make pipe where data can be written this.cis = null; this.in = new PipedInputStream(); this.out = null; try { this.out = new PipedOutputStream(this.in); this.dos = new DataOutputStream(this.out); // This is needed to know the number of bytes written } catch (IOException e) { e.printStackTrace(); } this.writeToBlob = blobStore.blobBuilder(fileInTheCloud).payload(in).contentLength(this.datasize).build(); this.responses = Maps.newHashMap(); this.responses.put(this.writeToBlob, context.getAsyncBlobStore().putBlob(containerName, writeToBlob)); return dos; }
From source file:org.whitesource.agent.utils.ZipUtils.java
private static void fillExportStreamDecompress(String text, StringBuilder stringBuilder) { try {/*from ww w . j av a2s . c om*/ try (PipedInputStream pipedInputStream = new PipedInputStream()) { try (PipedOutputStream pipedOutputStream = new PipedOutputStream()) { pipedInputStream.connect(pipedOutputStream); Runnable producer = new Runnable() { @Override public void run() { produceDecompressData(text, pipedOutputStream); } }; Runnable consumer = new Runnable() { @Override public void run() { consumeDecompressData(pipedInputStream, stringBuilder); } }; transferData(producer, consumer); } } } catch (IOException e) { // logger.error("Failed to decompress : ", e); } }
From source file:freenet.client.ArchiveManager.java
/** * Extract data to cache. Call synchronized on ctx. * @param key The key the data was fetched from. * @param archiveType The archive type. Must be Metadata.ARCHIVE_ZIP | Metadata.ARCHIVE_TAR. * @param data The actual data fetched.// w ww .j av a 2 s. co m * @param archiveContext The context for the whole fetch process. * @param ctx The ArchiveStoreContext for this key. * @param element A particular element that the caller is especially interested in, or null. * @param callback A callback to be called if we find that element, or if we don't. * @throws ArchiveFailureException If we could not extract the data, or it was too big, etc. * @throws ArchiveRestartException * @throws ArchiveRestartException If the request needs to be restarted because the archive * changed. */ public void extractToCache(FreenetURI key, ARCHIVE_TYPE archiveType, COMPRESSOR_TYPE ctype, final Bucket data, ArchiveContext archiveContext, ArchiveStoreContext ctx, String element, ArchiveExtractCallback callback, ClientContext context) throws ArchiveFailureException, ArchiveRestartException { logMINOR = Logger.shouldLog(LogLevel.MINOR, this); MutableBoolean gotElement = element != null ? new MutableBoolean() : null; if (logMINOR) Logger.minor(this, "Extracting " + key); ctx.removeAllCachedItems(this); // flush cache anyway final long expectedSize = ctx.getLastSize(); final long archiveSize = data.size(); /** Set if we need to throw a RestartedException rather than returning success, * after we have unpacked everything. */ boolean throwAtExit = false; if ((expectedSize != -1) && (archiveSize != expectedSize)) { throwAtExit = true; ctx.setLastSize(archiveSize); } byte[] expectedHash = ctx.getLastHash(); if (expectedHash != null) { byte[] realHash; try { realHash = BucketTools.hash(data); } catch (IOException e) { throw new ArchiveFailureException("Error reading archive data: " + e, e); } if (!Arrays.equals(realHash, expectedHash)) throwAtExit = true; ctx.setLastHash(realHash); } if (archiveSize > archiveContext.maxArchiveSize) throw new ArchiveFailureException( "Archive too big (" + archiveSize + " > " + archiveContext.maxArchiveSize + ")!"); else if (archiveSize <= 0) throw new ArchiveFailureException("Archive too small! (" + archiveSize + ')'); else if (logMINOR) Logger.minor(this, "Container size (possibly compressed): " + archiveSize + " for " + data); InputStream is = null; try { final ExceptionWrapper wrapper; if ((ctype == null) || (ARCHIVE_TYPE.ZIP == archiveType)) { if (logMINOR) Logger.minor(this, "No compression"); is = data.getInputStream(); wrapper = null; } else if (ctype == COMPRESSOR_TYPE.BZIP2) { if (logMINOR) Logger.minor(this, "dealing with BZIP2"); is = new BZip2CompressorInputStream(data.getInputStream()); wrapper = null; } else if (ctype == COMPRESSOR_TYPE.GZIP) { if (logMINOR) Logger.minor(this, "dealing with GZIP"); is = new GZIPInputStream(data.getInputStream()); wrapper = null; } else if (ctype == COMPRESSOR_TYPE.LZMA_NEW) { // LZMA internally uses pipe streams, so we may as well do it here. // In fact we need to for LZMA_NEW, because of the properties bytes. PipedInputStream pis = new PipedInputStream(); PipedOutputStream pos = new PipedOutputStream(); pis.connect(pos); final OutputStream os = new BufferedOutputStream(pos); wrapper = new ExceptionWrapper(); context.mainExecutor.execute(new Runnable() { @Override public void run() { InputStream is = null; try { Compressor.COMPRESSOR_TYPE.LZMA_NEW.decompress(is = data.getInputStream(), os, data.size(), expectedSize); } catch (CompressionOutputSizeException e) { Logger.error(this, "Failed to decompress archive: " + e, e); wrapper.set(e); } catch (IOException e) { Logger.error(this, "Failed to decompress archive: " + e, e); wrapper.set(e); } finally { try { os.close(); } catch (IOException e) { Logger.error(this, "Failed to close PipedOutputStream: " + e, e); } Closer.close(is); } } }); is = pis; } else if (ctype == COMPRESSOR_TYPE.LZMA) { if (logMINOR) Logger.minor(this, "dealing with LZMA"); is = new LzmaInputStream(data.getInputStream()); wrapper = null; } else { wrapper = null; } if (ARCHIVE_TYPE.ZIP == archiveType) handleZIPArchive(ctx, key, is, element, callback, gotElement, throwAtExit, context); else if (ARCHIVE_TYPE.TAR == archiveType) handleTARArchive(ctx, key, is, element, callback, gotElement, throwAtExit, context); else throw new ArchiveFailureException("Unknown or unsupported archive algorithm " + archiveType); if (wrapper != null) { Exception e = wrapper.get(); if (e != null) throw new ArchiveFailureException("An exception occured decompressing: " + e.getMessage(), e); } } catch (IOException ioe) { throw new ArchiveFailureException("An IOE occured: " + ioe.getMessage(), ioe); } finally { Closer.close(is); } }
From source file:org.cloudifysource.shell.commands.TestRecipe.java
/** * Execute a command line in with a given map of environment settings. The execution outupt is filtered unless * verbose is set to true.//from w ww. ja va2 s . co m * * @param cmdLine * The command to execute * @param env * Environment settings available for the command execution * @return the command's execution exit code, or -2 if the command failed to execute */ private int executeRecipe(final CommandLine cmdLine, final Map<Object, Object> env) { final DefaultExecutor executor = new DefaultExecutor(); // The watchdog will terminate the process if it does not end within the // specified timeout final int externalProcessTimeout = (this.timeout + EXTERNAL_PROCESS_WATCHDOG_ADDITIONAL_TIMEOUT) * 1000; final ExecuteWatchdog watchdog = new TestRecipeWatchdog(externalProcessTimeout); executor.setWatchdog(watchdog); executor.setExitValue(0); int result = -1; PipedInputStream in = null; PipedOutputStream out = null; BufferedReader reader = null; try { in = new PipedInputStream(); out = new PipedOutputStream(in); reader = new BufferedReader(new InputStreamReader(in)); final Thread thread = new Thread(new FilteredOutputHandler(reader, this.verbose)); thread.setDaemon(true); thread.start(); final PumpStreamHandler psh = new PumpStreamHandler(out, out); executor.setStreamHandler(psh); result = executor.execute(cmdLine, env); } catch (final ExecuteException e) { logger.log(Level.SEVERE, "A problem was encountered while executing the recipe: " + e.getMessage(), e); } catch (final IOException e) { logger.log(Level.SEVERE, "An IO Exception was encountered while executing the recipe: " + e.getMessage(), e); result = UNEXPECTED_ERROR_EXIT_CODE; } return result; }
From source file:lucee.commons.io.res.type.datasource.DatasourceResourceProvider.java
public synchronized OutputStream getOutputStream(ConnectionData data, int fullPathHash, int pathHash, String path, String name, boolean append) throws IOException { Attr attr = getAttr(data, fullPathHash, path, name); if (attr.getId() == 0) { create(data, fullPathHash, pathHash, path, name, Attr.TYPE_FILE); attr = getAttr(data, fullPathHash, path, name); }//ww w.ja v a 2 s . c o m PipedInputStream pis = new PipedInputStream(); PipedOutputStream pos = new PipedOutputStream(); pis.connect(pos); DatasourceConnection dc = null; //Connection c=null; try { dc = getDatasourceConnection(data); //Connection c = dc.getConnection(); DataWriter writer = new DataWriter(getCore(data), dc, data.getPrefix(), attr, pis, this, append); writer.start(); return new DatasourceResourceOutputStream(writer, pos); //core.getOutputStream(dc, name, attr, pis); } catch (PageException e) { throw new PageRuntimeException(e); } finally { removeFromCache(data, path, name); //manager.releaseConnection(CONNECTION_ID,dc); } }
From source file:net.wastl.webmail.plugins.SendMessage.java
public HTMLDocument handleURL(String suburl, HTTPSession sess1, HTTPRequestHeader head) throws WebMailException, ServletException { if (sess1 == null) { throw new WebMailException( "No session was given. If you feel this is incorrect, please contact your system administrator"); }/*from ww w.j a va 2 s . c om*/ WebMailSession session = (WebMailSession) sess1; UserData user = session.getUser(); HTMLDocument content; Locale locale = user.getPreferredLocale(); /* Save message in case there is an error */ session.storeMessage(head); if (head.isContentSet("SEND")) { /* The form was submitted, now we will send it ... */ try { MimeMessage msg = new MimeMessage(mailsession); Address from[] = new Address[1]; try { /** * Why we need * org.bulbul.util.TranscodeUtil.transcodeThenEncodeByLocale()? * * Because we specify client browser's encoding to UTF-8, IE seems * to send all data encoded in UTF-8. We have to transcode all byte * sequences we received to UTF-8, and next we encode those strings * using MimeUtility.encodeText() depending on user's locale. Since * MimeUtility.encodeText() is used to convert the strings into its * transmission format, finally we can use the strings in the * outgoing e-mail which relies on receiver's email agent to decode * the strings. * * As described in JavaMail document, MimeUtility.encodeText() conforms * to RFC2047 and as a result, we'll get strings like "=?Big5?B......". */ /** * Since data in session.getUser() is read from file, the encoding * should be default encoding. */ // from[0]=new InternetAddress(MimeUtility.encodeText(session.getUser().getEmail()), // MimeUtility.encodeText(session.getUser().getFullName())); from[0] = new InternetAddress( TranscodeUtil.transcodeThenEncodeByLocale(head.getContent("FROM"), null, locale), TranscodeUtil.transcodeThenEncodeByLocale(session.getUser().getFullName(), null, locale)); } catch (UnsupportedEncodingException e) { log.warn("Unsupported Encoding while trying to send message: " + e.getMessage()); from[0] = new InternetAddress(head.getContent("FROM"), session.getUser().getFullName()); } StringTokenizer t; try { /** * Since data in session.getUser() is read from file, the encoding * should be default encoding. */ // t=new StringTokenizer(MimeUtility.encodeText(head.getContent("TO")).trim(),","); t = new StringTokenizer( TranscodeUtil.transcodeThenEncodeByLocale(head.getContent("TO"), null, locale).trim(), ","); } catch (UnsupportedEncodingException e) { log.warn("Unsupported Encoding while trying to send message: " + e.getMessage()); t = new StringTokenizer(head.getContent("TO").trim(), ",;"); } /* Check To: field, when empty, throw an exception */ if (t.countTokens() < 1) { throw new MessagingException("The recipient field must not be empty!"); } Address to[] = new Address[t.countTokens()]; int i = 0; while (t.hasMoreTokens()) { to[i] = new InternetAddress(t.nextToken().trim()); i++; } try { /** * Since data in session.getUser() is read from file, the encoding * should be default encoding. */ // t=new StringTokenizer(MimeUtility.encodeText(head.getContent("CC")).trim(),","); t = new StringTokenizer( TranscodeUtil.transcodeThenEncodeByLocale(head.getContent("CC"), null, locale).trim(), ","); } catch (UnsupportedEncodingException e) { log.warn("Unsupported Encoding while trying to send message: " + e.getMessage()); t = new StringTokenizer(head.getContent("CC").trim(), ",;"); } Address cc[] = new Address[t.countTokens()]; i = 0; while (t.hasMoreTokens()) { cc[i] = new InternetAddress(t.nextToken().trim()); i++; } try { /** * Since data in session.getUser() is read from file, the encoding * should be default encoding. */ // t=new StringTokenizer(MimeUtility.encodeText(head.getContent("BCC")).trim(),","); t = new StringTokenizer( TranscodeUtil.transcodeThenEncodeByLocale(head.getContent("BCC"), null, locale).trim(), ","); } catch (UnsupportedEncodingException e) { log.warn("Unsupported Encoding while trying to send message: " + e.getMessage()); t = new StringTokenizer(head.getContent("BCC").trim(), ",;"); } Address bcc[] = new Address[t.countTokens()]; i = 0; while (t.hasMoreTokens()) { bcc[i] = new InternetAddress(t.nextToken().trim()); i++; } session.setSent(false); msg.addFrom(from); if (to.length > 0) { msg.addRecipients(Message.RecipientType.TO, to); } if (cc.length > 0) { msg.addRecipients(Message.RecipientType.CC, cc); } if (bcc.length > 0) { msg.addRecipients(Message.RecipientType.BCC, bcc); } msg.addHeader("X-Mailer", WebMailServer.getVersion() + ", " + getName() + " plugin v" + getVersion()); String subject = null; if (!head.isContentSet("SUBJECT")) { subject = "no subject"; } else { try { // subject=MimeUtility.encodeText(head.getContent("SUBJECT")); subject = TranscodeUtil.transcodeThenEncodeByLocale(head.getContent("SUBJECT"), "ISO8859_1", locale); } catch (UnsupportedEncodingException e) { log.warn("Unsupported Encoding while trying to send message: " + e.getMessage()); subject = head.getContent("SUBJECT"); } } msg.addHeader("Subject", subject); if (head.isContentSet("REPLY-TO")) { // msg.addHeader("Reply-To",head.getContent("REPLY-TO")); msg.addHeader("Reply-To", TranscodeUtil.transcodeThenEncodeByLocale(head.getContent("REPLY-TO"), "ISO8859_1", locale)); } msg.setSentDate(new Date(System.currentTimeMillis())); String contnt = head.getContent("BODY"); //String charset=MimeUtility.mimeCharset(MimeUtility.getDefaultJavaCharset()); String charset = "utf-8"; MimeMultipart cont = new MimeMultipart(); MimeBodyPart txt = new MimeBodyPart(); // Transcode to UTF-8 contnt = new String(contnt.getBytes("ISO8859_1"), "UTF-8"); // Encode text if (locale.getLanguage().equals("zh") && locale.getCountry().equals("TW")) { txt.setText(contnt, "Big5"); txt.setHeader("Content-Type", "text/plain; charset=\"Big5\""); txt.setHeader("Content-Transfer-Encoding", "quoted-printable"); // JavaMail defaults to QP? } else { txt.setText(contnt, "utf-8"); txt.setHeader("Content-Type", "text/plain; charset=\"utf-8\""); txt.setHeader("Content-Transfer-Encoding", "quoted-printable"); // JavaMail defaults to QP? } /* Add an advertisement if the administrator requested to do so */ cont.addBodyPart(txt); if (store.getConfig("ADVERTISEMENT ATTACH").equals("YES")) { MimeBodyPart adv = new MimeBodyPart(); String file = ""; if (store.getConfig("ADVERTISEMENT SIGNATURE PATH").startsWith("/")) { file = store.getConfig("ADVERTISEMENT SIGNATURE PATH"); } else { file = parent.getProperty("webmail.data.path") + System.getProperty("file.separator") + store.getConfig("ADVERTISEMENT SIGNATURE PATH"); } String advcont = ""; try { BufferedReader fin = new BufferedReader(new FileReader(file)); String line = fin.readLine(); while (line != null && !line.equals("")) { advcont += line + "\n"; line = fin.readLine(); } fin.close(); } catch (IOException ex) { } /** * Transcode to UTF-8; Since advcont comes from file, we transcode * it from default encoding. */ // Encode text if (locale.getLanguage().equals("zh") && locale.getCountry().equals("TW")) { advcont = new String(advcont.getBytes(), "Big5"); adv.setText(advcont, "Big5"); adv.setHeader("Content-Type", "text/plain; charset=\"Big5\""); adv.setHeader("Content-Transfer-Encoding", "quoted-printable"); } else { advcont = new String(advcont.getBytes(), "UTF-8"); adv.setText(advcont, "utf-8"); adv.setHeader("Content-Type", "text/plain; charset=\"utf-8\""); adv.setHeader("Content-Transfer-Encoding", "quoted-printable"); } cont.addBodyPart(adv); } for (String attachmentKey : session.getAttachments().keySet()) { ByteStore bs = session.getAttachment(attachmentKey); InternetHeaders ih = new InternetHeaders(); ih.addHeader("Content-Transfer-Encoding", "BASE64"); PipedInputStream pin = new PipedInputStream(); PipedOutputStream pout = new PipedOutputStream(pin); /* This is used to write to the Pipe asynchronously to avoid blocking */ StreamConnector sconn = new StreamConnector(pin, (int) (bs.getSize() * 1.6) + 1000); BufferedOutputStream encoder = new BufferedOutputStream(MimeUtility.encode(pout, "BASE64")); encoder.write(bs.getBytes()); encoder.flush(); encoder.close(); //MimeBodyPart att1=sconn.getResult(); MimeBodyPart att1 = new MimeBodyPart(ih, sconn.getResult().getBytes()); if (bs.getDescription() != "") { att1.setDescription(bs.getDescription(), "utf-8"); } /** * As described in FileAttacher.java line #95, now we need to * encode the attachment file name. */ // att1.setFileName(bs.getName()); String fileName = bs.getName(); String localeCharset = getLocaleCharset(locale.getLanguage(), locale.getCountry()); String encodedFileName = MimeUtility.encodeText(fileName, localeCharset, null); if (encodedFileName.equals(fileName)) { att1.addHeader("Content-Type", bs.getContentType()); att1.setFileName(fileName); } else { att1.addHeader("Content-Type", bs.getContentType() + "; charset=" + localeCharset); encodedFileName = encodedFileName.substring(localeCharset.length() + 5, encodedFileName.length() - 2); encodedFileName = encodedFileName.replace('=', '%'); att1.addHeaderLine("Content-Disposition: attachment; filename*=" + localeCharset + "''" + encodedFileName); } cont.addBodyPart(att1); } msg.setContent(cont); // } msg.saveChanges(); boolean savesuccess = true; msg.setHeader("Message-ID", session.getUserModel().getWorkMessage().getAttribute("msgid")); if (session.getUser().wantsSaveSent()) { String folderhash = session.getUser().getSentFolder(); try { Folder folder = session.getFolder(folderhash); Message[] m = new Message[1]; m[0] = msg; folder.appendMessages(m); } catch (MessagingException e) { savesuccess = false; } catch (NullPointerException e) { // Invalid folder: savesuccess = false; } } boolean sendsuccess = false; try { Transport.send(msg); Address sent[] = new Address[to.length + cc.length + bcc.length]; int c1 = 0; int c2 = 0; for (c1 = 0; c1 < to.length; c1++) { sent[c1] = to[c1]; } for (c2 = 0; c2 < cc.length; c2++) { sent[c1 + c2] = cc[c2]; } for (int c3 = 0; c3 < bcc.length; c3++) { sent[c1 + c2 + c3] = bcc[c3]; } sendsuccess = true; throw new SendFailedException("success", new Exception("success"), sent, null, null); } catch (SendFailedException e) { session.handleTransportException(e); } //session.clearMessage(); content = new XHTMLDocument(session.getModel(), store.getStylesheet("sendresult.xsl", user.getPreferredLocale(), user.getTheme())); // if(sendsuccess) session.clearWork(); } catch (Exception e) { log.error("Could not send messsage", e); throw new DocumentNotFoundException("Could not send message. (Reason: " + e.getMessage() + ")"); } } else if (head.isContentSet("ATTACH")) { /* Redirect request for attachment (unfortunately HTML forms are not flexible enough to have two targets without Javascript) */ content = parent.getURLHandler().handleURL("/compose/attach", session, head); } else { throw new DocumentNotFoundException("Could not send message. (Reason: No content given)"); } return content; }
From source file:org.liveSense.service.email.EmailServiceImpl.java
/** * {@inheritDoc}//from w w w .j a v a 2 s .co m */ @Override public void sendEmail(Session session, final MimeMessage message) throws Exception { boolean haveSession = false; try { if (session != null && session.isLive()) { haveSession = true; } else { session = repository.loginAdministrative(null); } // Store mail to Spool folder Node mailNode = session.getRootNode().getNode(spoolFolder).addNode(UUID.randomUUID().toString(), nodeType); mailNode = mailNode.addNode(propertyName, "nt:resource"); PipedInputStream in = new PipedInputStream(); final PipedOutputStream out = new PipedOutputStream(in); new Thread(new Runnable() { @Override public void run() { try { message.writeTo(out); out.close(); } catch (IOException e) { log.error("Could not write mail message stream", e); } catch (MessagingException e) { log.error("Could not write mail message stream", e); } } }).start(); BinaryValue bv = null; try { bv = new BinaryValue(in); } catch (IllegalArgumentException e) { // The jackrabbit closes the PipedInputStream, thats incorrect } if (bv != null) { mailNode.setProperty("jcr:data", bv); } mailNode.setProperty("jcr:lastModified", Calendar.getInstance()); mailNode.setProperty("jcr:mimeType", "message/rfc822"); } catch (Exception ex) { log.error("Cannot create mail: ", ex); throw ex; } finally { if (!haveSession && session != null) { if (session.hasPendingChanges()) try { session.save(); } catch (Throwable th) { } session.logout(); } } }