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 2014-2015 ForgeRock AS. 015 */ 016 017package org.forgerock.http; 018 019import org.forgerock.services.context.Context; 020import org.forgerock.services.context.RootContext; 021import org.forgerock.http.protocol.Request; 022import org.forgerock.http.protocol.Response; 023import org.forgerock.util.promise.NeverThrowsException; 024import org.forgerock.util.promise.Promise; 025 026/** 027 * An HTTP client which forwards requests to a wrapped {@link Handler}. 028 */ 029public final class Client { 030 private final Context defaultContext; 031 private final Handler handler; 032 033 /** 034 * Creates a new {@code Client} which will route HTTP requests to the 035 * provided {@link Handler} using a {@link RootContext} allocated during 036 * construction when none is provided. 037 * 038 * @param handler 039 * The HTTP handler. 040 */ 041 public Client(final Handler handler) { 042 this(handler, new RootContext()); 043 } 044 045 /** 046 * Creates a new {@code Client} which will route HTTP requests to the 047 * provided {@link Handler} using the specified {@link Context} if none is provided. 048 * 049 * @param handler 050 * The HTTP handler. 051 * @param defaultContext 052 * The context to pass in with HTTP request when none is provided. 053 */ 054 public Client(final Handler handler, final Context defaultContext) { 055 this.handler = handler; 056 this.defaultContext = defaultContext; 057 } 058 059 /** 060 * Sends an HTTP request and returns a {@code Promise} representing the 061 * pending HTTP response. 062 * 063 * @param request 064 * The HTTP request to send. 065 * @return A promise representing the pending HTTP response. 066 */ 067 public Promise<Response, NeverThrowsException> send(final Request request) { 068 return handler.handle(defaultContext, request); 069 } 070 071 /** 072 * Sends an HTTP request and returns a {@code Promise} representing the 073 * pending HTTP response. 074 * 075 * @param context 076 * The associated processing context. 077 * @param request 078 * The HTTP request to send. 079 * @return A promise representing the pending HTTP response. 080 */ 081 public Promise<Response, NeverThrowsException> send(final Context context, final Request request) { 082 return handler.handle(context, request); 083 } 084}