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 */ 020 package org.apache.directory.shared.dsmlv2.request; 021 022 023 import org.apache.directory.shared.dsmlv2.ParserUtils; 024 import org.apache.directory.shared.ldap.codec.MessageTypeEnum; 025 import org.apache.directory.shared.ldap.codec.add.AddRequestCodec; 026 import org.apache.directory.shared.ldap.entry.Entry; 027 import org.apache.directory.shared.ldap.entry.EntryAttribute; 028 import org.apache.directory.shared.ldap.entry.Value; 029 import org.apache.directory.shared.ldap.exception.LdapException; 030 import org.apache.directory.shared.ldap.name.DN; 031 import org.dom4j.Element; 032 import org.dom4j.Namespace; 033 import org.dom4j.QName; 034 035 036 /** 037 * DSML Decorator for AddRequest 038 * 039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 040 * @version $Rev$, $Date$ 041 */ 042 public class AddRequestDsml extends AbstractRequestDsml 043 { 044 /** 045 * Creates a new instance of AddRequestDsml. 046 */ 047 public AddRequestDsml() 048 { 049 super( new AddRequestCodec() ); 050 } 051 052 053 /** 054 * Creates a new instance of AddRequestDsml. 055 * 056 * @param ldapMessage 057 * the message to decorate 058 */ 059 public AddRequestDsml( AddRequestCodec ldapMessage ) 060 { 061 super( ldapMessage ); 062 } 063 064 065 /** 066 * {@inheritDoc} 067 */ 068 public MessageTypeEnum getMessageType() 069 { 070 return instance.getMessageType(); 071 } 072 073 074 /** 075 * {@inheritDoc} 076 */ 077 public Element toDsml( Element root ) 078 { 079 Element element = super.toDsml( root ); 080 081 AddRequestCodec request = ( AddRequestCodec ) instance; 082 083 // DN 084 if ( request.getEntry() != null ) 085 { 086 element.addAttribute( "dn", request.getEntry().getDn().getName() ); 087 } 088 089 // Attributes 090 Entry entry = request.getEntry(); 091 if ( entry != null ) 092 { 093 for ( EntryAttribute attribute : entry ) 094 { 095 Element attributeElement = element.addElement( "attr" ); 096 attributeElement.addAttribute( "name", attribute.getId() ); 097 // Looping on Values 098 for ( Value<?> value : attribute ) 099 { 100 if ( ParserUtils.needsBase64Encoding( value.get() ) ) 101 { 102 Namespace xsdNamespace = new Namespace( "xsd", ParserUtils.XML_SCHEMA_URI ); 103 Namespace xsiNamespace = new Namespace( "xsi", ParserUtils.XML_SCHEMA_INSTANCE_URI ); 104 attributeElement.getDocument().getRootElement().add( xsdNamespace ); 105 attributeElement.getDocument().getRootElement().add( xsiNamespace ); 106 107 Element valueElement = attributeElement.addElement( "value" ).addText( 108 ParserUtils.base64Encode( value.get() ) ); 109 valueElement 110 .addAttribute( new QName( "type", xsiNamespace ), "xsd:" + ParserUtils.BASE64BINARY ); 111 } 112 else 113 { 114 attributeElement.addElement( "value" ).addText( value.getString() ); 115 } 116 } 117 } 118 } 119 120 return element; 121 } 122 123 124 /** 125 * Initialize the Entry. 126 */ 127 public void initEntry() 128 { 129 ( ( AddRequestCodec ) instance ).initEntry(); 130 } 131 132 133 /** 134 * Get the entry with its attributes. 135 * 136 * @return Returns the entry. 137 */ 138 public Entry getEntry() 139 { 140 return ( ( AddRequestCodec ) instance ).getEntry(); 141 } 142 143 144 /** 145 * Create a new attributeValue 146 * 147 * @param type The attribute's name (called 'type' in the grammar) 148 * @throws LdapException 149 */ 150 public void addAttributeType( String type ) throws LdapException 151 { 152 ( ( AddRequestCodec ) instance ).addAttributeType( type ); 153 } 154 155 156 /** 157 * Add a new value to the current attribute 158 * 159 * @param value The value to be added 160 */ 161 public void addAttributeValue( Object value ) 162 { 163 if ( value instanceof Value<?> ) 164 { 165 ( ( AddRequestCodec ) instance ).addAttributeValue( ( Value<?> ) value ); 166 } 167 else if ( value instanceof String ) 168 { 169 ( ( AddRequestCodec ) instance ).addAttributeValue( ( String ) value ); 170 } 171 else if ( value instanceof byte[] ) 172 { 173 ( ( AddRequestCodec ) instance ).addAttributeValue( ( byte[] ) value ); 174 } 175 } 176 177 178 /** 179 * Get the added DN 180 * 181 * @return Returns the entry DN. 182 */ 183 public DN getEntryDn() 184 { 185 return ( ( AddRequestCodec ) instance ).getEntryDn(); 186 } 187 188 189 /** 190 * Set the added DN. 191 * 192 * @param entry The entry DN to set. 193 */ 194 public void setEntryDn( DN entryDn ) 195 { 196 ( ( AddRequestCodec ) instance ).setEntryDn( entryDn ); 197 } 198 199 200 /** 201 * Sets the entry. 202 * 203 * @param entry 204 * the entry 205 */ 206 public void setEntry( Entry entry ) 207 { 208 ( ( AddRequestCodec ) instance ).setEntry( entry ); 209 } 210 211 212 /** 213 * @return Returns the currentAttribute type. 214 */ 215 public String getCurrentAttributeType() 216 { 217 return ( ( AddRequestCodec ) instance ).getCurrentAttributeType(); 218 } 219 }