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  
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      public SearchResultReferenceDsml( LdapApiService codec )
51      {
52          super( codec, new SearchResultReferenceImpl() );
53      }
54  
55  
56      /**
57       * Creates a new getDecoratedMessage() of SearchResultReferenceDsml.
58       *
59       * @param ldapMessage
60       *      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      public MessageTypeEnum getType()
72      {
73          return getDecorated().getType();
74      }
75  
76  
77      /**
78       * {@inheritDoc}
79       */
80      public Element toDsml( Element root )
81      {
82          Element element = null;
83  
84          if ( root != null )
85          {
86              element = root.addElement( SEARCH_RESULT_REFERENCE_TAG );
87          }
88          else
89          {
90              element = new DefaultElement( SEARCH_RESULT_REFERENCE_TAG );
91          }
92  
93          // Adding References
94          for ( String url : getDecorated().getReferral().getLdapUrls() )
95          {
96              element.addElement( "ref" ).addText( url );
97          }
98  
99          return element;
100     }
101 
102 
103     /**
104      * Add a new reference to the list.
105      * 
106      * @param searchResultReference The search result reference
107      */
108     public void addSearchResultReference( LdapUrl searchResultReference )
109     {
110         getDecorated().getReferral().addLdapUrl( searchResultReference.toString() );
111     }
112 
113 
114     /**
115      * Get the list of references
116      * 
117      * @return An ArrayList of SearchResultReferences
118      */
119     public Collection<String> getSearchResultReferences()
120     {
121         return getDecorated().getReferral().getLdapUrls();
122     }
123 
124 
125     /**
126      * {@inheritDoc}
127      */
128     public Referral getReferral()
129     {
130         return getDecorated().getReferral();
131     }
132 
133 
134     /**
135      * {@inheritDoc}
136      */
137     public void setReferral( Referral referral )
138     {
139         getDecorated().setReferral( referral );
140     }
141 }