List of usage examples for java.io PrintStream write
public void write(byte buf[], int off, int len)
From source file:org.exjello.mail.ExchangeConnection.java
private void findInbox() throws Exception { inbox = null;//from w ww .ja va2 s . c o m drafts = null; submissionUri = null; sentitems = null; outbox = null; HttpClient client = getClient(); ExchangeMethod op = new ExchangeMethod(PROPFIND_METHOD, server + "/exchange/" + mailbox); op.setHeader("Content-Type", XML_CONTENT_TYPE); op.setHeader("Depth", "0"); op.setHeader("Brief", "t"); op.setRequestEntity(createFindInboxEntity()); InputStream stream = null; try { int status = client.executeMethod(op); stream = op.getResponseBodyAsStream(); if (status >= 300) { throw new IllegalStateException("Unable to obtain inbox."); } SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); SAXParser parser = spf.newSAXParser(); parser.parse(stream, new DefaultHandler() { private final StringBuilder content = new StringBuilder(); public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { content.setLength(0); } public void characters(char[] ch, int start, int length) throws SAXException { content.append(ch, start, length); } public void endElement(String uri, String localName, String qName) throws SAXException { if (!HTTPMAIL_NAMESPACE.equals(uri)) return; if ("inbox".equals(localName)) { ExchangeConnection.this.inbox = content.toString(); } else if ("drafts".equals(localName)) { ExchangeConnection.this.drafts = content.toString(); } else if ("sentitems".equals(localName)) { ExchangeConnection.this.sentitems = content.toString(); } else if ("outbox".equals(localName)) { ExchangeConnection.this.outbox = content.toString(); } else if ("sendmsg".equals(localName)) { ExchangeConnection.this.submissionUri = content.toString(); } } }); stream.close(); stream = null; } finally { try { if (stream != null) { byte[] buf = new byte[65536]; try { if (session.getDebug()) { PrintStream log = session.getDebugOut(); log.println("Response Body:"); int count; while ((count = stream.read(buf, 0, 65536)) != -1) { log.write(buf, 0, count); } log.flush(); log.println(); } else { while (stream.read(buf, 0, 65536) != -1) ; } } catch (Exception ignore) { } finally { try { stream.close(); } catch (Exception ignore2) { } } } } finally { op.releaseConnection(); } } }
From source file:org.exjello.mail.Exchange2003Connection.java
public void send(MimeMessage message) throws Exception { Address[] bccRecipients = message.getRecipients(Message.RecipientType.BCC); if (bccRecipients == null || bccRecipients.length == 0) { bccRecipients = null;// w ww.j a va 2 s .c om } message.setRecipients(Message.RecipientType.BCC, (Address[]) null); synchronized (this) { if (!isConnected()) { throw new IllegalStateException("Not connected."); } if (!canSend()) { throw new IllegalStateException("Unable to access outbox."); } HttpClient client = getClient(); String path = drafts; if (!path.endsWith("/")) path += "/"; String messageName = generateMessageName(); path += escape(messageName + ".eml"); PutMethod op = new PutMethod(path); op.setRequestHeader("Content-Type", MESSAGE_CONTENT_TYPE); op.setRequestEntity(createMessageEntity(message)); InputStream stream = null; try { int status = client.executeMethod(op); stream = op.getResponseBodyAsStream(); if (status >= 300) { throw new IllegalStateException("Unable to post message to draft folder."); } } finally { try { if (stream != null) { byte[] buf = new byte[65536]; try { if (session.getDebug()) { PrintStream log = session.getDebugOut(); log.println("Response Body:"); int count; while ((count = stream.read(buf, 0, 65536)) != -1) { log.write(buf, 0, count); } log.flush(); log.println(); } else { while (stream.read(buf, 0, 65536) != -1) ; } } catch (Exception ignore) { } finally { try { stream.close(); } catch (Exception ignore2) { } } } } finally { op.releaseConnection(); } } if (bccRecipients != null) { ExchangeMethod patch = new ExchangeMethod(PROPPATCH_METHOD, path); patch.setHeader("Content-Type", XML_CONTENT_TYPE); patch.addHeader("Depth", "0"); patch.addHeader("Translate", "f"); patch.addHeader("Brief", "t"); patch.setRequestEntity(createAddBccEntity(bccRecipients)); stream = null; try { int status = client.executeMethod(patch); stream = patch.getResponseBodyAsStream(); if (status >= 300) { throw new IllegalStateException("Unable to add BCC recipients. Status: " + status); } } finally { try { if (stream != null) { byte[] buf = new byte[65536]; try { if (session.getDebug()) { PrintStream log = session.getDebugOut(); log.println("Response Body:"); int count; while ((count = stream.read(buf, 0, 65536)) != -1) { log.write(buf, 0, count); } log.flush(); log.println(); } else { while (stream.read(buf, 0, 65536) != -1) ; } } catch (Exception ignore) { } finally { try { stream.close(); } catch (Exception ignore2) { } } } } finally { patch.releaseConnection(); } } } ExchangeMethod move = new ExchangeMethod(MOVE_METHOD, path); String destination = submissionUri; if (!destination.endsWith("/")) destination += "/"; move.setHeader("Destination", destination); stream = null; try { int status = client.executeMethod(move); stream = move.getResponseBodyAsStream(); if (status >= 300) { throw new IllegalStateException("Unable to move message to outbox: Status " + status); } } finally { try { if (stream != null) { byte[] buf = new byte[65536]; try { if (session.getDebug()) { PrintStream log = session.getDebugOut(); log.println("Response Body:"); int count; while ((count = stream.read(buf, 0, 65536)) != -1) { log.write(buf, 0, count); } log.flush(); log.println(); } else { while (stream.read(buf, 0, 65536) != -1) ; } } catch (Exception ignore) { } finally { try { stream.close(); } catch (Exception ignore2) { } } } } finally { move.releaseConnection(); } } if (session.getDebug()) { session.getDebugOut().println("Sent successfully."); } } }
From source file:josejamilena.pfc.servidor.chartserver.ClientHandler.java
/** * Mtodo que ejecuta el Thread.//from w w w . jav a 2 s. c om */ @Override public void run() { try { BufferedReader in = new BufferedReader(new InputStreamReader(miSocketServidor.getInputStream())); PrintStream out = new PrintStream(new BufferedOutputStream(miSocketServidor.getOutputStream())); String s = in.readLine(); // System.out.println(s); // Salida para el Log // Atiende a servir archivos. String filename = ""; StringTokenizer st = new StringTokenizer(s); try { // Parsea la peticin desde el comando GET if (st.hasMoreElements() && st.nextToken().equalsIgnoreCase("GET") && st.hasMoreElements()) { filename = st.nextToken(); } else { throw new FileNotFoundException(); } // Aade "/" with "index.html" if (filename.endsWith("/")) { filename += nombreFichero; } // Quita / del nombre del archivo while (filename.indexOf("/") == 0) { filename = filename.substring(1); } // Reemplaza "/" por "\" en el path para servidores en Win32 filename = filename.replace('/', File.separator.charAt(0)); /* Comprueba caracteres no permitidos, para impedir acceder a directorios superiores */ if ((filename.indexOf("..") >= 0) || (filename.indexOf(':') >= 0) || (filename.indexOf('|') >= 0)) { throw new FileNotFoundException(); } // Si la peticin de acceso a un directorio no tiene "/", // envia al cliente un respuesta HTTP if (new File(filename).isDirectory()) { filename = filename.replace('\\', '/'); out.print( "HTTP/1.0 301 Movido permantemente \r\n" + "Albergado en: /" + filename + "/\r\n\r\n"); out.close(); return; } // Abriendo el archivo (puede lanzar FileNotFoundException) InputStream f = new FileInputStream(filename); // Determina el tipe MIME e imprime la cabecera HTTP String mimeType = "text/plain"; if (filename.endsWith(".html") || filename.endsWith(".htm")) { mimeType = "text/html"; } else if (filename.endsWith(".jpg") || filename.endsWith(".jpeg")) { mimeType = "image/jpeg"; } else if (filename.endsWith(".gif")) { mimeType = "image/gif"; } else if (filename.endsWith(".class")) { mimeType = "application/octet-stream"; } else if (filename.endsWith(".jnlp")) { mimeType = "application/x-java-jnlp-file"; } out.print("HTTP/1.0 200 OK\r\n" + "Server: josejamilena_Torito-Chart-Server\r\n" + "Content-type: " + mimeType + "\r\n\r\n"); // Envia el fichero ala cliente, y cierra la conexin byte[] a = new byte[bufferSize]; int n; while ((n = f.read(a)) > 0) { out.write(a, 0, n); } f.close(); out.close(); } catch (FileNotFoundException x) { out.println("HTTP/1.0 404 No encontrado.\r\n" + "Server: josejamilena_Torito-Chart-Server\r\n" + "Content-type: text/html\r\n\r\n" + "<html><head></head><body>" + "El fichero " + filename + " no ha sido encontrado.</body></html>\n"); out.close(); } } catch (IOException x) { System.out.println(x); } finally { File aborrar = new File(nombreFichero); org.apache.commons.io.FileUtils.deleteQuietly(aborrar); } }