jmcnally 01/12/11 18:14:41
Modified: src/java/org/apache/turbine DynamicURI.java
RelativeDynamicURI.java
src/tool/org/apache/turbine/tool TemplateLink.java
Log:
added a flag to generate absolute or relative url's, this eliminates the need
for the RelativeDynamicURI class.
added a flag to turn off url rewriting. In the normal current usage, one
DynamicURI may be used to generate several links as it is used within one or
more templates. TemplateLink was using a feature of the implementation that
does not encode urls if the request is null, in order to shut off encoding
when not appropriate (links inside of emails, for example). One problem with
the approach is that the DynamicURI could not be made to encode url's again
once the request was nullified. If an action fires off an email (and turns off
rewriting) then the same link tool is used to generate a web page response,
a user will lose their session, if depending on the rewriting.
The flag is now explicit in DynamicURI and it must be turned off in every
$link reference that does not want url rewriting.
Revision Changes Path
1.3 +79 -21 jakarta-turbine-3/src/java/org/apache/turbine/DynamicURI.java
Index: DynamicURI.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-3/src/java/org/apache/turbine/DynamicURI.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DynamicURI.java 2001/09/14 01:03:24 1.2
+++ DynamicURI.java 2001/12/12 02:14:41 1.3
@@ -83,7 +83,8 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jon S. Stevens</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * @version $Id: DynamicURI.java,v 1.2 2001/09/14 01:03:24 jon Exp $
+ * @author <a href="mailto:[EMAIL PROTECTED]">John McNally</a>
+ * @version $Id: DynamicURI.java,v 1.3 2001/12/12 02:14:41 jmcnally Exp $
*/
public class DynamicURI
{
@@ -126,7 +127,16 @@
/** Q = 1 for query data. */
protected static final int QUERY_DATA = 1;
+ /** true=absolute url's; false=relative url's */
+ private boolean isAbsolute = true;
+
/**
+ * true=url will be rewritten with session_id if needed; false=url will
+ * not be encoded
+ */
+ private boolean encodeUrl = true;
+
+ /**
* Constructor sets up some variables.
*
* @param data A Turbine RunData object.
@@ -747,6 +757,31 @@
}
/**
+ * if true, the scheme, domain, and port will not be included in the
+ * String representation of this uri..
+ *
+ * @param b a <code>boolean</code>
+ * @return a <code>DynamicURI</code> (self)
+ */
+ public DynamicURI setRelative(boolean b)
+ {
+ isAbsolute = !b;
+ return this;
+ }
+
+ /**
+ * Can be used to disable url rewriting.
+ *
+ * @param b a <code>boolean</code>
+ * @return a <code>DynamicURI</code> (self)
+ */
+ public DynamicURI setEncodeUrl(boolean b)
+ {
+ encodeUrl = b;
+ return this;
+ }
+
+ /**
* Builds the URL with all of the data URL-encoded as well as
* encoded using HttpServletResponse.encodeUrl().
*
@@ -767,16 +802,20 @@
public String toString()
{
StringBuffer output = new StringBuffer();
- output.append ( getServerScheme() );
- output.append ( "://" );
- output.append ( getServerName() );
- if ( (getServerScheme().equals(HTTP) && getServerPort() != 80)
- || (getServerScheme().equals(HTTPS) && getServerPort() != 443)
- )
+ if (isAbsolute)
{
- output.append (':');
- output.append ( getServerPort() );
+ output.append ( getServerScheme() );
+ output.append ( "://" );
+ output.append ( getServerName() );
+ if ( (getServerScheme().equals(HTTP) && getServerPort() != 80)
+ || (getServerScheme().equals(HTTPS) && getServerPort() != 443)
+ )
+ {
+ output.append (':');
+ output.append ( getServerPort() );
+ }
}
+
output.append ( getScriptName() );
if ( this.hasPathInfo )
{
@@ -792,7 +831,7 @@
// There seems to be a bug in Apache JServ 1.0 where the
// session id is not appended to the end of the url when a
// cookie has not been set.
- if ( this.res != null )
+ if ( this.res != null && encodeUrl )
{
if ( this.redirect )
{
@@ -814,27 +853,46 @@
* necessary sometimes when you want the exact URL and don't want
* DynamicURI to be too smart and remove actions, screens, etc.
* This also returns the Query Data where DynamicURI normally
- * would not.
+ * would not. This method always returns absolute url's.
*
* @param data A Turbine RunData object.
* @return A String with the URL representing the RunData.
*/
public static String toString(RunData data)
{
+ return toString(data, true);
+ }
+
+ /**
+ * Given a RunData object, get a URI for the request. This is
+ * necessary sometimes when you want the exact URL and don't want
+ * DynamicURI to be too smart and remove actions, screens, etc.
+ * This also returns the Query Data where DynamicURI normally
+ * would not.
+ *
+ * @param data A Turbine RunData object.
+ * @param boolean to determine absolute vs. relative links.
+ * @return A String with the URL representing the RunData.
+ */
+ public static String toString(RunData data, boolean isAbsolute)
+ {
StringBuffer output = new StringBuffer();
HttpServletRequest request = data.getRequest();
- output.append (data.getServerScheme());
- output.append ( "://" );
- output.append (data.getServerName());
-
- if ( (data.getServerScheme().equals(HTTP) &&
- data.getServerPort() != 80) ||
- (data.getServerScheme().equals(HTTPS) &&
- data.getServerPort() != 443) )
+ if (isAbsolute)
{
- output.append (':');
- output.append (data.getServerPort());
+ output.append (data.getServerScheme());
+ output.append ( "://" );
+ output.append (data.getServerName());
+
+ if ( (data.getServerScheme().equals(HTTP) &&
+ data.getServerPort() != 80) ||
+ (data.getServerScheme().equals(HTTPS) &&
+ data.getServerPort() != 443) )
+ {
+ output.append (':');
+ output.append (data.getServerPort());
+ }
}
output.append (data.getScriptName());
1.3 +1 -0
jakarta-turbine-3/src/java/org/apache/turbine/RelativeDynamicURI.java
Index: RelativeDynamicURI.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-3/src/java/org/apache/turbine/RelativeDynamicURI.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- RelativeDynamicURI.java 2001/09/03 17:20:44 1.2
+++ RelativeDynamicURI.java 2001/12/12 02:14:41 1.3
@@ -80,6 +80,7 @@
*
<AHREF="/servlets/Turbine/screen=UserScreen&amp;user=jon">ClickHere</A>
*
* @author <a href="mailto:[EMAIL PROTECTED]">David S. Faller</a>
+ * @deprecated use org.apache.turbine.DynamicURI#setRelative(boolean)
*/
public class RelativeDynamicURI
extends DynamicURI
1.3 +19 -5
jakarta-turbine-3/src/tool/org/apache/turbine/tool/TemplateLink.java
Index: TemplateLink.java
===================================================================
RCS file:
/home/cvs/jakarta-turbine-3/src/tool/org/apache/turbine/tool/TemplateLink.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TemplateLink.java 2001/09/03 02:01:59 1.2
+++ TemplateLink.java 2001/12/12 02:14:41 1.3
@@ -73,7 +73,7 @@
* @author <a href="[EMAIL PROTECTED]">Dave Bryson</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Jon S. Stevens</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Henning P. Schmiedehausen</a>
- * @version $Id: TemplateLink.java,v 1.2 2001/09/03 02:01:59 jvanzyl Exp $
+ * @version $Id: TemplateLink.java,v 1.3 2001/12/12 02:14:41 jmcnally Exp $
*/
public class TemplateLink
extends DynamicURI
@@ -127,13 +127,12 @@
}
/**
- * This will turn off the execution of res.encodeURL()
- * by making res == null. This is a hack for cases
- * where you don't want to see the session information
+ * This will turn off the execution of res.encodeURL(). This is useful
+ * for cases where you don't want to see the session information
*/
public TemplateLink setEncodeURLOff()
{
- this.res = null;
+ setEncodeUrl(false);
return this;
}
@@ -162,6 +161,19 @@
}
/**
+ * Set to false to skip the scheme, host, and port sections of the url.
+ * The default is to return absolute url's from the toString method.
+ *
+ * @param b a <code>boolean</code> value
+ * @return a <code>TemplateLink</code> value
+ */
+ public TemplateLink setAbsolute(boolean b)
+ {
+ setRelative(!b);
+ return this;
+ }
+
+ /**
* Returns the URI. After rendering the URI, it clears the
* pathInfo and QueryString portions of the DynamicURI.
*
@@ -176,6 +188,8 @@
// template.
removePathInfo();
removeQueryData();
+ setEncodeUrl(true);
+ setAbsolute(true);
return output;
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>