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.reponse;
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 result
59       *      the LdapResult to decorate
60       * @param message
61       *      the associated message
62       * @param the ldap codec service 
63       */
64      public LdapResultDsml( LdapApiService codec, LdapResult result, Message message )
65      {
66          this.codec = codec;
67          this.result = result;
68          this.message = message;
69      }
70  
71  
72      /**
73       * {@inheritDoc}
74       */
75      public Element toDsml( Element root )
76      {
77  
78          // RequestID
79          int requestID = message.getMessageId();
80          if ( requestID > 0 )
81          {
82              root.addAttribute( "requestID", "" + requestID );
83          }
84  
85          // Matched Dn
86          Dn matchedDn = result.getMatchedDn();
87  
88          if ( !Dn.isNullOrEmpty( matchedDn ) )
89          {
90              root.addAttribute( "matchedDn", matchedDn.getName() );
91          }
92  
93          // Controls
94          ParserUtils.addControls( codec, root, message.getControls().values() );
95  
96          // ResultCode
97          Element resultCodeElement = root.addElement( "resultCode" );
98          resultCodeElement.addAttribute( "code", "" + result.getResultCode().getResultCode() );
99          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 }