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 2016 ForgeRock AS.
015 */
016
017package org.forgerock.json.resource;
018
019import org.forgerock.api.models.ApiDescription;
020import org.forgerock.http.ApiProducer;
021import org.forgerock.services.context.Context;
022import org.forgerock.services.descriptor.Describable;
023
024/**
025 * Version of {@link SynchronousRequestHandlerAdapter} that exposes a described handler.
026 */
027public class DescribedSyncRequestHandlerAdapter extends SynchronousRequestHandlerAdapter
028        implements Describable<ApiDescription, Request> {
029
030    private final Describable<ApiDescription, Request> described;
031
032    @SuppressWarnings("unchecked")
033    DescribedSyncRequestHandlerAdapter(SynchronousRequestHandler syncHandler) {
034        super(syncHandler);
035        if (!(syncHandler instanceof Describable)) {
036            throw new IllegalArgumentException("Handler must be Describable");
037        }
038        this.described = (Describable<ApiDescription, Request>) syncHandler;
039    }
040
041    @Override
042    public ApiDescription api(ApiProducer<ApiDescription> producer) {
043        return described.api(producer);
044    }
045
046    @Override
047    public ApiDescription handleApiRequest(Context context, Request request) {
048        return described.handleApiRequest(context, request);
049    }
050
051    @Override
052    public void addDescriptorListener(Describable.Listener listener) {
053        described.addDescriptorListener(listener);
054    }
055
056    @Override
057    public void removeDescriptorListener(Describable.Listener listener) {
058        described.removeDescriptorListener(listener);
059    }
060}