Skip to content

Instantly share code, notes, and snippets.

@isapir
Created March 14, 2012 08:42
Show Gist options
  • Save isapir/2035151 to your computer and use it in GitHub Desktop.
Save isapir/2035151 to your computer and use it in GitHub Desktop.
creates a Railo function for GetRequestUrl()
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE func-lib PUBLIC "-//Railo//DTD CFML Function Library 1.0//EN"
"dtd/web-cfmfunctionlibrary_1_0.dtd">
<!-- saved in /WEB-INF/railo/library/fld/ -->
<func-lib>
<flib-version>1.00</flib-version>
<short-name>RailoExtFunctions</short-name>
<uri>http://www.railo.ch/functions/1_0_0</uri>
<display-name>Railo Extension Function Library</display-name>
<description>Adds Functions to Railo</description>
<function>
<name>GetRequestUrl</name>
<class>net.twentyonesolutions.railo.functions.GetRequestUrl</class>
<description>returns the current request's full URL</description>
<return>
<type>string</type>
</return>
</function>
</func-lib>
package net.twentyonesolutions.railo.functions;
import javax.servlet.http.HttpServletRequest;
import railo.loader.engine.CFMLEngineFactory;
import railo.runtime.PageContext;
import railo.runtime.exp.PageException;
import railo.runtime.ext.function.Function;
/** creates a Railo function for GetRequestUrl() (requires function definition file of type .fld) */
public class GetRequestUrl implements Function {
public static final String SUBSTR_PORT_HTTPS = ":443/";
public static final String SUBSTR_PORT_HTTP = ":80/";
/** handles call to the function without arguments
*
* @param pc pageContext of the current request
* @throws PageException
*/
public static String call( PageContext pc ) throws PageException {
String result = "";
try {
HttpServletRequest req = (HttpServletRequest) pc.getRequest();
result = req.getRequestURL().toString();
result = removeDefaultPort( result );
String queryString = req.getQueryString();
if ( queryString != null && !queryString.isEmpty() )
result = result + "?" + queryString;
} catch ( Exception ex ) {
throw CFMLEngineFactory.getInstance().getCastUtil().toPageException( ex );
}
return result;
}
/**
* removes the :443 from https URLs or the :80 from http URLs
*
* @param url
* @return
*/
public static String removeDefaultPort( String url ) {
String result = url;
String sb6 = url.substring( 0, 6 ).toLowerCase();
int posSla3 = url.indexOf( "/", 8 );
if ( posSla3 > -1 ) {
String sPort = ":-1/";
if ( sb6.equals( "https:" ) ) {
sPort = SUBSTR_PORT_HTTPS;
} else if ( sb6.equals( "http:/" ) ) {
sPort = SUBSTR_PORT_HTTP;
}
int posPort = url.indexOf( sPort );
if ( posPort > -1 && posPort < posSla3 ) {
result = url.substring( 0, posPort ) + url.substring( posPort + sPort.length() - 1 );
}
}
return result;
}
/** test method */
public static void main(String[] args) {
String[] urls = { "http://www.google.com:80/", "https://www.google.com:443/",
"http://www.google.com:80/search?q=abc", "https://www.google.com:443/search?q=abc",
"http://www.google.com:8080/search?q=abc", "https://www.google.com/search?q=abc" };
for ( String u : urls )
System.out.println("u: " + u + "\t>>\t" + removeDefaultPort(u));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment