use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class ServletRedirectResultTest method testIncludeParameterInResult.
public void testIncludeParameterInResult() throws Exception {
ResultConfig resultConfig = new ResultConfig.Builder("", "").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("statusCode", "333").addParam("param1", "value 1").addParam("param2", "value 2").addParam("param3", "value 3").build();
ActionContext context = ActionContext.getContext();
MockHttpServletRequest req = new MockHttpServletRequest();
MockHttpServletResponse res = new MockHttpServletResponse();
context.put(ServletActionContext.HTTP_REQUEST, req);
context.put(ServletActionContext.HTTP_RESPONSE, res);
Map<String, ResultConfig> results = new HashMap<>();
results.put("myResult", resultConfig);
ActionConfig actionConfig = new ActionConfig.Builder("", "", "").addResultConfigs(results).build();
ServletRedirectResult result = new ServletRedirectResult();
result.setLocation("/myNamespace/myAction.action");
result.setParse(false);
result.setEncode(false);
result.setPrependServletContext(false);
result.setAnchor("fragment");
result.setUrlHelper(new DefaultUrlHelper());
IMocksControl control = createControl();
ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
expect(mockInvocation.getProxy()).andReturn(mockActionProxy);
expect(mockInvocation.getResultCode()).andReturn("myResult");
expect(mockActionProxy.getConfig()).andReturn(actionConfig);
expect(mockInvocation.getInvocationContext()).andReturn(context);
control.replay();
result.setActionMapper(container.getInstance(ActionMapper.class));
result.execute(mockInvocation);
assertEquals("/myNamespace/myAction.action?param1=value+1¶m2=value+2¶m3=value+3#fragment", res.getRedirectedUrl());
control.verify();
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class StrutsResultSupportTest method testNoParseAndEncode.
public void testNoParseAndEncode() throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new ActionSupport() {
public String getMyLocation() {
return "myLocation.jsp";
}
});
ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
EasyMock.replay(mockActionInvocation);
InternalStrutsResultSupport result = new InternalStrutsResultSupport();
result.setParse(false);
// don't really need this, as encode is only valid when parse is true.
result.setEncode(false);
result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
result.execute(mockActionInvocation);
assertNotNull(result.getInternalLocation());
assertEquals("/pages/myJsp.jsp?location=${myLocation}", result.getInternalLocation());
EasyMock.verify(mockActionInvocation);
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class PostbackResult method makePostbackUri.
protected String makePostbackUri(ActionInvocation invocation) {
ActionContext ctx = invocation.getInvocationContext();
HttpServletRequest request = ctx.getServletRequest();
String postbackUri;
if (actionName != null) {
actionName = conditionalParse(actionName, invocation);
parseLocation = false;
if (namespace == null) {
namespace = invocation.getProxy().getNamespace();
} else {
namespace = conditionalParse(namespace, invocation);
}
if (method == null) {
method = "";
} else {
method = conditionalParse(method, invocation);
}
postbackUri = request.getContextPath() + actionMapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null));
} else {
String location = getLocation();
// Do not prepend if the URL is a FQN
if (!location.matches("^([a-zA-Z]+:)?//.*")) {
// If the URL is relative to the servlet context, prepend the servlet context path
if (prependServletContext && (request.getContextPath() != null) && (request.getContextPath().length() > 0)) {
location = request.getContextPath() + location;
}
}
postbackUri = location;
}
return postbackUri;
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class ServletRedirectResult method doExecute.
/**
* Redirects to the location specified by calling
* {@link HttpServletResponse#sendRedirect(String)}.
*
* @param finalLocation the location to redirect to.
* @param invocation an encapsulation of the action execution state.
* @throws Exception if an error occurs when redirecting.
*/
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
ActionContext ctx = invocation.getInvocationContext();
HttpServletRequest request = ctx.getServletRequest();
HttpServletResponse response = ctx.getServletResponse();
if (isPathUrl(finalLocation)) {
if (!finalLocation.startsWith("/")) {
ActionMapping mapping = actionMapper.getMapping(request, Dispatcher.getInstance().getConfigurationManager());
String namespace = null;
if (mapping != null) {
namespace = mapping.getNamespace();
}
if ((namespace != null) && (namespace.length() > 0) && (!"/".equals(namespace))) {
finalLocation = namespace + "/" + finalLocation;
} else {
finalLocation = "/" + finalLocation;
}
}
// if the URL's are relative to the servlet context, append the servlet context path
if (prependServletContext && (request.getContextPath() != null) && (request.getContextPath().length() > 0)) {
finalLocation = request.getContextPath() + finalLocation;
}
}
ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get(invocation.getResultCode());
if (resultConfig != null) {
Map<String, String> resultConfigParams = resultConfig.getParams();
List<String> prohibitedResultParams = getProhibitedResultParams();
for (Map.Entry<String, String> e : resultConfigParams.entrySet()) {
if (!prohibitedResultParams.contains(e.getKey())) {
Collection<String> values = conditionalParseCollection(e.getValue(), invocation, suppressEmptyParameters);
if (!suppressEmptyParameters || !values.isEmpty()) {
requestParameters.put(e.getKey(), values);
}
}
}
}
StringBuilder tmpLocation = new StringBuilder(finalLocation);
urlHelper.buildParametersString(requestParameters, tmpLocation, "&");
// add the anchor
if (anchor != null) {
tmpLocation.append('#').append(anchor);
}
finalLocation = response.encodeRedirectURL(tmpLocation.toString());
LOG.debug("Redirecting to finalLocation: {}", finalLocation);
sendRedirect(response, finalLocation);
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class StrutsJavaConfigurationProvider method registerBean.
private void registerBean(Map<String, Object> loadedBeans, ContainerBuilder containerBuilder, BeanConfig beanConf) {
try {
if (beanConf.isOnlyStatic()) {
// Force loading of class to detect no class def found
// exceptions
beanConf.getClazz().getDeclaredClasses();
containerBuilder.injectStatics(beanConf.getClazz());
} else {
if (containerBuilder.contains(beanConf.getType(), beanConf.getName())) {
Location loc = LocationUtils.getLocation(loadedBeans.get(beanConf.getType().getName() + beanConf.getName()));
if (throwExceptionOnDuplicateBeans) {
throw new ConfigurationException("Bean type " + beanConf.getType() + " with the name " + beanConf.getName() + " has already been loaded by " + loc, javaConfig);
}
}
// Force loading of class to detect no class def found
// exceptions
beanConf.getClazz().getDeclaredConstructors();
LOG.debug("Loaded type: {} name: {} clazz: {}", beanConf.getType(), beanConf.getName(), beanConf.getClazz());
containerBuilder.factory(beanConf.getType(), beanConf.getName(), new LocatableFactory(beanConf.getName(), beanConf.getType(), beanConf.getClazz(), beanConf.getScope(), javaConfig), beanConf.getScope());
}
loadedBeans.put(beanConf.getType().getName() + beanConf.getName(), javaConfig);
} catch (Throwable ex) {
if (!beanConf.isOptional()) {
throw new ConfigurationException("Unable to load bean: type:" + beanConf.getType() + " class:" + beanConf.getClazz(), ex);
} else {
LOG.debug("Unable to load optional class: {}", beanConf.getClazz());
}
}
}
Aggregations