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    /**
030     * Creates a new abstract asynchronous connection.
031     */
032    protected AbstractAsynchronousConnection() {
033        // No implementation required.
034    }
035
036    @Override
037    public ActionResponse action(final Context context, final ActionRequest request)
038            throws ResourceException {
039        try {
040            return actionAsync(context, request).getOrThrow();
041        } catch (InterruptedException e) {
042            throw newTimeoutException(e);
043        }
044    }
045
046    @Override
047    public ResourceResponse create(final Context context, final CreateRequest request)
048            throws ResourceException {
049        try {
050            return createAsync(context, request).getOrThrow();
051        } catch (InterruptedException e) {
052            throw newTimeoutException(e);
053        }
054    }
055
056    @Override
057    public ResourceResponse delete(final Context context, final DeleteRequest request)
058            throws ResourceException {
059        try {
060            return deleteAsync(context, request).getOrThrow();
061        } catch (InterruptedException e) {
062            throw newTimeoutException(e);
063        }
064    }
065
066    @Override
067    public ResourceResponse patch(final Context context, final PatchRequest request)
068            throws ResourceException {
069        try {
070            return patchAsync(context, request).getOrThrow();
071        } catch (InterruptedException e) {
072            throw newTimeoutException(e);
073        }
074    }
075
076    @Override
077    public QueryResponse query(final Context context, final QueryRequest request,
078            final QueryResourceHandler handler) throws ResourceException {
079        try {
080            return queryAsync(context, request, handler).getOrThrow();
081        } catch (InterruptedException e) {
082            throw newTimeoutException(e);
083        }
084    }
085
086    @Override
087    public QueryResponse query(final Context context, final QueryRequest request,
088            final Collection<? super ResourceResponse> results) throws ResourceException {
089        return query(context, request, new QueryResourceHandler() {
090            @Override
091            public boolean handleResource(final ResourceResponse resource) {
092                results.add(resource);
093                return true;
094            }
095        });
096    }
097
098    @Override
099    public ResourceResponse read(final Context context, final ReadRequest request) throws ResourceException {
100        try {
101            return readAsync(context, request).getOrThrow();
102        } catch (InterruptedException e) {
103            throw newTimeoutException(e);
104        }
105    }
106
107    @Override
108    public ResourceResponse update(final Context context, final UpdateRequest request)
109            throws ResourceException {
110        try {
111            return updateAsync(context, request).getOrThrow();
112        } catch (InterruptedException e) {
113            throw newTimeoutException(e);
114        }
115    }
116
117    private ResourceException newTimeoutException(Exception cause) {
118        return ResourceException.newResourceException(503, "Request was interrupted", cause);
119    }
120}