Skip to contentPackage: ID
ID
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.Argument;
22: import org.eclipse.angus.mail.iap.ProtocolException;
23: import org.eclipse.angus.mail.iap.Response;
24:
25: /**
26: * This class represents the response to the ID command. <p>
27: *
28: * See <A HREF="http://www.ietf.org/rfc/rfc2971.txt">RFC 2971</A>.
29: *
30: * @since JavaMail 1.5.1
31: * @author Bill Shannon
32: */
33:
34: public class ID {
35:
36: private Map<String, String> serverParams = null;
37:
38: /**
39: * Parse the server parameter list out of the response.
40: *
41: * @param        r        the response
42: * @exception ProtocolException for protocol failures
43: */
44: public ID(Response r) throws ProtocolException {
45:         // id_response ::= "ID" SPACE id_params_list
46:         // id_params_list ::= "(" #(string SPACE nstring) ")" / nil
47:         // ;; list of field value pairs
48:
49:         r.skipSpaces();
50:         int c = r.peekByte();
51:•        if (c == 'N' || c == 'n')        // assume NIL
52:          return;
53:
54:•        if (c != '(')
55:          throw new ProtocolException("Missing '(' at start of ID");
56:
57:         serverParams = new HashMap<>();
58:
59:         String[] v = r.readStringList();
60:•        if (v != null) {
61:•         for (int i = 0; i < v.length; i += 2) {
62:                 String name = v[i];
63:•                if (name == null)
64:                  throw new ProtocolException("ID field name null");
65:•                if (i + 1 >= v.length)
66:                  throw new ProtocolException("ID field without value: " +
67:                                                                         name);
68:                 String value = v[i + 1];
69:                 serverParams.put(name, value);
70:          }
71:         }
72:         serverParams = Collections.unmodifiableMap(serverParams);
73: }
74:
75: /**
76: * Return the parsed server params.
77: */
78: Map<String, String> getServerParams() {
79:         return serverParams;
80: }
81:
82: /**
83: * Convert the client parameters into an argument list for the ID command.
84: */
85: static Argument getArgumentList(Map<String,String> clientParams) {
86:         Argument arg = new Argument();
87:•        if (clientParams == null) {
88:          arg.writeAtom("NIL");
89:          return arg;
90:         }
91:         Argument list = new Argument();
92:         // add params to list
93:•        for (Map.Entry<String, String> e : clientParams.entrySet()) {
94:          list.writeNString(e.getKey());        // assume these are ASCII only
95:          list.writeNString(e.getValue());
96:         }
97:         arg.writeArgument(list);
98:         return arg;
99: }
100: }