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