use of i2p.bote.fileencryption.PasswordException in project i2p.i2p-bote by i2p.
the class ContactsCompletionView method defaultObject.
@Override
protected Object defaultObject(String completionText) {
// Stupid simple example of guessing if we have an email or not
int index = completionText.indexOf('@');
if (index == -1) {
try {
// Check if it is a known Destination
Contact c = BoteHelper.getContact(completionText);
if (c != null)
return new Person(c.getName(), c.getBase64Dest(), BoteHelper.decodePicture(c.getPictureBase64()));
// Check if it is a name
SortedSet<Contact> contacts = I2PBote.getInstance().getAddressBook().getAll();
for (Contact contact : contacts) {
if (contact.getName().startsWith(completionText))
return new Person(contact.getName(), contact.getBase64Dest(), BoteHelper.decodePicture(contact.getPictureBase64()));
}
// Try as a new Destination
try {
new EmailDestination(completionText);
return new Person(completionText.substring(0, 5), completionText, null);
} catch (GeneralSecurityException e) {
// Not a valid Destination
// Assume the user meant an external address
completionText = completionText.replace(" ", "") + "@example.com";
return new Person(completionText, completionText, null, true);
}
} catch (PasswordException e) {
// TODO handle
completionText = completionText.replace(" ", "") + "@example.com";
return new Person(completionText, completionText, null, true);
}
} else {
return new Person(completionText, completionText, null, true);
}
}
use of i2p.bote.fileencryption.PasswordException in project i2p.i2p-bote by i2p.
the class ShowAttachment method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String folderName = request.getParameter("folder");
String messageID = request.getParameter("messageID");
Email email;
try {
email = JSPHelper.getEmail(folderName, messageID);
} catch (PasswordException e) {
throw new ServletException(e);
}
if (email == null)
throw new ServletException("Message ID <" + messageID + "> not found in folder <" + folderName + ">.");
List<Part> parts = null;
try {
parts = email.getParts();
} catch (MessagingException e) {
throw new ServletException("Can't parse mail. Message ID=" + messageID, e);
}
String partIndexStr = request.getParameter("part");
int partIndex = -1;
try {
partIndex = Integer.valueOf(partIndexStr);
} catch (NumberFormatException e) {
}
if (partIndex < 0 || partIndex >= parts.size())
throw new ServletException("Invalid part index: <" + partIndexStr + ">, must be a number between 0 and " + (parts.size() - 1));
Part part = parts.get(partIndex);
try {
response.setContentType(part.getContentType());
String[] dispositionHeaders = part.getHeader("Content-Disposition");
if (dispositionHeaders == null || dispositionHeaders.length == 0)
response.setHeader("Content-Disposition", "attachment; filename=attachment");
else
response.setHeader("Content-Disposition", dispositionHeaders[0]);
} catch (MessagingException e) {
log.error("Can't get MIME type of part " + partIndex + ". Message ID=" + messageID, e);
}
// write the attachments' content to the servlet response stream
try {
InputStream input = part.getInputStream();
OutputStream output = response.getOutputStream();
byte[] buffer = new byte[1024];
while (true) {
int bytesRead = input.read(buffer);
if (bytesRead <= 0)
return;
output.write(buffer, 0, bytesRead);
}
} catch (MessagingException e) {
throw new ServletException("Can't get MIME type of part " + partIndex + ". Message ID=" + messageID, e);
}
}
use of i2p.bote.fileencryption.PasswordException in project i2p.i2p-bote by i2p.
the class ExportIdentities method doPost.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String password = request.getParameter("nofilter_password");
String confirm = request.getParameter("nofilter_confirm");
if (password != null && !password.equals(confirm))
throw new ServletException("Passwords do not match.");
if ("".equals(password))
password = null;
if (password == null) {
response.setContentType("text/plain");
response.setHeader("Content-Disposition", "attachment; filename=identities.txt");
} else {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=identities.bote");
}
// write the identities to the servlet response stream
OutputStream output = response.getOutputStream();
try {
I2PBote.getInstance().getIdentities().export(output, password);
} catch (GeneralSecurityException e) {
throw new ServletException("Failed to export identities", e);
} catch (PasswordException e) {
throw new ServletException("You are not logged in to I2P-Bote");
}
}
use of i2p.bote.fileencryption.PasswordException in project i2p.i2p-bote by i2p.
the class Folder method iterate.
/**
* An {@link Iterator} implementation that loads one file into memory at a time.<br/>
* Files that cannot be read or contain invalid data are skipped.
*/
public final FolderIterator<T> iterate() {
final File[] files = getFilenames();
log.debug(files.length + " files with the extension '" + fileExtension + "' found in '" + storageDir + "'.");
return new FolderIterator<T>() {
Iterator<File> fileIterator = Arrays.asList(files).iterator();
// the next value to return
T nextElement;
// the file that corresponds to the last element returned by next()
File lastFile;
// the last file read in findNextElement()
File currentFile;
@Override
public boolean hasNext() throws PasswordException {
if (nextElement == null)
findNextElement();
return nextElement != null;
}
@Override
public T next() throws PasswordException {
if (nextElement == null)
findNextElement();
if (nextElement == null)
throw new NoSuchElementException("No more folder elements!");
else {
lastFile = currentFile;
T retVal = nextElement;
findNextElement();
return retVal;
}
}
/**
* Reads the next valid file into <code>currentElement</code>.<br/>
* If there are no more files, <code>currentElement</code> is set to <code>null</code>.
* <p/>
* <code>currentFile</code> is set to the last file read.
* @param updateCurrentFile
* @throws PasswordException
*/
void findNextElement() throws PasswordException {
while (fileIterator.hasNext()) {
currentFile = fileIterator.next();
String filePath = currentFile.getAbsolutePath();
log.debug("Reading file: '" + filePath + "'");
try {
nextElement = createFolderElement(currentFile);
if (nextElement != null)
return;
} catch (PasswordException e) {
throw e;
} catch (Exception e) {
log.error("Can't create a FolderElement from file: " + filePath + " (file size=" + currentFile.length() + ")", e);
}
}
nextElement = null;
}
@Override
public void remove() {
if (lastFile == null)
throw new IllegalStateException("remove() was called before next()");
if (!lastFile.delete())
log.error("Can't delete file: <" + lastFile.getAbsolutePath() + ">");
}
};
}
use of i2p.bote.fileencryption.PasswordException in project i2p.i2p-bote by i2p.
the class BoteMailbox method startListening.
protected void startListening() {
folderListener = new FolderListener() {
public void elementAdded(String messageId) {
try {
// Add new emails to map
Email email = folder.getEmail(messageId);
email.setFlag(Flag.RECENT, true);
messageMap.put(email, new BoteMessage(email, getMailboxId()));
updateMessages();
} catch (PasswordException e) {
throw new RuntimeException(_t("Password required or invalid password provided"), e);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void elementUpdated() {
// Noop, BoteMessage has a reference to the Email
}
public void elementRemoved(String messageId) {
// Remove old email from map
Set<Email> emails = messageMap.keySet();
Iterator<Email> iter = emails.iterator();
while (iter.hasNext()) {
Email email = iter.next();
if (email.getMessageID().equals(messageId)) {
iter.remove();
break;
}
}
updateMessages();
}
};
folder.addFolderListener(folderListener);
}
Aggregations