1 17 package org.apache.ldap.server.exception; 18 19 20 import org.apache.ldap.common.exception.LdapContextNotEmptyException; 21 import org.apache.ldap.common.exception.LdapNameAlreadyBoundException; 22 import org.apache.ldap.common.exception.LdapNameNotFoundException; 23 import org.apache.ldap.common.exception.LdapNamingException; 24 import org.apache.ldap.common.message.ResultCodeEnum; 25 import org.apache.ldap.common.name.LdapName; 26 import org.apache.ldap.server.BackingStore; 27 import org.apache.ldap.server.RootNexus; 28 import org.apache.ldap.server.interceptor.BaseInterceptor; 29 import org.apache.ldap.server.interceptor.InterceptorContext; 30 import org.apache.ldap.server.interceptor.NextInterceptor; 31 import org.apache.ldap.server.invocation.*; 32 33 import javax.naming.Name ; 34 import javax.naming.NamingEnumeration ; 35 import javax.naming.NamingException ; 36 import javax.naming.directory.Attribute ; 37 import javax.naming.directory.Attributes ; 38 39 40 49 public class ExceptionService extends BaseInterceptor 50 { 51 54 private RootNexus nexus; 55 56 57 60 public ExceptionService() 61 { 62 } 63 64 65 public void init( InterceptorContext ctx ) 66 { 67 this.nexus = ctx.getRootNexus(); 68 } 69 70 71 public void destroy() 72 { 73 } 74 75 76 80 protected void process( NextInterceptor nextInterceptor, Add call ) throws NamingException 81 { 82 Name normName = call.getNormalizedName(); 84 String upName = call.getUserProvidedName(); 85 if ( nexus.hasEntry( normName ) ) 86 { 87 NamingException ne = new LdapNameAlreadyBoundException(); 88 ne.setResolvedName( new LdapName( upName ) ); 89 throw ne; 90 } 91 92 Name parentDn = new LdapName( upName ); 93 parentDn = parentDn.getSuffix( 1 ); 94 95 assertHasEntry( "Attempt to add under non-existant parent: ", parentDn ); 97 98 Attributes attrs = nexus.lookup( normName.getSuffix( 1 ) ); 100 Attribute objectClass = attrs.get( "objectClass" ); 101 if ( objectClass.contains( "alias" ) ) 102 { 103 String msg = "Attempt to add entry to alias '" + upName 104 + "' not allowed."; 105 ResultCodeEnum rc = ResultCodeEnum.ALIASPROBLEM; 106 NamingException e = new LdapNamingException( msg, rc ); 107 e.setResolvedName( parentDn ); 108 throw e; 109 } 110 111 nextInterceptor.process( call ); 112 } 113 114 115 119 protected void process( NextInterceptor nextInterceptor, Delete call ) throws NamingException 120 { 121 Name name = call.getName(); 122 123 String msg = "Attempt to delete non-existant entry: "; 125 assertHasEntry( msg, name ); 126 127 boolean hasChildren = false; 129 NamingEnumeration list = nexus.list( name ); 130 if ( list.hasMore() ) 131 { 132 hasChildren = true; 133 } 134 135 list.close(); 136 if ( hasChildren ) 137 { 138 LdapContextNotEmptyException e = new LdapContextNotEmptyException(); 139 e.setResolvedName( name ); 140 throw e; 141 } 142 143 nextInterceptor.process( call ); 144 } 145 146 147 150 protected void process( NextInterceptor nextInterceptor, List call ) throws NamingException 151 { 152 String msg = "Attempt to search under non-existant entry: "; 154 assertHasEntry( msg, call.getBaseName() ); 155 156 nextInterceptor.process( call ); 157 } 158 159 160 163 protected void process( NextInterceptor nextInterceptor, Lookup call ) throws NamingException 164 { 165 String msg = "Attempt to lookup non-existant entry: "; 166 assertHasEntry( msg, call.getName() ); 167 168 nextInterceptor.process( call ); 169 } 170 171 172 175 protected void process( NextInterceptor nextInterceptor, LookupWithAttrIds call ) throws NamingException 176 { 177 String msg = "Attempt to lookup non-existant entry: "; 179 assertHasEntry( msg, call.getName() ); 180 181 nextInterceptor.process( call ); 182 } 183 184 185 188 protected void process( NextInterceptor nextInterceptor, Modify call ) throws NamingException 189 { 190 String msg = "Attempt to modify non-existant entry: "; 192 assertHasEntry( msg, call.getName() ); 193 194 nextInterceptor.process( call ); 195 } 196 197 198 201 protected void process( NextInterceptor nextInterceptor, ModifyMany call ) throws NamingException 202 { 203 String msg = "Attempt to modify non-existant entry: "; 205 assertHasEntry( msg, call.getName() ); 206 207 nextInterceptor.process( call ); 208 } 209 210 211 214 protected void process( NextInterceptor nextInterceptor, ModifyRN call ) throws NamingException 215 { 216 Name dn = call.getName(); 217 String newRdn = call.getNewRelativeName(); 218 219 String msg = "Attempt to rename non-existant entry: "; 221 assertHasEntry( msg, dn ); 222 223 Name target = dn.getSuffix( 1 ).add( newRdn ); 225 if ( nexus.hasEntry( target ) ) 226 { 227 LdapNameAlreadyBoundException e = null; 228 e = new LdapNameAlreadyBoundException( "target entry " + target 229 + " already exists!" ); 230 e.setResolvedName( target ); 231 throw e; 232 } 233 234 nextInterceptor.process( call ); 235 } 236 237 238 242 protected void process( NextInterceptor nextInterceptor, Move call ) throws NamingException 243 { 244 String msg = "Attempt to move to non-existant parent: "; 246 assertHasEntry( msg, call.getName() ); 247 248 msg = "Attempt to move to non-existant parent: "; 250 assertHasEntry( msg, call.getNewParentName() ); 251 252 String rdn = call.getName().get( call.getName().size() - 1 ); 254 Name target = ( Name ) call.getNewParentName().clone(); 255 target.add( rdn ); 256 if ( nexus.hasEntry( target ) ) 257 { 258 LdapNameAlreadyBoundException e = null; 259 e = new LdapNameAlreadyBoundException( "target entry " + target 260 + " already exists!" ); 261 e.setResolvedName( target ); 262 throw e; 263 } 264 265 nextInterceptor.process( call ); 266 } 267 268 269 273 protected void process( NextInterceptor nextInterceptor, MoveAndModifyRN call ) throws NamingException 274 { 275 String msg = "Attempt to move to non-existant parent: "; 277 assertHasEntry( msg, call.getName() ); 278 279 msg = "Attempt to move to non-existant parent: "; 281 assertHasEntry( msg, call.getNewParentName() ); 282 283 Name target = ( Name ) call.getNewParentName().clone(); 285 target.add( call.getNewRelativeName() ); 286 if ( nexus.hasEntry( target ) ) 287 { 288 LdapNameAlreadyBoundException e = null; 289 e = new LdapNameAlreadyBoundException( "target entry " + target 290 + " already exists!" ); 291 e.setResolvedName( target ); 292 throw e; 293 } 294 295 nextInterceptor.process( call ); 296 } 297 298 299 302 protected void process( NextInterceptor nextInterceptor, Search call ) throws NamingException 303 { 304 String msg = "Attempt to search under non-existant entry: "; 305 306 Name base = call.getBaseName(); 307 if ( base.size() == 0 ) 308 { 309 nextInterceptor.process( call ); 310 return; 311 } 312 313 Attribute attr = nexus.getRootDSE().get( "subschemaSubentry" ); 314 if ( ( ( String ) attr.get() ).equalsIgnoreCase( base.toString() ) ) 315 { 316 nextInterceptor.process( call ); 317 return; 318 } 319 320 assertHasEntry( msg, base ); 321 322 nextInterceptor.process( call ); 323 } 324 325 326 334 private void assertHasEntry( String msg, Name dn ) throws NamingException 335 { 336 if ( !nexus.hasEntry( dn ) ) 337 { 338 LdapNameNotFoundException e = null; 339 340 if ( msg != null ) 341 { 342 e = new LdapNameNotFoundException( msg + dn ); 343 } 344 else 345 { 346 e = new LdapNameNotFoundException( dn.toString() ); 347 } 348 349 e.setResolvedName( nexus.getMatchedDn( dn, false ) ); 350 throw e; 351 } 352 } 353 } 354 | Popular Tags |