1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright (c) 1997-2018 Oracle and/or its affiliates. All rights reserved.
5  *
6  * The contents of this file are subject to the terms of either the GNU
7  * General Public License Version 2 only ("GPL") or the Common Development
8  * and Distribution License("CDDL") (collectively, the "License").  You
9  * may not use this file except in compliance with the License.  You can
10  * obtain a copy of the License at
11  * https://oss.oracle.com/licenses/CDDL+GPL-1.1
12  * or LICENSE.txt.  See the License for the specific
13  * language governing permissions and limitations under the License.
14  *
15  * When distributing the software, include this License Header Notice in each
16  * file and include the License file at LICENSE.txt.
17  *
18  * GPL Classpath Exception:
19  * Oracle designates this particular file as subject to the "Classpath"
20  * exception as provided by Oracle in the GPL Version 2 section of the License
21  * file that accompanied this code.
22  *
23  * Modifications:
24  * If applicable, add the following below the License Header, with the fields
25  * enclosed by brackets [] replaced by your own identifying information:
26  * "Portions Copyright [year] [name of copyright owner]"
27  *
28  * Contributor(s):
29  * If you wish your version of this file to be governed by only the CDDL or
30  * only the GPL Version 2, indicate your decision by adding "[Contributor]
31  * elects to include this software in this distribution under the [CDDL or GPL
32  * Version 2] license."  If you don't indicate a single choice of license, a
33  * recipient has the option to distribute your version of this file under
34  * either the CDDL, the GPL Version 2 or to extend the choice of license to
35  * its licensees as provided above.  However, if you add GPL Version 2 code
36  * and therefore, elected the GPL Version 2 license, then the option applies
37  * only if the new code is made subject to such option by the copyright
38  * holder.
39  */
40 
41 package com.sun.mail.imap;
42 
43 import javax.mail.Folder;
44 import javax.mail.Message;
45 import javax.mail.MessagingException;
46 import javax.mail.MethodNotSupportedException;
47 import com.sun.mail.iap.ProtocolException;
48 import com.sun.mail.imap.protocol.IMAPProtocol;
49 import com.sun.mail.imap.protocol.ListInfo;
50 
51 /**
52  * The default IMAP folder (root of the naming hierarchy).
53  *
54  * @author  John Mani
55  */
56 
57 public class DefaultFolder extends IMAPFolder {
58 
DefaultFolder(IMAPStore store)59     protected DefaultFolder(IMAPStore store) {
60 	super("", UNKNOWN_SEPARATOR, store, null);
61 	exists = true; // of course
62 	type = HOLDS_FOLDERS; // obviously
63     }
64 
65     @Override
getName()66     public synchronized String getName() {
67 	return fullName;
68     }
69 
70     @Override
getParent()71     public Folder getParent() {
72 	return null;
73     }
74 
75     @Override
list(final String pattern)76     public synchronized Folder[] list(final String pattern)
77 				throws MessagingException {
78 	ListInfo[] li = null;
79 
80 	li = (ListInfo[])doCommand(new ProtocolCommand() {
81 	    @Override
82 	    public Object doCommand(IMAPProtocol p) throws ProtocolException {
83 		return p.list("", pattern);
84 	    }
85 	});
86 
87 	if (li == null)
88 	    return new Folder[0];
89 
90 	IMAPFolder[] folders = new IMAPFolder[li.length];
91 	for (int i = 0; i < folders.length; i++)
92 	    folders[i] = ((IMAPStore)store).newIMAPFolder(li[i]);
93 	return folders;
94     }
95 
96     @Override
listSubscribed(final String pattern)97     public synchronized Folder[] listSubscribed(final String pattern)
98 				throws MessagingException {
99 	ListInfo[] li = null;
100 
101 	li = (ListInfo[])doCommand(new ProtocolCommand() {
102 	    @Override
103 	    public Object doCommand(IMAPProtocol p) throws ProtocolException {
104 		return p.lsub("", pattern);
105 	    }
106 	});
107 
108 	if (li == null)
109 	    return new Folder[0];
110 
111 	IMAPFolder[] folders = new IMAPFolder[li.length];
112 	for (int i = 0; i < folders.length; i++)
113 	    folders[i] = ((IMAPStore)store).newIMAPFolder(li[i]);
114 	return folders;
115     }
116 
117     @Override
hasNewMessages()118     public boolean hasNewMessages() throws MessagingException {
119 	// Not applicable on DefaultFolder
120 	return false;
121     }
122 
123     @Override
getFolder(String name)124     public Folder getFolder(String name) throws MessagingException {
125 	return ((IMAPStore)store).newIMAPFolder(name, UNKNOWN_SEPARATOR);
126     }
127 
128     @Override
delete(boolean recurse)129     public boolean delete(boolean recurse) throws MessagingException {
130 	// Not applicable on DefaultFolder
131 	throw new MethodNotSupportedException("Cannot delete Default Folder");
132     }
133 
134     @Override
renameTo(Folder f)135     public boolean renameTo(Folder f) throws MessagingException {
136 	// Not applicable on DefaultFolder
137 	throw new MethodNotSupportedException("Cannot rename Default Folder");
138     }
139 
140     @Override
appendMessages(Message[] msgs)141     public void appendMessages(Message[] msgs) throws MessagingException {
142 	// Not applicable on DefaultFolder
143 	throw new MethodNotSupportedException("Cannot append to Default Folder");
144     }
145 
146     @Override
expunge()147     public Message[] expunge() throws MessagingException {
148 	// Not applicable on DefaultFolder
149 	throw new MethodNotSupportedException("Cannot expunge Default Folder");
150     }
151 }
152