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 2012-2015 ForgeRock AS.
015 */
016
017package org.forgerock.json.resource;
018
019import java.util.Collection;
020
021import org.forgerock.services.context.Context;
022
023/**
024 * An abstract connection whose synchronous methods are implemented in terms of
025 * asynchronous methods.
026 */
027public abstract class AbstractAsynchronousConnection implements Connection {
028    /**
029     * Creates a new abstract asynchronous connection.
030     */
031    protected AbstractAsynchronousConnection() {
032        // No implementation required.
033    }
034
035    @Override
036    public ActionResponse action(final Context context, final ActionRequest request)
037            throws ResourceException {
038        return actionAsync(context, request).getOrThrowUninterruptibly();
039    }
040
041    @Override
042    public ResourceResponse create(final Context context, final CreateRequest request)
043            throws ResourceException {
044        return createAsync(context, request).getOrThrowUninterruptibly();
045    }
046
047    @Override
048    public ResourceResponse delete(final Context context, final DeleteRequest request)
049            throws ResourceException {
050        return deleteAsync(context, request).getOrThrowUninterruptibly();
051    }
052
053    @Override
054    public ResourceResponse patch(final Context context, final PatchRequest request)
055            throws ResourceException {
056        return patchAsync(context, request).getOrThrowUninterruptibly();
057    }
058
059    @Override
060    public QueryResponse query(final Context context, final QueryRequest request,
061            final QueryResourceHandler handler) throws ResourceException {
062        return queryAsync(context, request, handler).getOrThrowUninterruptibly();
063    }
064
065    @Override
066    public QueryResponse query(final Context context, final QueryRequest request,
067            final Collection<? super ResourceResponse> results) throws ResourceException {
068        return query(context, request, new QueryResourceHandler() {
069            @Override
070            public boolean handleResource(final ResourceResponse resource) {
071                results.add(resource);
072                return true;
073            }
074        });
075    }
076
077    @Override
078    public ResourceResponse read(final Context context, final ReadRequest request) throws ResourceException {
079        return readAsync(context, request).getOrThrowUninterruptibly();
080    }
081
082    @Override
083    public ResourceResponse update(final Context context, final UpdateRequest request)
084            throws ResourceException {
085        return updateAsync(context, request).getOrThrowUninterruptibly();
086    }
087}