Search in sources :

Example 1 with WriterOutputStream

use of net.i2p.servlet.util.WriterOutputStream in project i2p.i2p by i2p.

the class StatHelper method dumpProfile.

/**
 *  dump the profile
 *  @since 0.8.5
 */
private String dumpProfile(Hash peer) {
    try {
        WriterOutputStream wos = new WriterOutputStream(_out);
        boolean success = _context.profileOrganizer().exportProfile(peer, wos);
        if (success) {
            wos.flush();
            _out.flush();
            return "";
        } else {
            return "Unknown peer " + _peer;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return "IO Error " + e;
    }
}
Also used : IOException(java.io.IOException) WriterOutputStream(net.i2p.servlet.util.WriterOutputStream)

Example 2 with WriterOutputStream

use of net.i2p.servlet.util.WriterOutputStream in project i2p.i2p by i2p.

the class WebMail method showMessage.

/**
 * @param out
 * @param sessionObject
 * @param reallyDelete was the delete button pushed, if so, show the really delete? message
 */
private static void showMessage(PrintWriter out, SessionObject sessionObject, String showUIDL, boolean reallyDelete) {
    if (reallyDelete) {
        out.println("<p class=\"error\">" + _t("Really delete this message?") + ' ' + button(REALLYDELETE, _t("Yes, really delete it!")) + ' ' + button(CANCEL, _t("Cancel")) + "</p>");
    }
    Mail mail = sessionObject.mailCache.getMail(showUIDL, MailCache.FetchMode.ALL);
    if (!RELEASE && mail != null && mail.hasBody() && mail.getSize() < 16384) {
        out.println("<!--");
        out.println("Debug: Mail header and body follow");
        Buffer body = mail.getBody();
        InputStream in = null;
        OutputStream sout = null;
        try {
            in = body.getInputStream();
            sout = new EscapeHTMLOutputStream(new WriterOutputStream(out));
            DataHelper.copy(in, sout);
        } catch (IOException ioe) {
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException ioe) {
                }
            if (sout != null)
                try {
                    sout.close();
                } catch (IOException ioe) {
                }
            body.readComplete(true);
        }
        out.println("-->");
    }
    out.println("<div class=\"topbuttons\">");
    out.println(button(NEW, _t("New")) + spacer);
    boolean hasHeader = mail != null && mail.hasHeader();
    if (hasHeader) {
        out.println(button(REPLY, _t("Reply")) + button(REPLYALL, _t("Reply All")) + button(FORWARD, _t("Forward")) + spacer + button(SAVE_AS, _t("Save As")) + spacer);
        if (sessionObject.reallyDelete)
            out.println(button2(DELETE, _t("Delete")));
        else
            out.println(button(DELETE, _t("Delete")));
    }
    out.println(button(LOGOUT, _t("Logout")));
    // processRequest() will P-R-G the PREV and NEXT so we have a consistent URL
    out.println("<div id=\"messagenav\">");
    Folder<String> folder = sessionObject.folder;
    if (hasHeader) {
        String uidl = folder.getPreviousElement(showUIDL);
        String text = _t("Previous");
        if (uidl == null || folder.isFirstElement(showUIDL)) {
            out.println(button2(PREV, text));
        } else {
            String b64UIDL = Base64.encode(uidl);
            out.println("<input type=\"hidden\" name=\"" + PREV_B64UIDL + "\" value=\"" + b64UIDL + "\">");
            out.println(button(PREV, text));
        }
        out.print(spacer);
    }
    int page = folder.getPageOf(showUIDL);
    out.println("<input type=\"hidden\" name=\"" + CUR_PAGE + "\" value=\"" + page + "\">");
    out.println(button(LIST, _t("Back to Folder")) + spacer);
    if (hasHeader) {
        String uidl = folder.getNextElement(showUIDL);
        String text = _t("Next");
        if (uidl == null || folder.isLastElement(showUIDL)) {
            out.println(button2(NEXT, text));
        } else {
            String b64UIDL = Base64.encode(uidl);
            out.println("<input type=\"hidden\" name=\"" + NEXT_B64UIDL + "\" value=\"" + b64UIDL + "\">");
            out.println(button(NEXT, text));
        }
        out.print(spacer);
    }
    out.println("</div></div>");
    // if (Config.hasConfigFile())
    // out.println(button( RELOAD, _t("Reload Config") ) + spacer);
    out.println("<div id=\"viewmail\"><table id=\"message_full\" cellspacing=\"0\" cellpadding=\"5\">\n");
    if (hasHeader) {
        String subj = mail.subject;
        if (subj.length() > 0)
            subj = quoteHTML(subj);
        else
            subj = "<i>" + _t("no subject") + "</i>";
        out.println("<tr><td colspan=\"2\"><table id=\"mailhead\">\n" + "<tr><td colspan=\"2\" align=\"center\"><hr></td></tr>\n" + "<tr><td align=\"right\">" + _t("From") + ":</td><td align=\"left\">" + quoteHTML(mail.sender) + "</td></tr>\n" + "<tr><td align=\"right\">" + _t("Subject") + ":</td><td align=\"left\"><b>" + subj + "</b></td></tr>\n");
        if (mail.to != null) {
            out.println("<tr><td align=\"right\">" + _t("To") + ":</td><td align=\"left\">" + buildRecipientLine(mail.to) + "</td></tr>\n");
        }
        if (mail.cc != null) {
            out.println("<tr><td align=\"right\">" + _t("Cc") + ":</td><td align=\"left\">" + buildRecipientLine(mail.cc) + "</td></tr>\n");
        }
        out.println("<tr><td align=\"right\">" + _t("Date") + ":</td><td align=\"left\">" + mail.quotedDate + "</td></tr>\n" + "<tr><td colspan=\"2\" align=\"center\"><hr></td></tr>" + "</table></td></tr>\n");
        if (mail.hasPart()) {
            mail.setNew(false);
            showPart(out, mail.getPart(), 0, SHOW_HTML);
        } else {
            out.println("<tr class=\"mailbody\"><td colspan=\"2\" align=\"center\"><p class=\"error\">" + _t("Could not fetch mail body.") + "</p></td></tr>\n");
        }
    } else {
        out.println("<tr class=\"mailbody\"><td colspan=\"2\" align=\"center\"><p class=\"error\">" + _t("Message not found.") + "</p></td></tr>\n");
    }
    out.println("</table></div>");
}
Also used : OutputStreamBuffer(i2p.susi.util.OutputStreamBuffer) Buffer(i2p.susi.util.Buffer) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) DecodingOutputStream(i2p.susi.util.DecodingOutputStream) WriterOutputStream(net.i2p.servlet.util.WriterOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) EscapeHTMLOutputStream(i2p.susi.util.EscapeHTMLOutputStream) OutputStream(java.io.OutputStream) EscapeHTMLOutputStream(i2p.susi.util.EscapeHTMLOutputStream) IOException(java.io.IOException) WriterOutputStream(net.i2p.servlet.util.WriterOutputStream)

