use of i2p.susi.util.OutputStreamBuffer in project i2p.i2p by i2p.
the class MailPart method decode.
/**
* @param offset 2 for sendAttachment, 0 otherwise, probably for \r\n
* @since 0.9.13
*/
public void decode(int offset, Buffer out) throws IOException {
String encg = encoding;
if (encg == null) {
// throw new DecodingException("No encoding specified");
Debug.debug(Debug.DEBUG, "Warning: no transfer encoding found, fallback to 7bit.");
encg = "7bit";
}
Encoding enc = EncodingFactory.getEncoding(encg);
if (enc == null)
throw new DecodingException(_t("No encoder found for encoding \\''{0}\\''.", WebMail.quoteHTML(encg)));
InputStream in = null;
LimitInputStream lin = null;
CountingOutputStream cos = null;
Buffer dout = null;
try {
in = buffer.getInputStream();
DataHelper.skip(in, buffer.getOffset() + beginBody + offset);
lin = new LimitInputStream(in, end - beginBody - offset);
if (decodedLength < 0) {
cos = new CountingOutputStream(out.getOutputStream());
dout = new OutputStreamBuffer(cos);
} else {
dout = out;
}
enc.decode(lin, dout);
// dout.getOutputStream().flush();
} catch (IOException ioe) {
if (lin != null)
Debug.debug(Debug.DEBUG, "Decode IOE at in position " + lin.getRead() + " offset " + offset, ioe);
else if (cos != null)
Debug.debug(Debug.DEBUG, "Decode IOE at out position " + cos.getWritten() + " offset " + offset, ioe);
else
Debug.debug(Debug.DEBUG, "Decode IOE", ioe);
throw ioe;
} finally {
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
;
if (lin != null)
try {
lin.close();
} catch (IOException ioe) {
}
;
buffer.readComplete(true);
// let the servlet do this
// if (cos != null) try { cos.close(); } catch (IOException ioe) {};
// if (dout != null)
// dout.writeComplete(true);
// out.writeComplete(true);
}
if (cos != null)
decodedLength = (int) cos.getWritten();
}
use of i2p.susi.util.OutputStreamBuffer in project i2p.i2p by i2p.
the class WebMail method showPart.
/**
* recursively render all mail body parts
*
* 1. if type is multipart/alternative, look for text/plain section and ignore others
* 2. if type is multipart/*, recursively call all these parts
* 3. if type is text/plain (or mail is not mime), print out
* 4. in all other cases print out message, that part is not displayed
*
* @param out
* @param mailPart
* @param level is increased by recursively calling sub parts
*/
private static void showPart(PrintWriter out, MailPart mailPart, int level, boolean html) {
String br = html ? "<br>\r\n" : "\r\n";
if (html) {
out.println("<!-- ");
out.println("Debug: Showing Mail Part at level " + level + " with ID " + mailPart.getID());
out.println("Debug: Mail Part headers follow");
for (int i = 0; i < mailPart.headerLines.length; i++) {
// fix Content-Type: multipart/alternative; boundary="----------8CDE39ECAF2633"
out.println(mailPart.headerLines[i].replace("--", "--"));
}
out.println("-->");
}
if (mailPart.multipart) {
if (mailPart.type.equals("multipart/alternative")) {
MailPart chosen = null;
for (MailPart subPart : mailPart.parts) {
if (subPart.type != null && subPart.type.equals("text/plain"))
chosen = subPart;
}
if (chosen != null) {
showPart(out, chosen, level + 1, html);
if (html) {
// DEBUG
for (MailPart subPart : mailPart.parts) {
if (chosen.equals(subPart))
continue;
out.println("<!-- ");
out.println("Debug: Not showing alternative Mail Part at level " + (level + 1) + " with ID " + subPart.getID());
out.println("Debug: Mail Part headers follow");
for (int i = 0; i < subPart.headerLines.length; i++) {
out.println(subPart.headerLines[i].replace("--", "--"));
}
out.println("-->");
}
}
return;
}
}
for (MailPart part : mailPart.parts) {
showPart(out, part, level + 1, html);
}
} else if (mailPart.message) {
for (MailPart part : mailPart.parts) {
showPart(out, part, level + 1, html);
}
} else {
boolean showBody = false;
boolean prepareAttachment = false;
String reason = "";
String ident = quoteHTML((mailPart.description != null ? mailPart.description + ", " : "") + (mailPart.filename != null ? mailPart.filename + ", " : "") + (mailPart.name != null ? mailPart.name + ", " : "") + (mailPart.type != null ? '(' + mailPart.type + ')' : _t("unknown")));
if (level == 0 && mailPart.version == null) {
/*
* not a MIME mail, so simply print it literally
*/
showBody = true;
}
if (!showBody && mailPart.type != null) {
if (mailPart.type.equals("text/plain")) {
showBody = true;
} else
prepareAttachment = true;
}
if (reason != null && reason.length() > 0) {
if (html)
out.println("<p class=\"info\">");
out.println(reason);
if (html)
out.println("</p>");
reason = "";
}
if (html)
out.println("<tr class=\"mailbody\"><td colspan=\"2\" align=\"center\">");
if (showBody) {
if (html)
out.println("<p class=\"mailbody\"><br>");
String charset = mailPart.charset;
if (charset == null) {
charset = "ISO-8859-1";
// don't show this in text mode which is used to include the mail in the reply or forward
// Too common, don't show this at all.
// if (html)
// reason = _t("Warning: no charset found, fallback to US-ASCII.") + br;
}
try {
Writer escaper;
if (html)
escaper = new EscapeHTMLWriter(out);
else
escaper = out;
Buffer ob = new OutputStreamBuffer(new DecodingOutputStream(escaper, charset));
mailPart.decode(0, ob);
// todo Finally
ob.writeComplete(true);
} catch (UnsupportedEncodingException uee) {
showBody = false;
reason = _t("Charset \\''{0}\\'' not supported.", quoteHTML(mailPart.charset)) + br;
} catch (IOException e1) {
showBody = false;
reason += _t("Part ({0}) not shown, because of {1}", ident, e1.toString()) + br;
}
if (html)
out.println("<br></p>");
}
if (reason != null && reason.length() > 0) {
// FIXME css has -32 margin
if (html)
out.println("<p class=\"info\">");
out.println(reason);
if (html)
out.println("</p>");
}
if (prepareAttachment) {
if (html) {
out.println("<hr><div class=\"attached\">");
String type = mailPart.type;
if (type != null && type.startsWith("image/")) {
// we at least show images safely...
String name = mailPart.filename;
if (name == null) {
name = mailPart.name;
if (name == null)
name = mailPart.description;
}
name = quoteHTML(name);
out.println("<img src=\"" + myself + '?' + RAW_ATTACHMENT + '=' + mailPart.getID() + "&" + B64UIDL + '=' + Base64.encode(mailPart.uidl) + "\" alt=\"" + name + "\">");
} else if (type != null && (// type list from snark
type.startsWith("audio/") || type.equals("application/ogg") || type.startsWith("video/") || (type.startsWith("text/") && !type.equals("text/html")) || type.equals("application/zip") || type.equals("application/x-gtar") || type.equals("application/x-zip-compressed") || type.equals("application/compress") || type.equals("application/gzip") || type.equals("application/x-7z-compressed") || type.equals("application/x-rar-compressed") || type.equals("application/x-tar") || type.equals("application/x-bzip2") || type.equals("application/pdf") || type.equals("application/x-bittorrent") || type.equals("application/pgp-encrypted") || type.equals("application/pgp-signature"))) {
out.println("<a href=\"" + myself + '?' + RAW_ATTACHMENT + '=' + mailPart.getID() + "&" + B64UIDL + '=' + Base64.encode(mailPart.uidl) + "\">" + _t("Download attachment {0}", ident) + "</a>");
} else {
out.println("<a target=\"_blank\" href=\"" + myself + '?' + DOWNLOAD + '=' + mailPart.getID() + "&" + B64UIDL + '=' + Base64.encode(mailPart.uidl) + "\">" + _t("Download attachment {0}", ident) + "</a>" + " (" + _t("File is packed into a zipfile for security reasons.") + ')');
}
out.println("</div>");
} else {
out.println(_t("Attachment ({0}).", ident));
}
}
if (html)
out.println("</td></tr>");
}
if (html) {
out.println("<!-- ");
out.println("Debug: End of Mail Part at level " + level + " with ID " + mailPart.getID());
out.println("-->");
}
}
use of i2p.susi.util.OutputStreamBuffer in project i2p.i2p by i2p.
the class WebMail method sendAttachment.
/**
* @param sessionObject
* @param response
* @param isRaw if true, don't zip it
* @return success
*/
private static boolean sendAttachment(SessionObject sessionObject, MailPart part, HttpServletResponse response, boolean isRaw) {
boolean shown = false;
if (part != null) {
String name = part.filename;
if (name == null) {
name = part.name;
if (name == null) {
name = part.description;
if (name == null)
name = "part" + part.getID();
}
}
String name2 = FilenameUtil.sanitizeFilename(name);
String name3 = FilenameUtil.encodeFilenameRFC5987(name);
response.setHeader("Cache-Control", "public, max-age=604800");
if (isRaw) {
try {
response.addHeader("Content-Disposition", "inline; filename=\"" + name2 + "\"; " + "filename*=" + name3);
if (part.type != null)
response.setContentType(part.type);
if (part.decodedLength >= 0)
response.setContentLength(part.decodedLength);
Debug.debug(Debug.DEBUG, "Sending raw attachment " + name + " length " + part.decodedLength);
part.decode(0, new OutputStreamBuffer(response.getOutputStream()));
shown = true;
} catch (IOException e) {
Debug.debug(Debug.ERROR, "Error sending raw attachment " + name + " length " + part.decodedLength, e);
}
} else {
ZipOutputStream zip = null;
try {
zip = new ZipOutputStream(response.getOutputStream());
response.setContentType("application/zip; name=\"" + name2 + ".zip\"");
response.addHeader("Content-Disposition", "attachment; filename=\"" + name2 + ".zip\"; " + "filename*=" + name3 + ".zip");
ZipEntry entry = new ZipEntry(name);
zip.putNextEntry(entry);
// was 2
part.decode(0, new OutputStreamBuffer(zip));
zip.closeEntry();
zip.finish();
shown = true;
} catch (IOException e) {
Debug.debug(Debug.ERROR, "Error sending zip attachment " + name + " length " + part.decodedLength, e);
} finally {
if (zip != null)
try {
zip.close();
} catch (IOException ioe) {
}
}
}
}
return shown;
}
Aggregations