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   *    https://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.DsmlLiterals;
28  import org.apache.directory.api.dsmlv2.ParserUtils;
29  import org.apache.directory.api.ldap.codec.api.LdapApiService;
30  import org.apache.directory.api.ldap.model.message.LdapResult;
31  import org.apache.directory.api.ldap.model.message.Message;
32  import org.apache.directory.api.ldap.model.message.Referral;
33  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
34  import org.apache.directory.api.ldap.model.name.Dn;
35  import org.apache.directory.api.ldap.model.url.LdapUrl;
36  import org.dom4j.Element;
37  
38  
39  /**
40   * DSML Decorator for the LdapResult class.
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  public class LdapResultDsml implements DsmlDecorator<LdapResult>, LdapResult
45  {
46      /** The LDAP Result to decorate */
47      private LdapResult result;
48  
49      /** The associated LDAP Message */
50      private Message message;
51  
52      /** The ldap codec service */
53      private LdapApiService codec;
54  
55  
56      /**
57       * Creates a new instance of LdapResultDsml.
58       *
59       * @param codec The LDAP Service to use
60       * @param result the LdapResult to decorate
61       * @param message the associated message
62       */
63      public LdapResultDsml( LdapApiService codec, LdapResult result, Message message )
64      {
65          this.codec = codec;
66          this.result = result;
67          this.message = message;
68      }
69  
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public Element toDsml( Element root )
76      {
77  
78          // RequestID
79          int requestID = message.getMessageId();
80          if ( requestID > 0 )
81          {
82              root.addAttribute( DsmlLiterals.REQUEST_ID, Integer.toString( requestID ) );
83          }
84  
85          // Matched Dn
86          Dn matchedDn = result.getMatchedDn();
87  
88          if ( !Dn.isNullOrEmpty( matchedDn ) )
89          {
90              root.addAttribute( DsmlLiterals.MATCHED_DN, matchedDn.getName() );
91          }
92  
93          // Controls
94          ParserUtils.addControls( codec, root, message.getControls().values(), false );
95  
96          // ResultCode
97          Element resultCodeElement = root.addElement( DsmlLiterals.RESULT_CODE );
98          resultCodeElement.addAttribute( DsmlLiterals.CODE, Integer.toString( result.getResultCode().getResultCode() ) );
99          resultCodeElement.addAttribute( DsmlLiterals.DESCR, result.getResultCode().getMessage() );
100 
101         // ErrorMessage
102         String errorMessage = result.getDiagnosticMessage();
103 
104         if ( ( errorMessage != null ) && ( errorMessage.length() != 0 ) )
105         {
106             Element errorMessageElement = root.addElement( DsmlLiterals.ERROR_MESSAGE );
107             errorMessageElement.addText( errorMessage );
108         }
109 
110         // Referrals
111         Referral referral = result.getReferral();
112         if ( referral != null )
113         {
114             Collection<String> ldapUrls = referral.getLdapUrls();
115             if ( ldapUrls != null )
116             {
117                 for ( String ldapUrl : ldapUrls )
118                 {
119                     Element referalElement = root.addElement( DsmlLiterals.REFERRAL );
120                     referalElement.addText( ldapUrl );
121                 }
122             }
123         }
124 
125         return root;
126     }
127 
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public String getDiagnosticMessage()
134     {
135         return result.getDiagnosticMessage();
136     }
137 
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public void setDiagnosticMessage( String diagnosticMessage )
144     {
145         result.setDiagnosticMessage( diagnosticMessage );
146     }
147 
148 
149     /**
150      * Get the matched Dn
151      *
152      * @return Returns the matchedDN.
153      */
154     @Override
155     public Dn getMatchedDn()
156     {
157         return result.getMatchedDn();
158     }
159 
160 
161     /**
162      * Set the Matched Dn
163      *
164      * @param matchedDn The matchedDn to set.
165      */
166     @Override
167     public void setMatchedDn( Dn matchedDn )
168     {
169         result.setMatchedDn( matchedDn );
170     }
171 
172 
173     /**
174      * Get the referrals
175      *
176      * @return Returns the referrals.
177      */
178     public List<String> getReferrals()
179     {
180         return ( List<String> ) result.getReferral().getLdapUrls();
181     }
182 
183 
184     /**
185      * Add a referral
186      *
187      * @param referral The referral to add.
188      */
189     public void addReferral( LdapUrl referral )
190     {
191         result.getReferral().addLdapUrl( referral.toString() );
192     }
193 
194 
195     /**
196      * Get the result code
197      *
198      * @return Returns the resultCode.
199      */
200     @Override
201     public ResultCodeEnum getResultCode()
202     {
203         return result.getResultCode();
204     }
205 
206 
207     /**
208      * Set the result code
209      *
210      * @param resultCode The resultCode to set.
211      */
212     @Override
213     public void setResultCode( ResultCodeEnum resultCode )
214     {
215         result.setResultCode( resultCode );
216     }
217 
218 
219     /**
220      * {@inheritDoc}
221      */
222     @Override
223     public LdapResult getDecorated()
224     {
225         return result;
226     }
227 
228 
229     /**
230      * {@inheritDoc}
231      */
232     @Override
233     public boolean isReferral()
234     {
235         return getDecorated().isReferral();
236     }
237 
238 
239     /**
240      * {@inheritDoc}
241      */
242     @Override
243     public Referral getReferral()
244     {
245         return getDecorated().getReferral();
246     }
247 
248 
249     /**
250      * {@inheritDoc}
251      */
252     @Override
253     public void setReferral( Referral referral )
254     {
255         getDecorated().setReferral( referral );
256     }
257 
258 
259     /**
260      * {@inheritDoc}
261      */
262     @Override
263     public boolean isDefaultSuccess()
264     {
265         return false;
266     }
267 }