mobile/android/thirdparty/ch/boye/httpclientandroidlib/protocol/UriPatternMatcher.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/protocol/UriPatternMatcher.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,164 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.protocol;
    1.32 +
    1.33 +import java.util.HashMap;
    1.34 +import java.util.Iterator;
    1.35 +import java.util.Map;
    1.36 +
    1.37 +/**
    1.38 + * Maintains a map of objects keyed by a request URI pattern.
    1.39 + * <br>
    1.40 + * Patterns may have three formats:
    1.41 + * <ul>
    1.42 + *   <li><code>*</code></li>
    1.43 + *   <li><code>*&lt;uri&gt;</code></li>
    1.44 + *   <li><code>&lt;uri&gt;*</code></li>
    1.45 + * </ul>
    1.46 + * <br>
    1.47 + * This class can be used to resolve an object matching a particular request
    1.48 + * URI.
    1.49 + *
    1.50 + * @since 4.0
    1.51 + */
    1.52 +public class UriPatternMatcher {
    1.53 +
    1.54 +    /**
    1.55 +     * TODO: Replace with ConcurrentHashMap
    1.56 +     */
    1.57 +    private final Map map;
    1.58 +
    1.59 +    public UriPatternMatcher() {
    1.60 +        super();
    1.61 +        this.map = new HashMap();
    1.62 +    }
    1.63 +
    1.64 +    /**
    1.65 +     * Registers the given object for URIs matching the given pattern.
    1.66 +     *
    1.67 +     * @param pattern the pattern to register the handler for.
    1.68 +     * @param obj the object.
    1.69 +     */
    1.70 +    public synchronized void register(final String pattern, final Object obj) {
    1.71 +        if (pattern == null) {
    1.72 +            throw new IllegalArgumentException("URI request pattern may not be null");
    1.73 +        }
    1.74 +        this.map.put(pattern, obj);
    1.75 +    }
    1.76 +
    1.77 +    /**
    1.78 +     * Removes registered object, if exists, for the given pattern.
    1.79 +     *
    1.80 +     * @param pattern the pattern to unregister.
    1.81 +     */
    1.82 +    public synchronized void unregister(final String pattern) {
    1.83 +        if (pattern == null) {
    1.84 +            return;
    1.85 +        }
    1.86 +        this.map.remove(pattern);
    1.87 +    }
    1.88 +
    1.89 +    /**
    1.90 +     * @deprecated use {@link #setObjects(Map)}
    1.91 +     */
    1.92 +    public synchronized void setHandlers(final Map map) {
    1.93 +        if (map == null) {
    1.94 +            throw new IllegalArgumentException("Map of handlers may not be null");
    1.95 +        }
    1.96 +        this.map.clear();
    1.97 +        this.map.putAll(map);
    1.98 +    }
    1.99 +
   1.100 +    /**
   1.101 +     * Sets objects from the given map.
   1.102 +     * @param map the map containing objects keyed by their URI patterns.
   1.103 +     */
   1.104 +    public synchronized void setObjects(final Map map) {
   1.105 +        if (map == null) {
   1.106 +            throw new IllegalArgumentException("Map of handlers may not be null");
   1.107 +        }
   1.108 +        this.map.clear();
   1.109 +        this.map.putAll(map);
   1.110 +    }
   1.111 +
   1.112 +    /**
   1.113 +     * Looks up an object matching the given request URI.
   1.114 +     *
   1.115 +     * @param requestURI the request URI
   1.116 +     * @return object or <code>null</code> if no match is found.
   1.117 +     */
   1.118 +    public synchronized Object lookup(String requestURI) {
   1.119 +        if (requestURI == null) {
   1.120 +            throw new IllegalArgumentException("Request URI may not be null");
   1.121 +        }
   1.122 +        //Strip away the query part part if found
   1.123 +        int index = requestURI.indexOf("?");
   1.124 +        if (index != -1) {
   1.125 +            requestURI = requestURI.substring(0, index);
   1.126 +        }
   1.127 +
   1.128 +        // direct match?
   1.129 +        Object obj = this.map.get(requestURI);
   1.130 +        if (obj == null) {
   1.131 +            // pattern match?
   1.132 +            String bestMatch = null;
   1.133 +            for (Iterator it = this.map.keySet().iterator(); it.hasNext();) {
   1.134 +                String pattern = (String) it.next();
   1.135 +                if (matchUriRequestPattern(pattern, requestURI)) {
   1.136 +                    // we have a match. is it any better?
   1.137 +                    if (bestMatch == null
   1.138 +                            || (bestMatch.length() < pattern.length())
   1.139 +                            || (bestMatch.length() == pattern.length() && pattern.endsWith("*"))) {
   1.140 +                        obj = this.map.get(pattern);
   1.141 +                        bestMatch = pattern;
   1.142 +                    }
   1.143 +                }
   1.144 +            }
   1.145 +        }
   1.146 +        return obj;
   1.147 +    }
   1.148 +
   1.149 +    /**
   1.150 +     * Tests if the given request URI matches the given pattern.
   1.151 +     *
   1.152 +     * @param pattern the pattern
   1.153 +     * @param requestUri the request URI
   1.154 +     * @return <code>true</code> if the request URI matches the pattern,
   1.155 +     *   <code>false</code> otherwise.
   1.156 +     */
   1.157 +    protected boolean matchUriRequestPattern(final String pattern, final String requestUri) {
   1.158 +        if (pattern.equals("*")) {
   1.159 +            return true;
   1.160 +        } else {
   1.161 +            return
   1.162 +            (pattern.endsWith("*") && requestUri.startsWith(pattern.substring(0, pattern.length() - 1))) ||
   1.163 +            (pattern.startsWith("*") && requestUri.endsWith(pattern.substring(1, pattern.length())));
   1.164 +        }
   1.165 +    }
   1.166 +
   1.167 +}

mercurial