use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class LocatableProperties method load.
@Override
public void load(InputStream in) throws IOException {
try (PropertiesReader pr = new PropertiesReader(new InputStreamReader(in))) {
while (pr.nextProperty()) {
String name = pr.getPropertyName();
String val = pr.getPropertyValue();
int line = pr.getLineNumber();
String desc = convertCommentsToString(pr.getCommentLines());
Location loc = new LocationImpl(desc, location.getURI(), line, 0);
setProperty(name, val, loc);
}
}
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class StrutsJavaConfigurationProvider method register.
@Override
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
Map<String, Object> loadedBeans = new HashMap<>();
// bean
List<BeanConfig> beanConfigs = javaConfig.beans();
if (beanConfigs != null) {
for (BeanConfig bc : beanConfigs) {
if (bc != null) {
registerBean(loadedBeans, builder, bc);
}
}
}
// constant
List<ConstantConfig> constantConfigList = javaConfig.constants();
if (constantConfigList != null) {
for (ConstantConfig constantConf : constantConfigList) {
if (constantConf != null) {
Map<String, String> constantMap = constantConf.getAllAsStringsMap();
for (Entry<String, String> entr : constantMap.entrySet()) {
if (entr.getKey() != null && entr.getValue() != null) {
registerConstant(props, entr.getKey(), entr.getValue());
}
}
}
}
}
// bean-selection
javaConfig.beanSelection().ifPresent(beanSelectionConfig -> {
try {
LOG.debug("Registering bean selection provider {} of type {}", beanSelectionConfig.getName(), beanSelectionConfig.getClazz().getName());
BeanSelectionProvider provider = beanSelectionConfig.getClazz().newInstance();
provider.register(builder, props);
} catch (IllegalAccessException | InstantiationException e) {
throw new ConfigurationException("Unable to load : name:" + beanSelectionConfig.getName() + " class:" + beanSelectionConfig.getClazz().getName());
}
});
// unknown-handler-stack
List<String> unknownHandlers = javaConfig.unknownHandlerStack();
if (unknownHandlers != null) {
List<UnknownHandlerConfig> unknownHandlerStack = new ArrayList<>();
for (String unknownHandler : unknownHandlers) {
Location location = LocationUtils.getLocation(unknownHandler);
unknownHandlerStack.add(new UnknownHandlerConfig(unknownHandler, location));
}
if (!unknownHandlerStack.isEmpty()) {
configuration.setUnknownHandlerStack(unknownHandlerStack);
}
}
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class PortletVelocityResult method executeRenderResult.
/**
* Creates a Velocity context from the action, loads a Velocity template and
* executes the template. Output is written to the servlet output stream.
*
* @param finalLocation the location of the Velocity template
* @param invocation an encapsulation of the action execution state.
* @throws Exception if an error occurs when creating the Velocity context,
* loading or executing the template or writing output to the
* servlet response stream.
*/
public void executeRenderResult(String finalLocation, ActionInvocation invocation) throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletContext servletContext = ServletActionContext.getServletContext();
Servlet servlet = JspSupportServlet.jspSupportServlet;
velocityManager.init(servletContext);
boolean usedJspFactory = false;
PageContext pageContext = ActionContext.getContext().getPageContext();
if (pageContext == null && servlet != null) {
pageContext = jspFactory.getPageContext(servlet, request, response, null, true, 8192, true);
ActionContext.getContext().withPageContext(pageContext);
usedJspFactory = true;
}
try {
String encoding = getEncoding(finalLocation);
String contentType = getContentType(finalLocation);
if (encoding != null) {
contentType = contentType + ";charset=" + encoding;
}
response.setContentType(contentType);
Template t = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, finalLocation, encoding);
Context context = createContext(velocityManager, stack, request, response, finalLocation);
Writer writer = new OutputStreamWriter(response.getOutputStream(), encoding);
t.merge(context, writer);
// always flush the writer (we used to only flush it if this was a
// jspWriter, but someone asked
// to do it all the time (WW-829). Since Velocity support is being
// deprecated, we'll oblige :)
writer.flush();
} catch (Exception e) {
LOG.error("Unable to render Velocity Template, '" + finalLocation + "'", e);
throw e;
} finally {
if (usedJspFactory) {
jspFactory.releasePageContext(pageContext);
}
}
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class ServletActionRedirectResultTest method testBuildResultWithParameter.
public void testBuildResultWithParameter() throws Exception {
ResultConfig resultConfig = new ResultConfig.Builder("", ServletActionRedirectResult.class.getName()).addParam("actionName", "someActionName").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("param1", "value 1").addParam("param2", "value 2").addParam("param3", "value 3").addParam("anchor", "fragment").build();
ObjectFactory factory = container.getInstance(ObjectFactory.class);
ServletActionRedirectResult result = (ServletActionRedirectResult) factory.buildResult(resultConfig, ActionContext.getContext().getContextMap());
assertNotNull(result);
}
use of com.opensymphony.xwork2.util.location.Location in project struts by apache.
the class ServletActionRedirectResultTest method testExpressionParameterInResultWithConditionParseOn.
public void testExpressionParameterInResultWithConditionParseOn() throws Exception {
ResultConfig resultConfig = new ResultConfig.Builder("", "").addParam("actionName", "someActionName").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("statusCode", "333").addParam("param1", "${#value1}").addParam("param2", "${#value2}").addParam("param3", "${#value3}").addParam("anchor", "${#fragment}").build();
ActionContext context = ActionContext.getContext();
ValueStack stack = context.getValueStack();
context.getContextMap().put("value1", "value 1");
context.getContextMap().put("value2", "value 2");
context.getContextMap().put("value3", "value 3");
context.getContextMap().put("namespaceName", "${1-1}");
context.getContextMap().put("actionName", "${1-1}");
context.getContextMap().put("methodName", "${1-1}");
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();
ServletActionRedirectResult result = new ServletActionRedirectResult();
result.setNamespace("/myNamespace${#namespaceName}");
result.setActionName("myAction${#actionName}");
result.setMethod("myMethod${#methodName}");
result.setParse(true);
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).anyTimes();
expect(mockInvocation.getResultCode()).andReturn("myResult").anyTimes();
expect(mockActionProxy.getConfig()).andReturn(actionConfig).anyTimes();
expect(mockInvocation.getInvocationContext()).andReturn(context).anyTimes();
expect(mockInvocation.getStack()).andReturn(stack).anyTimes();
control.replay();
result.setActionMapper(container.getInstance(ActionMapper.class));
result.execute(mockInvocation);
assertEquals("/myNamespace${1-1}/myAction${1-1}!myMethod${1-1}.action?param1=value+1¶m2=value+2¶m3=value+3#fragment", res.getRedirectedUrl());
req = new MockHttpServletRequest();
res = new MockHttpServletResponse();
context.put(ServletActionContext.HTTP_REQUEST, req);
context.put(ServletActionContext.HTTP_RESPONSE, res);
result.execute(mockInvocation);
assertEquals("/myNamespace0/myAction0!myMethod0.action?param1=value+1¶m2=value+2¶m3=value+3#fragment", res.getRedirectedUrl());
control.verify();
}
Aggregations