001/*
002 * Copyright 2011-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2011-2018 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.listener;
022
023
024
025import java.util.Collections;
026import java.util.List;
027
028import com.unboundid.ldap.sdk.Control;
029import com.unboundid.ldap.sdk.ExtendedRequest;
030import com.unboundid.ldap.sdk.ExtendedResult;
031import com.unboundid.ldap.sdk.ResultCode;
032import com.unboundid.ldap.sdk.extensions.WhoAmIExtendedRequest;
033import com.unboundid.ldap.sdk.extensions.WhoAmIExtendedResult;
034import com.unboundid.util.NotMutable;
035import com.unboundid.util.ThreadSafety;
036import com.unboundid.util.ThreadSafetyLevel;
037
038import static com.unboundid.ldap.listener.ListenerMessages.*;
039
040
041
042/**
043 * This class provides an implementation of an extended operation handler for
044 * the in-memory directory server that can be used to process the "Who Am I?"
045 * extended operation as defined in
046 * <A HREF="http://www.ietf.org/rfc/rfc4532.txt">RFC 4532</A>.
047 */
048@NotMutable()
049@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
050public final class WhoAmIExtendedOperationHandler
051       extends InMemoryExtendedOperationHandler
052{
053  /**
054   * Creates a new instance of this extended operation handler.
055   */
056  public WhoAmIExtendedOperationHandler()
057  {
058    // No initialization is required.
059  }
060
061
062
063  /**
064   * {@inheritDoc}
065   */
066  @Override()
067  public String getExtendedOperationHandlerName()
068  {
069    return "Who Am I?";
070  }
071
072
073
074  /**
075   * {@inheritDoc}
076   */
077  @Override()
078  public List<String> getSupportedExtendedRequestOIDs()
079  {
080    return Collections.singletonList(
081         WhoAmIExtendedRequest.WHO_AM_I_REQUEST_OID);
082  }
083
084
085
086  /**
087   * {@inheritDoc}
088   */
089  @Override()
090  public ExtendedResult processExtendedOperation(
091                             final InMemoryRequestHandler handler,
092                             final int messageID, final ExtendedRequest request)
093  {
094    // This extended operation handler does not support any controls.  If the
095    // request has any critical controls, then reject it.
096    for (final Control c : request.getControls())
097    {
098      if (c.isCritical())
099      {
100        return new ExtendedResult(messageID,
101             ResultCode.UNAVAILABLE_CRITICAL_EXTENSION,
102             ERR_WHO_AM_I_EXTOP_UNSUPPORTED_CONTROL.get(c.getOID()), null, null,
103             null, null, null);
104      }
105    }
106
107    final String authorizationID =
108         "dn:" + handler.getAuthenticatedDN().toString();
109    return new WhoAmIExtendedResult(messageID, ResultCode.SUCCESS,  null,
110         null, null, authorizationID, null);
111  }
112}