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