Search in sources :

Example 11 with StrutsException

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

the class XWorkBasicConverterTest method testDateConversionWithInvalidValue.

public void testDateConversionWithInvalidValue() {
    Map<String, String> map = new HashMap<>();
    map.put(org.apache.struts2.components.Date.DATETAG_PROPERTY, "yyyy-MM-dd");
    ValueStack stack = new StubValueStack();
    stack.push(new StubTextProvider(map));
    ActionContext context = ActionContext.of(new HashMap<>()).withLocale(new Locale("es_MX", "MX")).withValueStack(stack);
    try {
        basicConverter.convertValue(context.getContextMap(), null, null, null, "asdsd", Date.class);
        fail("StrutsException expected - conversion error occurred");
    } catch (StrutsException e) {
        assertEquals("Could not parse date", e.getMessage());
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) StubValueStack(com.opensymphony.xwork2.StubValueStack) StubTextProvider(com.opensymphony.xwork2.StubTextProvider) StubValueStack(com.opensymphony.xwork2.StubValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 12 with StrutsException

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

the class ExceptionMappingInterceptorTest method testThrownExceptionMatching.

public void testThrownExceptionMatching() throws Exception {
    this.setUpWithExceptionMappings();
    Mock action = new Mock(Action.class);
    Exception exception = new StrutsException("test");
    mockInvocation.expectAndThrow("invoke", exception);
    mockInvocation.matchAndReturn("getAction", action.proxy());
    String result = interceptor.intercept(invocation);
    assertNotNull(stack.findValue("exception"));
    assertEquals(stack.findValue("exception"), exception);
    assertEquals(result, "spooky");
    // is on top of the root
    ExceptionHolder holder = (ExceptionHolder) stack.getRoot().get(0);
    // to invoke the method for unit test
    assertNotNull(holder.getExceptionStack());
}
Also used : StrutsException(org.apache.struts2.StrutsException) Mock(com.mockobjects.dynamic.Mock) StrutsException(org.apache.struts2.StrutsException) ValidationException(com.opensymphony.xwork2.validator.ValidationException)

Example 13 with StrutsException

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

the class PortletUrlRenderer method renderUrl.

/**
 * {@inheritDoc}
 */
public void renderUrl(Writer writer, UrlProvider urlComponent) {
    if (PortletActionContext.getPortletContext() == null) {
        servletRenderer.renderUrl(writer, urlComponent);
        return;
    }
    String result;
    if (isPortletModeChange(urlComponent, PortletActionContext.getRequest().getPortletMode()) && StringUtils.isEmpty(urlComponent.getNamespace())) {
        String mode = urlComponent.getPortletMode();
        PortletMode portletMode = new PortletMode(mode);
        String action = urlComponent.getAction();
        if (StringUtils.isEmpty(action)) {
            action = PortletActionContext.getModeActionMap().get(portletMode).getName();
        }
        String modeNamespace = PortletActionContext.getModeNamespaceMap().get(portletMode);
        result = portletUrlHelper.buildUrl(action, modeNamespace, urlComponent.getMethod(), urlComponent.getParameters(), urlComponent.getPortletUrlType(), mode, urlComponent.getWindowState());
    } else {
        String namespace = urlComponent.determineNamespace(urlComponent.getNamespace(), urlComponent.getStack(), urlComponent.getHttpServletRequest());
        urlComponent.setNamespace(namespace);
        if (onlyActionSpecified(urlComponent)) {
            if (StringUtils.isNotEmpty(urlComponent.getAction())) {
                String action = urlComponent.findString(urlComponent.getAction());
                result = portletUrlHelper.buildUrl(action, urlComponent.getNamespace(), urlComponent.getMethod(), urlComponent.getParameters(), urlComponent.getPortletUrlType(), urlComponent.getPortletMode(), urlComponent.getWindowState());
            } else {
                result = portletUrlHelper.buildUrl(urlComponent.getAction(), urlComponent.getNamespace(), urlComponent.getMethod(), urlComponent.getParameters(), urlComponent.getPortletUrlType(), urlComponent.getPortletMode(), urlComponent.getWindowState());
            }
        } else if (onlyValueSpecified(urlComponent)) {
            result = portletUrlHelper.buildResourceUrl(urlComponent.getValue(), urlComponent.getParameters());
        } else {
            result = createDefaultUrl(urlComponent);
        }
    }
    String anchor = urlComponent.getAnchor();
    if (StringUtils.isNotEmpty(anchor)) {
        result += '#' + urlComponent.findString(anchor);
    }
    String var = urlComponent.getVar();
    if (var != null) {
        urlComponent.putInContext(result);
        // add to the request and page scopes as well
        urlComponent.getHttpServletRequest().setAttribute(var, result);
    } else {
        try {
            writer.write(result);
        } catch (IOException e) {
            throw new StrutsException("IOError: " + e.getMessage(), e);
        }
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException) PortletMode(javax.portlet.PortletMode)

Example 14 with StrutsException

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

the class StrutsOsgiListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    LOG.trace("StrutsOsgiListener attempting to start. ContextInitialized called.  SCE: [{}]", sce);
    if (sce == null) {
        // Better than a NPE.
        throw new StrutsException("ServletContextEvent is null.  Cannot init OSGi platform!");
    }
    ServletContext servletContext = sce.getServletContext();
    LOG.trace("StrutsOsgiListener attempting to start. ServletContext: [{}]", servletContext);
    if (servletContext == null) {
        // Better than a NPE.
        throw new StrutsException("ServletContext is null within the ServletContextEvent.  Cannot init OSGi platform!");
    }
    String platform = servletContext.getInitParameter(PLATFORM_KEY);
    LOG.debug("Defined OSGi platform as [{}] via context-param [{}]", platform, PLATFORM_KEY);
    try {
        osgiHost = OsgiHostFactory.createOsgiHost(platform);
        LOG.trace("StrutsOsgiListener attempting to start. OSGi Host constructed: [{}]", osgiHost);
    } catch (Throwable t) {
        throw new StrutsException("StrutsOsgiListener failed to create an OSGi Host!", t);
    }
    servletContext.setAttribute(OSGI_HOST, osgiHost);
    try {
        osgiHost.init(servletContext);
    } catch (Exception e) {
        throw new StrutsException("StrutsOsgiListener failed to initialize the OSGi Host platform!", e);
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) ServletContext(javax.servlet.ServletContext) StrutsException(org.apache.struts2.StrutsException)

Example 15 with StrutsException

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

the class DefaultDispatcherErrorHandler method init.

public void init(ServletContext ctx) {
    try {
        freemarker.template.Configuration config = freemarkerManager.getConfiguration(ctx);
        template = config.getTemplate("/org/apache/struts2/dispatcher/error.ftl");
    } catch (IOException e) {
        throw new StrutsException(e);
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException)

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