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 java.nio.ByteBuffer; 024 025import org.apache.directory.api.asn1.EncoderException; 026import org.apache.directory.api.dsmlv2.AbstractDsmlMessageDecorator; 027import org.apache.directory.api.dsmlv2.ParserUtils; 028import org.apache.directory.api.ldap.codec.api.LdapApiService; 029import org.apache.directory.api.ldap.model.message.Request; 030import org.dom4j.Element; 031 032 033/** 034 * Abstract class for DSML requests. 035 * 036 * @param <E> The request type 037 * 038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 039 */ 040public abstract class AbstractRequestDsml<E extends Request> 041 extends AbstractDsmlMessageDecorator<E> 042 implements Request 043{ 044 /** 045 * Creates a new instance of AbstractRequestDsml. 046 * 047 * @param codec The LDAP Service to use 048 * @param ldapMessage the message to decorate 049 */ 050 public AbstractRequestDsml( LdapApiService codec, E ldapMessage ) 051 { 052 super( codec, ldapMessage ); 053 } 054 055 056 /** 057 * Creates the Request Element and adds RequestID and Controls. 058 * 059 * @param root the root element 060 * @return the Request Element of the given name containing 061 */ 062 public Element toDsml( Element root ) 063 { 064 Element element = root.addElement( getRequestName() ); 065 066 // Request ID 067 int requestID = getDecorated().getMessageId(); 068 if ( requestID > 0 ) 069 { 070 element.addAttribute( "requestID", Integer.toString( requestID ) ); 071 } 072 073 // Controls 074 ParserUtils.addControls( getCodecService(), element, getDecorated().getControls().values() ); 075 076 return element; 077 } 078 079 080 /** 081 * Gets the name of the request according to the type of the decorated element. 082 * 083 * @return 084 * the name of the request according to the type of the decorated element. 085 */ 086 private String getRequestName() 087 { 088 switch ( getDecorated().getType() ) 089 { 090 case ABANDON_REQUEST: 091 return "abandonRequest"; 092 093 case ADD_REQUEST: 094 return "addRequest"; 095 096 case BIND_REQUEST: 097 return "authRequest"; 098 099 case COMPARE_REQUEST: 100 return "compareRequest"; 101 102 case DEL_REQUEST: 103 return "delRequest"; 104 105 case EXTENDED_REQUEST: 106 return "extendedRequest"; 107 108 case MODIFYDN_REQUEST: 109 return "modDNRequest"; 110 111 case MODIFY_REQUEST: 112 return "modifyRequest"; 113 114 case SEARCH_REQUEST: 115 return "searchRequest"; 116 117 default: 118 return "error"; 119 } 120 } 121 122 123 /** 124 * @return the buffer's length (always 0) 125 */ 126 public int computeLength() 127 { 128 return 0; 129 } 130 131 132 /** 133 * Encode the request. Always return an empty buffer. 134 * 135 * @param buffer The buffer to allocate 136 * @return The resulting buffer 137 * @throws EncoderException If we had an error while encoding the request 138 */ 139 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException 140 { 141 return null; 142 } 143 144 145 /** 146 * {@inheritDoc} 147 */ 148 public boolean hasResponse() 149 { 150 return getDecorated().hasResponse(); 151 } 152}