001/* 002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 */ 019package org.nuxeo.ecm.webengine; 020 021import java.util.Arrays; 022import java.util.Collections; 023import java.util.HashSet; 024import java.util.Set; 025 026import javax.servlet.http.HttpServletRequest; 027 028/** 029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 030 */ 031public class RequestConfiguration { 032 033 /** 034 * Synchronized list of configured paths 035 */ 036 protected Set<PathDescriptor> paths; 037 038 /** 039 * Thread safe cache 040 */ 041 protected volatile PathDescriptor[] cache; 042 043 public RequestConfiguration() { 044 paths = Collections.synchronizedSet(new HashSet<PathDescriptor>()); 045 } 046 047 public void addPathDescriptor(PathDescriptor path) { 048 path.createMatcher(); 049 paths.add(path); 050 cache = null; 051 } 052 053 public void removePathDescriptor(PathDescriptor path) { 054 paths.remove(path); 055 cache = null; 056 } 057 058 public PathDescriptor[] getPaths() { 059 PathDescriptor[] result = cache; 060 if (result == null) { 061 result = paths.toArray(new PathDescriptor[paths.size()]); 062 Arrays.sort(result); 063 cache = result; 064 } 065 return result; 066 } 067 068 public PathDescriptor getMatchingConfiguration(HttpServletRequest req) { 069 String pathInfo = req.getPathInfo(); 070 if (pathInfo == null || pathInfo.length() == 0) { 071 pathInfo = "/"; 072 } 073 for (PathDescriptor pd : getPaths()) { 074 if (pd.match(pathInfo)) { 075 return pd; 076 } 077 } 078 return null; 079 } 080 081}