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  
25  import org.apache.directory.api.ldap.codec.api.LdapApiService;
26  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
27  import org.apache.directory.api.ldap.model.message.Referral;
28  import org.apache.directory.api.ldap.model.message.SearchResultReference;
29  import org.apache.directory.api.ldap.model.message.SearchResultReferenceImpl;
30  import org.apache.directory.api.ldap.model.url.LdapUrl;
31  import org.dom4j.Element;
32  import org.dom4j.tree.DefaultElement;
33  
34  
35  /**
36   * DSML Decorator for SearchResultReference
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class SearchResultReferenceDsml
41      extends AbstractResponseDsml<SearchResultReference>
42      implements SearchResultReference
43  {
44      private static final String SEARCH_RESULT_REFERENCE_TAG = "searchResultReference";
45  
46  
47      /**
48       * Creates a new getDecoratedMessage() of SearchResultReferenceDsml.
49       * 
50       * @param codec The LDAP Service to use
51       */
52      public SearchResultReferenceDsml( LdapApiService codec )
53      {
54          super( codec, new SearchResultReferenceImpl() );
55      }
56  
57  
58      /**
59       * Creates a new getDecoratedMessage() of SearchResultReferenceDsml.
60       *
61       * @param codec The LDAP Service to use
62       * @param ldapMessage the message to decorate
63       */
64      public SearchResultReferenceDsml( LdapApiService codec, SearchResultReference ldapMessage )
65      {
66          super( codec, ldapMessage );
67      }
68  
69  
70      /**
71       * {@inheritDoc}
72       */
73      public MessageTypeEnum getType()
74      {
75          return getDecorated().getType();
76      }
77  
78  
79      /**
80       * {@inheritDoc}
81       */
82      public Element toDsml( Element root )
83      {
84          Element element = null;
85  
86          if ( root != null )
87          {
88              element = root.addElement( SEARCH_RESULT_REFERENCE_TAG );
89          }
90          else
91          {
92              element = new DefaultElement( SEARCH_RESULT_REFERENCE_TAG );
93          }
94  
95          // Adding References
96          for ( String url : getDecorated().getReferral().getLdapUrls() )
97          {
98              element.addElement( "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     public Referral getReferral()
131     {
132         return getDecorated().getReferral();
133     }
134 
135 
136     /**
137      * {@inheritDoc}
138      */
139     public void setReferral( Referral referral )
140     {
141         getDecorated().setReferral( referral );
142     }
143 }