geirm 01/04/15 20:27:55
Added: contrib/temporary/struts-velocity/src/java/org/apache/velocity/struts
ErrorsBean.java MessageBean.java VelServlet.java
VelocityBean.java
Log:
initial commit
Revision Changes Path
1.1
jakarta-velocity/contrib/temporary/struts-velocity/src/java/org/apache/velocity/struts/ErrorsBean.java
Index: ErrorsBean.java
===================================================================
package org.apache.velocity.struts;
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Velocity", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.IOException;
import java.util.Locale;
import java.util.Iterator;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletRequest;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import org.apache.struts.util.ErrorMessages;
import org.apache.struts.util.MessageResources;
/**
* <p>
* Kluged class to handle the Struts Action error system. It's
* based on outright theft of the code from ErrorsTag.
* </p>
* <p>
* This will be redone to avoid the code duplication.
* </p>
* @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
* $Id: ErrorsBean.java,v 1.1 2001/04/16 03:27:54 geirm Exp $
*/
public class ErrorsBean extends VelocityBean
{
/**
* key used to find the error stuff produced by Struts
*/
private String name = Action.ERROR_KEY;
/**
* CTOR
*/
public ErrorsBean( ServletContext context, HttpSession sess, ServletRequest
request )
{
super( context, sess, request );
}
/**
* Public method to retrieve formatted error string
*
* @return string containing the error message, formatted
*/
public String getErrors()
{
/*
* Were any error messages specified?
*/
ActionErrors errors = new ActionErrors();
Object value = request.getAttribute(name);
if (value == null)
{
;
}
else if (value instanceof String)
{
errors.add( ActionErrors.GLOBAL_ERROR, new ActionError((String) value));
}
else if (value instanceof String[])
{
String keys[] = (String[]) value;
for (int i = 0; i < keys.length; i++)
{
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError(keys[i]));
}
}
else if (value instanceof ErrorMessages)
{
String keys[] = ((ErrorMessages) value).getErrors();
if (keys == null)
keys = new String[0];
for (int i = 0; i < keys.length; i++)
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError(keys[i]));
}
else if (value instanceof ActionErrors)
{
errors = (ActionErrors) value;
}
if (errors.empty())
{
return "";
}
/*
* Render the error messages appropriately
*/
Locale locale = null;
locale = (Locale) session.getAttribute(Action.LOCALE_KEY);
if (locale == null)
locale = Locale.getDefault();
MessageResources messages = (MessageResources)
context.getAttribute(Action.MESSAGES_KEY);
String message = null;
StringBuffer results = new StringBuffer();
message = messages.getMessage(locale, "errors.header");
if (message != null)
{
results.append(message);
results.append("\r\n");
}
Iterator reports = errors.get();
while (reports.hasNext())
{
ActionError report = (ActionError) reports.next();
message =
messages.getMessage(locale,
report.getKey(), report.getValues());
if (message != null)
{
results.append(message);
results.append("\r\n");
}
}
message = messages.getMessage(locale, "errors.footer");
if (message != null)
{
results.append(message);
results.append("\r\n");
}
return results.toString();
}
}
1.1
jakarta-velocity/contrib/temporary/struts-velocity/src/java/org/apache/velocity/struts/MessageBean.java
Index: MessageBean.java
===================================================================
package org.apache.velocity.struts;
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Velocity", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.IOException;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletRequest;
import org.apache.struts.action.Action;
import org.apache.struts.util.MessageResources;
/**
* <p>
* Kluged class to handle the Struts Action message resourcesystem. It's
* based on outright theft of the code from MessageTag.
* </p>
* <p>
* This will be redone to avoid the code duplication.
* </p>
* @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
* $Id: MessageBean.java,v 1.1 2001/04/16 03:27:54 geirm Exp $
*/
public class MessageBean extends VelocityBean
{
/**
* CTOR
*/
public MessageBean( ServletContext context, HttpSession sess, ServletRequest
request )
{
super( context, sess, request );
}
/**
* Returns the requested string resource for the
* locale stuffed in the session.
*
* Note : I want to change this to 'key()' as that
* is a closer parallel to the MessageTag.
*
* @param msgKey string resource name
* @return resource string
*/
public String get( String msgKey )
{
/*
* Acquire the resources object containing our messages
*/
MessageResources resources = (MessageResources)
context.getAttribute( Action.MESSAGES_KEY );
if (resources == null)
{
return "";
}
/*
* Calculate the Locale we will be using
*/
Locale locale = null;
locale = (Locale) session.getAttribute( Action.LOCALE_KEY );
if (locale == null)
{
locale = Locale.getDefault();
}
/*
* Construct the optional arguments array we will be using
*/
Object args[] = new Object[5];
args[0] = null;
args[1] = null;
args[2] = null;
args[3] = null;
args[4] = null;
/*
* Retrieve the message string we are looking for
*/
return resources.getMessage(locale, msgKey, args);
}
}
1.1
jakarta-velocity/contrib/temporary/struts-velocity/src/java/org/apache/velocity/struts/VelServlet.java
Index: VelServlet.java
===================================================================
package org.apache.velocity.struts;
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Velocity", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Properties;
import java.io.IOException;
import java.io.FileNotFoundException;
import org.apache.velocity.servlet.VelocityServlet;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.struts.MessageBean;
import org.apache.velocity.struts.ErrorsBean;
import org.apache.struts.action.Action;
/**
* <p>
* Kluged class to handle rendering of Velocity templates for
* the <a href="http://jakarta.apache.org/struts/">Struts</a> framework.
* </p>
* <p>
* This ain't done...
* </p>
* @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
* $Id: VelServlet.java,v 1.1 2001/04/16 03:27:55 geirm Exp $
*/
public class VelServlet extends VelocityServlet
{
/**
* sets up the configuration for Velocity. Right now
* we just want to set the path for the template loader
*
* @param config ServletConfig - can use to get params if needed
* @return Properties used for initializing Velocity
*/
protected Properties loadConfiguration(ServletConfig config )
throws IOException, FileNotFoundException
{
Properties p = new Properties();
String path = getServletContext().getRealPath("/");
p.setProperty("file.resource.loader.path",
getServletContext().getRealPath("/") );
p.setProperty("runtime.log",
getServletContext().getRealPath("/velocity.log") );
return p;
}
/**
* Handled the request. Current responsibilities :
* <ul>
* <li> fill context with all context/session/request attributes
* <li> create MessageBean to handle Strut's message resource system
* <li> create ErrorsBean to handle Strut's Action error system
* <li> find and return Template
* </ul>
* @param request client request
* @param response client response
* @param ctx VelocityContext to fill
* @return Velocity Template object or null
*/
protected Template handleRequest( HttpServletRequest request,
HttpServletResponse response, Context ctx )
throws Exception
{
ServletContext sc = getServletContext();
HttpSession sess = request.getSession();
/*
* we need to fill the context with whatever we can find. Need a better
way of doing this
* but there doesn't seem to be a map of what Struts shoves where
*/
fillContext( ctx, sc, sess, request );
/*
* kludge - this should really be externally specified
*/
ctx.put("message", new MessageBean(sc, sess, request) );
ctx.put("errors", new ErrorsBean(sc, sess, request));
return getTemplate(request.getServletPath() );
}
/**
* kludgy routine to scrape out all 'stuff' punched into the 3 contexts by
Struts
*/
private boolean fillContext( Context ctx, ServletContext sc, HttpSession sess,
HttpServletRequest request )
{
/* --- CONTEXT --- */
for (Enumeration e = sc.getAttributeNames(); e.hasMoreElements(); )
{
String name = (String) e.nextElement();
Object o = sc.getAttribute( name );
if ( o != null )
ctx.put(name, o );
}
/* ---- SESSION ----- */
for (Enumeration e = sess.getAttributeNames(); e.hasMoreElements(); )
{
String name = (String) e.nextElement();
Object o = sess.getAttribute( name );
if ( o != null )
ctx.put(name, o );
/*
* little kludge to get the transaction toke key thing to work
*/
if (name.equals( Action.TRANSACTION_TOKEN_KEY ))
{
System.out.println("SessionToken => " + o );
ctx.put( "sessionToken", o );
}
}
/* --- REQUEST --- */
for (Enumeration e = request.getAttributeNames(); e.hasMoreElements(); )
{
String name = (String) e.nextElement();
Object o = request.getAttribute( name );
if ( o != null )
ctx.put(name, o );
}
return true;
}
}
1.1
jakarta-velocity/contrib/temporary/struts-velocity/src/java/org/apache/velocity/struts/VelocityBean.java
Index: VelocityBean.java
===================================================================
package org.apache.velocity.struts;
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Velocity", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletRequest;
/**
* Baseclass for Strut's Tag -> Bean work
*
* Nicht sehr gut.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
* $Id: VelocityBean.java,v 1.1 2001/04/16 03:27:55 geirm Exp $
*/
public class VelocityBean
{
protected ServletContext context;
protected HttpSession session;
protected ServletRequest request;
public VelocityBean( ServletContext context, HttpSession sess, ServletRequest
request )
{
this.context = context;
this.session = sess;
this.request = request;
}
}