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.dsmlv2.DsmlLiterals; 025import org.apache.directory.api.dsmlv2.ParserUtils; 026import org.apache.directory.api.ldap.codec.api.LdapApiService; 027import org.apache.directory.api.ldap.model.entry.Attribute; 028import org.apache.directory.api.ldap.model.entry.DefaultAttribute; 029import org.apache.directory.api.ldap.model.entry.Entry; 030import org.apache.directory.api.ldap.model.entry.Value; 031import org.apache.directory.api.ldap.model.exception.LdapException; 032import org.apache.directory.api.ldap.model.message.MessageTypeEnum; 033import org.apache.directory.api.ldap.model.message.SearchResultEntry; 034import org.apache.directory.api.ldap.model.message.SearchResultEntryImpl; 035import org.apache.directory.api.ldap.model.name.Dn; 036import org.dom4j.Document; 037import org.dom4j.Element; 038import org.dom4j.Namespace; 039import org.dom4j.QName; 040import org.dom4j.tree.DefaultElement; 041 042 043/** 044 * DSML Decorator for SearchResultEntry 045 * 046 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 047 */ 048public class SearchResultEntryDsml 049 extends AbstractResponseDsml<SearchResultEntry> 050 implements SearchResultEntry 051{ 052 /** The current attribute being processed */ 053 private Attribute currentAttribute; 054 055 056 /** 057 * Creates a new getDecoratedMessage() of SearchResultEntryDsml. 058 * 059 * @param codec The LDAP Service to use 060 */ 061 public SearchResultEntryDsml( LdapApiService codec ) 062 { 063 super( codec, new SearchResultEntryImpl() ); 064 } 065 066 067 /** 068 * Creates a new getDecoratedMessage() of SearchResultEntryDsml. 069 * 070 * @param codec The LDAP Service to use 071 * @param ldapMessage the message to decorate 072 */ 073 public SearchResultEntryDsml( LdapApiService codec, SearchResultEntry ldapMessage ) 074 { 075 super( codec, ldapMessage ); 076 } 077 078 079 /** 080 * @return The current ATtributeType 081 */ 082 public Attribute getCurrentAttribute() 083 { 084 return currentAttribute; 085 } 086 087 088 /** 089 * Create a new attribute 090 * 091 * @param type The attribute's type 092 * @throws LdapException If we can't add the new attributeType 093 */ 094 public void addAttribute( String type ) throws LdapException 095 { 096 currentAttribute = new DefaultAttribute( type ); 097 098 getDecorated().getEntry().put( currentAttribute ); 099 } 100 101 102 /** 103 * Add a new value to the current attribute 104 * 105 * @param value The added value 106 * @throws LdapException If we can't add the new attributeType 107 */ 108 public void addAttributeValue( Object value ) throws LdapException 109 { 110 if ( value instanceof String ) 111 { 112 currentAttribute.add( ( String ) value ); 113 } 114 else 115 { 116 currentAttribute.add( ( byte[] ) value ); 117 } 118 } 119 120 121 /** 122 * {@inheritDoc} 123 */ 124 @Override 125 public MessageTypeEnum getType() 126 { 127 return getDecorated().getType(); 128 } 129 130 131 /** 132 * {@inheritDoc} 133 */ 134 @Override 135 public Element toDsml( Element root ) 136 { 137 Element element; 138 139 if ( root != null ) 140 { 141 element = root.addElement( DsmlLiterals.SEARCH_RESULT_ENTRY ); 142 } 143 else 144 { 145 element = new DefaultElement( DsmlLiterals.SEARCH_RESULT_ENTRY ); 146 } 147 148 SearchResultEntry searchResultEntry = getDecorated(); 149 element.addAttribute( DsmlLiterals.DN, searchResultEntry.getObjectName().getName() ); 150 151 Entry entry = searchResultEntry.getEntry(); 152 for ( Attribute attribute : entry ) 153 { 154 155 Element attributeElement = element.addElement( DsmlLiterals.ATTR ); 156 attributeElement.addAttribute( DsmlLiterals.NAME, attribute.getUpId() ); 157 158 for ( Value value : attribute ) 159 { 160 if ( value.isHumanReadable() ) 161 { 162 attributeElement.addElement( DsmlLiterals.VALUE ).addText( StringEscapeUtils.escapeXml11( value.getString() ) ); 163 } 164 else 165 { 166 Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI ); 167 Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI ); 168 Document doc = attributeElement.getDocument(); 169 170 if ( doc != null ) 171 { 172 Element docRoot = doc.getRootElement(); 173 docRoot.add( xsdNamespace ); 174 docRoot.add( xsiNamespace ); 175 } 176 177 Element valueElement = attributeElement.addElement( DsmlLiterals.VALUE ).addText( 178 ParserUtils.base64Encode( value.getBytes() ) ); 179 valueElement.addAttribute( new QName( DsmlLiterals.TYPE, xsiNamespace ), ParserUtils.XSD_COLON 180 + ParserUtils.BASE64BINARY ); 181 } 182 } 183 } 184 185 return element; 186 } 187 188 189 /** 190 * Get the entry Dn 191 * 192 * @return Returns the objectName. 193 */ 194 @Override 195 public Dn getObjectName() 196 { 197 return getDecorated().getObjectName(); 198 } 199 200 201 /** 202 * Set the entry Dn 203 * 204 * @param objectName The objectName to set. 205 */ 206 @Override 207 public void setObjectName( Dn objectName ) 208 { 209 getDecorated().setObjectName( objectName ); 210 } 211 212 213 /** 214 * Get the entry. 215 * 216 * @return Returns the entry. 217 */ 218 @Override 219 public Entry getEntry() 220 { 221 return getDecorated().getEntry(); 222 } 223 224 225 /** 226 * Initialize the entry. 227 * 228 * @param entry the entry 229 */ 230 @Override 231 public void setEntry( Entry entry ) 232 { 233 getDecorated().setEntry( entry ); 234 } 235}