View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.server.ldap.handlers.extended;
21  
22  
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.apache.directory.api.ldap.extras.extended.ads_impl.whoAmI.WhoAmIFactory;
28  import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIRequest;
29  import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponse;
30  import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponseImpl;
31  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
32  import org.apache.directory.api.util.Strings;
33  import org.apache.directory.server.core.api.LdapPrincipal;
34  import org.apache.directory.server.ldap.ExtendedOperationHandler;
35  import org.apache.directory.server.ldap.LdapServer;
36  import org.apache.directory.server.ldap.LdapSession;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  
41  /**
42   * An handler to manage the WhoAmI extended request operation
43   *
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class WhoAmIHandler implements ExtendedOperationHandler<WhoAmIRequest, WhoAmIResponse>
47  {
48      private static final Logger LOG = LoggerFactory.getLogger( WhoAmIHandler.class );
49      public static final Set<String> EXTENSION_OIDS;
50  
51      static
52      {
53          Set<String> set = new HashSet<>( 2 );
54          set.add( WhoAmIRequest.EXTENSION_OID );
55          set.add( WhoAmIResponse.EXTENSION_OID );
56          EXTENSION_OIDS = Collections.unmodifiableSet( set );
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      public String getOid()
64      {
65          return WhoAmIRequest.EXTENSION_OID;
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public void handleExtendedOperation( LdapSession requestor, WhoAmIRequest req ) throws Exception
73      {
74          LOG.debug( "WhoAmI requested" );
75  
76          LdapPrincipal ldapPrincipal = requestor.getCoreSession().getAuthenticatedPrincipal();
77          
78          WhoAmIResponseImpl whoAmIResponse = new WhoAmIResponseImpl( req.getMessageId(), ResultCodeEnum.SUCCESS );
79  
80          String authzId = "dn:" + ldapPrincipal.getDn();
81         
82          WhoAmIFactory.decode( whoAmIResponse, Strings.getBytesUtf8( authzId ) );
83          whoAmIResponse.setAuthzId( Strings.getBytesUtf8( authzId ) );
84          
85          // write the response
86          requestor.getIoSession().write( whoAmIResponse );
87      }
88  
89  
90      /**
91       * {@inheritDoc}
92       */
93      public Set<String> getExtensionOids()
94      {
95          return EXTENSION_OIDS;
96      }
97  
98  
99      /**
100      * {@inheritDoc}
101      */
102     public void setLdapServer( LdapServer ldapServer )
103     {
104     }
105 }