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 *    https://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    @Override
074    public MessageTypeEnum getType()
075    {
076        return getDecorated().getType();
077    }
078
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public Element toDsml( Element root )
085    {
086        Element element;
087
088        if ( root != null )
089        {
090            element = root.addElement( SEARCH_RESULT_REFERENCE_TAG );
091        }
092        else
093        {
094            element = new DefaultElement( SEARCH_RESULT_REFERENCE_TAG );
095        }
096
097        // Adding References
098        for ( String url : getDecorated().getReferral().getLdapUrls() )
099        {
100            element.addElement( "ref" ).addText( url );
101        }
102
103        return element;
104    }
105
106
107    /**
108     * Add a new reference to the list.
109     * 
110     * @param searchResultReference The search result reference
111     */
112    public void addSearchResultReference( LdapUrl searchResultReference )
113    {
114        getDecorated().getReferral().addLdapUrl( searchResultReference.toString() );
115    }
116
117
118    /**
119     * Get the list of references
120     * 
121     * @return An ArrayList of SearchResultReferences
122     */
123    public Collection<String> getSearchResultReferences()
124    {
125        return getDecorated().getReferral().getLdapUrls();
126    }
127
128
129    /**
130     * {@inheritDoc}
131     */
132    @Override
133    public Referral getReferral()
134    {
135        return getDecorated().getReferral();
136    }
137
138
139    /**
140     * {@inheritDoc}
141     */
142    @Override
143    public void setReferral( Referral referral )
144    {
145        getDecorated().setReferral( referral );
146    }
147}