Search in sources :

Example 21 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class AnnotationWorkflowInterceptor method beforeResult.

/**
 * Invokes any @BeforeResult annotated methods
 *
 * @see com.opensymphony.xwork2.interceptor.PreResultListener#beforeResult(com.opensymphony.xwork2.ActionInvocation,String)
 */
public void beforeResult(ActionInvocation invocation, String resultCode) {
    Object action = invocation.getAction();
    List<Method> methods = new ArrayList<Method>(MethodUtils.getMethodsListWithAnnotation(action.getClass(), BeforeResult.class, true, true));
    if (methods.size() > 0) {
        // methods are only sorted by priority
        Collections.sort(methods, new Comparator<Method>() {

            public int compare(Method method1, Method method2) {
                return comparePriorities(MethodUtils.getAnnotation(method1, BeforeResult.class, true, true).priority(), MethodUtils.getAnnotation(method2, BeforeResult.class, true, true).priority());
            }
        });
        for (Method m : methods) {
            try {
                MethodUtils.invokeMethod(action, true, m.getName());
            } catch (Exception e) {
                throw new StrutsException(e);
            }
        }
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) StrutsException(org.apache.struts2.StrutsException)

Example 22 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class DomHelper method parse.

/**
 * Creates a W3C Document that remembers the location of each element in
 * the source file. The location of element nodes can then be retrieved
 * using the {@link #getLocationObject(Element)} method.
 *
 * @param inputSource the inputSource to read the document from
 * @param dtdMappings a map of DTD names and public ids
 *
 * @return the W3C Document
 */
public static Document parse(InputSource inputSource, Map<String, String> dtdMappings) {
    SAXParserFactory factory = null;
    String parserProp = System.getProperty("xwork.saxParserFactory");
    if (parserProp != null) {
        try {
            ObjectFactory objectFactory = ActionContext.getContext().getContainer().getInstance(ObjectFactory.class);
            Class clazz = objectFactory.getClassInstance(parserProp);
            factory = (SAXParserFactory) clazz.newInstance();
        } catch (Exception e) {
            LOG.error("Unable to load saxParserFactory set by system property 'xwork.saxParserFactory': {}", parserProp, e);
        }
    }
    if (factory == null) {
        factory = SAXParserFactory.newInstance();
    }
    factory.setValidating((dtdMappings != null));
    factory.setNamespaceAware(true);
    SAXParser parser;
    try {
        parser = factory.newSAXParser();
    } catch (Exception ex) {
        throw new StrutsException("Unable to create SAX parser", ex);
    }
    DOMBuilder builder = new DOMBuilder();
    // Enhance the sax stream with location information
    ContentHandler locationHandler = new LocationAttributes.Pipe(builder);
    try {
        parser.parse(inputSource, new StartHandler(locationHandler, dtdMappings));
    } catch (Exception ex) {
        throw new StrutsException(ex);
    }
    return builder.getDocument();
}
Also used : StrutsException(org.apache.struts2.StrutsException) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) SAXParser(javax.xml.parsers.SAXParser) StrutsException(org.apache.struts2.StrutsException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 23 with StrutsException

use of org.apache.struts2.StrutsException in project struts by apache.

the class DefaultActionInvocation method createResult.

public Result createResult() throws Exception {
    LOG.trace("Creating result related to resultCode [{}]", resultCode);
    if (explicitResult != null) {
        Result ret = explicitResult;
        explicitResult = null;
        return ret;
    }
    ActionConfig config = proxy.getConfig();
    Map<String, ResultConfig> results = config.getResults();
    ResultConfig resultConfig = null;
    try {
        resultConfig = results.get(resultCode);
    } catch (NullPointerException e) {
        LOG.debug("Got NPE trying to read result configuration for resultCode [{}]", resultCode);
    }
    if (resultConfig == null) {
        // If no result is found for the given resultCode, try to get a wildcard '*' match.
        resultConfig = results.get("*");
    }
    if (resultConfig != null) {
        try {
            return objectFactory.buildResult(resultConfig, invocationContext.getContextMap());
        } catch (Exception e) {
            LOG.error("There was an exception while instantiating the result of type {}", resultConfig.getClassName(), e);
            throw new StrutsException(e, resultConfig);
        }
    } else if (resultCode != null && !Action.NONE.equals(resultCode) && unknownHandlerManager.hasUnknownHandlers()) {
        return unknownHandlerManager.handleUnknownResult(invocationContext, proxy.getActionName(), proxy.getConfig(), resultCode);
    }
    return null;
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) StrutsException(org.apache.struts2.StrutsException) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) NoSuchPropertyException(ognl.NoSuchPropertyException) StrutsException(org.apache.struts2.StrutsException) MethodFailedException(ognl.MethodFailedException)

Example 24 with StrutsException

use of org.apache.struts2.StrutsException in project entando-core by entando.

the class ApsActionParamComponent method end.

@Override
public boolean end(Writer writer, String body) {
    String actionParam = ApsRequestParamsUtil.createApsActionParam(this.getAction(), this.getParams());
    if (this.getVar() != null) {
        this.getStack().getContext().put(this.getVar(), actionParam);
        // add to the request and page scopes as well
        _req.setAttribute(this.getVar(), actionParam);
    } else {
        try {
            writer.write(actionParam);
        } catch (IOException e) {
            throw new StrutsException("IOError: " + e.getMessage(), e);
        }
    }
    return true;
}
Also used : StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException)

Example 25 with StrutsException

use of org.apache.struts2.StrutsException in project struts-examples by apache.

the class TilesUnknownHandler method handleUnknownResult.

public Result handleUnknownResult(ActionContext actionContext, String actionName, ActionConfig actionConfig, String resultCode) throws StrutsException {
    ServletContext servletContext = ServletActionContext.getServletContext();
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    ServletApplicationContext context = new ServletApplicationContext(servletContext);
    TilesContainer container = TilesAccess.getContainer(context);
    String namespace = ServletActionContext.getActionMapping().getNamespace();
    Set<String> definitions = buildDefinitionNames(namespace, actionName, resultCode);
    for (String definition : definitions) {
        LOG.debug("Looking for tiles definition: {}", definition);
        if (container.isValidDefinition(definition, new ServletRequest(context, request, response))) {
            return new TilesResult(definition);
        }
    }
    LOG.warn("Couldn't find tiles definition for namespace {}, action name {} and result code {}", namespace, actionName, resultCode);
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.apache.tiles.request.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletApplicationContext(org.apache.tiles.request.servlet.ServletApplicationContext) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) TilesResult(org.apache.struts2.views.tiles.TilesResult) TilesContainer(org.apache.tiles.TilesContainer)

Aggregations

StrutsException (org.apache.struts2.StrutsException)46 IOException (java.io.IOException)17 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)9 ArrayList (java.util.ArrayList)7 InputStream (java.io.InputStream)6 URL (java.net.URL)6 ValueStack (com.opensymphony.xwork2.util.ValueStack)5 List (java.util.List)4 ServletContext (javax.servlet.ServletContext)4 ActionContext (com.opensymphony.xwork2.ActionContext)3 IntrospectionException (java.beans.IntrospectionException)3 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 CompoundRoot (com.opensymphony.xwork2.util.CompoundRoot)2 File (java.io.File)2 Method (java.lang.reflect.Method)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Properties (java.util.Properties)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2