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.asn1.primitives.OID;
025    import org.apache.directory.shared.dsmlv2.DsmlDecorator;
026    import org.apache.directory.shared.dsmlv2.ParserUtils;
027    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
028    import org.apache.directory.shared.ldap.codec.extended.ExtendedResponseCodec;
029    import org.dom4j.Element;
030    import org.dom4j.Namespace;
031    import org.dom4j.QName;
032    
033    
034    /**
035     * DSML Decorator for ExtendedResponse
036     *
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev$, $Date$
039     */
040    public class ExtendedResponseDsml extends LdapResponseDecorator implements DsmlDecorator
041    {
042        /**
043         * Creates a new instance of ExtendedResponseDsml.
044         */
045        public ExtendedResponseDsml()
046        {
047            super( new ExtendedResponseCodec() );
048        }
049    
050    
051        /**
052         * Creates a new instance of ExtendedResponseDsml.
053         *
054         * @param ldapMessage
055         *      the message to decorate
056         */
057        public ExtendedResponseDsml( ExtendedResponseCodec ldapMessage )
058        {
059            super( ldapMessage );
060        }
061    
062    
063        /* (non-Javadoc)
064         * @see org.apache.directory.shared.dsmlv2.reponse.LdapMessageDecorator#getMessageType()
065         */
066        public MessageTypeEnum getMessageType()
067        {
068            return instance.getMessageType();
069        }
070    
071    
072        /* (non-Javadoc)
073         * @see org.apache.directory.shared.dsmlv2.reponse.DsmlDecorator#toDsml(org.dom4j.Element)
074         */
075        public Element toDsml( Element root )
076        {
077            Element element = root.addElement( "extendedResponse" );
078            ExtendedResponseCodec extendedResponse = ( ExtendedResponseCodec ) instance;
079    
080            // LDAP Result
081            LdapResultDsml ldapResultDsml = new LdapResultDsml( extendedResponse.getLdapResult(), instance );
082            ldapResultDsml.toDsml( element );
083    
084            // ResponseName
085            String responseName = extendedResponse.getResponseName();
086            if ( responseName != null )
087            {
088                element.addElement( "responseName" ).addText( responseName );
089            }
090    
091            // Response
092            Object response = extendedResponse.getResponse();
093            if ( response != null )
094            {
095                if ( ParserUtils.needsBase64Encoding( response ) )
096                {
097                    Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI );
098                    Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI );
099                    element.getDocument().getRootElement().add( xsdNamespace );
100                    element.getDocument().getRootElement().add( xsiNamespace );
101    
102                    Element responseElement = element.addElement( "response" )
103                        .addText( ParserUtils.base64Encode( response ) );
104                    responseElement.addAttribute( new QName( "type", xsiNamespace ), ParserUtils.XSD + ":"
105                        + ParserUtils.BASE64BINARY );
106                }
107                else
108                {
109                    element.addElement( "response" ).addText( response.toString() );
110                }
111            }
112    
113            return element;
114        }
115    
116    
117        /**
118         * Get the extended response name
119         * 
120         * @return Returns the name.
121         */
122        public String getResponseName()
123        {
124            return ( ( ExtendedResponseCodec ) instance ).getResponseName();
125        }
126    
127    
128        /**
129         * Set the extended response name
130         * 
131         * @param responseName The name to set.
132         */
133        public void setResponseName( OID responseName )
134        {
135            ( ( ExtendedResponseCodec ) instance ).setResponseName( responseName );
136        }
137    
138    
139        /**
140         * Get the extended response
141         * 
142         * @return Returns the response.
143         */
144        public Object getResponse()
145        {
146            return ( ( ExtendedResponseCodec ) instance ).getResponse();
147        }
148    
149    
150        /**
151         * Set the extended response
152         * 
153         * @param response The response to set.
154         */
155        public void setResponse( Object response )
156        {
157            ( ( ExtendedResponseCodec ) instance ).setResponse( response );
158        }
159    }