/** * Copyright (C) 2007 KRONK Ltd. * * This program is free software; you can redistribute it and/or modify it under the terms of the * GNU Lesser General Public License as published by the Free Software Foundation; either version * 2.1 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html */ package net.kronkltd.oxf.processor.generator; import org.apache.log4j.Logger; import org.orbeon.oxf.common.OXFException; import org.orbeon.oxf.pipeline.api.ExternalContext; import org.orbeon.oxf.processor.ProcessorImpl; import org.orbeon.oxf.processor.ProcessorInputOutputInfo; import org.orbeon.oxf.processor.ProcessorOutput; import org.orbeon.oxf.util.LoggerFactory; import org.orbeon.oxf.xml.XMLUtils; import org.xml.sax.ContentHandler; import java.util.*; public class CookieGenerator extends ProcessorImpl { static Logger logger = LoggerFactory.createLogger(CookieGenerator.class); public CookieGenerator() { addOutputInfo(new ProcessorInputOutputInfo(OUTPUT_DATA)); } public ProcessorOutput createOutput(String name) { ProcessorOutput output = new ProcessorImpl.ProcessorOutputImpl(getClass(), name) { public void readImpl(org.orbeon.oxf.pipeline.api.PipelineContext context, final ContentHandler contentHandler) { try { ExternalContext externalContext = (ExternalContext) context.getAttribute(org.orbeon.oxf.pipeline.api.PipelineContext.EXTERNAL_CONTEXT); ExternalContext.Request request = externalContext.getRequest(); Map headers = request.getHeaderMap(); String cookieString = (String) headers.get("cookie"); contentHandler.startDocument(); contentHandler.startElement("", "cookies", "cookies", XMLUtils.EMPTY_ATTRIBUTES); if (cookieString != null) { String[] cookies = cookieString.split(";"); for(int i=0; i < cookies.length; i++) { String cookie = cookies[i]; String[] parts = cookie.split("=", 2); char[] key = parts[0].trim().toCharArray(); char[] value = parts[1].trim().toCharArray(); contentHandler.startElement("", "cookie", "cookie", XMLUtils.EMPTY_ATTRIBUTES); contentHandler.startElement("", "name", "name", XMLUtils.EMPTY_ATTRIBUTES); contentHandler.characters(key, 0, key.length); contentHandler.endElement("", "name", "name"); contentHandler.startElement("", "value", "value", XMLUtils.EMPTY_ATTRIBUTES); contentHandler.characters(value, 0, value.length); contentHandler.endElement("", "value", "value"); contentHandler.endElement("", "cookie", "cookie"); } } contentHandler.endElement("", "cookies", "cookies"); contentHandler.endDocument(); } catch (Exception e) { throw new OXFException(e); } } }; addOutput(OUTPUT_DATA, output); return output; } }