Return-Path: Mailing-List: contact cocoon-users-help@xml.apache.org; run by ezmlm Delivered-To: mailing list cocoon-users@xml.apache.org Received: (qmail 65389 invoked from network); 28 Jan 2000 13:20:42 -0000 Received: from unknown (HELO aquarium.politel.com) (195.219.34.1) by 63.211.145.10 with SMTP; 28 Jan 2000 13:20:42 -0000 Message-ID: From: "Eric SCHAEFFER" To: , References: Subject: Re: redirect Date: Fri, 28 Jan 2000 14:20:34 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit You need to write your own processor. Here is the one I made. It searches the pi '' and sends an HTTP redirect to the href parameter. It can be updated to send other HTTP headers... The code is the same than the DCPProcessor one. You also need to stop the Engine processing (better). To do so, I set the document to null, but it would be better to throw an exception ("StopProcessing"). Don't forget to "register" your processor in cocoon.properties. Eric. package org.apache.cocoon.processor.redirect; import java.io.*; import java.util.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.w3c.dom.*; import org.apache.cocoon.framework.*; import org.apache.cocoon.processor.*; /** * A processor that performs HTTP redirects. * * @author Eric SCHAEFFER * @version $Revision: 0.1 $ $Date: 2000/01/14 12:00:00 $ */ public class RedirectProcessor extends AbstractActor implements Processor, Status { private static final String REDIRECT_PI = "http-redirect"; private static final String HREF_ATTRIBUTE = "href"; private Document m_document; /** * Process the DOM tree. */ public Document process(Document document, Dictionary parameters) throws Exception { this.m_document = document; HttpServletRequest request = (HttpServletRequest)parameters.get("request"); HttpServletResponse response = (HttpServletResponse)parameters.get("response"); /* * Process the document */ String redirectURL = doProcess(document); /* * if find tag, * send HTTP redirect and return null document to stop processing */ if (redirectURL != null) { response.sendRedirect(redirectURL); return null; } /* * else, return the document */ return document; } /** * Right now, always return true. How else do we handle this? */ public boolean hasChanged(Object context) { return true; } public String getStatus() { return "Redirect Processor"; } private String doProcess(Node node) { short nodeType = node.getNodeType(); switch (nodeType) { case Node.ELEMENT_NODE: case Node.DOCUMENT_NODE: Node[] children = getChildren(node); String redirectURL; for (int i = 0; i < children.length; i++) { redirectURL = doProcess(children[i]); if (redirectURL != null) { return redirectURL; } } break; case Node.PROCESSING_INSTRUCTION_NODE: Node parent = node.getParentNode(); ProcessingInstruction pi = (ProcessingInstruction) node; try { String target = pi.getTarget(); if (target.equals(REDIRECT_PI)) { return processObject(pi); } } catch (Exception e) { String message = e.getMessage(); String className = e.getClass().getName(); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw, true); e.printStackTrace(pw); Text errorText = this.m_document.createTextNode( "{Redirect Error: " + className + ": " + message + "}\n" + sw.toString() ); parent.replaceChild(errorText, pi); } break; } return null; } private String processObject(ProcessingInstruction pi) throws Exception { Hashtable attributes = new Hashtable(); parseAttributes(pi.getData(), attributes); String redirectURL = (String) attributes.get(HREF_ATTRIBUTE); if (redirectURL == null) { // URL missing throw new Exception("Missing HREF in redirect definition"); } return redirectURL; } private void parseAttributes(String data, Hashtable attributes) { int length = data.length(); char[] chars = data.toCharArray(); try { for (int index = 0; index < length; index++) { /* Skip leading blanks */ while (index < length && chars[index] <= ' ') { index++; } /* Get variable name */ StringBuffer nameBuffer = new StringBuffer(); while (index < length &&!(chars[index] == '=' || chars[index] <= ' ')) { nameBuffer.append(chars[index++]); } String name = nameBuffer.toString(); // Skip blanks while (index < length && chars[index] <= ' ') { index++; } /* Get variable value */ if (chars[index++] != '=') { throw new Exception("Invalid attribute name: '" + name + "'"); } // Skip blanks while (index < length && chars[index] <= ' ') { index++; } if (chars[index++] != '"') { throw new Exception("Invalid attribute value for '" + name + "'"); } StringBuffer valueBuffer = new StringBuffer(); while (index < length && chars[index] != '"') { valueBuffer.append(chars[index++]); } String value = valueBuffer.toString(); if (index == length || chars[index] != '"') { throw new Exception("Unterminated string '" + value + "' in attribute '" + name + "'"); } // Store name/value pair attributes.put(name, value); } } catch (Exception e) { System.err.println(e.getMessage()); } } private Node[] getChildren(Node node) { NodeList nodeList = node.getChildNodes(); int childCount = nodeList.getLength(); Node[] children = new Node[childCount]; for (int i = 0; i < childCount; i++) { children[i] = nodeList.item(i); } return children; } } _______________________________________ Eric SCHAEFFER eschaeffer@posterconseil.com POSTER CONSEIL 118 rue de Tocqueville 75017 PARIS FRANCE Tel. : 33-140541058 Fax : 33-140541059 ---------------------------------------------------------------------------- ----------------------------- Come to the first official Apache Software Foundation Conference! ----------------------------------- http://ApacheCon.Com ------------------------------------ _______________________________________ ----- Original Message ----- From: Jan Sundin To: Sent: Friday, January 28, 2000 2:02 PM Subject: redirect > Hi, > im using Cocoon-Producers. For some occasions i would like to do an > HTTP-redirect from the producer, is this possible? Or do i have to implement > my own servlet-wrapper around the Cocoon engine and let the servlet do the > redirect? > > Regards, > > Jan Sundin > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: cocoon-users-unsubscribe@xml.apache.org > For additional commands, e-mail: cocoon-users-help@xml.apache.org >