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  
25  import org.apache.directory.api.dsmlv2.DsmlLiterals;
26  import org.apache.directory.api.ldap.codec.api.LdapApiService;
27  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
28  import org.apache.directory.api.ldap.model.message.Referral;
29  import org.apache.directory.api.ldap.model.message.SearchResultReference;
30  import org.apache.directory.api.ldap.model.message.SearchResultReferenceImpl;
31  import org.apache.directory.api.ldap.model.url.LdapUrl;
32  import org.dom4j.Element;
33  import org.dom4j.tree.DefaultElement;
34  
35  
36  /**
37   * DSML Decorator for SearchResultReference
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class SearchResultReferenceDsml
42      extends AbstractResponseDsml<SearchResultReference>
43      implements SearchResultReference
44  {
45      /**
46       * Creates a new getDecoratedMessage() of SearchResultReferenceDsml.
47       * 
48       * @param codec The LDAP Service to use
49       */
50      public SearchResultReferenceDsml( LdapApiService codec )
51      {
52          super( codec, new SearchResultReferenceImpl() );
53      }
54  
55  
56      /**
57       * Creates a new getDecoratedMessage() of SearchResultReferenceDsml.
58       *
59       * @param codec The LDAP Service to use
60       * @param ldapMessage the message to decorate
61       */
62      public SearchResultReferenceDsml( LdapApiService codec, SearchResultReference ldapMessage )
63      {
64          super( codec, ldapMessage );
65      }
66  
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public MessageTypeEnum getType()
73      {
74          return getDecorated().getType();
75      }
76  
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public Element toDsml( Element root )
83      {
84          Element element;
85  
86          if ( root != null )
87          {
88              element = root.addElement( DsmlLiterals.SEARCH_RESULT_REFERENCE );
89          }
90          else
91          {
92              element = new DefaultElement( DsmlLiterals.SEARCH_RESULT_REFERENCE );
93          }
94  
95          // Adding References
96          for ( String url : getDecorated().getReferral().getLdapUrls() )
97          {
98              element.addElement( DsmlLiterals.REF ).addText( url );
99          }
100 
101         return element;
102     }
103 
104 
105     /**
106      * Add a new reference to the list.
107      * 
108      * @param searchResultReference The search result reference
109      */
110     public void addSearchResultReference( LdapUrl searchResultReference )
111     {
112         getDecorated().getReferral().addLdapUrl( searchResultReference.toString() );
113     }
114 
115 
116     /**
117      * Get the list of references
118      * 
119      * @return An ArrayList of SearchResultReferences
120      */
121     public Collection<String> getSearchResultReferences()
122     {
123         return getDecorated().getReferral().getLdapUrls();
124     }
125 
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public Referral getReferral()
132     {
133         return getDecorated().getReferral();
134     }
135 
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public void setReferral( Referral referral )
142     {
143         getDecorated().setReferral( referral );
144     }
145 }