001/* 002 * (C) Copyright 2006-2013 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.jaxrs.servlet.config; 020 021import java.util.ArrayList; 022import java.util.List; 023 024import org.nuxeo.common.xmap.annotation.XNode; 025import org.nuxeo.common.xmap.annotation.XNodeList; 026import org.nuxeo.common.xmap.annotation.XObject; 027import org.nuxeo.ecm.webengine.jaxrs.servlet.FilterSet; 028import org.nuxeo.ecm.webengine.jaxrs.servlet.mapping.Path; 029import org.nuxeo.ecm.webengine.jaxrs.servlet.mapping.PathMatcher; 030 031/** 032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 033 */ 034@XObject("filters") 035public class FilterSetDescriptor { 036 037 /** 038 * To be used only when a filter set is declared outside a servlet. This is the ID of the contributed filter set. 039 */ 040 @XNode("@id") 041 protected String id; 042 043 /** 044 * To be used only when a filter set is declared outside a servlet. This is the target servlet name where the filter 045 * should be added. 046 */ 047 @XNode("@target") 048 protected String targetServlet; 049 050 @XNodeList(value = "filter", type = ArrayList.class, componentType = FilterDescriptor.class, nullByDefault = false) 051 protected ArrayList<FilterDescriptor> filters; 052 053 @XNode("@pathInfo") 054 public void setPathInfo(String pathInfo) { 055 path = PathMatcher.compile(pathInfo); 056 } 057 058 private PathMatcher path; 059 060 public PathMatcher getPath() { 061 return path; 062 } 063 064 public List<FilterDescriptor> getFilters() { 065 return filters; 066 } 067 068 public FilterSet getFilterSet() { 069 return new FilterSet(this); 070 } 071 072 public boolean matches(String pathInfo) { 073 if (path == null) { 074 return true; 075 } 076 if (pathInfo == null || pathInfo.length() == 0) { 077 pathInfo = "/"; 078 } 079 return path.matches(pathInfo); 080 } 081 082 public boolean matches(Path pathInfo) { 083 if (path == null) { 084 return true; 085 } 086 return path.matches(pathInfo); 087 } 088 089 public String getId() { 090 return id; 091 } 092 093 public String getTargetServlet() { 094 return targetServlet; 095 } 096 097 @Override 098 public String toString() { 099 StringBuilder buf = new StringBuilder(); 100 if (id != null) { 101 buf.append(id).append("@").append(targetServlet).append(": "); 102 } 103 String p = path == null ? "/**" : path.toString(); 104 buf.append(p).append(" ").append(filters); 105 return buf.toString(); 106 } 107 108 @Override 109 public boolean equals(Object obj) { 110 if (obj == this) { 111 return true; 112 } 113 if (obj instanceof FilterSetDescriptor) { 114 String objId = ((FilterSetDescriptor) obj).id; 115 if (objId == null && this.id == null) { 116 return super.equals(obj); 117 } 118 if (objId != null) { 119 return objId.equals(this.id); 120 } 121 } 122 return false; 123 } 124}