Skip to content

Package: Namespaces

Namespaces

nameinstructionbranchcomplexitylinemethod
Namespaces(Response)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getNamespaces(Response)
M: 50 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.imap.protocol;
18:
19: import java.util.*;
20: import org.eclipse.angus.mail.iap.*;
21: import org.eclipse.angus.mail.iap.ProtocolException;
22: import org.eclipse.angus.mail.iap.Response;
23:
24: /**
25: * This class and its inner class represent the response to the
26: * NAMESPACE command. <p>
27: *
28: * See <A HREF="http://www.ietf.org/rfc/rfc2342.txt">RFC 2342</A>.
29: *
30: * @author Bill Shannon
31: */
32:
33: public class Namespaces {
34:
35: /**
36: * A single namespace entry.
37: */
38: public static class Namespace {
39:         /**
40:          * Prefix string for the namespace.
41:          */
42:         public String prefix;
43:
44:         /**
45:          * Delimiter between names in this namespace.
46:          */
47:         public char delimiter;
48:
49:         /**
50:          * Parse a namespace element out of the response.
51:          *
52:          * @param        r        the Response to parse
53:          * @exception ProtocolException for any protocol errors
54:          */
55:         public Namespace(Response r) throws ProtocolException {
56:          // Namespace_Element = "(" string SP (<"> QUOTED_CHAR <"> / nil)
57:          //                *(Namespace_Response_Extension) ")"
58:          if (!r.isNextNonSpace('('))
59:                 throw new ProtocolException(
60:                                         "Missing '(' at start of Namespace");
61:          // first, the prefix
62:          prefix = r.readString();
63:          if (!r.supportsUtf8())
64:                 prefix = BASE64MailboxDecoder.decode(prefix);
65:          r.skipSpaces();
66:          // delimiter is a quoted character or NIL
67:          if (r.peekByte() == '"') {
68:                 r.readByte();
69:                 delimiter = (char)r.readByte();
70:                 if (delimiter == '\\')
71:                  delimiter = (char)r.readByte();
72:                 if (r.readByte() != '"')
73:                  throw new ProtocolException(
74:                                  "Missing '\"' at end of QUOTED_CHAR");
75:          } else {
76:                 String s = r.readAtom();
77:                 if (s == null)
78:                  throw new ProtocolException("Expected NIL, got null");
79:                 if (!s.equalsIgnoreCase("NIL"))
80:                  throw new ProtocolException("Expected NIL, got " + s);
81:                 delimiter = 0;
82:          }
83:          // at end of Namespace data?
84:          if (r.isNextNonSpace(')'))
85:                 return;
86:
87:          // otherwise, must be a Namespace_Response_Extension
88:          // Namespace_Response_Extension = SP string SP
89:          //         "(" string *(SP string) ")"
90:          r.readString();
91:          r.skipSpaces();
92:          r.readStringList();
93:          if (!r.isNextNonSpace(')'))
94:                 throw new ProtocolException("Missing ')' at end of Namespace");
95:         }
96: };
97:
98: /**
99: * The personal namespaces.
100: * May be null.
101: */
102: public Namespace[] personal;
103:
104: /**
105: * The namespaces for other users.
106: * May be null.
107: */
108: public Namespace[] otherUsers;
109:
110: /**
111: * The shared namespace.
112: * May be null.
113: */
114: public Namespace[] shared;
115:
116: /**
117: * Parse out all the namespaces.
118: *
119: * @param        r        the Response to parse
120: * @throws        ProtocolException        for any protocol errors
121: */
122: public Namespaces(Response r) throws ProtocolException {
123:         personal = getNamespaces(r);
124:         otherUsers = getNamespaces(r);
125:         shared = getNamespaces(r);
126: }
127:
128: /**
129: * Parse out one of the three sets of namespaces.
130: */
131: private Namespace[] getNamespaces(Response r) throws ProtocolException {
132:         // Namespace = nil / "(" 1*( Namespace_Element) ")"
133:•        if (r.isNextNonSpace('(')) {
134:          List<Namespace> v = new ArrayList<>();
135:          do {
136:                 Namespace ns = new Namespace(r);
137:                 v.add(ns);
138:•         } while (!r.isNextNonSpace(')'));
139:          return v.toArray(new Namespace[v.size()]);
140:         } else {
141:          String s = r.readAtom();
142:•         if (s == null)
143:                 throw new ProtocolException("Expected NIL, got null");
144:•         if (!s.equalsIgnoreCase("NIL"))
145:                 throw new ProtocolException("Expected NIL, got " + s);
146:          return null;
147:         }
148: }
149: }