001/*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: DomainComponent.java,v 1.4 2008/06/25 05:41:47 qcheng Exp $
026 *
027 * Portions Copyright 2015 ForgeRock AS.
028 */
029
030package com.iplanet.ums.dctree;
031
032import com.iplanet.services.ldap.Attr;
033import com.iplanet.services.util.I18n;
034import com.iplanet.sso.SSOException;
035import com.iplanet.sso.SSOToken;
036import com.iplanet.sso.SSOTokenManager;
037import com.iplanet.ums.Guid;
038import com.iplanet.ums.IUMSConstants;
039import com.iplanet.ums.PersistentObject;
040import com.iplanet.ums.UMSException;
041import com.iplanet.ums.UMSObject;
042import org.forgerock.opendj.ldap.ModificationType;
043
044/**
045 * Represents a domain component in the domain componet tree. Each Domain
046 * Component object represent a node in the dctree and each dc may be associated
047 * with an organization in the customer DIT (the convergence tree as noted in
048 * the nortel specication).
049 * 
050 * @see DomainComponentTree
051 * @supported.api
052 */
053public class DomainComponent extends PersistentObject {
054
055    static final String[] dcObjectClasses = { "top", "domain", "inetDomain" };
056
057    static final String DEFAULT_NAMING_ATTR = "dc";
058
059    static final String TAG_ORG_LINK = "inetDomainBaseDN";
060
061    static final String TAG_DOMAIN_STATUS = "inetDomainStatus";
062
063    private static I18n i18n = I18n.getInstance(IUMSConstants.UMS_PKG);
064
065    /**
066     * Default constructor
067     */
068    public DomainComponent() {
069    }
070
071    /**
072     * Given a name for domain component, construct
073     * the dc object in memory
074     * 
075     * @param dcName
076     *            Domain Componet name
077     * @supported.api
078     */
079    public DomainComponent(SSOToken token, String dcName) throws UMSException {
080        setAttribute(new Attr("objectclass", dcObjectClasses));
081        setAttribute(new Attr("dc", dcName));
082        try {
083            SSOTokenManager.getInstance().validateToken(token);
084        } catch (SSOException se) {
085            throw new UMSException(i18n.getString(IUMSConstants.INVALID_TOKEN),
086                    se);
087        }
088        m_token = token;
089    }
090
091    /**
092     * Sets the mapping of a dc entry in the dctree to an
093     * organization or OrganizationalUnit entry in the convergence DIT, the
094     * directory tree hosting all the actual entries
095     * 
096     * @param org
097     *            Organization entry to be associated with the dc entry in the
098     *            dctree
099     * @supported.api
100     */
101    public void setAssociatedOrganization(PersistentObject org)
102            throws UMSException {
103        setAssociatedOrganization(org.getGuid());
104    }
105
106    /**
107     * Sets the mapping of a dc entry in the dctree to an
108     * organization or organizational unit entry in the convergence DIT, the
109     * directory tree hosting all the actual entries.
110     * 
111     * @param orgGuid Identifier for organization.
112     * @throws UMSException if mapping of dc entry cannot be set.
113     * @supported.api
114     */
115    public void setAssociatedOrganization(Guid orgGuid) throws UMSException {
116        if (orgGuid == null || orgGuid.getDn().length() == 0) {
117            throw new IllegalArgumentException();
118        }
119
120        modify(new Attr(TAG_ORG_LINK, orgGuid.getDn()), ModificationType.REPLACE);
121
122        // Only save if it is already a persistent object
123        //
124        if (isPersistent()) {
125            save();
126        }
127    }
128
129    /**
130     * Get the domain mapping from this dc entry to the
131     * organization entry in the customer DIT. Return the organization object
132     * associated with thic dc entry
133     * 
134     * @return PersistentObject representing the organization entry associated
135     *         with dc entry
136     * @supported.api
137     */
138    public PersistentObject getOrganization() throws UMSException {
139
140        Guid orgGuid = getAssociatedOrganizationGuid();
141        return UMSObject.getObject(getSSOToken(), orgGuid);
142    }
143
144    /**
145     * Get the domain mapping from this dc entry. Domain
146     * mapping is in terms of DN
147     * 
148     * @return identifier for the domain mapping associated with this dc entry
149     * @supported.api
150     */
151    public Guid getAssociatedOrganizationGuid() throws UMSException {
152
153        Attr attr = getAttribute(TAG_ORG_LINK);
154
155        if (attr == null) {
156            return null;
157        }
158
159        return (new Guid(attr.getValue()));
160    }
161
162    /**
163     * Get the domain status in the dc entry
164     * 
165     * @return Domain status in the dc entry
166     * @supported.api
167     */
168    public String getDomainStatus() throws UMSException {
169        Attr attr = getAttribute(TAG_DOMAIN_STATUS);
170
171        if (attr == null) {
172            return null;
173        }
174
175        return attr.getValue();
176    }
177
178    /**
179     * Set the domain status in the dc entry
180     * 
181     * @param status
182     *            Domain status to be set
183     * @supported.api
184     */
185    public void setDomainStatus(String status) throws UMSException {
186        modify(new Attr(TAG_DOMAIN_STATUS, status), ModificationType.REPLACE);
187
188        // Only save if it is already a persistent object
189        //
190        if (isPersistent()) {
191            save();
192        }
193    }
194
195    /**
196     * Get the naming attribute for the dc entry
197     * @supported.api
198     */
199    public String getNamingAttribute() {
200        return (DEFAULT_NAMING_ATTR);
201    }
202
203    /**
204     * get the SSO Token
205     * 
206     * @return SSOToken authenticated SSO token
207     */
208    SSOToken getSSOToken() {
209        return m_token;
210    }
211
212    /**
213     * set the SSO Token
214     */
215    void setSSOToken(SSOToken token) throws UMSException {
216        try {
217            SSOTokenManager.getInstance().validateToken(token);
218        } catch (SSOException se) {
219            throw new UMSException(i18n.getString(IUMSConstants.INVALID_TOKEN),
220                    se);
221        }
222        m_token = token;
223    }
224
225    private SSOToken m_token = null;
226
227}