Search in sources :

Example 36 with Context

use of com.opensymphony.xwork2.inject.Context in project entando-core by entando.

the class ApsTimestampTypeConverter method convertFromString.

@Override
public Object convertFromString(Map context, String[] values, Class toType) {
    if (null == values)
        return null;
    if (values.length > 1) {
        throw new XWorkException("Multiple values");
    }
    Date result = null;
    String value = values[0];
    if (value instanceof String && value != null && ((String) value).length() > 0) {
        String sa = (String) value;
        DateFormat df = new SimpleDateFormat(ApsAdminSystemConstants.CALENDAR_TIMESTAMP_PATTERN);
        try {
            // let's use strict parsing (XW-341)
            df.setLenient(false);
            result = df.parse(sa);
            if (!(Date.class == toType)) {
                try {
                    Constructor constructor = toType.getConstructor(new Class[] { long.class });
                    return constructor.newInstance(new Object[] { Long.valueOf(result.getTime()) });
                } catch (Exception e) {
                    throw new XWorkException("Couldn't create class " + toType + " using default (long) constructor", e);
                }
            }
        } catch (ParseException e) {
            throw new XWorkException("Could not parse date", e);
        }
    }
    return result;
}
Also used : XWorkException(com.opensymphony.xwork2.XWorkException) Constructor(java.lang.reflect.Constructor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) XWorkException(com.opensymphony.xwork2.XWorkException) ParseException(java.text.ParseException)

Example 37 with Context

use of com.opensymphony.xwork2.inject.Context in project entando-core by entando.

the class ApsAdminBaseTestCase method initAction.

/**
 * Created action class based on namespace and name
 *
 * @param namespace The namespace
 * @param name The name of the action
 * @throws java.lang.Exception In case of error
 */
protected void initAction(String namespace, String name) throws Exception {
    // create a proxy class which is just a wrapper around the action call.
    // The proxy is created by checking the namespace and name against the
    // struts.xml configuration
    ActionProxyFactory proxyFactory = (ActionProxyFactory) this.dispatcher.getContainer().getInstance(ActionProxyFactory.class);
    this.proxy = proxyFactory.createActionProxy(namespace, name, null, null, true, false);
    // set to true if you want to process Freemarker or JSP results
    this.proxy.setExecuteResult(false);
    // by default, don't pass in any request parameters
    // set the actions context to the one which the proxy is using
    this.proxy.getInvocation().getInvocationContext().setSession(new HashMap<>());
    ServletActionContext.setContext(this.proxy.getInvocation().getInvocationContext());
    ServletActionContext.setRequest(this.request);
    ServletActionContext.setResponse(response);
    ServletActionContext.setServletContext(servletContext);
    this.action = (ActionSupport) this.proxy.getAction();
    // reset previsious params
    List<String> paramNames = new ArrayList<String>(this.request.getParameterMap().keySet());
    for (int i = 0; i < paramNames.size(); i++) {
        String paramName = (String) paramNames.get(i);
        this.removeParameter(paramName);
    }
}
Also used : ActionProxyFactory(com.opensymphony.xwork2.ActionProxyFactory) ArrayList(java.util.ArrayList)

Aggregations

ActionContext (com.opensymphony.xwork2.ActionContext)21 ValueStack (com.opensymphony.xwork2.util.ValueStack)14 AgiActionName (org.onebusaway.probablecalls.AgiActionName)7 List (java.util.List)5 ActionProxy (com.opensymphony.xwork2.ActionProxy)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)3 StopBean (org.onebusaway.transit_data.model.StopBean)3 XWorkException (com.opensymphony.xwork2.XWorkException)2 Inject (com.opensymphony.xwork2.inject.Inject)2 Constructor (java.lang.reflect.Constructor)2 DateFormat (java.text.DateFormat)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Map (java.util.Map)2 ServletActionContext (org.apache.struts2.ServletActionContext)2 SessionAware (org.apache.struts2.interceptor.SessionAware)2 XWorkRequestAttributes (org.onebusaway.presentation.impl.users.XWorkRequestAttributes)2 BookmarkWithStopsBean (org.onebusaway.presentation.model.BookmarkWithStopsBean)2