1/*
2 * Copyright (c) 1999, 2002, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.jndi.ldap;
27
28import java.io.IOException;
29import javax.naming.*;
30import javax.naming.directory.*;
31
32/**
33 * This class implements the LDAPv3 Response Control for entry-change
34 * notification as defined in
35 * <a href="http://www.ietf.org/internet-drafts/draft-ietf-ldapext-psearch-02.txt">draft-ietf-ldapext-psearch-02.txt</a>.
36 *
37 * The control's value has the following ASN.1 definition:
38 * <pre>
39 *
40 *     EntryChangeNotification ::= SEQUENCE {
41 *         changeType ENUMERATED {
42 *             add              (1),
43 *             delete           (2),
44 *             modify           (4),
45 *             modDN            (8)
46 *         },
47 *         previousDN   LDAPDN OPTIONAL,        -- modifyDN ops. only
48 *         changeNumber INTEGER OPTIONAL,       -- if supported
49 *    }
50 *
51 * </pre>
52 *
53 * @see PersistentSearchControl
54 * @see com.sun.jndi.ldap.ctl.ResponseControlFactory ResponseControlFactory
55 * @author Vincent Ryan
56 */
57final public class EntryChangeResponseControl extends BasicControl {
58
59    /**
60     * The entry-change response control's assigned object identifier
61     * is 2.16.840.1.113730.3.4.7.
62     */
63    public static final String OID = "2.16.840.1.113730.3.4.7";
64
65    /**
66     * Indicates an entry which has been added.
67     */
68    public static final int ADD = 1;
69
70    /**
71     * Indicates an entry which has been deleted.
72     */
73    public static final int DELETE = 2;
74
75    /**
76     * Indicates an entry which has been modified.
77     */
78    public static final int MODIFY = 4;
79
80    /**
81     * Indicates an entry which has been renamed.
82     */
83    public static final int RENAME = 8;
84
85    /**
86     * The type of change that occurred.
87     *
88     * @serial
89     */
90    private int changeType;
91
92    /**
93     * The previous distinguished name (only applies to RENAME changes).
94     *
95     * @serial
96     */
97    private String previousDN = null;
98
99    /**
100     * The change number (if supported by the server).
101     *
102     * @serial
103     */
104    private long changeNumber = -1L;
105
106    private static final long serialVersionUID = -2087354136750180511L;
107
108    /**
109     * Constructs a new instance of EntryChangeResponseControl.
110     *
111     * @param   id              The control's object identifier string.
112     * @param   criticality     The control's criticality.
113     * @param   value           The control's ASN.1 BER encoded value.
114     *                          May be null.
115     * @exception               IOException if an error is encountered
116     *                          while decoding the control's value.
117     */
118    public EntryChangeResponseControl(String id, boolean criticality,
119        byte[] value) throws IOException {
120
121        super(id, criticality, value);
122
123        // decode value
124        if ((value != null) && (value.length > 0)) {
125            BerDecoder ber = new BerDecoder(value, 0, value.length);
126
127            ber.parseSeq(null);
128            changeType = ber.parseEnumeration();
129
130            if ((ber.bytesLeft() > 0) && (ber.peekByte() == Ber.ASN_OCTET_STR)){
131                previousDN = ber.parseString(true);
132            }
133            if ((ber.bytesLeft() > 0) && (ber.peekByte() == Ber.ASN_INTEGER)) {
134                changeNumber = ber.parseInt();
135            }
136        }
137    }
138
139    /**
140     * Retrieves the type of change that occurred.
141     *
142     * @return    The type of change.
143     */
144    public int getChangeType() {
145        return changeType;
146    }
147
148    /**
149     * Retrieves the previous distinguished name of the entry before it was
150     * renamed and/or moved. This method applies only to RENAME changes.
151     *
152     * @return    The previous distinguished name or null if not applicable.
153     */
154    public String getPreviousDN() {
155        return previousDN;
156    }
157
158    /**
159     * Retrieves the change number assigned by the server for this change.
160     * Returns -1 if this feature is not supported by the server.
161     *
162     * @return    The change number or -1 if unsupported.
163     */
164    public long getChangeNumber() {
165        return changeNumber;
166    }
167}
168