View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.protocol;
29  
30  import org.apache.http.HttpRequest;
31  import org.apache.http.annotation.ThreadSafe;
32  import org.apache.http.util.Args;
33  
34  /**
35   * Maintains a map of HTTP request handlers keyed by a request URI pattern.
36   * <br>
37   * Patterns may have three formats:
38   * <ul>
39   *   <li><code>*</code></li>
40   *   <li><code>*&lt;uri&gt;</code></li>
41   *   <li><code>&lt;uri&gt;*</code></li>
42   * </ul>
43   * <br>
44   * This class can be used to map an instance of
45   * {@link HttpRequestHandler} matching a particular request URI. Usually the
46   * mapped request handler will be used to process the request with the
47   * specified request URI.
48   *
49   * @since 4.3
50   */
51  @ThreadSafe // provided injected dependencies are thread-safe
52  public class UriHttpRequestHandlerMapper implements HttpRequestHandlerMapper {
53  
54      private final UriPatternMatcher<HttpRequestHandler> matcher;
55  
56      protected UriHttpRequestHandlerMapper(final UriPatternMatcher<HttpRequestHandler> matcher) {
57          super();
58          this.matcher = Args.notNull(matcher, "Pattern matcher");
59      }
60  
61      public UriHttpRequestHandlerMapper() {
62          this(new UriPatternMatcher<HttpRequestHandler>());
63      }
64  
65      /**
66       * Registers the given {@link HttpRequestHandler} as a handler for URIs
67       * matching the given pattern.
68       *
69       * @param pattern the pattern to register the handler for.
70       * @param handler the handler.
71       */
72      public void register(final String pattern, final HttpRequestHandler handler) {
73          Args.notNull(pattern, "Pattern");
74          Args.notNull(handler, "Handler");
75          matcher.register(pattern, handler);
76      }
77  
78      /**
79       * Removes registered handler, if exists, for the given pattern.
80       *
81       * @param pattern the pattern to unregister the handler for.
82       */
83      public void unregister(final String pattern) {
84          matcher.unregister(pattern);
85      }
86  
87      /**
88       * Extracts request path from the given {@link HttpRequest}
89       */
90      protected String getRequestPath(final HttpRequest request) {
91          String uriPath = request.getRequestLine().getUri();
92          int index = uriPath.indexOf("?");
93          if (index != -1) {
94              uriPath = uriPath.substring(0, index);
95          } else {
96              index = uriPath.indexOf("#");
97              if (index != -1) {
98                  uriPath = uriPath.substring(0, index);
99              }
100         }
101         return uriPath;
102     }
103 
104     /**
105      * Looks up a handler matching the given request URI.
106      *
107      * @param request the request
108      * @return handler or <code>null</code> if no match is found.
109      */
110     @Override
111     public HttpRequestHandler lookup(final HttpRequest request) {
112         Args.notNull(request, "HTTP request");
113         return matcher.lookup(getRequestPath(request));
114     }
115 
116 }