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.api.dsmlv2.response;
021
022
023import java.util.Collection;
024
025import org.apache.directory.api.ldap.codec.api.LdapApiService;
026import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
027import org.apache.directory.api.ldap.model.message.Referral;
028import org.apache.directory.api.ldap.model.message.SearchResultReference;
029import org.apache.directory.api.ldap.model.message.SearchResultReferenceImpl;
030import org.apache.directory.api.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     * @param codec The LDAP Service to use
051     */
052    public SearchResultReferenceDsml( LdapApiService codec )
053    {
054        super( codec, new SearchResultReferenceImpl() );
055    }
056
057
058    /**
059     * Creates a new getDecoratedMessage() of SearchResultReferenceDsml.
060     *
061     * @param codec The LDAP Service to use
062     * @param ldapMessage the message to decorate
063     */
064    public SearchResultReferenceDsml( LdapApiService codec, SearchResultReference ldapMessage )
065    {
066        super( codec, ldapMessage );
067    }
068
069
070    /**
071     * {@inheritDoc}
072     */
073    public MessageTypeEnum getType()
074    {
075        return getDecorated().getType();
076    }
077
078
079    /**
080     * {@inheritDoc}
081     */
082    public Element toDsml( Element root )
083    {
084        Element element = null;
085
086        if ( root != null )
087        {
088            element = root.addElement( SEARCH_RESULT_REFERENCE_TAG );
089        }
090        else
091        {
092            element = new DefaultElement( SEARCH_RESULT_REFERENCE_TAG );
093        }
094
095        // Adding References
096        for ( String url : getDecorated().getReferral().getLdapUrls() )
097        {
098            element.addElement( "ref" ).addText( url );
099        }
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}