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     */
020    
021    package org.apache.directory.shared.dsmlv2.reponse;
022    
023    
024    import org.apache.directory.shared.dsmlv2.DsmlDecorator;
025    import org.apache.directory.shared.dsmlv2.ParserUtils;
026    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
027    import org.apache.directory.shared.ldap.codec.search.SearchResultEntryCodec;
028    import org.apache.directory.shared.ldap.entry.Entry;
029    import org.apache.directory.shared.ldap.entry.EntryAttribute;
030    import org.apache.directory.shared.ldap.entry.Value;
031    import org.apache.directory.shared.ldap.name.DN;
032    import org.dom4j.Element;
033    import org.dom4j.Namespace;
034    import org.dom4j.QName;
035    
036    
037    /**
038     * DSML Decorator for SearchResultEntry
039     *
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev$, $Date$
042     */
043    public class SearchResultEntryDsml extends LdapResponseDecorator implements DsmlDecorator
044    {
045        /**
046         * Creates a new instance of SearchResultEntryDsml.
047         */
048        public SearchResultEntryDsml()
049        {
050            super( new SearchResultEntryCodec() );
051        }
052    
053    
054        /**
055         * Creates a new instance of SearchResultEntryDsml.
056         *
057         * @param ldapMessage
058         *      the message to decorate
059         */
060        public SearchResultEntryDsml( SearchResultEntryCodec ldapMessage )
061        {
062            super( ldapMessage );
063        }
064    
065    
066        /* (non-Javadoc)
067         * @see org.apache.directory.shared.dsmlv2.reponse.LdapMessageDecorator#getMessageType()
068         */
069        public MessageTypeEnum getMessageType()
070        {
071            return instance.getMessageType();
072        }
073    
074    
075        /* (non-Javadoc)
076         * @see org.apache.directory.shared.dsmlv2.reponse.DsmlDecorator#toDsml(org.dom4j.Element)
077         */
078        public Element toDsml( Element root )
079        {
080            Element element = root.addElement( "searchResultEntry" );
081            SearchResultEntryCodec searchResultEntry = ( SearchResultEntryCodec ) instance;
082            element.addAttribute( "dn", searchResultEntry.getObjectName().getName() );
083    
084            Entry entry = searchResultEntry.getEntry();
085            for ( EntryAttribute attribute : entry )
086            {
087    
088                Element attributeElement = element.addElement( "attr" );
089                attributeElement.addAttribute( "name", attribute.getId() );
090    
091                for ( Value<?> value : attribute )
092                {
093                    if ( ParserUtils.needsBase64Encoding( value.get() ) )
094                    {
095                        Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI );
096                        Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI );
097                        attributeElement.getDocument().getRootElement().add( xsdNamespace );
098                        attributeElement.getDocument().getRootElement().add( xsiNamespace );
099    
100                        Element valueElement = attributeElement.addElement( "value" ).addText(
101                            ParserUtils.base64Encode( value.get() ) );
102                        valueElement.addAttribute( new QName( "type", xsiNamespace ), ParserUtils.XSD + ":"
103                            + ParserUtils.BASE64BINARY );
104                    }
105                    else
106                    {
107                        attributeElement.addElement( "value" ).addText( value.getString() );
108                    }
109                }
110            }
111    
112            return element;
113        }
114    
115    
116        /**
117         * Get the entry DN
118         * 
119         * @return Returns the objectName.
120         */
121        public DN getObjectName()
122        {
123            return ( ( SearchResultEntryCodec ) instance ).getObjectName();
124        }
125    
126    
127        /**
128         * Set the entry DN
129         * 
130         * @param objectName The objectName to set.
131         */
132        public void setObjectName( DN objectName )
133        {
134            ( ( SearchResultEntryCodec ) instance ).setObjectName( objectName );
135        }
136    
137    
138        /**
139         * Get the entry.
140         * 
141         * @return Returns the entry.
142         */
143        public Entry getEntry()
144        {
145            return ( ( SearchResultEntryCodec ) instance ).getEntry();
146        }
147    
148    
149        /**
150         * Initialize the entry.
151         * 
152         * @param entry the entry
153         */
154        public void setEntry( Entry entry )
155        {
156            ( ( SearchResultEntryCodec ) instance ).setEntry( entry );
157        }
158    
159    
160        /**
161         * Create a new attributeValue
162         * 
163         * @param type The attribute's name
164         */
165        public void addAttributeValues( String type )
166        {
167            ( ( SearchResultEntryCodec ) instance ).addAttributeValues( type );
168        }
169    
170    
171        /**
172         * Add a new value to the current attribute
173         * 
174         * @param value
175         */
176        public void addAttributeValue( Object value )
177        {
178            ( ( SearchResultEntryCodec ) instance ).addAttributeValue( value );
179        }
180    
181    
182        /**
183         * @return Returns the currentAttributeValue.
184         */
185        public String getCurrentAttributeValueType()
186        {
187            return ( ( SearchResultEntryCodec ) instance ).getCurrentAttributeValueType();
188        }
189    }