001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.shared.dsmlv2.reponse;
021
022
023import java.util.Collection;
024
025import org.apache.directory.shared.ldap.codec.api.LdapApiService;
026import org.apache.directory.shared.ldap.model.message.MessageTypeEnum;
027import org.apache.directory.shared.ldap.model.message.Referral;
028import org.apache.directory.shared.ldap.model.message.SearchResultReference;
029import org.apache.directory.shared.ldap.model.message.SearchResultReferenceImpl;
030import org.apache.directory.shared.ldap.model.url.LdapUrl;
031import org.dom4j.Element;
032import org.dom4j.tree.DefaultElement;
033
034
035/**
036 * DSML Decorator for SearchResultReference
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class SearchResultReferenceDsml 
041    extends AbstractResponseDsml<SearchResultReference>
042    implements SearchResultReference
043{
044    private static final String SEARCH_RESULT_REFERENCE_TAG = "searchResultReference";
045
046
047    /**
048     * Creates a new getDecoratedMessage() of SearchResultReferenceDsml.
049     */
050    public SearchResultReferenceDsml( LdapApiService codec )
051    {
052        super( codec, new SearchResultReferenceImpl() );
053    }
054
055
056    /**
057     * Creates a new getDecoratedMessage() of SearchResultReferenceDsml.
058     *
059     * @param ldapMessage
060     *      the message to decorate
061     */
062    public SearchResultReferenceDsml( LdapApiService codec, SearchResultReference ldapMessage )
063    {
064        super( codec, ldapMessage );
065    }
066
067
068    /**
069     * {@inheritDoc}
070     */
071    public MessageTypeEnum getType()
072    {
073        return getDecorated().getType();
074    }
075
076
077    /**
078     * {@inheritDoc}
079     */
080    public Element toDsml( Element root )
081    {
082        Element element = null;
083
084        if ( root != null )
085        {
086            element = root.addElement( SEARCH_RESULT_REFERENCE_TAG );
087        }
088        else
089        {
090            element = new DefaultElement( SEARCH_RESULT_REFERENCE_TAG );
091        }
092
093
094        // Adding References
095        for ( String url : getDecorated().getReferral().getLdapUrls() )
096        {
097            element.addElement( "ref" ).addText( url );
098        }
099
100        return element;
101    }
102
103
104    /**
105     * Add a new reference to the list.
106     * 
107     * @param searchResultReference The search result reference
108     */
109    public void addSearchResultReference( LdapUrl searchResultReference )
110    {
111        getDecorated().getReferral().addLdapUrl( searchResultReference.toString() );
112    }
113
114
115    /**
116     * Get the list of references
117     * 
118     * @return An ArrayList of SearchResultReferences
119     */
120    public Collection<String> getSearchResultReferences()
121    {
122        return getDecorated().getReferral().getLdapUrls();
123    }
124
125
126    /**
127     * {@inheritDoc}
128     */
129    public Referral getReferral()
130    {
131        return getDecorated().getReferral();
132    }
133
134
135    /**
136     * {@inheritDoc}
137     */
138    public void setReferral( Referral referral )
139    {
140        getDecorated().setReferral( referral );
141    }
142}