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());
}
}
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());
}
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);
}
}
}
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);
}
}
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);
}
}
Aggregations