📄 filemailbox.java
字号:
/** * Returns the message attributes for a message. * * @param uid unique identifier * @param username String represnting user * @return MessageAttributes for message, null if no such message. * Changing the MessageAttributes object must not affect the actual * MessageAttributes. * @throws AccessControlException if user does not have read rights for * this mailbox. * @throws AuthorizationException if user has lookup rights but does not * have delete rights. */ public synchronized MessageAttributes getMessageAttributesUID(int uid, String user) throws AccessControlException, AuthorizationException { if (!hasReadRights(user)) { //throws AccessControlException throw new AuthorizationException("Not authorized to read."); } System.out.println("getMessageAttributesUID()"); System.out.println("uid: "+uid); System.out.println("user: "+user); System.out.println("sequence.size: "+sequence.size()); SimpleMessageAttributes response = null; if (sequence.contains(new Integer(uid))) { System.out.println("reading from disk"); ObjectInputStream inAttrs = null; try { path = getPath( absoluteName, owner, rootPath ); System.out.println( "FileInputStream("+(path + File.separator + uid + ATTRIBUTES_EXTENSION)); inAttrs = new ObjectInputStream( new FileInputStream(path + File.separator + uid + ATTRIBUTES_EXTENSION)); System.out.println("inAttrs="+inAttrs); response = (SimpleMessageAttributes)inAttrs.readObject(); System.out.println("response="+response); if (response != null) { System.out.println("response.parts="+response.parts); if (response.parts != null) { System.out.println("response.parts.len="+response.parts.length); System.out.println("response.parts[0]="+response.parts[0]); } } setupLogger(response); } catch(Exception e) { getLogger().error("Error reading attributes from disc: " + e); e.printStackTrace(); throw new RuntimeException("Exception caught while retrieving Message attributes: " + e); } finally { try { inAttrs.close(); } catch (IOException ie) { getLogger().error("Error closing streams: " + ie); } } getLogger().info("MessageAttributes for " + uid + " read from " + absoluteName); return response; } else { return null; } } /** * Updates the attributes of a message.This may be incorporated into setFlags(). * * @param MessageAttributes of a message already in this Mailbox * @throws AccessControlException if user does not have read rights for * this mailbox. * @throws AuthorizationException if user has lookup rights but does not * have delete rights. */ public boolean updateMessageAttributes(MessageAttributes attrs, String user) throws AccessControlException, AuthorizationException { if (!hasKeepSeenRights(user)) { //throws AccessControlException throw new AuthorizationException("Not authorized to store flags."); } int uid = attrs.getUID(); if (sequence.contains(new Integer(uid))) { // Really, we should check whether the exact change is authorized. ObjectOutputStream outAttrs = null; try { path = getPath( absoluteName, owner, rootPath ); outAttrs = new ObjectOutputStream( new FileOutputStream(path + File.separator + uid + ATTRIBUTES_EXTENSION)); outAttrs.writeObject(attrs); outAttrs.close(); } catch(Exception e) { getLogger().error("Error writing message to disc: " + e); e.printStackTrace(); throw new RuntimeException("Exception caught while storing Attributes: " + e); } finally { try { outAttrs.close(); } catch (IOException ie) { getLogger().error("Error closing streams: " + ie); } } getLogger().info("MessageAttributes for " + uid + " written in " + absoluteName); return true; } else { return false; } } /** * Get the IMAP-formatted String of flags for specified message. * * @param msn message sequence number for a message in this mailbox * @param username String represnting user * @return flags for this message and user, null if no such message. * @throws AccessControlException if user does not have lookup rights for * this mailbox. * @throws AuthorizationException if user has lookup rights but does not * have read rights. */ public synchronized String getFlags(int msn, String user) throws AccessControlException, AuthorizationException { if (!hasReadRights(user)) { //throws AccessControlException throw new AuthorizationException("Not authorized to read."); } if (msn > sequence.size()) { return null; } else { int uid = ((Integer)sequence.get(msn - 1)).intValue(); return getFlagsUID(uid, user); } } /** * Get the IMAP-formatted String of flags for specified message. * * @param uid UniqueIdentifier for a message in this mailbox * @param username String represnting user * @return flags for this message and user, null if no such message. * @throws AccessControlException if user does not have lookup rights for * this mailbox. * @throws AuthorizationException if user has lookup rights but does not * have read rights. */ public synchronized String getFlagsUID(int uid, String user) throws AccessControlException, AuthorizationException { if (!hasReadRights(user)) { //throws AccessControlException throw new AuthorizationException("Not authorized to read."); } java.util.Iterator it = sequence.iterator(); while(it.hasNext()) System.out.println("FILEMESSAGES...."+it.next().toString()); if (!sequence.contains(new Integer(uid))) { System.out.println("SEQUENCENOOO"); return null; } else { System.out.println("FLAGSRETURNED"); Flags flags = readFlags(uid); return flags.getFlags(user); } } /** * Updates the flags for a message. * * @param msn MessageSequenceNumber of a message already in this Mailbox * @param username String represnting user * @param request IMAP-formatted String representing requested change to * flags. * @return true if succeeded, false otherwise, including no such message * @throws AccessControlException if user does not have read rights for * this mailbox. * @throws AuthorizationException if user has lookup rights but does not * have delete rights. */ public synchronized boolean setFlags(int msn, String user, String request) throws AccessControlException, AuthorizationException, IllegalArgumentException { if (!hasKeepSeenRights(user)) { //throws AccessControlException throw new AuthorizationException("Not authorized to store any flags."); } if (msn > sequence.size()) { return false; } else { int uid = ((Integer)sequence.get(msn - 1)).intValue(); return setFlagsUID(uid, user, request); } } /** * Updates the flags for a message. * * @param uid Unique Identifier of a message already in this Mailbox * @param username String represnting user * @param request IMAP-formatted String representing requested change to * flags. * @throws AccessControlException if user does not have read rights for * this mailbox. * @throws AuthorizationException if user has lookup rights but does not * have delete rights. */ public synchronized boolean setFlagsUID(int uid, String user, String request) throws AccessControlException, AuthorizationException, IllegalArgumentException { if (!hasKeepSeenRights(user)) { //throws AccessControlException throw new AuthorizationException("Not authorized to store any flags."); } if ((request.toUpperCase().indexOf("DELETED") != -1) && (!hasDeleteRights(user))) { //throws AccessControlException throw new AuthorizationException("Not authorized to delete."); } if (sequence.contains(new Integer(uid))) { Flags flags = readFlags(uid); boolean wasRecent = flags.isRecent(); boolean wasDeleted = flags.isDeleted(); boolean wasSeen = flags.isSeen(user); if (flags.setFlags(request, user)) { if (flags.isDeleted()) { if (! wasDeleted) { messagesForDeletion.add(new Integer(uid)); } } if (flags.isSeen(user) != wasSeen) { if (flags.isSeen(user)) { int previousOld = ((Integer)oldestUnseenMessage.get(user)).intValue(); if (uid == previousOld) { int newOld = findOldestUnseen(user, previousOld); oldestUnseenMessage.put(user, (new Integer(newOld))); } } else { // seen flag unset if (uid < ((Integer)oldestUnseenMessage.get(user)).intValue()) { oldestUnseenMessage.put(user, (new Integer(uid))); } } } writeFlags(uid, flags); getLogger().debug("Flags for message uid " + uid + " in " + absoluteName + " updated."); return true; } else { return false; } } else { return false; } } private int findOldestUnseen(String user, int previousOld) throws AccessControlException, AuthorizationException { int response = 0; //indicates no unseen messages try { ListIterator lit = sequence.listIterator(previousOld); boolean found = false; while (!found && lit.hasNext() ) { int uid = ((Integer)lit.next()).intValue(); Flags flags = readFlags(uid); if (!flags.isSeen(user)) { response = uid; found = true; } } }catch(Exception e) { // (because) BUG: Do nothing. Have investigated an error on fetching sequence.listIterator(previousOld); // with an error - but currently I don't know why :) }finally{ return response; } } private Flags readFlags(int uid) { Flags response = null; if (sequence.contains(new Integer(uid))) { ObjectInputStream inFlags = null; try { path = getPath( absoluteName, owner, rootPath ); inFlags = new ObjectInputStream( new FileInputStream(path + File.separator + uid + FLAGS_EXTENSION)); response = (Flags)inFlags.readObject(); } catch(Exception e) { getLogger().error("Error reading flags from disc: " + e); e.printStackTrace(); throw new RuntimeException("Exception caught while retrieving Message flags: " + e); } finally { try { inFlags.close(); } catch (IOException ie) { getLogger().error("Error closing streams: " + ie); } } getLogger().info("Flags for " + uid + " read from " + absoluteName); } return response; } private boolean writeFlags(int uid, Flags flags) { if (sequence.contains(new Integer(uid))) { ObjectOutputStream outFlags = null; try { path = getPath( absoluteName, owner, rootPath ); outFlags = new ObjectOutputStream( new FileOutputStream(path + File.separator + uid + FLAGS_EXTENSION)); outFlags.writeObject(flags); outFlags.close(); } catch(Exception e) { getLogger().error("Error writing message to disc: " + e); e.printStackTrace(); throw new RuntimeException("Exception caught while storing Flags: " + e); } finally { try { outFlags.close(); } catch (IOException ie) { getLogger().error("Error closing streams: " + ie); } } getLogger().info("Flags for " + uid + " written in " + absoluteName); writeMailbox(); return true; } else { writeMailbox(); return false; } } /** * Removes all messages marke
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -