it.openutils.mgnlspring.UrlFunctions Maven / Gradle / Ivy
/*
* Copyright 2007 Fabrizio Giustina.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package it.openutils.mgnlspring;
import info.magnolia.context.MgnlContext;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
/**
* @author fgiust
* @version $Id: UrlFunctions.java 344 2007-06-30 15:31:28Z fgiust $
*/
public final class UrlFunctions
{
/**
* Don't instantiate.
*/
private UrlFunctions()
{
// unused
}
public static String url(String url)
{
return encodeUrl(RewriteVarsThreadLocal.getCurrentPageUrl(), url);
}
public static String urlWithDestination(String url, String destination)
{
// check RewriteVarsThreadLocal because if not running under magnolia we should not replace urls
if (RewriteVarsThreadLocal.getCurrentPageUrl() == null || destination == null)
{
return url(url);
}
return encodeUrl(RewriteVarsThreadLocal.getContextPath() + destination, url);
}
/**
* @param baseUrl
* @param actionUrl
* @return
*/
private static String encodeUrl(final String baseUrl, String actionUrl)
{
String contextPath = MgnlContext.getContextPath();
if (StringUtils.isNotEmpty(contextPath) && actionUrl != null && actionUrl.startsWith(contextPath))
{
actionUrl = StringUtils.substringAfter(actionUrl, contextPath);
}
if (baseUrl == null)
{
if (actionUrl.startsWith("/"))
{
return contextPath + actionUrl;
}
return actionUrl;
}
int paramIndex = baseUrl.indexOf('?');
String page = baseUrl;
String params = null;
if (paramIndex != -1)
{
page = StringUtils.substring(baseUrl, 0, paramIndex + 1);
params = StringUtils.substring(baseUrl, paramIndex + 1);
}
StringBuffer outUrl = new StringBuffer();
outUrl.append(page);
outUrl.append("?");
if (params != null)
{
outUrl.append(params);
outUrl.append("&");
}
outUrl.append("_action=");
outUrl.append(StringUtils.replace(actionUrl, "?", "&"));
String encodedUrl = StringEscapeUtils.escapeXml(outUrl.toString());
return encodedUrl;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy