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.response;
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 codec The LDAP Service to use
059     * @param result the LdapResult to decorate
060     * @param message the associated message
061     */
062    public LdapResultDsml( LdapApiService codec, LdapResult result, Message message )
063    {
064        this.codec = codec;
065        this.result = result;
066        this.message = message;
067    }
068
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public Element toDsml( Element root )
075    {
076
077        // RequestID
078        int requestID = message.getMessageId();
079        if ( requestID > 0 )
080        {
081            root.addAttribute( "requestID", Integer.toString( requestID ) );
082        }
083
084        // Matched Dn
085        Dn matchedDn = result.getMatchedDn();
086
087        if ( !Dn.isNullOrEmpty( matchedDn ) )
088        {
089            root.addAttribute( "matchedDn", matchedDn.getName() );
090        }
091
092        // Controls
093        ParserUtils.addControls( codec, root, message.getControls().values(), false );
094
095        // ResultCode
096        Element resultCodeElement = root.addElement( "resultCode" );
097        resultCodeElement.addAttribute( "code", Integer.toString( result.getResultCode().getResultCode() ) );
098        resultCodeElement.addAttribute( "descr", result.getResultCode().getMessage() );
099
100        // ErrorMessage
101        String errorMessage = result.getDiagnosticMessage();
102
103        if ( ( errorMessage != null ) && ( errorMessage.length() != 0 ) )
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    @Override
132    public String getDiagnosticMessage()
133    {
134        return result.getDiagnosticMessage();
135    }
136
137
138    /**
139     * {@inheritDoc}
140     */
141    @Override
142    public void setDiagnosticMessage( String diagnosticMessage )
143    {
144        result.setDiagnosticMessage( diagnosticMessage );
145    }
146
147
148    /**
149     * Get the matched Dn
150     *
151     * @return Returns the matchedDN.
152     */
153    @Override
154    public Dn getMatchedDn()
155    {
156        return result.getMatchedDn();
157    }
158
159
160    /**
161     * Set the Matched Dn
162     *
163     * @param matchedDn The matchedDn to set.
164     */
165    @Override
166    public void setMatchedDn( Dn matchedDn )
167    {
168        result.setMatchedDn( matchedDn );
169    }
170
171
172    /**
173     * Get the referrals
174     *
175     * @return Returns the referrals.
176     */
177    public List<String> getReferrals()
178    {
179        return ( List<String> ) result.getReferral().getLdapUrls();
180    }
181
182
183    /**
184     * Add a referral
185     *
186     * @param referral The referral to add.
187     */
188    public void addReferral( LdapUrl referral )
189    {
190        result.getReferral().addLdapUrl( referral.toString() );
191    }
192
193
194    /**
195     * Get the result code
196     *
197     * @return Returns the resultCode.
198     */
199    @Override
200    public ResultCodeEnum getResultCode()
201    {
202        return result.getResultCode();
203    }
204
205
206    /**
207     * Set the result code
208     *
209     * @param resultCode The resultCode to set.
210     */
211    @Override
212    public void setResultCode( ResultCodeEnum resultCode )
213    {
214        result.setResultCode( resultCode );
215    }
216
217
218    /**
219     * {@inheritDoc}
220     */
221    @Override
222    public LdapResult getDecorated()
223    {
224        return result;
225    }
226
227
228    /**
229     * {@inheritDoc}
230     */
231    @Override
232    public boolean isReferral()
233    {
234        return getDecorated().isReferral();
235    }
236
237
238    /**
239     * {@inheritDoc}
240     */
241    @Override
242    public Referral getReferral()
243    {
244        return getDecorated().getReferral();
245    }
246
247
248    /**
249     * {@inheritDoc}
250     */
251    @Override
252    public void setReferral( Referral referral )
253    {
254        getDecorated().setReferral( referral );
255    }
256
257
258    /**
259     * {@inheritDoc}
260     */
261    @Override
262    public boolean isDefaultSuccess()
263    {
264        return false;
265    }
266}