/*
* Filter.java
*
*
* Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
* All rights reserved.
*
* This package is written by Lars Andersen <lars@rimfaxe.com>
* and licensed by Rimfaxe ApS.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by Rimfaxe ApS
(www.rimfaxe.com)"
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
* "Rimfaxe WebServer" must not be used to endorse or promote products
* derived from this software without prior written permission. For written
* permission, please contact info@rimfaxe.com
*
* 5. Products derived from this software may not be called "Rimfaxe"
* nor may "Rimfaxe" appear in their names without prior written
* permission of the Rimfaxe ApS.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
package com.rimfaxe.webserver.webapp;
import java.util.*;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.rimfaxe.util.*;
import com.rimfaxe.webserver.servletapi.RimfaxeFilterConfig;
import com.rimfaxe.webserver.WebContext;
/**
*
* @author Lars Andersen
*/
public class Filter
{
String filtername = "";
String filterclass = "";
ArrayList params = new ArrayList(10);
/** Creates a new instance of Servlet */
public Filter(Node node)
{
traverse(node);
}
public String getKey()
{
return filtername;
}
public String getClassName()
{
return filterclass;
}
public Iterator getParameters()
{
return params.iterator();
}
public javax.servlet.Filter getInstance(WebContext webcontext)
{
javax.servlet.Filter filter = null;
try
{
RimfaxeFilterConfig rfc = new RimfaxeFilterConfig(filtername, webcontext.getServletContext(), this);
filter = (javax.servlet.Filter) Class.forName(getClassName()).newInstance();
filter.init(rfc);
}
catch (ClassNotFoundException cnfe)
{
System.out.println("Filter, ClassNotFound : "+getClassName());
}
catch (Exception e)
{
System.out.println("Filter, Exception -> "+e);
}
return filter;
}
private String traverse(Node node)
{
StringBuffer str = new StringBuffer();
if (node == null)
{
return "";
}
int type = node.getNodeType();
switch (type)
{
case Node.DOCUMENT_NODE:
{
traverse(((Document)node).getDocumentElement());
break;
}
case Node.ELEMENT_NODE:
{
if (node.getNodeName().equalsIgnoreCase("filter-name"))
{
NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i < len; i++)
{
String val = traverse(children.item(i));
filtername = val.trim();
}
}
}
else if (node.getNodeName().equalsIgnoreCase("filter-class"))
{
NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i < len; i++)
{
String val = traverse(children.item(i));
filterclass = val.trim();
}
}
}
else if (node.getNodeName().equalsIgnoreCase("init-param"))
{
InitParameter ip = new InitParameter(node);
params.add(ip);
}
else
{
NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i < len; i++)
{
String val = traverse(children.item(i));
}
}
}
break;
}
case Node.TEXT_NODE:
{
str.append(node.getNodeValue());
break;
}
}
return str.toString();
}
public String toXML()
{
StringBuffer buf = new StringBuffer();
buf.append(" <filter> \n");
buf.append(" <filter-name>"+filtername+"</filter-name> \n");
buf.append(" <filter-class>"+filterclass+"</filter-class> \n");
Object[] objarray = params.toArray();
for (int i=0; i<objarray.length; i++)
{
InitParameter ip = (InitParameter) objarray[i];
buf.append(ip.toXML());
}
buf.append(" </filter>\n");
return ""+buf;
}
}