1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 11:12:22 -05:00

Improve exception handling

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@48 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2007-04-25 22:04:37 +00:00
parent d2e141e9f7
commit aad97b08b4

View File

@ -665,8 +665,9 @@ public class ExchangeSession {
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
boolean mstnefDetected = false; boolean mstnefDetected = false;
String boundary = null; String boundary = null;
BufferedReader reader = null;
try { try {
BufferedReader reader = new BufferedReader(new StringReader(fullHeaders)); reader = new BufferedReader(new StringReader(fullHeaders));
String line; String line;
line = reader.readLine(); line = reader.readLine();
while (line != null && line.length() > 0) { while (line != null && line.length() > 0) {
@ -754,33 +755,52 @@ public class ExchangeSession {
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Unable to preprocess headers " + e + " " + e.getMessage()); throw new RuntimeException("Unable to preprocess headers " + e + " " + e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
} }
} }
public void write(OutputStream os) throws IOException { public void write(OutputStream os) throws IOException {
// TODO : filter submessage headers in fullHeaders // TODO : filter submessage headers in fullHeaders
BufferedReader reader = new BufferedReader(new StringReader(fullHeaders)); BufferedReader reader = null;
// skip first line try {
reader.readLine(); reader = new BufferedReader(new StringReader(fullHeaders));
MimeHeader mimeHeader = new MimeHeader(); // skip first line
mimeHeader.processHeaders(reader, os); reader.readLine();
// non MIME message without attachments, append body MimeHeader mimeHeader = new MimeHeader();
if (mimeHeader.boundary == null) { mimeHeader.processHeaders(reader, os);
os.write('\r'); // non MIME message without attachments, append body
os.write('\n'); if (mimeHeader.boundary == null) {
writeBody(os, mimeHeader); os.write('\r');
os.write('\n');
writeBody(os, mimeHeader);
if (hasAttachment) { if (hasAttachment) {
os.write("**warning : missing attachments**".getBytes()); os.write("**warning : missing attachments**".getBytes());
}
} else {
attachmentIndex = 0;
loadAttachments();
writeMimeMessage(reader, os, mimeHeader);
}
os.flush();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// ignore
}
} }
} else {
attachmentIndex = 0;
loadAttachments();
writeMimeMessage(reader, os, mimeHeader);
} }
os.flush();
} }
public void writeMimeMessage(BufferedReader reader, OutputStream os, MimeHeader mimeHeader) throws IOException { public void writeMimeMessage(BufferedReader reader, OutputStream os, MimeHeader mimeHeader) throws IOException {
@ -852,8 +872,8 @@ public class ExchangeSession {
} }
protected void writeAttachment(OutputStream os, MimeHeader mimeHeader, Attachment attachment) throws IOException { protected void writeAttachment(OutputStream os, MimeHeader mimeHeader, Attachment attachment) throws IOException {
OutputStream quotedOs = null;
try { try {
OutputStream quotedOs;
try { try {
// try another base64Encoder implementation // try another base64Encoder implementation
if ("base64".equalsIgnoreCase(mimeHeader.contentTransferEncoding)) { if ("base64".equalsIgnoreCase(mimeHeader.contentTransferEncoding)) {
@ -896,11 +916,22 @@ public class ExchangeSession {
wdr.retrieveSessionInstance().executeMethod(method); wdr.retrieveSessionInstance().executeMethod(method);
// encode attachment // encode attachment
BufferedInputStream bis = new BufferedInputStream(method.getResponseBodyAsStream()); BufferedInputStream bis = null;
byte[] buffer = new byte[4096]; try {
int count; bis = new BufferedInputStream(method.getResponseBodyAsStream());
while ((count = bis.read(buffer)) >= 0) { byte[] buffer = new byte[4096];
quotedOs.write(buffer, 0, count); int count;
while ((count = bis.read(buffer)) >= 0) {
quotedOs.write(buffer, 0, count);
}
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// ignore
}
}
} }
bis.close(); bis.close();
quotedOs.flush(); quotedOs.flush();
@ -910,6 +941,14 @@ public class ExchangeSession {
} catch (HttpException e) { } catch (HttpException e) {
throw new IOException(e + " " + e.getMessage()); throw new IOException(e + " " + e.getMessage());
} finally {
if (quotedOs != null) {
try {
quotedOs.close();
} catch (IOException e) {
// ignore
}
}
} }
} }
@ -1009,15 +1048,26 @@ public class ExchangeSession {
public void printHeaders(OutputStream os) throws IOException { public void printHeaders(OutputStream os) throws IOException {
String line; String line;
BufferedReader reader = new BufferedReader(new StringReader(fullHeaders)); BufferedReader reader = null;
// skip first line try {
reader.readLine(); reader = new BufferedReader(new StringReader(fullHeaders));
line = reader.readLine(); // skip first line
while (line != null && line.length() > 0) { reader.readLine();
os.write(line.getBytes());
os.write('\r');
os.write('\n');
line = reader.readLine(); line = reader.readLine();
while (line != null && line.length() > 0) {
os.write(line.getBytes());
os.write('\r');
os.write('\n');
line = reader.readLine();
}
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
} }
} }