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.request;
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.Control;
027import org.apache.directory.api.ldap.model.message.ExtendedRequest;
028import org.apache.directory.api.ldap.model.message.ExtendedResponse;
029import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
030import org.dom4j.Element;
031import org.dom4j.Namespace;
032import org.dom4j.QName;
033
034
035/**
036 * DSML Decorator for ExtendedRequest
037 * 
038 * @param <Q> The extended request type
039 * @param <P> The extended response type
040 *
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public class ExtendedRequestDsml<Q extends ExtendedRequest, P extends ExtendedResponse>
044    extends AbstractResultResponseRequestDsml<Q, P>
045    implements ExtendedRequest
046{
047    private byte[] requestValue;
048
049
050    /**
051     * Creates a new getDecoratedMessage() of ExtendedRequestDsml.
052     *
053     * @param codec The LDAP Service to use
054     * @param ldapMessage the message to decorate
055     */
056    public ExtendedRequestDsml( LdapApiService codec, Q ldapMessage )
057    {
058        super( codec, ldapMessage );
059    }
060
061
062    /**
063     * {@inheritDoc}
064     */
065    public MessageTypeEnum getType()
066    {
067        return getDecorated().getType();
068    }
069
070
071    /**
072     * {@inheritDoc}
073     */
074    public Element toDsml( Element root )
075    {
076        Element element = super.toDsml( root );
077
078        // Request Name
079        if ( getDecorated().getRequestName() != null )
080        {
081            element.addElement( "requestName" ).setText(
082                getDecorated().getRequestName() );
083        }
084
085        // Request Value        
086        Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI );
087        Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI );
088        element.getDocument().getRootElement().add( xsdNamespace );
089        element.getDocument().getRootElement().add( xsiNamespace );
090
091        Element valueElement = element.addElement( "requestValue" ).addText(
092            ParserUtils.base64Encode( getRequestValue() ) );
093        valueElement.addAttribute( new QName( "type", xsiNamespace ),
094            "xsd:" + ParserUtils.BASE64BINARY );
095
096        return element;
097    }
098
099
100    /**
101     * Get the extended request name
102     * 
103     * @return Returns the request name.
104     */
105    public String getRequestName()
106    {
107        return getDecorated().getRequestName();
108    }
109
110
111    /**
112     * Set the extended request name
113     * 
114     * @param requestName The request name to set.
115     */
116    public void setRequestName( Oid requestName )
117    {
118        getDecorated().setRequestName( requestName.toString() );
119    }
120
121
122    /**
123     * Get the extended request value
124     * 
125     * @return Returns the request value.
126     */
127    public byte[] getRequestValue()
128    {
129        return this.requestValue;
130    }
131
132
133    /**
134     * Set the extended request value
135     * 
136     * @param requestValue The request value to set.
137     */
138    public void setRequestValue( byte[] requestValue )
139    {
140        this.requestValue = requestValue;
141    }
142
143
144    /**
145     * {@inheritDoc}
146     */
147    public MessageTypeEnum getResponseType()
148    {
149        return getDecorated().getResponseType();
150    }
151
152
153    /**
154     * {@inheritDoc}
155     */
156    public ExtendedRequest setRequestName( String oid )
157    {
158        getDecorated().setRequestName( oid );
159
160        return this;
161    }
162
163
164    /**
165     * {@inheritDoc}
166     */
167    public ExtendedRequest setMessageId( int messageId )
168    {
169        super.setMessageId( messageId );
170
171        return this;
172    }
173
174
175    /**
176     * {@inheritDoc}
177     */
178    public ExtendedRequest addControl( Control control )
179    {
180        return ( ExtendedRequest ) super.addControl( control );
181    }
182
183
184    /**
185     * {@inheritDoc}
186     */
187    public ExtendedRequest addAllControls( Control[] controls )
188    {
189        return ( ExtendedRequest ) super.addAllControls( controls );
190    }
191
192
193    /**
194     * {@inheritDoc}
195     */
196    public ExtendedRequest removeControl( Control control )
197    {
198        return ( ExtendedRequest ) super.removeControl( control );
199    }
200}