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 2013-2015 ForgeRock AS. 015 */ 016 017package org.forgerock.json.resource; 018 019import java.util.Collection; 020 021import org.forgerock.services.context.Context; 022import org.forgerock.util.promise.Promise; 023 024/** 025 * An abstract base class from which connection wrappers may be easily 026 * implemented. The default implementation of each method is to delegate to the 027 * wrapped connection. 028 * 029 * @param <C> 030 * The type of wrapped connection. 031 */ 032public abstract class AbstractConnectionWrapper<C extends Connection> implements Connection { 033 /** 034 * The wrapped connection. 035 */ 036 protected final C connection; 037 038 /** 039 * Creates a new connection wrapper. 040 * 041 * @param connection 042 * The connection to be wrapped. 043 */ 044 protected AbstractConnectionWrapper(final C connection) { 045 this.connection = connection; 046 } 047 048 /** 049 * Optional Context-transformation function if the implementer has 050 * requirements to override the {@link Context} provided in the 051 * {@link Connection}'s method invocations. 052 * <p> 053 * The default implementation is a pass-through no-op. 054 * 055 * @param context 056 * the request context 057 * @return the transformed context 058 */ 059 protected Context transform(Context context) { 060 return context; 061 } 062 063 /** 064 * {@inheritDoc} 065 * <p> 066 * The default implementation is to delegate. 067 */ 068 public ActionResponse action(Context context, ActionRequest request) throws ResourceException { 069 return connection.action(transform(context), request); 070 } 071 072 /** 073 * {@inheritDoc} 074 * <p> 075 * The default implementation is to delegate. 076 */ 077 public Promise<ActionResponse, ResourceException> actionAsync(Context context, ActionRequest request) { 078 return connection.actionAsync(transform(context), request); 079 } 080 081 /** 082 * {@inheritDoc} 083 * <p> 084 * The default implementation is to delegate. 085 */ 086 public void close() { 087 connection.close(); 088 } 089 090 /** 091 * {@inheritDoc} 092 * <p> 093 * The default implementation is to delegate. 094 */ 095 public ResourceResponse create(Context context, CreateRequest request) throws ResourceException { 096 return connection.create(transform(context), request); 097 } 098 099 /** 100 * {@inheritDoc} 101 * <p> 102 * The default implementation is to delegate. 103 */ 104 public Promise<ResourceResponse, ResourceException> createAsync(Context context, CreateRequest request) { 105 return connection.createAsync(transform(context), request); 106 } 107 108 /** 109 * {@inheritDoc} 110 * <p> 111 * The default implementation is to delegate. 112 */ 113 public ResourceResponse delete(Context context, DeleteRequest request) throws ResourceException { 114 return connection.delete(transform(context), request); 115 } 116 117 /** 118 * {@inheritDoc} 119 * <p> 120 * The default implementation is to delegate. 121 */ 122 public Promise<ResourceResponse, ResourceException> deleteAsync(Context context, DeleteRequest request) { 123 return connection.deleteAsync(transform(context), request); 124 } 125 126 /** 127 * {@inheritDoc} 128 * <p> 129 * The default implementation is to delegate. 130 */ 131 public boolean isClosed() { 132 return connection.isClosed(); 133 } 134 135 /** 136 * {@inheritDoc} 137 * <p> 138 * The default implementation is to delegate. 139 */ 140 public boolean isValid() { 141 return connection.isValid(); 142 } 143 144 /** 145 * {@inheritDoc} 146 * <p> 147 * The default implementation is to delegate. 148 */ 149 public ResourceResponse patch(Context context, PatchRequest request) throws ResourceException { 150 return connection.patch(transform(context), request); 151 } 152 153 /** 154 * {@inheritDoc} 155 * <p> 156 * The default implementation is to delegate. 157 */ 158 public Promise<ResourceResponse, ResourceException> patchAsync(Context context, PatchRequest request) { 159 return connection.patchAsync(transform(context), request); 160 } 161 162 /** 163 * {@inheritDoc} 164 * <p> 165 * The default implementation is to delegate. 166 */ 167 public QueryResponse query(Context context, QueryRequest request, QueryResourceHandler handler) 168 throws ResourceException { 169 return connection.query(transform(context), request, handler); 170 } 171 172 /** 173 * {@inheritDoc} 174 * <p> 175 * The default implementation is to delegate. 176 */ 177 public QueryResponse query(Context context, QueryRequest request, 178 Collection<? super ResourceResponse> results) throws ResourceException { 179 return connection.query(transform(context), request, results); 180 } 181 182 /** 183 * {@inheritDoc} 184 * <p> 185 * The default implementation is to delegate. 186 */ 187 public Promise<QueryResponse, ResourceException> queryAsync(Context context, 188 QueryRequest request, QueryResourceHandler handler) { 189 return connection.queryAsync(transform(context), request, handler); 190 } 191 192 /** 193 * {@inheritDoc} 194 * <p> 195 * The default implementation is to delegate. 196 */ 197 public ResourceResponse read(Context context, ReadRequest request) throws ResourceException { 198 return connection.read(transform(context), request); 199 } 200 201 /** 202 * {@inheritDoc} 203 * <p> 204 * The default implementation is to delegate. 205 */ 206 public Promise<ResourceResponse, ResourceException> readAsync(Context context, ReadRequest request) { 207 return connection.readAsync(transform(context), request); 208 } 209 210 /** 211 * {@inheritDoc} 212 * <p> 213 * The default implementation is to delegate. 214 */ 215 public ResourceResponse update(Context context, UpdateRequest request) throws ResourceException { 216 return connection.update(transform(context), request); 217 } 218 219 /** 220 * {@inheritDoc} 221 * <p> 222 * The default implementation is to delegate. 223 */ 224 public Promise<ResourceResponse, ResourceException> updateAsync(Context context, UpdateRequest request) { 225 return connection.updateAsync(transform(context), request); 226 } 227 228}