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.reponse;
021
022
023import org.apache.directory.api.asn1.util.Oid;
024import org.apache.directory.api.dsmlv2.ParserUtils;
025import org.apache.directory.api.ldap.codec.api.LdapApiService;
026import org.apache.directory.api.ldap.model.message.ExtendedResponse;
027import org.apache.directory.api.ldap.model.message.ExtendedResponseImpl;
028import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
029import org.apache.directory.api.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 byte[] response;
046
047
048    /**
049     * Creates a new getDecoratedMessage() of ExtendedResponseDsml.
050     */
051    public ExtendedResponseDsml( LdapApiService codec )
052    {
053        super( codec, new ExtendedResponseImpl( "" ) );
054    }
055
056
057    /**
058     * Creates a new getDecoratedMessage() of ExtendedResponseDsml.
059     *
060     * @param ldapMessage
061     *      the message to decorate
062     */
063    public ExtendedResponseDsml( LdapApiService codec, ExtendedResponse ldapMessage )
064    {
065        super( codec, ldapMessage );
066    }
067
068
069    /**
070     * {@inheritDoc}
071     */
072    public MessageTypeEnum getType()
073    {
074        return getDecorated().getType();
075    }
076
077
078    /**
079     * {@inheritDoc}
080     */
081    public Element toDsml( Element root )
082    {
083        Element element = null;
084
085        if ( root != null )
086        {
087            element = root.addElement( EXTENDED_RESPONSE_TAG );
088        }
089        else
090        {
091            element = new DefaultElement( EXTENDED_RESPONSE_TAG );
092        }
093
094        ExtendedResponse extendedResponse = getDecorated();
095
096        // LDAP Result
097        LdapResultDsml ldapResultDsml = new LdapResultDsml( getCodecService(),
098            getDecorated().getLdapResult(), getDecorated() );
099        ldapResultDsml.toDsml( element );
100
101        // ResponseName
102        String responseName = extendedResponse.getResponseName();
103        if ( responseName != null )
104        {
105            element.addElement( "responseName" ).addText( responseName );
106        }
107
108        // Response
109        Object response = getResponseValue();
110
111        if ( response != null )
112        {
113            if ( ParserUtils.needsBase64Encoding( response ) )
114            {
115                Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI );
116                Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI );
117                element.getDocument().getRootElement().add( xsdNamespace );
118                element.getDocument().getRootElement().add( xsiNamespace );
119
120                Element responseElement = element.addElement( "response" )
121                    .addText( ParserUtils.base64Encode( response ) );
122                responseElement.addAttribute( new QName( "type", xsiNamespace ), ParserUtils.XSD + ":"
123                    + ParserUtils.BASE64BINARY );
124            }
125            else
126            {
127                element.addElement( "response" ).addText( Strings.utf8ToString( ( byte[] ) response ) );
128            }
129        }
130
131        return element;
132    }
133
134
135    /**
136     * {@inheritDoc}
137     */
138    public void setResponseName( String oid )
139    {
140        getDecorated().setResponseName( oid );
141    }
142
143
144    /**
145     * Get the extended response name
146     * 
147     * @return Returns the name.
148     */
149    public String getResponseName()
150    {
151        return getDecorated().getResponseName();
152    }
153
154
155    /**
156     * Set the extended response name
157     * 
158     * @param responseName The name to set.
159     */
160    public void setResponseName( Oid responseName )
161    {
162        getDecorated().setResponseName( responseName.toString() );
163    }
164
165
166    /**
167     * Get the extended response
168     * 
169     * @return Returns the response.
170     */
171    public byte[] getResponseValue()
172    {
173        return this.response;
174    }
175
176
177    /**
178     * Set the extended response
179     * 
180     * @param response The response to set.
181     */
182    public void setResponseValue( byte[] response )
183    {
184        this.response = response;
185    }
186}