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