2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.http.pathmap;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
27 import org.eclipse.jetty.util.annotation.ManagedAttribute;
28 import org.eclipse.jetty.util.annotation.ManagedObject;
29 import org.eclipse.jetty.util.component.ContainerLifeCycle;
30 import org.eclipse.jetty.util.component.Dumpable;
31 import org.eclipse.jetty.util.log.Log;
32 import org.eclipse.jetty.util.log.Logger;
35 * Path Mappings of PathSpec to Resource.
37 * Sorted into search order upon entry into the Set
39 * @param <E> the type of mapping endpoint
41 @ManagedObject("Path Mappings")
42 public class PathMappings<E> implements Iterable<MappedResource<E>>, Dumpable
44 private static final Logger LOG = Log.getLogger(PathMappings.class);
45 private List<MappedResource<E>> mappings = new ArrayList<MappedResource<E>>();
46 private MappedResource<E> defaultResource = null;
47 private MappedResource<E> rootResource = null;
52 return ContainerLifeCycle.dump(this);
56 public void dump(Appendable out, String indent) throws IOException
58 ContainerLifeCycle.dump(out,indent,mappings);
61 @ManagedAttribute(value = "mappings", readonly = true)
62 public List<MappedResource<E>> getMappings()
73 * Return a list of MappedResource matches for the specified path.
75 * @param path the path to return matches on
76 * @return the list of mapped resource the path matches on
78 public List<MappedResource<E>> getMatches(String path)
80 boolean matchRoot = "/".equals(path);
82 List<MappedResource<E>> ret = new ArrayList<>();
83 int len = mappings.size();
84 for (int i = 0; i < len; i++)
86 MappedResource<E> mr = mappings.get(i);
88 switch (mr.getPathSpec().group)
95 if (matchRoot || mr.getPathSpec().matches(path))
99 if (mr.getPathSpec().matches(path))
107 public MappedResource<E> getMatch(String path)
109 if (path.equals("/") && rootResource != null)
114 int len = mappings.size();
115 for (int i = 0; i < len; i++)
117 MappedResource<E> mr = mappings.get(i);
118 if (mr.getPathSpec().matches(path))
123 return defaultResource;
127 public Iterator<MappedResource<E>> iterator()
129 return mappings.iterator();
132 @SuppressWarnings("incomplete-switch")
133 public void put(PathSpec pathSpec, E resource)
135 MappedResource<E> entry = new MappedResource<>(pathSpec,resource);
136 switch (pathSpec.group)
139 defaultResource = entry;
142 rootResource = entry;
146 // TODO: add warning when replacing an existing pathspec?
149 if (LOG.isDebugEnabled())
150 LOG.debug("Added {} to {}",entry,this);
151 Collections.sort(mappings);
155 public String toString()
157 return String.format("%s[size=%d]",this.getClass().getSimpleName(),mappings.size());