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 2009 Sun Microsystems Inc.
015 * Portions Copyright 2010–2011 ApexIdentity Inc.
016 * Portions Copyright 2011-2015 ForgeRock AS.
017 */
018
019package org.forgerock.http;
020
021import org.forgerock.http.protocol.Request;
022import org.forgerock.http.protocol.Response;
023import org.forgerock.services.context.Context;
024import org.forgerock.util.promise.NeverThrowsException;
025import org.forgerock.util.promise.Promise;
026
027/**
028 * Asynchronously handles an HTTP {@link Request} by producing an associated {@link Response}.
029 */
030public interface Handler {
031
032    /**
033     * Returns a {@link Promise} representing the asynchronous {@link Response} of the given {@code request}.
034     * If any (asynchronous) processing goes wrong, the promise still contains a {@link Response} (probably from the
035     * {@literal 4xx} or {@literal 5xx} status code family).
036     * <p>
037     * A handler that doesn't hand-off the processing to another downstream handler is responsible for
038     * creating the response.
039     * <p>
040     * The returned {@link Promise} contains the response returned from the server as-is.
041     * This is responsibility of the handler to produce the appropriate error response ({@literal 404},
042     * {@literal 500}, ...) in case of processing error.
043     * <p>
044     * <b>Note:</b> As of Promise 2.0 implementation, it is <b>not permitted</b> to throw any runtime exception here.
045     * Doing so produce unexpected behaviour (most likely a server-side hang of the processing thread).
046     *
047     * @param context
048     *            The request context.
049     * @param request
050     *            The request.
051     * @return A {@code Promise} representing the response to be returned to the caller.
052     */
053    Promise<Response, NeverThrowsException> handle(Context context, Request request);
054}