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 org.apache.directory.shared.asn1.util.Oid;
024import org.apache.directory.shared.dsmlv2.ParserUtils;
025import org.apache.directory.shared.ldap.codec.api.LdapApiService;
026import org.apache.directory.shared.ldap.model.message.ExtendedResponse;
027import org.apache.directory.shared.ldap.model.message.ExtendedResponseImpl;
028import org.apache.directory.shared.ldap.model.message.MessageTypeEnum;
029import org.apache.directory.shared.util.Strings;
030import org.dom4j.Element;
031import org.dom4j.Namespace;
032import org.dom4j.QName;
033import org.dom4j.tree.DefaultElement;
034
035
036/**
037 * DSML Decorator for ExtendedResponse
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public class ExtendedResponseDsml extends AbstractResultResponseDsml<ExtendedResponse>
042    implements ExtendedResponse
043{
044    private static final String EXTENDED_RESPONSE_TAG = "extendedResponse";
045    private static final long serialVersionUID = -3989420095112650346L;
046    private byte[] response;
047
048
049    /**
050     * Creates a new getDecoratedMessage() of ExtendedResponseDsml.
051     */
052    public ExtendedResponseDsml( LdapApiService codec )
053    {
054        super( codec, new ExtendedResponseImpl( "" ) );
055    }
056
057
058    /**
059     * Creates a new getDecoratedMessage() of ExtendedResponseDsml.
060     *
061     * @param ldapMessage
062     *      the message to decorate
063     */
064    public ExtendedResponseDsml( LdapApiService codec, ExtendedResponse 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( EXTENDED_RESPONSE_TAG );
089        }
090        else
091        {
092            element = new DefaultElement( EXTENDED_RESPONSE_TAG );
093        }
094        
095        ExtendedResponse extendedResponse = ( ExtendedResponse ) getDecorated();
096
097        // LDAP Result
098        LdapResultDsml ldapResultDsml = new LdapResultDsml( getCodecService(), 
099            getDecorated().getLdapResult(), getDecorated() );
100        ldapResultDsml.toDsml( element );
101
102        // ResponseName
103        String responseName = extendedResponse.getResponseName();
104        if ( responseName != null )
105        {
106            element.addElement( "responseName" ).addText( responseName );
107        }
108
109        // Response
110        Object response = getResponseValue();
111
112        if ( response != null )
113        {
114            if ( ParserUtils.needsBase64Encoding( response ) )
115            {
116                Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI );
117                Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI );
118                element.getDocument().getRootElement().add( xsdNamespace );
119                element.getDocument().getRootElement().add( xsiNamespace );
120
121                Element responseElement = element.addElement( "response" )
122                    .addText( ParserUtils.base64Encode( response ) );
123                responseElement.addAttribute( new QName( "type", xsiNamespace ), ParserUtils.XSD + ":"
124                    + ParserUtils.BASE64BINARY );
125            }
126            else
127            {
128                element.addElement( "response" ).addText( Strings.utf8ToString((byte[]) response) );
129            }
130        }
131
132        return element;
133    }
134
135
136    /**
137     * {@inheritDoc}
138     */
139    public void setResponseName( String oid )
140    {
141        getDecorated().setResponseName( oid );
142    }
143
144    
145    /**
146     * Get the extended response name
147     * 
148     * @return Returns the name.
149     */
150    public String getResponseName()
151    {
152        return getDecorated().getResponseName();
153    }
154
155
156    /**
157     * Set the extended response name
158     * 
159     * @param responseName The name to set.
160     */
161    public void setResponseName( Oid responseName )
162    {
163        getDecorated().setResponseName( responseName.toString() );
164    }
165
166
167    /**
168     * Get the extended response
169     * 
170     * @return Returns the response.
171     */
172    public byte[] getResponseValue()
173    {
174        return this.response;
175    }
176
177
178    /**
179     * Set the extended response
180     * 
181     * @param response The response to set.
182     */
183    public void setResponseValue( byte[] response )
184    {
185        this.response = response;
186    }
187}