001/*
002 * Copyright 2011-2014 UnboundID Corp.
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2011-2014 UnboundID Corp.
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.Arrays;
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 Arrays.asList(WhoAmIExtendedRequest.WHO_AM_I_REQUEST_OID);
081  }
082
083
084
085  /**
086   * {@inheritDoc}
087   */
088  @Override()
089  public ExtendedResult processExtendedOperation(
090                             final InMemoryRequestHandler handler,
091                             final int messageID, final ExtendedRequest request)
092  {
093    // This extended operation handler does not support any controls.  If the
094    // request has any critical controls, then reject it.
095    for (final Control c : request.getControls())
096    {
097      if (c.isCritical())
098      {
099        return new ExtendedResult(messageID,
100             ResultCode.UNAVAILABLE_CRITICAL_EXTENSION,
101             ERR_WHO_AM_I_EXTOP_UNSUPPORTED_CONTROL.get(c.getOID()), null, null,
102             null, null, null);
103      }
104    }
105
106    final String authorizationID =
107         "dn:" + handler.getAuthenticatedDN().toString();
108    return new WhoAmIExtendedResult(messageID, ResultCode.SUCCESS,  null,
109         null, null, authorizationID, null);
110  }
111}