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 java.util.Collection; 024import java.util.List; 025 026import org.apache.directory.api.dsmlv2.DsmlDecorator; 027import org.apache.directory.api.dsmlv2.ParserUtils; 028import org.apache.directory.api.ldap.codec.api.LdapApiService; 029import org.apache.directory.api.ldap.model.message.LdapResult; 030import org.apache.directory.api.ldap.model.message.Message; 031import org.apache.directory.api.ldap.model.message.Referral; 032import org.apache.directory.api.ldap.model.message.ResultCodeEnum; 033import org.apache.directory.api.ldap.model.name.Dn; 034import org.apache.directory.api.ldap.model.url.LdapUrl; 035import org.dom4j.Element; 036 037 038/** 039 * DSML Decorator for the LdapResult class. 040 * 041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 042 */ 043public class LdapResultDsml implements DsmlDecorator<LdapResult>, LdapResult 044{ 045 /** The LDAP Result to decorate */ 046 private LdapResult result; 047 048 /** The associated LDAP Message */ 049 private Message message; 050 051 /** The ldap codec service */ 052 private LdapApiService codec; 053 054 055 /** 056 * Creates a new instance of LdapResultDsml. 057 * 058 * @param codec The LDAP Service to use 059 * @param result the LdapResult to decorate 060 * @param message the associated message 061 */ 062 public LdapResultDsml( LdapApiService codec, LdapResult result, Message message ) 063 { 064 this.codec = codec; 065 this.result = result; 066 this.message = message; 067 } 068 069 070 /** 071 * {@inheritDoc} 072 */ 073 public Element toDsml( Element root ) 074 { 075 076 // RequestID 077 int requestID = message.getMessageId(); 078 if ( requestID > 0 ) 079 { 080 root.addAttribute( "requestID", Integer.toString( requestID ) ); 081 } 082 083 // Matched Dn 084 Dn matchedDn = result.getMatchedDn(); 085 086 if ( !Dn.isNullOrEmpty( matchedDn ) ) 087 { 088 root.addAttribute( "matchedDn", matchedDn.getName() ); 089 } 090 091 // Controls 092 ParserUtils.addControls( codec, root, message.getControls().values() ); 093 094 // ResultCode 095 Element resultCodeElement = root.addElement( "resultCode" ); 096 resultCodeElement.addAttribute( "code", Integer.toString( result.getResultCode().getResultCode() ) ); 097 resultCodeElement.addAttribute( "descr", result.getResultCode().getMessage() ); 098 099 // ErrorMessage 100 String errorMessage = ( result.getDiagnosticMessage() ); 101 102 if ( ( errorMessage != null ) && ( errorMessage.length() != 0 ) ) 103 { 104 Element errorMessageElement = root.addElement( "errorMessage" ); 105 errorMessageElement.addText( errorMessage ); 106 } 107 108 // Referrals 109 Referral referral = result.getReferral(); 110 if ( referral != null ) 111 { 112 Collection<String> ldapUrls = referral.getLdapUrls(); 113 if ( ldapUrls != null ) 114 { 115 for ( String ldapUrl : ldapUrls ) 116 { 117 Element referalElement = root.addElement( "referal" ); 118 referalElement.addText( ldapUrl ); 119 } 120 } 121 } 122 123 return root; 124 } 125 126 127 /** 128 * {@inheritDoc} 129 */ 130 public String getDiagnosticMessage() 131 { 132 return result.getDiagnosticMessage(); 133 } 134 135 136 /** 137 * {@inheritDoc} 138 */ 139 public void setDiagnosticMessage( String diagnosticMessage ) 140 { 141 result.setDiagnosticMessage( diagnosticMessage ); 142 } 143 144 145 /** 146 * Get the matched Dn 147 * 148 * @return Returns the matchedDN. 149 */ 150 public Dn getMatchedDn() 151 { 152 return result.getMatchedDn(); 153 } 154 155 156 /** 157 * Set the Matched Dn 158 * 159 * @param matchedDn The matchedDn to set. 160 */ 161 public void setMatchedDn( Dn matchedDn ) 162 { 163 result.setMatchedDn( matchedDn ); 164 } 165 166 167 /** 168 * Get the referrals 169 * 170 * @return Returns the referrals. 171 */ 172 public List<String> getReferrals() 173 { 174 return ( List<String> ) result.getReferral().getLdapUrls(); 175 } 176 177 178 /** 179 * Add a referral 180 * 181 * @param referral The referral to add. 182 */ 183 public void addReferral( LdapUrl referral ) 184 { 185 result.getReferral().addLdapUrl( referral.toString() ); 186 } 187 188 189 /** 190 * Get the result code 191 * 192 * @return Returns the resultCode. 193 */ 194 public ResultCodeEnum getResultCode() 195 { 196 return result.getResultCode(); 197 } 198 199 200 /** 201 * Set the result code 202 * 203 * @param resultCode The resultCode to set. 204 */ 205 public void setResultCode( ResultCodeEnum resultCode ) 206 { 207 result.setResultCode( resultCode ); 208 } 209 210 211 /** 212 * {@inheritDoc} 213 */ 214 public LdapResult getDecorated() 215 { 216 return result; 217 } 218 219 220 /** 221 * {@inheritDoc} 222 */ 223 public boolean isReferral() 224 { 225 return getDecorated().isReferral(); 226 } 227 228 229 /** 230 * {@inheritDoc} 231 */ 232 public Referral getReferral() 233 { 234 return getDecorated().getReferral(); 235 } 236 237 238 /** 239 * {@inheritDoc} 240 */ 241 public void setReferral( Referral referral ) 242 { 243 getDecorated().setReferral( referral ); 244 } 245 246 247 /** 248 * {@inheritDoc} 249 */ 250 public boolean isDefaultSuccess() 251 { 252 return false; 253 } 254}