/*
* Copyright (c) 2002-2007 eshops.cz. All Rights Reserved.
* eshops.cz PROPRIETARY/CONFIDENTAL. Use is subject to license terms.
*
* Redistribution of this file or of an unauthorized byte-code version
* of this file is strictly forbidden.
*/
package cz.eshops.webcommonx.tag.input;
import cz.eshops.common.log.Log;
import cz.eshops.webcommon.WebConst;
import cz.eshops.webcommon.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Extension of the standard Stripes tag to support submiting of Stripes
* forms over explicitely specified:
*
* - protocol (httpp/https) - defaults to the current request's protocol
* - host - default's to the current request's host
* - port - default's to the protocol specific port(80 for http, 443 for https)
* - context - defaults to an empty string
*
*
* @author Jan Moravec <jan.moravec@eshops.cz>
* @version $Id:$
*/
public class FormTag
extends net.sourceforge.stripes.tag.FormTag
{
private static final Log log = Log.getInstance( FormTag.class );
private String protocol;
private String host;
private int port = -1;
private String context;
@Override
public int doEndTag()
throws JspException
{
String action = get( "action" );
HttpServletRequest request = (HttpServletRequest) getPageContext().getRequest();
URL currentUrl = null;
try
{
currentUrl = new URL( request.getRequestURL().toString() );
}
catch ( MalformedURLException e )
{
throw new JspTagException( "Invalid request URL: " + request.getRequestURL(), e );
}
String currentProtocol = currentUrl.getProtocol();
String currentHost = currentUrl.getHost();
int currentPort = currentUrl.getPort();
if ( context != null )
{
String contextPath = request.getContextPath();
if ( !contextPath.equals( "/" ) && action.startsWith( contextPath ) )
{
// strip context path so that we can prepend the custom context
action = action.substring( contextPath.length() );
}
action = context + action;
}
if ( protocol == null )
protocol = currentProtocol;
if ( host == null )
host = currentHost;
if ( port == -1 )
port = currentPort;
if ( !currentProtocol.equals( protocol ) || !currentHost.equals( host ) || currentPort != port )
{
if ( port == -1 )
{
port = WebConst.URL_SCHEME_HTTP.equals( protocol ) ? WebConst.URL_STANDARD_PORT_HTTP :
WebConst.URL_STANDARD_PORT_HTTPS;
}
action = protocol + "://" + host + ":" + port + action;
action = WebUtils.normalizeUrl( action ).toString(); // normalize (remove default port numbers etc.)
}
set( "action", action );
return super.doEndTag();
}
@Override
public void release()
{
super.release();
protocol = null;
host = null;
port = 0;
context = null;
}
public String getProtocol()
{
return protocol;
}
public void setProtocol( String protocol )
{
this.protocol = protocol;
}
public String getHost()
{
return host;
}
public void setHost( String host )
{
this.host = host;
}
public int getPort()
{
return port;
}
public void setPort( int port )
{
this.port = port;
}
public String getContext()
{
return context;
}
public void setContext( String context )
{
if ( context != null && !context.startsWith( "/" ) )
throw new IllegalArgumentException( "Context attribute must start with '/'." );
this.context = context;
}
}