View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.client;
20  
21  import java.util.LinkedHashMap;
22  import java.util.Map;
23  
24  import org.eclipse.jetty.client.api.Request;
25  import org.eclipse.jetty.client.api.Response;
26  
27  /**
28   * <p>A container for {@link ProtocolHandler}s accessible from {@link HttpClient#getProtocolHandlers()}.</p>
29   */
30  public class ProtocolHandlers
31  {
32      private final Map<String, ProtocolHandler> handlers = new LinkedHashMap<>();
33  
34      protected ProtocolHandlers()
35      {
36      }
37  
38      /**
39       * <p>Stores the given {@code protocolHandler} in this container.</p>
40       * <p>If a protocol handler with the same name exists, it is
41       * replaced by the given one, and the existing returned.</p>
42       *
43       * @param protocolHandler the protocol handler to store
44       * @return the existing protocol handler with the same name,
45       * or null if no protocol handler with that name was already stored
46       * @see #remove(String)
47       */
48      public ProtocolHandler put(ProtocolHandler protocolHandler)
49      {
50          return handlers.put(protocolHandler.getName(), protocolHandler);
51      }
52  
53      /**
54       * <p>Removes the protocol handler with the given name.</p>
55       *
56       * @param name the name of the protocol handler to remove
57       * @return the removed protocol handler, or null if no
58       * protocol handler with that name was already stored
59       * @see #put(ProtocolHandler)
60       * @see #clear()
61       */
62      public ProtocolHandler remove(String name)
63      {
64          return handlers.remove(name);
65      }
66  
67      /**
68       * <p>Removes all protocol handlers from this container.</p>
69       */
70      public void clear()
71      {
72          handlers.clear();
73      }
74  
75      /**
76       * <p>Finds the first protocol handler that
77       * {@link ProtocolHandler#accept(Request, Response) accepts}
78       * the given request and response.</p>
79       *
80       * @param request  the request to accept
81       * @param response the response to accept
82       * @return the protocol handler that accepted the request and response,
83       * or null if none of the protocol handlers accepted the request and response
84       */
85      public ProtocolHandler find(Request request, Response response)
86      {
87          for (ProtocolHandler handler : handlers.values())
88          {
89              if (handler.accept(request, response))
90                  return handler;
91          }
92          return null;
93      }
94  }