invalidate.xsp
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsp:page
language="java"
xmlns:xsp="http://apache.org/xsp"
>
<xsp:structure>
<xsp:include>org.apache.cocoon.components.language.markup.xsp.XSPUtil</xsp:include>
<xsp:include>org.apache.avalon.framework.context.ContextException</xsp:include>
<xsp:include>org.apache.avalon.framework.parameters.Parameters</xsp:include>
<xsp:include>org.apache.avalon.framework.parameters.ParameterException</xsp:include>
<xsp:include>org.apache.cocoon.xml.XMLConsumer</xsp:include>
<xsp:include>org.apache.cocoon.components.cache.Server</xsp:include>
<xsp:include>org.apache.cocoon.components.cache.ESIKey</xsp:include>
<xsp:include>org.apache.log.Logger</xsp:include>
<xsp:include>java.io.BufferedReader</xsp:include>
<xsp:include>java.io.InputStreamReader</xsp:include>
<xsp:include>java.text.DateFormat</xsp:include>
<xsp:include>java.text.ParseException</xsp:include>
<xsp:include>java.lang.IllegalArgumentException</xsp:include>
<xsp:include>java.util.StringTokenizer</xsp:include>
<xsp:include>java.util.Enumeration</xsp:include>
<xsp:include>java.util.Hashtable</xsp:include>
<xsp:include>java.util.HashMap</xsp:include>
<xsp:include>java.util.Locale</xsp:include>
<xsp:include>java.util.Vector</xsp:include>
<xsp:include>java.security.Principal</xsp:include>
<xsp:include>javax.servlet.http.HttpServletRequest</xsp:include>
<xsp:include>javax.servlet.http.Cookie</xsp:include>
<xsp:include>javax.servlet.http.HttpSession</xsp:include>
<xsp:include>javax.servlet.ServletInputStream</xsp:include>
<xsp:include>javax.servlet.RequestDispatcher</xsp:include>
<xsp:include>org.apache.cocoon.environment.http.HttpEnvironment</xsp:include>
<xsp:include>org.w3c.dom.Document</xsp:include>
<xsp:include>org.xml.sax.Attributes</xsp:include>
<xsp:include>org.apache.cocoon.xml.AbstractXMLPipe</xsp:include>
<xsp:include>sun.misc.BASE64Decoder</xsp:include>
</xsp:structure>
<xsp:logic><![CDATA[
// Class attributes
private static String EXTRACT_URI="http://www.dbprism.com.ar/dbprism/invalidation";
private static String EXTRACT_ELEMENT_INVALIDATION="INVALIDATION";
private static String EXTRACT_ELEMENT_OBJECT="OBJECT";
private static String EXTRACT_ELEMENT_BASIC="BASICSELECTOR";
private static String EXTRACT_ELEMENT_ADVANCED="ADVANCEDSELECTOR";
private static String EXTRACT_ELEMENT_INFO="INFO";
private static String EXTRACT_ELEMENT_ACTION="ACTION";
private static String EXTRACT_ELEMENT_COOKIE="COOKIE";
private static String EXTRACT_ELEMENT_HEADER="HEADER";
private static String EXTRACT_ELEMENT_OTHER="OTHER";
Server cacheServer = null;
String name = "";
String passwd = "";
] ]></xsp:logic>
<xsp:logic><![CDATA[
// Inher class, private request
// Note that it only implement getter/setter class for parameters and cookies
// other methots returns null, be carefull with this.
public class MyHtppRequest implements HttpServletRequest
{
HttpServletRequest request;
Hashtable parameters;
Hashtable headers;
Vector cookies;
MyHtppRequest(HttpServletRequest request) {
this.cookies = new Vector();
this.request = request;
this.parameters = new Hashtable();
this.headers = new Hashtable();
}
public String getAuthType(){ return request.getAuthType(); }
public Cookie[] getCookies() {
if (cookies.isEmpty())
return null;
else
return (Cookie[])cookies.toArray(new Cookie[cookies.size()]);
}
public void setCookies(Cookie cookie) {
cookies.add(cookie);
}
public long getDateHeader(String s) throws IllegalArgumentException
{
DateFormat df = DateFormat.getDateInstance();
long dt;
try {
dt = df.parse((String)this.headers.get(s)).getTime();
} catch (ParseException pe) {
throw new IllegalArgumentException(pe.getMessage());
}
return dt;
}
public String getHeader(String s)
{
return (String)this.headers.get(s);
}
public Enumeration getHeaders(String s)
{
StringTokenizer st = new StringTokenizer((String)this.headers.get(s));
return st;
}
public Enumeration getHeaderNames()
{
return this.headers.keys();
}
public int getIntHeader(String s)
{
return Integer.parseInt((String)this.headers.get(s));
}
public void setHeader(String name, String value)
{
this.headers.put(name,value);
}
public String getMethod(){ return request.getMethod(); }
public String getPathInfo(){ return request.getPathInfo(); }
public String getPathTranslated(){ return request.getPathTranslated(); }
public String getContextPath(){ return request.getContextPath(); }
public String getQueryString(){ return request.getQueryString(); }
public String getRemoteUser(){ return request.getRemoteUser(); }
public boolean isUserInRole(String s){ return request.isUserInRole(s); }
public Principal getUserPrincipal(){ return request.getUserPrincipal(); }
public String getRequestedSessionId(){ return request.getRequestedSessionId(); }
public String getRequestURI(){ return request.getRequestURI(); }
public String getServletPath(){ return this.getServletPath(); }
public HttpSession getSession(boolean flag){ return request.getSession(flag); }
public HttpSession getSession(){ return request.getSession(); }
public boolean isRequestedSessionIdValid(){ return request.isRequestedSessionIdValid(); }
public boolean isRequestedSessionIdFromCookie(){ return request.isRequestedSessionIdFromCookie(); }
public boolean isRequestedSessionIdFromURL(){ return request.isRequestedSessionIdFromURL(); }
public boolean isRequestedSessionIdFromUrl(){ return request.isRequestedSessionIdFromUrl(); }
public Object getAttribute(String s){ return request.getAttribute(s); }
public Enumeration getAttributeNames(){ return request.getAttributeNames(); }
public String getCharacterEncoding(){ return request.getCharacterEncoding(); }
public int getContentLength(){ return request.getContentLength(); }
public String getContentType(){ return request.getContentType(); }
public ServletInputStream getInputStream() throws IOException{ return request.getInputStream(); }
public String getParameter(String s)
{
return (String)this.parameters.get(s);
}
public Enumeration getParameterNames()
{
return this.parameters.keys();
}
public String[] getParameterValues(String s) {
String [] val = new String[1];
val[0] = (String)parameters.get(s);
return val;
}
public void setParameter(String name, String value)
{
this.parameters.put(name,value);
}
public String getProtocol(){ return request.getProtocol(); }
public String getScheme(){ return request.getScheme(); }
public String getServerName(){ return request.getServerName(); }
public int getServerPort(){ return request.getServerPort(); }
public BufferedReader getReader()
throws IOException{ return request.getReader(); }
public String getRemoteAddr(){ return request.getRemoteAddr(); }
public String getRemoteHost(){ return request.getRemoteHost(); }
public void setAttribute(String s, Object obj){ request.setAttribute(s,obj); }
public void removeAttribute(String s){ request.removeAttribute(s); }
public Locale getLocale(){ return request.getLocale(); }
public Enumeration getLocales(){ return request.getLocales(); }
public boolean isSecure(){ return request.isSecure(); }
public RequestDispatcher getRequestDispatcher(String s){ return request.getRequestDispatcher(s); }
public String getRealPath(String s){ return request.getRealPath(s); }
public java.lang.StringBuffer getRequestURL() { return null; }
public int getLocalPort() { return request.getLocalPort(); }
public int getRemotePort() { return request.getRemotePort(); }
public String getLocalAddr() { return request.getLocalAddr(); }
public String getLocalName() { return request.getLocalName(); }
public java.util.Map getParameterMap()
{
return null;
}
public void setCharacterEncoding(java.lang.String $1) { }
} // end inher class
] ]></xsp:logic>
<xsp:logic><![CDATA[
// Inher class
public class InvalidateExtractor extends AbstractXMLPipe
{
HttpServletRequest request;
private int state = 0;
int count = 0;
private String url;
private String urlRegExp;
private String info;
private int ttl;
private boolean advanced = false;
private boolean defineParameters = false;
private boolean defineHttpParameters = false;
Parameters par;
Cookie cookie;
Logger log;
MyHtppRequest httpParameter;
AttributesImpl xspAttr;
InvalidateExtractor(XMLConsumer consumer, HttpServletRequest request, Logger logger) {
this.xspAttr = new AttributesImpl();
this.setConsumer(consumer);
this.url = "";
this.urlRegExp = "";
this.info = "";
this.ttl = 0;
this.advanced = false;
this.defineParameters = false;
this.defineHttpParameters = false;
this.par = new Parameters();
this.request = request;
this.httpParameter = new MyHtppRequest(this.request);
this.cookie = null;
this.log = logger;
this.xspAttr.clear();
}
/**
* Receive notification of the beginning of an element.
*
* @param uri The Namespace URI, or the empty string if the element has no
* Namespace URI or if Namespace
* processing is not being performed.
* @param loc The local name (without prefix), or the empty string if
* Namespace processing is not being performed.
* @param raw The raw XML 1.0 name (with prefix), or the empty string if
* raw names are not available.
* @param a The attributes attached to the element. If there are no
* attributes, it shall be an empty Attributes object.
*/
public void startElement(String uri, String loc, String raw,
Attributes a) throws SAXException {
//System.out.println(" Start uri="+uri+" loc="+loc+" raw="+raw+" state="+state);
if (state == 0 && EXTRACT_ELEMENT_INVALIDATION.equals(loc))
state = 1;
else if (state == 1 && EXTRACT_ELEMENT_OBJECT.equals(loc))
state = 2;
else if (state == 2 && EXTRACT_ELEMENT_INFO.equals(loc)) {
info = a.getValue("VALUE");
state = 3;
} else if (state == 2 && EXTRACT_ELEMENT_BASIC.equals(loc)) {
url = a.getValue("URI");
advanced = false;
state = 3;
//System.out.println("basic URI="+url);
} else if (state == 2 && EXTRACT_ELEMENT_ADVANCED.equals(loc)) {
url = a.getValue("URIPREFIX");
urlRegExp = a.getValue("URIEXP");
advanced = true;
state = 3;
//System.out.println("avanced URIPREFIX="+url+" URIEXP="+urlRegExp);
} else if (state == 2 && EXTRACT_ELEMENT_ACTION.equals(loc)) {
try {
ttl = Integer.parseInt(a.getValue("REMOVALTTL"));
} catch (Exception e) {
ttl = 0; // invalid number, use 0 as default
}
state = 3;
//System.out.println("ttl="+ttl);
} else if (state == 3 && EXTRACT_ELEMENT_OTHER.equals(loc)) {
String mode = a.getValue("TYPE");
String name = a.getValue("NAME");
String value = a.getValue("VALUE");
if ("sitemap".equalsIgnoreCase(mode))
par.setParameter(name,value);
else {
httpParameter.setParameter(name,value);
if (!this.defineHttpParameters)
par.setParameter(ESIKey.ESI_ARG_NAME,ESIKey.ESI_PATTERN_ALL);
this.defineHttpParameters = true;
}
this.defineParameters = true;
//System.out.println(mode+" argument name="+name+" value="+value);
} else if (state == 3 && EXTRACT_ELEMENT_HEADER.equals(loc)) {
String name = a.getValue("NAME");
String value = a.getValue("VALUE");
par.setParameter(name,value);
if (!this.defineHttpParameters)
par.setParameter(ESIKey.ESI_ARG_NAME,ESIKey.ESI_PATTERN_ALL);
this.defineHttpParameters = true;
this.defineParameters = true;
//System.out.println("header argument name="+name+" value="+value);
} else if (state == 3 && EXTRACT_ELEMENT_COOKIE.equals(loc)) {
String name = a.getValue("NAME");
String value = a.getValue("VALUE");
cookie = new Cookie(name,value);
httpParameter.setCookies(cookie);
if (!this.defineHttpParameters)
par.setParameter(ESIKey.ESI_ARG_NAME,ESIKey.ESI_PATTERN_ALL);
this.defineHttpParameters = true;
this.defineParameters = true;
//System.out.println("cookie name="+name+" value="+value);
}
}
/**
* Receive notification of the end of an element.
*
* @param uri The Namespace URI, or the empty string if the element has no
* Namespace URI or if Namespace
* processing is not being performed.
* @param loc The local name (without prefix), or the empty string if
* Namespace processing is not being performed.
* @param raw The raw XML 1.0 name (with prefix), or the empty string if
* raw names are not available.
*/
public void endElement(String uri, String loc, String raw)
throws SAXException {
//System.out.println("End uri="+uri+" loc="+loc+" raw="+raw+" state="+state);
if (state==3 && EXTRACT_ELEMENT_BASIC.equals(loc))
state=2;
else if (state==3 && EXTRACT_ELEMENT_ADVANCED.equals(loc))
state=2;
else if (state==3 && EXTRACT_ELEMENT_INFO.equals(loc))
state=2;
else if (state==3 && EXTRACT_ELEMENT_ACTION.equals(loc))
state=2;
else if (state==2 && EXTRACT_ELEMENT_OBJECT.equals(loc)) {
this.contentHandler.startElement("", "OBJECTRESULT", "OBJECTRESULT", xspAttr);
if (advanced) {
xspAttr.addAttribute("", "URIPREFIX", "URIPREFIX", "CDATA", url);
xspAttr.addAttribute("", "URIEXP", "URIEXP", "CDATA", urlRegExp);
this.contentHandler.startElement("", "ADVANCEDSELECTOR", "ADVANCEDSELECTOR", xspAttr);
this.contentHandler.endElement("", "ADVANCEDSELECTOR", "ADVANCEDSELECTOR");
} else {
xspAttr.addAttribute("", "URI", "URI", "CDATA", url);
this.contentHandler.startElement("", "BASICSELECTOR", "BASICSELECTOR", xspAttr);
this.contentHandler.endElement("", "BASICSELECTOR", "BASICSELECTOR");
}
this.xspAttr.clear();
xspAttr.addAttribute("", "ID", "ID", "CDATA", ""+count);
try {
count++;
if (!advanced) {
// Transform a basic selector into an advanced selector.
urlRegExp = "^"+url+"$";
url = url.substring(0,url.lastIndexOf('/')+1);
}
ESIKey ekey = null;
if (this.defineParameters)
ekey = new ESIKey(url,par,httpParameter);
//System.out.println("remove uri ='"+url+"' uriRegExp='"+urlRegExp+"' with ttl="+ttl);
int numInv = cacheServer.removeCacheablePage(name,
passwd,
url,
urlRegExp,
ekey,
ttl);
xspAttr.addAttribute("", "STATUS", "STATUS", "CDATA", "SUCCESS");
xspAttr.addAttribute("", "NUMINV", "NUMINV", "CDATA", ""+numInv);
if (this.getLogger().isInfoEnabled())
this.getLogger().info("Invalidation with info '"+this.info+"' has returned with status 'SUCCESS'; number of documents invalidated: "+numInv);
} catch (Exception e) {
xspAttr.addAttribute("", "STATUS", "STATUS", "CDATA", "URI NOT FOUND");
xspAttr.addAttribute("", "NUMINV", "NUMINV", "CDATA", "0");
if (this.getLogger().isErrorEnabled())
this.getLogger().error("Invalidation with info '"+this.info+"' has returned with status 'URI NOT FOUND'; number of documents invalidated: 0",e);
} finally {
this.contentHandler.startElement("", "RESULT", "RESULT", xspAttr);
this.contentHandler.endElement("", "RESULT", "RESULT");
this.contentHandler.endElement("", "OBJECTRESULT", "OBJECTRESULT");
this.url = "";
this.urlRegExp = "";
this.info = "";
this.ttl = 0;
this.advanced = false;
this.par = new Parameters();
this.httpParameter = new MyHtppRequest(this.request);
this.cookie = null;
this.xspAttr.clear();
}
state=1;
} else if (state==1 && EXTRACT_ELEMENT_INVALIDATION.equals(loc))
state=0;
}
}
] ]></xsp:logic>
<INVALIDATIONRESULT VERSION="WCS-1.0"><xsp:logic><![CDATA[
try {
this.cacheServer = (Server) this.manager.lookup(Server.ROLE);
} catch (ComponentException ce) {
//System.out.println("invalidate.xsp: Can't get External Invalidator Server instance");
throw new ProcessingException("invalidate.xsp: Can't get External Invalidator Server instance");
}
HttpServletRequest httpRequest =
(HttpServletRequest)objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT);
if (httpRequest == null) {
throw new ProcessingException("HttpServletRequest object not available");
}
Parser parser = null;
BASE64Decoder B64 = new BASE64Decoder();
int i;
String str;
try {
str = httpRequest.getHeader("Authorization").substring(6);
str = new String(B64.decodeBuffer(str),"iso-8859-1");
} catch (Exception e) {
str = ":";
}
i = str.indexOf(':');
name = str.substring(0,i);
passwd = str.substring(i+1);
BufferedReader ir = new BufferedReader(new InputStreamReader(httpRequest.getInputStream()));
String line;
StringBuffer msg = new StringBuffer();
while((line=ir.readLine())!=null)
msg.append(line+"\n");
//System.out.println("User=>"+name);
//System.out.println("Password=>"+passwd);
//System.out.println("POST Message=>");
//System.out.println(msg.toString());
InvalidateExtractor extractor = new InvalidateExtractor((XMLConsumer)this.contentHandler,httpRequest,getLogger());
try {
parser = (Parser)this.manager.lookup(Parser.ROLE);
if (parser==null)
throw new ProcessingException("invalidate.xsp: Parser is null");
else {
parser.setConsumer(extractor);
parser.parse(new InputSource(new StringReader(msg.toString())));
}
} catch (ComponentException e) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("ComponentException in invalidate.generate()", e);
getLogger().debug("Embedded ComponentException in invalidate.generate()", e);
}
//System.out.println("ComponentException="+e.getMessage());
} catch (SAXException e) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("SAXException in invalidate.generate()", e);
getLogger().debug("Embedded SAXException in invalidate.generate()", e);
}
//System.out.println("SAXException="+e.getMessage());
} catch (IOException e) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("IOException in invalidate.generate()", e);
getLogger().debug("Embedded IOException in invalidate.generate()", e);
}
//System.out.println("IOException="+e.getMessage());
} finally {
// Cleanup local resources
if (parser != null) this.manager.release(parser);
}
] ]></xsp:logic></INVALIDATIONRESULT>
</xsp:page>
This work is licensed under a
Creative Commons License
.


