Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.http;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 /* ------------------------------------------------------------ */
URI path map to Object. This mapping implements the path specification recommended in the 2.2 Servlet API. Path specifications can be of the following forms:
 /foo/bar           - an exact path specification.
 /foo/*             - a prefix path specification (must end '/*').
 *.ext              - a suffix path specification.
 /                  - the default path specification.
 
Matching is performed in the following order <NL>
  • Exact match.
  • Longest prefix match.
  • Longest suffix match.
  • default. </NL> Multiple path specifications can be mapped by providing a list of specifications. By default this class uses characters ":," as path separators, unless configured differently by calling the static method

    See also:
    PathMap.setPathSpecSeparators(String)

    Special characters within paths such as '?� and ';' are not treated specially as it is assumed they would have been either encoded in the original URL or stripped from the path.

    This class is not synchronized. If concurrent modifications are possible then it should be synchronized at a higher level.

  •  
     public class PathMap extends HashMap implements Externalizable
     {
         /* ------------------------------------------------------------ */
         private static String __pathSpecSeparators = ":,";
     
         /* ------------------------------------------------------------ */
        
    Set the path spec separator. Multiple path specification may be included in a single string if they are separated by the characters set in this string. By default this class uses ":," characters as path separators.

    Parameters:
    s separators
     
         public static void setPathSpecSeparators(String s)
         {
             =s;
         }
     
         /* --------------------------------------------------------------- */
         final StringMap _prefixMap=new StringMap();
         final StringMap _suffixMap=new StringMap();
         final StringMap _exactMap=new StringMap();
     
         List _defaultSingletonList=null;
         Entry _prefixDefault=null;
         Entry _default=null;
         final Set _entrySet;
         boolean _nodefault=false;
     
         /* --------------------------------------------------------------- */
        
    Construct empty PathMap.
     
         public PathMap()
         {
             super(11);
             =entrySet();
         }
        /* --------------------------------------------------------------- */
        
    Construct empty PathMap.
        public PathMap(boolean nodefault)
        {
            super(11);
            =entrySet();
            =nodefault;
        }
        /* --------------------------------------------------------------- */
        
    Construct empty PathMap.
        public PathMap(int capacity)
        {
            super (capacity);
            =entrySet();
        }
        /* --------------------------------------------------------------- */
        
    Construct from dictionary PathMap.
        public PathMap(Map m)
        {
            putAll(m);
            =entrySet();
        }
        /* ------------------------------------------------------------ */
        public void writeExternal(java.io.ObjectOutput out)
            throws java.io.IOException
        {
            HashMap map = new HashMap(this);
            out.writeObject(map);
        }
        /* ------------------------------------------------------------ */
        public void readExternal(java.io.ObjectInput in)
            throws java.io.IOExceptionClassNotFoundException
        {
            HashMap map = (HashMap)in.readObject();
            this.putAll(map);
        }
        /* --------------------------------------------------------------- */
        
    Add a single path match to the PathMap.

    Parameters:
    pathSpec The path specification, or comma separated list of path specifications.
    object The object the path maps to
        @Override
        public Object put(Object pathSpecObject object)
        {
            String str = pathSpec.toString();
            if ("".equals(str.trim()))
            {          
                Entry entry = new Entry("",object);
                entry.setMapped("");
                .put(""entry);
                return super.put(""object);
            }
            
            StringTokenizer tok = new StringTokenizer(str,);
            Object old =null;
            while (tok.hasMoreTokens())
            {
                String spec=tok.nextToken();
                if (!spec.startsWith("/") && !spec.startsWith("*."))
                    throw new IllegalArgumentException("PathSpec "+spec+". must start with '/' or '*.'");
                old = super.put(spec,object);
                // Make entry that was just created.
                Entry entry = new Entry(spec,object);
                if (entry.getKey().equals(spec))
                {
                    if (spec.equals("/*"))
                        =entry;
                    else if (spec.endsWith("/*"))
                    {
                        String mapped=spec.substring(0,spec.length()-2);
                        entry.setMapped(mapped);
                        .put(mapped,entry);
                        .put(mapped,entry);
                        .put(spec.substring(0,spec.length()-1),entry);
                    }
                    else if (spec.startsWith("*."))
                        .put(spec.substring(2),entry);
                    else if (spec.equals(.))
                    {
                        if ()
                            .put(spec,entry);
                        else
                        {
                            =entry;
                            =
                                Collections.singletonList();
                        }
                    }
                    else
                    {
                        entry.setMapped(spec);
                        .put(spec,entry);
                    }
                }
            }
            return old;
        }
        /* ------------------------------------------------------------ */
        
    Get object matched by the path.

    Parameters:
    path the path.
    Returns:
    Best matched object or null.
        public Object match(String path)
        {
            Map.Entry entry = getMatch(path);
            if (entry!=null)
                return entry.getValue();
            return null;
        }
        /* --------------------------------------------------------------- */
        
    Get the entry mapped by the best specification.

    Parameters:
    path the path.
    Returns:
    Map.Entry of the best matched or null.
        public Entry getMatch(String path)
        {
            Map.Entry entry=null;
            if (path==null)
                return null;
            int l=path.length();
            
            //special case
            if (l == 1 && path.charAt(0)=='/')
            {
                entry = (Map.Entry).get("");
                if (entry != null)
                    return (Entry)entry;
            }
            
            // try exact match
            entry=.getEntry(path,0,l);
            if (entry!=null)
                return (Entryentry.getValue();
            // prefix search
            int i=l;
            while((i=path.lastIndexOf('/',i-1))>=0)
            {
                entry=.getEntry(path,0,i);
                if (entry!=null)
                    return (Entryentry.getValue();
            }
            // Prefix Default
            if (!=null)
                return ;
            // Extension search
            i=0;
            while ((i=path.indexOf('.',i+1))>0)
            {
                entry=.getEntry(path,i+1,l-i-1);
                if (entry!=null)
                    return (Entryentry.getValue();
            }
            // Default
            return ;
        }
        /* --------------------------------------------------------------- */
        
    Get all entries matched by the path. Best match first.

    Parameters:
    path Path to match
    Returns:
    LazyList of Map.Entry instances key=pathSpec
        public Object getLazyMatches(String path)
        {
            Map.Entry entry;
            Object entries=null;
            if (path==null)
                return LazyList.getList(entries);
            int l=path.length();
            // try exact match
            entry=.getEntry(path,0,l);
            if (entry!=null)
                entries=LazyList.add(entries,entry.getValue());
            // prefix search
            int i=l-1;
            while((i=path.lastIndexOf('/',i-1))>=0)
            {
                entry=.getEntry(path,0,i);
                if (entry!=null)
                    entries=LazyList.add(entries,entry.getValue());
            }
            // Prefix Default
            if (!=null)
                entries=LazyList.add(entries,);
            // Extension search
            i=0;
            while ((i=path.indexOf('.',i+1))>0)
            {
                entry=.getEntry(path,i+1,l-i-1);
                if (entry!=null)
                    entries=LazyList.add(entries,entry.getValue());
            }
            // Default
            if (!=null)
            {
                // Optimization for just the default
                if (entries==null)
                    return ;
                entries=LazyList.add(entries,);
            }
            return entries;
        }
        /* --------------------------------------------------------------- */
        
    Get all entries matched by the path. Best match first.

    Parameters:
    path Path to match
    Returns:
    List of Map.Entry instances key=pathSpec
        public List getMatches(String path)
        {
            return LazyList.getList(getLazyMatches(path));
        }
        /* --------------------------------------------------------------- */
        
    Return whether the path matches any entries in the PathMap, excluding the default entry

    Parameters:
    path Path to match
    Returns:
    Whether the PathMap contains any entries that match this
        public boolean containsMatch(String path)
        {
        	Entry match = getMatch(path);
        	return match!=null && !match.equals();
        }
        /* --------------------------------------------------------------- */
        @Override
        public Object remove(Object pathSpec)
        {
            if (pathSpec!=null)
            {
                String spec=(StringpathSpec;
                if (spec.equals("/*"))
                    =null;
                else if (spec.endsWith("/*"))
                {
                    .remove(spec.substring(0,spec.length()-2));
                    .remove(spec.substring(0,spec.length()-1));
                    .remove(spec.substring(0,spec.length()-2));
                }
                else if (spec.startsWith("*."))
                    .remove(spec.substring(2));
                else if (spec.equals(.))
                {
                    =null;
                    =null;
                }
                else
                    .remove(spec);
            }
            return super.remove(pathSpec);
        }
        /* --------------------------------------------------------------- */
        @Override
        public void clear()
        {
            .clear();
            .clear();
            .clear();
            =null;
            =null;
            super.clear();
        }
        /* --------------------------------------------------------------- */
        

    Returns:
    true if match.
        public static boolean match(String pathSpecString path)
            throws IllegalArgumentException
        {
            return match(pathSpecpathfalse);
        }
        /* --------------------------------------------------------------- */
        

    Returns:
    true if match.
        public static boolean match(String pathSpecString pathboolean noDefault)
        {
            char c = pathSpec.charAt(0);
            if (c=='/')
            {
                if (!noDefault && pathSpec.length()==1 || pathSpec.equals(path))
                    return true;
                if(isPathWildcardMatch(pathSpecpath))
                    return true;
            }
            else if (c=='*')
                return path.regionMatches(path.length()-pathSpec.length()+1,
                                          pathSpec,1,pathSpec.length()-1);
            return false;
        }
        /* --------------------------------------------------------------- */
        private static boolean isPathWildcardMatch(String pathSpecString path)
        {
            // For a spec of "/foo/*" match "/foo" , "/foo/..." but not "/foobar"
            int cpl=pathSpec.length()-2;
            if (pathSpec.endsWith("/*") && path.regionMatches(0,pathSpec,0,cpl))
            {
                if (path.length()==cpl || '/'==path.charAt(cpl))
                    return true;
            }
            return false;
        }
        /* --------------------------------------------------------------- */
        
    Return the portion of a path that matches a path spec.

    Returns:
    null if no match at all.
        public static String pathMatch(String pathSpecString path)
        {
            char c = pathSpec.charAt(0);
            if (c=='/')
            {
                if (pathSpec.length()==1)
                    return path;
                if (pathSpec.equals(path))
                    return path;
                if (isPathWildcardMatch(pathSpecpath))
                    return path.substring(0,pathSpec.length()-2);
            }
            else if (c=='*')
            {
                if (path.regionMatches(path.length()-(pathSpec.length()-1),
                                       pathSpec,1,pathSpec.length()-1))
                    return path;
            }
            return null;
        }
        /* --------------------------------------------------------------- */
        
    Return the portion of a path that is after a path spec.

    Returns:
    The path info string
        public static String pathInfo(String pathSpecString path)
        {
            if ("".equals(pathSpec))
                return path//servlet 3 spec sec 12.2 will be '/'
            
            char c = pathSpec.charAt(0);
            if (c=='/')
            {
                if (pathSpec.length()==1)
                    return null;
                boolean wildcard = isPathWildcardMatch(pathSpecpath);
                // handle the case where pathSpec uses a wildcard and path info is "/*"
                if (pathSpec.equals(path) && !wildcard)
                    return null;
                if (wildcard)
                {
                    if (path.length()==pathSpec.length()-2)
                        return null;
                    return path.substring(pathSpec.length()-2);
                }
            }
            return null;
        }
        /* ------------------------------------------------------------ */
        
    Relative path.

    Parameters:
    base The base the path is relative to.
    pathSpec The spec of the path segment to ignore.
    path the additional path
    Returns:
    base plus path with pathspec removed
        public static String relativePath(String base,
                                          String pathSpec,
                                          String path )
        {
            String info=pathInfo(pathSpec,path);
            if (info==null)
                info=path;
            ifinfo.startsWith"./"))
                info = info.substring( 2);
            ifbase.endsWith.))
                ifinfo.startsWith.))
                    path = base + info.substring(1);
                else
                    path = base + info;
            else
                ifinfo.startsWith.))
                    path = base + info;
                else
                    path = base + . + info;
            return path;
        }
        /* ------------------------------------------------------------ */
        /* ------------------------------------------------------------ */
        /* ------------------------------------------------------------ */
        public static class Entry implements Map.Entry
        {
            private final Object key;
            private final Object value;
            private String mapped;
            private transient String string;
            Entry(Object keyObject value)
            {
                this.=key;
                this.=value;
            }
            public Object getKey()
            {
                return ;
            }
            public Object getValue()
            {
                return ;
            }
            public Object setValue(Object o)
            {
                throw new UnsupportedOperationException();
            }
            @Override
            public String toString()
            {
                if (==null)
                    =+"="+;
                return ;
            }
            public String getMapped()
            {
                return ;
            }
            void setMapped(String mapped)
            {
                this. = mapped;
            }
        }
    New to GrepCode? Check out our FAQ X