001/*
002 * Copyright 2011-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2011-2020 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-2020 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.ThreadSafety;
051import com.unboundid.util.ThreadSafetyLevel;
052
053import static com.unboundid.ldap.listener.ListenerMessages.*;
054
055
056
057/**
058 * This class provides an implementation of an extended operation handler for
059 * the in-memory directory server that can be used to process the "Who Am I?"
060 * extended operation as defined in
061 * <A HREF="http://www.ietf.org/rfc/rfc4532.txt">RFC 4532</A>.
062 */
063@NotMutable()
064@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
065public final class WhoAmIExtendedOperationHandler
066       extends InMemoryExtendedOperationHandler
067{
068  /**
069   * Creates a new instance of this extended operation handler.
070   */
071  public WhoAmIExtendedOperationHandler()
072  {
073    // No initialization is required.
074  }
075
076
077
078  /**
079   * {@inheritDoc}
080   */
081  @Override()
082  public String getExtendedOperationHandlerName()
083  {
084    return "Who Am I?";
085  }
086
087
088
089  /**
090   * {@inheritDoc}
091   */
092  @Override()
093  public List<String> getSupportedExtendedRequestOIDs()
094  {
095    return Collections.singletonList(
096         WhoAmIExtendedRequest.WHO_AM_I_REQUEST_OID);
097  }
098
099
100
101  /**
102   * {@inheritDoc}
103   */
104  @Override()
105  public ExtendedResult processExtendedOperation(
106                             final InMemoryRequestHandler handler,
107                             final int messageID, final ExtendedRequest request)
108  {
109    // This extended operation handler does not support any controls.  If the
110    // request has any critical controls, then reject it.
111    for (final Control c : request.getControls())
112    {
113      if (c.isCritical())
114      {
115        return new ExtendedResult(messageID,
116             ResultCode.UNAVAILABLE_CRITICAL_EXTENSION,
117             ERR_WHO_AM_I_EXTOP_UNSUPPORTED_CONTROL.get(c.getOID()), null, null,
118             null, null, null);
119      }
120    }
121
122    final String authorizationID =
123         "dn:" + handler.getAuthenticatedDN().toString();
124    return new WhoAmIExtendedResult(messageID, ResultCode.SUCCESS,  null,
125         null, null, authorizationID, null);
126  }
127}