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