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.response; 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.ExtendedResponse; 027import org.apache.directory.api.ldap.model.message.ExtendedResponseImpl; 028import org.apache.directory.api.ldap.model.message.MessageTypeEnum; 029import org.apache.directory.api.util.Strings; 030import org.dom4j.Element; 031import org.dom4j.Namespace; 032import org.dom4j.QName; 033import org.dom4j.tree.DefaultElement; 034 035 036/** 037 * DSML Decorator for ExtendedResponse 038 * 039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 040 */ 041public class ExtendedResponseDsml extends AbstractResultResponseDsml<ExtendedResponse> 042 implements ExtendedResponse 043{ 044 private static final String EXTENDED_RESPONSE_TAG = "extendedResponse"; 045 private byte[] response; 046 047 048 /** 049 * Creates a new getDecoratedMessage() of ExtendedResponseDsml. 050 * 051 * @param codec The LDAP Service to use 052 */ 053 public ExtendedResponseDsml( LdapApiService codec ) 054 { 055 super( codec, new ExtendedResponseImpl( "" ) ); 056 } 057 058 059 /** 060 * Creates a new getDecoratedMessage() of ExtendedResponseDsml. 061 * 062 * @param codec The LDAP Service to use 063 * @param ldapMessage the message to decorate 064 */ 065 public ExtendedResponseDsml( LdapApiService codec, ExtendedResponse ldapMessage ) 066 { 067 super( codec, ldapMessage ); 068 } 069 070 071 /** 072 * {@inheritDoc} 073 */ 074 public MessageTypeEnum getType() 075 { 076 return getDecorated().getType(); 077 } 078 079 080 /** 081 * {@inheritDoc} 082 */ 083 public Element toDsml( Element root ) 084 { 085 Element element = null; 086 087 if ( root != null ) 088 { 089 element = root.addElement( EXTENDED_RESPONSE_TAG ); 090 } 091 else 092 { 093 element = new DefaultElement( EXTENDED_RESPONSE_TAG ); 094 } 095 096 ExtendedResponse extendedResponse = getDecorated(); 097 098 // LDAP Result 099 LdapResultDsml ldapResultDsml = new LdapResultDsml( getCodecService(), 100 getDecorated().getLdapResult(), getDecorated() ); 101 ldapResultDsml.toDsml( element ); 102 103 // ResponseName 104 String responseName = extendedResponse.getResponseName(); 105 if ( responseName != null ) 106 { 107 element.addElement( "responseName" ).addText( responseName ); 108 } 109 110 // Response 111 Object responseValue = getResponseValue(); 112 113 if ( responseValue != null ) 114 { 115 if ( ParserUtils.needsBase64Encoding( responseValue ) ) 116 { 117 Namespace xsdNamespace = new Namespace( ParserUtils.XSD, ParserUtils.XML_SCHEMA_URI ); 118 Namespace xsiNamespace = new Namespace( ParserUtils.XSI, ParserUtils.XML_SCHEMA_INSTANCE_URI ); 119 element.getDocument().getRootElement().add( xsdNamespace ); 120 element.getDocument().getRootElement().add( xsiNamespace ); 121 122 Element responseElement = element.addElement( "response" ) 123 .addText( ParserUtils.base64Encode( responseValue ) ); 124 responseElement.addAttribute( new QName( "type", xsiNamespace ), ParserUtils.XSD + ":" 125 + ParserUtils.BASE64BINARY ); 126 } 127 else 128 { 129 element.addElement( "response" ).addText( Strings.utf8ToString( ( byte[] ) responseValue ) ); 130 } 131 } 132 133 return element; 134 } 135 136 137 /** 138 * {@inheritDoc} 139 */ 140 public void setResponseName( String oid ) 141 { 142 getDecorated().setResponseName( oid ); 143 } 144 145 146 /** 147 * Get the extended response name 148 * 149 * @return Returns the name. 150 */ 151 public String getResponseName() 152 { 153 return getDecorated().getResponseName(); 154 } 155 156 157 /** 158 * Set the extended response name 159 * 160 * @param responseName The name to set. 161 */ 162 public void setResponseName( Oid responseName ) 163 { 164 getDecorated().setResponseName( responseName.toString() ); 165 } 166 167 168 /** 169 * Get the extended response 170 * 171 * @return Returns the response. 172 */ 173 public byte[] getResponseValue() 174 { 175 return this.response; 176 } 177 178 179 /** 180 * Set the extended response 181 * 182 * @param responseValue The response to set. 183 */ 184 public void setResponseValue( byte[] responseValue ) 185 { 186 this.response = responseValue; 187 } 188}