001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2010 Sun Microsystems, Inc.
015 * Portions Copyright 2012-2014 ForgeRock AS.
016 */
017
018package org.forgerock.opendj.ldap.responses;
019
020import static org.forgerock.opendj.ldap.LdapException.newLdapException;
021
022import org.forgerock.opendj.ldap.DecodeException;
023import org.forgerock.opendj.ldap.DecodeOptions;
024import org.forgerock.opendj.ldap.LdapException;
025import org.forgerock.opendj.ldap.ResultCode;
026import org.forgerock.opendj.ldap.LdapResultHandler;
027import org.forgerock.opendj.ldap.requests.ExtendedRequest;
028
029/**
030 * This class provides a skeletal implementation of the
031 * {@code ExtendedResultDecoder} interface, to minimize the effort required to
032 * implement this interface.
033 *
034 * @param <S>
035 *            The type of result.
036 */
037public abstract class AbstractExtendedResultDecoder<S extends ExtendedResult> implements
038        ExtendedResultDecoder<S> {
039    /**
040     * Creates a new abstract extended result decoder.
041     */
042    protected AbstractExtendedResultDecoder() {
043        // Nothing to do.
044    }
045
046    @Override
047    public S adaptDecodeException(final DecodeException exception) {
048        final S adaptedResult =
049                newExtendedErrorResult(ResultCode.PROTOCOL_ERROR, "", exception.getMessage());
050        adaptedResult.setCause(exception.getCause());
051        return adaptedResult;
052    }
053
054    @Override
055    public <R extends ExtendedResult> LdapResultHandler<S> adaptExtendedResultHandler(
056            final ExtendedRequest<R> request, final LdapResultHandler<? super R> resultHandler,
057            final DecodeOptions options) {
058        return new LdapResultHandler<S>() {
059
060            @Override
061            public void handleException(final LdapException error) {
062                final Result result = error.getResult();
063                final R adaptedResult =
064                        request.getResultDecoder().newExtendedErrorResult(result.getResultCode(),
065                                result.getMatchedDN(), result.getDiagnosticMessage());
066                adaptedResult.setCause(result.getCause());
067                resultHandler.handleException(newLdapException(adaptedResult));
068            }
069
070            @Override
071            public void handleResult(final S result) {
072                try {
073                    final R adaptedResult =
074                            request.getResultDecoder().decodeExtendedResult(result, options);
075                    resultHandler.handleResult(adaptedResult);
076                } catch (final DecodeException e) {
077                    final R adaptedResult = request.getResultDecoder().adaptDecodeException(e);
078                    resultHandler.handleException(newLdapException(adaptedResult));
079                }
080            }
081
082        };
083    }
084
085    @Override
086    public abstract S decodeExtendedResult(ExtendedResult result, DecodeOptions options)
087            throws DecodeException;
088
089    @Override
090    public abstract S newExtendedErrorResult(ResultCode resultCode, String matchedDN,
091            String diagnosticMessage);
092
093}