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.server.ldap.handlers.extended;
021
022
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.Set;
026
027import org.apache.directory.api.ldap.extras.extended.ads_impl.whoAmI.WhoAmIFactory;
028import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIRequest;
029import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponse;
030import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponseImpl;
031import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
032import org.apache.directory.api.util.Strings;
033import org.apache.directory.server.core.api.LdapPrincipal;
034import org.apache.directory.server.ldap.ExtendedOperationHandler;
035import org.apache.directory.server.ldap.LdapServer;
036import org.apache.directory.server.ldap.LdapSession;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040
041/**
042 * An handler to manage the WhoAmI extended request operation
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046public class WhoAmIHandler implements ExtendedOperationHandler<WhoAmIRequest, WhoAmIResponse>
047{
048    private static final Logger LOG = LoggerFactory.getLogger( WhoAmIHandler.class );
049    public static final Set<String> EXTENSION_OIDS;
050
051    static
052    {
053        Set<String> set = new HashSet<>( 2 );
054        set.add( WhoAmIRequest.EXTENSION_OID );
055        set.add( WhoAmIResponse.EXTENSION_OID );
056        EXTENSION_OIDS = Collections.unmodifiableSet( set );
057    }
058
059
060    /**
061     * {@inheritDoc}
062     */
063    public String getOid()
064    {
065        return WhoAmIRequest.EXTENSION_OID;
066    }
067
068
069    /**
070     * {@inheritDoc}
071     */
072    public void handleExtendedOperation( LdapSession requestor, WhoAmIRequest req ) throws Exception
073    {
074        LOG.debug( "WhoAmI requested" );
075
076        LdapPrincipal ldapPrincipal = requestor.getCoreSession().getAuthenticatedPrincipal();
077        
078        WhoAmIResponseImpl whoAmIResponse = new WhoAmIResponseImpl( req.getMessageId(), ResultCodeEnum.SUCCESS );
079
080        String authzId = "dn:" + ldapPrincipal.getDn();
081       
082        WhoAmIFactory.decode( whoAmIResponse, Strings.getBytesUtf8( authzId ) );
083        whoAmIResponse.setAuthzId( Strings.getBytesUtf8( authzId ) );
084        
085        // write the response
086        requestor.getIoSession().write( whoAmIResponse );
087    }
088
089
090    /**
091     * {@inheritDoc}
092     */
093    public Set<String> getExtensionOids()
094    {
095        return EXTENSION_OIDS;
096    }
097
098
099    /**
100     * {@inheritDoc}
101     */
102    public void setLdapServer( LdapServer ldapServer )
103    {
104    }
105}