huber 2003/01/25 23:27:32 Modified: src/java/org/apache/cocoon/transformation EncodeURLTransformer.java Log: fix bug 16246, getSession each time it is needed, don't save session as member minor doc fixes, added encodeURL handling for buggy servlet container Revision Changes Path 1.6 +108 -64 xml-cocoon2/src/java/org/apache/cocoon/transformation/EncodeURLTransformer.java Index: EncodeURLTransformer.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/transformation/EncodeURLTransformer.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- EncodeURLTransformer.java 25 Jan 2003 02:58:58 -0000 1.5 +++ EncodeURLTransformer.java 26 Jan 2003 07:27:32 -0000 1.6 @@ -77,7 +77,7 @@ * <p> * This transformer applies encodeURL method to URLs. * You may want to use this transform to avoid doing the manually - * encodeURL() call. + * encodeURL() calls. * </p> * <p> * Usually this transformer is appended as last transformer before @@ -85,27 +85,27 @@ * URLs introduced in the generator, and xslt transformer phase. * </p> * <p> - * You can specify which attributes hold URL values in order to restrict + * You can specify which attributes hold an URL values in order to restrict * URL rewriting to specific attributes only. * </p> * <p> * Usage in a sitemap: * </p> * <pre><tt> - * <map:composition> + * <map:composition> * ... - * <map:transformers> + * <map:transformers> * ... - * <map:transformer type="encodeURL" - * src="org.apache.cocoon.optional.transformation.EncodeURLTransformer"> - * <exclude-name>img/@src</exclude-name> - * <include-name>.&asterik;/@href|.&asterik;/@src|.&asterik;/@action</include-name> - * </map:transformer> + * <map:transformer type="encodeURL" + * src="org.apache.cocoon.optional.transformation.EncodeURLTransformer"> + * <exclude-name>img/@src</exclude-name> + * <include-name>.&asterik;/@href|.&asterik;/@src|.&asterik;/@action</include-name> + * </map:transformer> * ... - * <map:pipelines> - * <map:pipeline> + * <map:pipelines> + * <map:pipeline> * ... - * <map:transform type="encodeURL"/> + * <map:transform type="encodeURL"/> * ... * </pre></tt> * @@ -129,32 +129,24 @@ /** * Configuration name for specifying excluding patterns, * ie exclude-name. - * - * @since */ public final static String EXCLUDE_NAME = "exclude-name"; /** * Configuration name for specifying including patterns, * ie include-name. - * - * @since */ public final static String INCLUDE_NAME = "include-name"; /** * Configuration default exclude pattern, * ie img/@src - * - * @since */ public final static String EXCLUDE_NAME_DEFAULT = "img/@src"; /** * Configuration default exclude pattern, * ie .*\/@href|.*\/@action|frame/@src - * - * @since */ public final static String INCLUDE_NAME_DEFAULT = ".*/@href|.*/@action|frame/@src"; @@ -163,10 +155,44 @@ private ElementAttributeMatching elementAttributeMatching; private Response response; - private Session session; + private Request request; /** + * get the session from the request. + * + * @return Session object, or null if no session is available + */ + protected Session getSession() { + // assume that request is already set + Session session = this.request.getSession( false ); + return session; + } + + /** + * check if encoding of URLs is neccessary. + * + * @return true iff session object exists, and session-id + * was provided from URL, or session is new, + * else return false + */ + protected boolean isEncodeURLNeeded() { + Session session = getSession(); + boolean isEncodeURLNeeded = false; + + if ( null != session ) { + // do encoding if session id is from URL, or the session is new, + // fixes BUG #13855, due to [EMAIL PROTECTED] + if ( request.isRequestedSessionIdFromURL() || session.isNew()) { + isEncodeURLNeeded = true; + } + } + return isEncodeURLNeeded; + } + + + + /** * Setup the transformer. * <p> * Setup include, and exclude patterns from the parameters @@ -180,28 +206,21 @@ public void setup(SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws ProcessingException, SAXException, IOException { - final Request request = ObjectModelHelper.getRequest(objectModel); - this.session = request.getSession( false ); - if ( null != this.session ) { - // do encoding if session id is from URL, or the session is new, - // fixes BUG #13855, due to [EMAIL PROTECTED] - if ( request.isRequestedSessionIdFromURL() || this.session.isNew()) { - this.response = ObjectModelHelper.getResponse(objectModel); - final String includeName = parameters.getParameter(INCLUDE_NAME, - this.includeNameConfigure); - final String excludeName = parameters.getParameter(EXCLUDE_NAME, - this.excludeNameConfigure); - try { - this.elementAttributeMatching = new ElementAttributeMatching(includeName, excludeName); - } catch (RESyntaxException reex) { - final String message = "Cannot parse include-name: " + includeName + " " + - "or exclude-name: " + excludeName + "!"; - throw new ProcessingException(message, reex); - } - } else { - // we don't need to encode - this.session = null; - } + this.request = ObjectModelHelper.getRequest(objectModel); + this.response = ObjectModelHelper.getResponse(objectModel); + + // don't check if URL encoding is needed now, as + // a generator might create a new session + final String includeName = parameters.getParameter(INCLUDE_NAME, + this.includeNameConfigure); + final String excludeName = parameters.getParameter(EXCLUDE_NAME, + this.excludeNameConfigure); + try { + this.elementAttributeMatching = new ElementAttributeMatching(includeName, excludeName); + } catch (RESyntaxException reex) { + final String message = "Cannot parse include-name: " + includeName + " " + + "or exclude-name: " + excludeName + "!"; + throw new ProcessingException(message, reex); } } @@ -211,7 +230,6 @@ * * @param configuration Description of Parameter * @exception ConfigurationException Description of Exception - * @since */ public void configure(Configuration configuration) throws ConfigurationException { Configuration child; @@ -234,14 +252,12 @@ /** - *Description of the Method - * - * @since + * Recycle resources of this transformer */ public void recycle() { super.recycle(); this.response = null; - this.session = null; + this.request = null; this.elementAttributeMatching = null; } @@ -253,10 +269,11 @@ * @return The generated key hashes the src */ public java.io.Serializable generateKey() { - if ( null == this.session ) { + if (isEncodeURLNeeded()) { return "1"; + } else { + return null; } - return null; } /** @@ -266,10 +283,11 @@ * component is currently not cacheable. */ public SourceValidity generateValidity() { - if ( null == this.session ) { + if (isEncodeURLNeeded()) { return NOPValidity.SHARED_INSTANCE; + } else { + return null; } - return null; } /** @@ -280,12 +298,12 @@ * @param raw name of the element * @param attributes list * @exception SAXException Description of Exception - * @since */ public void startElement(String uri, String name, String raw, Attributes attributes) throws SAXException { + boolean isEncodeURLNeeded = isEncodeURLNeeded(); - if (this.session != null && this.elementAttributeMatching != null) { + if (isEncodeURLNeeded && this.elementAttributeMatching != null) { String lname = name; if (attributes != null && attributes.getLength() > 0) { AttributesImpl new_attributes = new AttributesImpl(attributes); @@ -295,7 +313,10 @@ String value = new_attributes.getValue(i); if (elementAttributeMatching.matchesElementAttribute(lname, attr_lname)) { - final String new_value = response.encodeURL( value ); + // don't use simply response.encodeURL + // but be more smart about the url encoding + //final String new_value = this.response.encodeURL( value ); + final String new_value = this.encodeURL( value, response ); if (getLogger().isDebugEnabled()) { this.getLogger().debug("element/@attribute matches: " + name + "/@" + attr_lname); this.getLogger().debug("encodeURL: " + value + " -> " + new_value); @@ -313,6 +334,36 @@ } /** + * Do the URL rewriting. + * <p> + * Check if <code>url</code> contains already the sessionid, some servlet-engines + * just appends the session-id without checking if the sessionid is already present. + * </p> + * + * @param url the URL probably without sessionid. + * @param request the http request + * @param response the http response + * @return String the original url inclusive the sessionid + */ + private String encodeURL(String url, Response response) { + String encoded_url; + if (response != null) { + // As some servlet-engine does not check if url has been already rewritten + Session session = getSession(); + if (session != null && url.indexOf(session.getId()) > -1) { + // url contains already the session id encoded + encoded_url = url; + } else { + // do encode the session id + encoded_url = response.encodeURL(url); + } + } else { + encoded_url = url; + } + return encoded_url; + } + + /** * A helper class for matching element names, and attribute names. * * <p> @@ -327,13 +378,11 @@ /** * Regular expression of including patterns * - * @since */ protected RE includeNameRE; /** * Regular expression of excluding patterns * - * @since */ protected RE excludeNameRE; @@ -344,7 +393,6 @@ * @param includeName Description of Parameter * @param excludeName Description of Parameter * @exception RESyntaxException Description of Exception - * @since */ public ElementAttributeMatching(String includeName, String excludeName) throws RESyntaxException { includeNameRE = new RE(includeName, RE.MATCH_CASEINDEPENDENT); @@ -360,7 +408,6 @@ * @param attr_name * @return boolean true iff value of attribute_name should get rewritten, else * false. - * @since */ public boolean matchesElementAttribute(String element_name, String attr_name) { String element_attr_name = canonicalizeElementAttribute(element_name, attr_name); @@ -384,7 +431,6 @@ * @param element_name Description of Parameter * @param attr_name Description of Parameter * @return Description of the Returned Value - * @since */ private String canonicalizeElementAttribute(String element_name, String attr_name) { return element_name + "/@" + attr_name; @@ -397,7 +443,6 @@ * @param element_attr_name * @return boolean true iff exclude-name matches element_name, attr_name, else * false. - * @since */ private boolean matchesExcludesElementAttribute(String element_attr_name) { boolean match = excludeNameRE.match(element_attr_name); @@ -411,7 +456,6 @@ * @param element_attr_name * @return boolean true iff include-name matches element_name, attr_name, else * false. - * @since */ private boolean matchesIncludesElementAttribute(String element_attr_name) { boolean match = includeNameRE.match(element_attr_name);
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]