001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.dsmlv2.reponse;
021
022
023import java.util.Collection;
024import java.util.List;
025
026import org.apache.directory.api.dsmlv2.DsmlDecorator;
027import org.apache.directory.api.dsmlv2.ParserUtils;
028import org.apache.directory.api.ldap.codec.api.LdapApiService;
029import org.apache.directory.api.ldap.model.message.LdapResult;
030import org.apache.directory.api.ldap.model.message.Message;
031import org.apache.directory.api.ldap.model.message.Referral;
032import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
033import org.apache.directory.api.ldap.model.name.Dn;
034import org.apache.directory.api.ldap.model.url.LdapUrl;
035import org.dom4j.Element;
036
037
038/**
039 * DSML Decorator for the LdapResult class.
040 *
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public class LdapResultDsml implements DsmlDecorator<LdapResult>, LdapResult
044{
045    /** The LDAP Result to decorate */
046    private LdapResult result;
047
048    /** The associated LDAP Message */
049    private Message message;
050
051    /** The ldap codec service */
052    private LdapApiService codec;
053
054
055    /**
056     * Creates a new instance of LdapResultDsml.
057     *
058     * @param result
059     *      the LdapResult to decorate
060     * @param message
061     *      the associated message
062     * @param the ldap codec service 
063     */
064    public LdapResultDsml( LdapApiService codec, LdapResult result, Message message )
065    {
066        this.codec = codec;
067        this.result = result;
068        this.message = message;
069    }
070
071
072    /**
073     * {@inheritDoc}
074     */
075    public Element toDsml( Element root )
076    {
077
078        // RequestID
079        int requestID = message.getMessageId();
080        if ( requestID > 0 )
081        {
082            root.addAttribute( "requestID", "" + requestID );
083        }
084
085        // Matched Dn
086        Dn matchedDn = result.getMatchedDn();
087
088        if ( !Dn.isNullOrEmpty( matchedDn ) )
089        {
090            root.addAttribute( "matchedDn", matchedDn.getName() );
091        }
092
093        // Controls
094        ParserUtils.addControls( codec, root, message.getControls().values() );
095
096        // ResultCode
097        Element resultCodeElement = root.addElement( "resultCode" );
098        resultCodeElement.addAttribute( "code", "" + result.getResultCode().getResultCode() );
099        resultCodeElement.addAttribute( "descr", result.getResultCode().getMessage() );
100
101        // ErrorMessage
102        String errorMessage = ( result.getDiagnosticMessage() );
103        if ( ( errorMessage != null ) && ( !errorMessage.equals( "" ) ) )
104        {
105            Element errorMessageElement = root.addElement( "errorMessage" );
106            errorMessageElement.addText( errorMessage );
107        }
108
109        // Referrals
110        Referral referral = result.getReferral();
111        if ( referral != null )
112        {
113            Collection<String> ldapUrls = referral.getLdapUrls();
114            if ( ldapUrls != null )
115            {
116                for ( String ldapUrl : ldapUrls )
117                {
118                    Element referalElement = root.addElement( "referal" );
119                    referalElement.addText( ldapUrl );
120                }
121            }
122        }
123
124        return root;
125    }
126
127
128    /**
129     * {@inheritDoc}
130     */
131    public String getDiagnosticMessage()
132    {
133        return result.getDiagnosticMessage();
134    }
135
136
137    /**
138     * {@inheritDoc}
139     */
140    public void setDiagnosticMessage( String diagnosticMessage )
141    {
142        result.setDiagnosticMessage( diagnosticMessage );
143    }
144
145
146    /**
147     * Get the matched Dn
148     * 
149     * @return Returns the matchedDN.
150     */
151    public Dn getMatchedDn()
152    {
153        return result.getMatchedDn();
154    }
155
156
157    /**
158     * Set the Matched Dn
159     * 
160     * @param matchedDn The matchedDn to set.
161     */
162    public void setMatchedDn( Dn matchedDn )
163    {
164        result.setMatchedDn( matchedDn );
165    }
166
167
168    /**
169     * Get the referrals
170     * 
171     * @return Returns the referrals.
172     */
173    public List<String> getReferrals()
174    {
175        return ( List<String> ) result.getReferral().getLdapUrls();
176    }
177
178
179    /**
180     * Add a referral
181     * 
182     * @param referral The referral to add.
183     */
184    public void addReferral( LdapUrl referral )
185    {
186        result.getReferral().addLdapUrl( referral.toString() );
187    }
188
189
190    /**
191     * Get the result code
192     * 
193     * @return Returns the resultCode.
194     */
195    public ResultCodeEnum getResultCode()
196    {
197        return result.getResultCode();
198    }
199
200
201    /**
202     * Set the result code
203     * 
204     * @param resultCode The resultCode to set.
205     */
206    public void setResultCode( ResultCodeEnum resultCode )
207    {
208        result.setResultCode( resultCode );
209    }
210
211
212    /**
213     * {@inheritDoc}
214     */
215    public LdapResult getDecorated()
216    {
217        return result;
218    }
219
220
221    /**
222     * {@inheritDoc}
223     */
224    public boolean isReferral()
225    {
226        return getDecorated().isReferral();
227    }
228
229
230    /**
231     * {@inheritDoc}
232     */
233    public Referral getReferral()
234    {
235        return getDecorated().getReferral();
236    }
237
238
239    /**
240     * {@inheritDoc}
241     */
242    public void setReferral( Referral referral )
243    {
244        getDecorated().setReferral( referral );
245    }
246
247
248    /**
249     * {@inheritDoc}
250     */
251    public boolean isDefaultSuccess()
252    {
253        return false;
254    }
255}