Example 3 with WriterOutputStream

use of net.i2p.servlet.util.WriterOutputStream in project i2p.i2p by i2p.

the class BasicServlet method sendData.

/* ------------------------------------------------------------ */
protected void sendData(HttpServletRequest request, HttpServletResponse response, HttpContent content) throws IOException {
    InputStream in = null;
    try {
        in = content.getInputStream();
    } catch (IOException e) {
        if (_log.shouldLog(Log.WARN))
            _log.warn("Not found: " + content);
        response.sendError(404);
        return;
    }
    OutputStream out = null;
    try {
        out = response.getOutputStream();
    } catch (IllegalStateException e) {
        out = new WriterOutputStream(response.getWriter());
    }
    long content_length = content.getContentLength();
    // see if there are any range headers
    Enumeration<?> reqRanges = request.getHeaders("Range");
    if (reqRanges == null || !reqRanges.hasMoreElements()) {
        // if there were no ranges, send entire entity
        // Write content normally
        writeHeaders(response, content, content_length);
        if (content_length >= 0 && request.getMethod().equals("HEAD")) {
            // if we know the content length, don't send it to be counted
            if (_log.shouldLog(Log.INFO))
                _log.info("HEAD: " + content);
        } else {
            // GET or unknown size for HEAD
            copy(in, out);
        }
        return;
    }
    // Parse the satisfiable ranges
    List<InclusiveByteRange> ranges = InclusiveByteRange.satisfiableRanges(reqRanges, content_length);
    // Completely punt on multiple ranges (unlike Default)
    if (ranges == null || ranges.size() != 1) {
        writeHeaders(response, content, content_length);
        response.setStatus(416);
        response.setHeader("Content-Range", InclusiveByteRange.to416HeaderRangeString(content_length));
        in.close();
        return;
    }
    // if there is only a single valid range (must be satisfiable
    // since were here now), send that range with a 216 response
    InclusiveByteRange singleSatisfiableRange = ranges.get(0);
    long singleLength = singleSatisfiableRange.getSize(content_length);
    writeHeaders(response, content, singleLength);
    response.setStatus(206);
    response.setHeader("Content-Range", singleSatisfiableRange.toHeaderRangeString(content_length));
    copy(in, singleSatisfiableRange.getFirst(content_length), out, singleLength);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) WriterOutputStream(net.i2p.servlet.util.WriterOutputStream) IOException(java.io.IOException) WriterOutputStream(net.i2p.servlet.util.WriterOutputStream)

Aggregations

IOException (java.io.IOException)3 WriterOutputStream (net.i2p.servlet.util.WriterOutputStream)3 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Buffer (i2p.susi.util.Buffer)1 DecodingOutputStream (i2p.susi.util.DecodingOutputStream)1 EscapeHTMLOutputStream (i2p.susi.util.EscapeHTMLOutputStream)1 OutputStreamBuffer (i2p.susi.util.OutputStreamBuffer)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)1