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.ThreadingBehavior;
32  import org.apache.http.annotation.Contract;
33  import org.apache.http.util.Args;
34  
35  /**
36   * Maintains a map of HTTP request handlers keyed by a request URI pattern.
37   * <br>
38   * Patterns may have three formats:
39   * <ul>
40   *   <li>{@code *}</li>
41   *   <li>{@code *&lt;uri&gt;}</li>
42   *   <li>{@code &lt;uri&gt;*}</li>
43   * </ul>
44   * <br>
45   * This class can be used to map an instance of
46   * {@link HttpRequestHandler} matching a particular request URI. Usually the
47   * mapped request handler will be used to process the request with the
48   * specified request URI.
49   *
50   * @since 4.3
51   */
52  @Contract(threading = ThreadingBehavior.SAFE)
53  public class UriHttpRequestHandlerMapper implements HttpRequestHandlerMapper {
54  
55      private final UriPatternMatcher<HttpRequestHandler> matcher;
56  
57      protected UriHttpRequestHandlerMapper(final UriPatternMatcher<HttpRequestHandler> matcher) {
58          super();
59          this.matcher = Args.notNull(matcher, "Pattern matcher");
60      }
61  
62      public UriHttpRequestHandlerMapper() {
63          this(new UriPatternMatcher<HttpRequestHandler>());
64      }
65  
66      /**
67       * Registers the given {@link HttpRequestHandler} as a handler for URIs
68       * matching the given pattern.
69       *
70       * @param pattern the pattern to register the handler for.
71       * @param handler the handler.
72       */
73      public void register(final String pattern, final HttpRequestHandler handler) {
74          Args.notNull(pattern, "Pattern");
75          Args.notNull(handler, "Handler");
76          matcher.register(pattern, handler);
77      }
78  
79      /**
80       * Removes registered handler, if exists, for the given pattern.
81       *
82       * @param pattern the pattern to unregister the handler for.
83       */
84      public void unregister(final String pattern) {
85          matcher.unregister(pattern);
86      }
87  
88      /**
89       * Extracts request path from the given {@link HttpRequest}
90       */
91      protected String getRequestPath(final HttpRequest request) {
92          String uriPath = request.getRequestLine().getUri();
93          int index = uriPath.indexOf("?");
94          if (index != -1) {
95              uriPath = uriPath.substring(0, index);
96          } else {
97              index = uriPath.indexOf("#");
98              if (index != -1) {
99                  uriPath = uriPath.substring(0, index);
100             }
101         }
102         return uriPath;
103     }
104 
105     /**
106      * Looks up a handler matching the given request URI.
107      *
108      * @param request the request
109      * @return handler or {@code null} if no match is found.
110      */
111     @Override
112     public HttpRequestHandler lookup(final HttpRequest request) {
113         Args.notNull(request, "HTTP request");
114         return matcher.lookup(getRequestPath(request));
115     }
116 
117 }