View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.dsmlv2.response;
21  
22  
23  import java.util.Collection;
24  import java.util.List;
25  
26  import org.apache.directory.api.dsmlv2.DsmlDecorator;
27  import org.apache.directory.api.dsmlv2.ParserUtils;
28  import org.apache.directory.api.ldap.codec.api.LdapApiService;
29  import org.apache.directory.api.ldap.model.message.LdapResult;
30  import org.apache.directory.api.ldap.model.message.Message;
31  import org.apache.directory.api.ldap.model.message.Referral;
32  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
33  import org.apache.directory.api.ldap.model.name.Dn;
34  import org.apache.directory.api.ldap.model.url.LdapUrl;
35  import org.dom4j.Element;
36  
37  
38  /**
39   * DSML Decorator for the LdapResult class.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public class LdapResultDsml implements DsmlDecorator<LdapResult>, LdapResult
44  {
45      /** The LDAP Result to decorate */
46      private LdapResult result;
47  
48      /** The associated LDAP Message */
49      private Message message;
50  
51      /** The ldap codec service */
52      private LdapApiService codec;
53  
54  
55      /**
56       * Creates a new instance of LdapResultDsml.
57       *
58       * @param codec The LDAP Service to use
59       * @param result the LdapResult to decorate
60       * @param message the associated message
61       */
62      public LdapResultDsml( LdapApiService codec, LdapResult result, Message message )
63      {
64          this.codec = codec;
65          this.result = result;
66          this.message = message;
67      }
68  
69  
70      /**
71       * {@inheritDoc}
72       */
73      public Element toDsml( Element root )
74      {
75  
76          // RequestID
77          int requestID = message.getMessageId();
78          if ( requestID > 0 )
79          {
80              root.addAttribute( "requestID", Integer.toString( requestID ) );
81          }
82  
83          // Matched Dn
84          Dn matchedDn = result.getMatchedDn();
85  
86          if ( !Dn.isNullOrEmpty( matchedDn ) )
87          {
88              root.addAttribute( "matchedDn", matchedDn.getName() );
89          }
90  
91          // Controls
92          ParserUtils.addControls( codec, root, message.getControls().values() );
93  
94          // ResultCode
95          Element resultCodeElement = root.addElement( "resultCode" );
96          resultCodeElement.addAttribute( "code", Integer.toString( result.getResultCode().getResultCode() ) );
97          resultCodeElement.addAttribute( "descr", result.getResultCode().getMessage() );
98  
99          // ErrorMessage
100         String errorMessage = ( result.getDiagnosticMessage() );
101         
102         if ( ( errorMessage != null ) && ( errorMessage.length() != 0 ) )
103         {
104             Element errorMessageElement = root.addElement( "errorMessage" );
105             errorMessageElement.addText( errorMessage );
106         }
107 
108         // Referrals
109         Referral referral = result.getReferral();
110         if ( referral != null )
111         {
112             Collection<String> ldapUrls = referral.getLdapUrls();
113             if ( ldapUrls != null )
114             {
115                 for ( String ldapUrl : ldapUrls )
116                 {
117                     Element referalElement = root.addElement( "referal" );
118                     referalElement.addText( ldapUrl );
119                 }
120             }
121         }
122 
123         return root;
124     }
125 
126 
127     /**
128      * {@inheritDoc}
129      */
130     public String getDiagnosticMessage()
131     {
132         return result.getDiagnosticMessage();
133     }
134 
135 
136     /**
137      * {@inheritDoc}
138      */
139     public void setDiagnosticMessage( String diagnosticMessage )
140     {
141         result.setDiagnosticMessage( diagnosticMessage );
142     }
143 
144 
145     /**
146      * Get the matched Dn
147      * 
148      * @return Returns the matchedDN.
149      */
150     public Dn getMatchedDn()
151     {
152         return result.getMatchedDn();
153     }
154 
155 
156     /**
157      * Set the Matched Dn
158      * 
159      * @param matchedDn The matchedDn to set.
160      */
161     public void setMatchedDn( Dn matchedDn )
162     {
163         result.setMatchedDn( matchedDn );
164     }
165 
166 
167     /**
168      * Get the referrals
169      * 
170      * @return Returns the referrals.
171      */
172     public List<String> getReferrals()
173     {
174         return ( List<String> ) result.getReferral().getLdapUrls();
175     }
176 
177 
178     /**
179      * Add a referral
180      * 
181      * @param referral The referral to add.
182      */
183     public void addReferral( LdapUrl referral )
184     {
185         result.getReferral().addLdapUrl( referral.toString() );
186     }
187 
188 
189     /**
190      * Get the result code
191      * 
192      * @return Returns the resultCode.
193      */
194     public ResultCodeEnum getResultCode()
195     {
196         return result.getResultCode();
197     }
198 
199 
200     /**
201      * Set the result code
202      * 
203      * @param resultCode The resultCode to set.
204      */
205     public void setResultCode( ResultCodeEnum resultCode )
206     {
207         result.setResultCode( resultCode );
208     }
209 
210 
211     /**
212      * {@inheritDoc}
213      */
214     public LdapResult getDecorated()
215     {
216         return result;
217     }
218 
219 
220     /**
221      * {@inheritDoc}
222      */
223     public boolean isReferral()
224     {
225         return getDecorated().isReferral();
226     }
227 
228 
229     /**
230      * {@inheritDoc}
231      */
232     public Referral getReferral()
233     {
234         return getDecorated().getReferral();
235     }
236 
237 
238     /**
239      * {@inheritDoc}
240      */
241     public void setReferral( Referral referral )
242     {
243         getDecorated().setReferral( referral );
244     }
245 
246 
247     /**
248      * {@inheritDoc}
249      */
250     public boolean isDefaultSuccess()
251     {
252         return false;
253     }
254 }