001/* 002 * Copyright (c) 2006-2013 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 */ 012package org.nuxeo.ecm.webengine.jaxrs.servlet.config; 013 014import java.util.ArrayList; 015import java.util.List; 016 017import org.nuxeo.common.xmap.annotation.XNode; 018import org.nuxeo.common.xmap.annotation.XNodeList; 019import org.nuxeo.common.xmap.annotation.XObject; 020import org.nuxeo.ecm.webengine.jaxrs.servlet.FilterSet; 021import org.nuxeo.ecm.webengine.jaxrs.servlet.mapping.Path; 022import org.nuxeo.ecm.webengine.jaxrs.servlet.mapping.PathMatcher; 023 024/** 025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 026 */ 027@XObject("filters") 028public class FilterSetDescriptor { 029 030 /** 031 * To be used only when a filter set is declared outside a servlet. This is the ID of the contributed filter set. 032 */ 033 @XNode("@id") 034 protected String id; 035 036 /** 037 * To be used only when a filter set is declared outside a servlet. This is the target servlet name where the filter 038 * should be added. 039 */ 040 @XNode("@target") 041 protected String targetServlet; 042 043 @XNodeList(value = "filter", type = ArrayList.class, componentType = FilterDescriptor.class, nullByDefault = false) 044 protected ArrayList<FilterDescriptor> filters; 045 046 @XNode("@pathInfo") 047 public void setPathInfo(String pathInfo) { 048 path = PathMatcher.compile(pathInfo); 049 } 050 051 private PathMatcher path; 052 053 public PathMatcher getPath() { 054 return path; 055 } 056 057 public List<FilterDescriptor> getFilters() { 058 return filters; 059 } 060 061 public FilterSet getFilterSet() { 062 return new FilterSet(this); 063 } 064 065 public boolean matches(String pathInfo) { 066 if (path == null) { 067 return true; 068 } 069 if (pathInfo == null || pathInfo.length() == 0) { 070 pathInfo = "/"; 071 } 072 return path.matches(pathInfo); 073 } 074 075 public boolean matches(Path pathInfo) { 076 if (path == null) { 077 return true; 078 } 079 return path.matches(pathInfo); 080 } 081 082 public String getId() { 083 return id; 084 } 085 086 public String getTargetServlet() { 087 return targetServlet; 088 } 089 090 @Override 091 public String toString() { 092 StringBuilder buf = new StringBuilder(); 093 if (id != null) { 094 buf.append(id).append("@").append(targetServlet).append(": "); 095 } 096 String p = path == null ? "/**" : path.toString(); 097 buf.append(p).append(" ").append(filters); 098 return buf.toString(); 099 } 100 101 @Override 102 public boolean equals(Object obj) { 103 if (obj == this) { 104 return true; 105 } 106 if (obj instanceof FilterSetDescriptor) { 107 String objId = ((FilterSetDescriptor) obj).id; 108 if (objId == null && this.id == null) { 109 return super.equals(obj); 110 } 111 if (objId != null) { 112 return objId.equals(this.id); 113 } 114 } 115 return false; 116 } 117}