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