View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions Copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2009 Sun Microsystems Inc.
15   * Portions Copyright 2010–2011 ApexIdentity Inc.
16   * Portions Copyright 2011-2016 ForgeRock AS.
17   */
18  
19  package org.forgerock.http.apache.sync;
20  
21  import static org.forgerock.http.io.IO.newBranchingInputStream;
22  import static org.forgerock.util.promise.Promises.newResultPromise;
23  
24  import java.io.IOException;
25  
26  import org.apache.http.HttpResponse;
27  import org.apache.http.client.methods.HttpUriRequest;
28  import org.apache.http.impl.client.CloseableHttpClient;
29  import org.forgerock.http.apache.AbstractHttpClient;
30  import org.forgerock.http.io.Buffer;
31  import org.forgerock.http.protocol.Request;
32  import org.forgerock.http.protocol.Response;
33  import org.forgerock.http.protocol.Status;
34  import org.forgerock.util.Factory;
35  import org.forgerock.util.promise.NeverThrowsException;
36  import org.forgerock.util.promise.Promise;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * Apache HTTP Client implementation.
42   */
43  final class SyncHttpClient extends AbstractHttpClient {
44  
45      private static final Logger logger = LoggerFactory.getLogger(SyncHttpClient.class);
46  
47      /** The Apache HTTP client to transmit requests through. */
48      private final CloseableHttpClient httpClient;
49      private final Factory<Buffer> storage;
50  
51      SyncHttpClient(final CloseableHttpClient httpClient, final Factory<Buffer> storage) {
52          this.httpClient = httpClient;
53          this.storage = storage;
54      }
55  
56      @Override
57      public void close() throws IOException {
58          httpClient.close();
59      }
60  
61      @Override
62      public Promise<Response, NeverThrowsException> sendAsync(final Request request) {
63          try {
64              // Convert the request to AHC then send it
65              HttpUriRequest clientRequest = createHttpUriRequest(request);
66              HttpResponse clientResponse = httpClient.execute(clientRequest);
67              // Convert the AHC response back into CHF
68              Response response = createResponseWithoutEntity(clientResponse);
69              response.getEntity().setRawContentInputStream(
70                      newBranchingInputStream(clientResponse.getEntity().getContent(), storage));
71              return newResultPromise(response);
72          } catch (final Exception ex) {
73              logger.trace("Failed to obtain response for {}", request.getUri(), ex);
74              Response response = new Response(Status.BAD_GATEWAY);
75              response.setCause(ex);
76              return newResultPromise(response);
77          }
78      }
79  
80  }