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