use of org.apache.velocity.context.Context in project camel by apache.
the class VelocityEndpoint method onExchange.
@Override
protected void onExchange(Exchange exchange) throws Exception {
String path = getResourceUri();
ObjectHelper.notNull(path, "resourceUri");
String newResourceUri = exchange.getIn().getHeader(VelocityConstants.VELOCITY_RESOURCE_URI, String.class);
if (newResourceUri != null) {
exchange.getIn().removeHeader(VelocityConstants.VELOCITY_RESOURCE_URI);
log.debug("{} set to {} creating new endpoint to handle exchange", VelocityConstants.VELOCITY_RESOURCE_URI, newResourceUri);
VelocityEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri);
newEndpoint.onExchange(exchange);
return;
}
Reader reader;
String content = exchange.getIn().getHeader(VelocityConstants.VELOCITY_TEMPLATE, String.class);
if (content != null) {
// use content from header
reader = new StringReader(content);
if (log.isDebugEnabled()) {
log.debug("Velocity content read from header {} for endpoint {}", VelocityConstants.VELOCITY_TEMPLATE, getEndpointUri());
}
// remove the header to avoid it being propagated in the routing
exchange.getIn().removeHeader(VelocityConstants.VELOCITY_TEMPLATE);
} else {
if (log.isDebugEnabled()) {
log.debug("Velocity content read from resource {} with resourceUri: {} for endpoint {}", new Object[] { getResourceUri(), path, getEndpointUri() });
}
reader = getEncoding() != null ? new InputStreamReader(getResourceAsInputStream(), getEncoding()) : new InputStreamReader(getResourceAsInputStream());
}
// getResourceAsInputStream also considers the content cache
StringWriter buffer = new StringWriter();
String logTag = getClass().getName();
Context velocityContext = exchange.getIn().getHeader(VelocityConstants.VELOCITY_CONTEXT, Context.class);
if (velocityContext == null) {
Map<String, Object> variableMap = ExchangeHelper.createVariableMap(exchange);
@SuppressWarnings("unchecked") Map<String, Object> supplementalMap = exchange.getIn().getHeader(VelocityConstants.VELOCITY_SUPPLEMENTAL_CONTEXT, Map.class);
if (supplementalMap != null) {
variableMap.putAll(supplementalMap);
}
velocityContext = new VelocityContext(variableMap);
}
// let velocity parse and generate the result in buffer
VelocityEngine engine = getVelocityEngine();
log.debug("Velocity is evaluating using velocity context: {}", velocityContext);
engine.evaluate(velocityContext, buffer, logTag, reader);
// now lets output the results to the exchange
Message out = exchange.getOut();
out.setBody(buffer.toString());
out.setHeaders(exchange.getIn().getHeaders());
out.setAttachments(exchange.getIn().getAttachments());
}
use of org.apache.velocity.context.Context in project OpenOLAT by OpenOLAT.
the class LocalizedXSLTransformer method initTransformer.
/**
* Helper to create XSLT transformer for this instance
*/
private void initTransformer() {
// translate xsl with velocity
Context vcContext = new VelocityContext();
vcContext.put("t", pT);
vcContext.put("staticPath", StaticMediaDispatcher.createStaticURIFor(""));
String xslAsString = "";
try (InputStream xslin = getClass().getResourceAsStream("/org/olat/ims/resources/xsl/" + XSLFILENAME)) {
xslAsString = slurp(xslin);
} catch (IOException e) {
log.error("Could not convert xsl to string!", e);
}
String replacedOutput = evaluateValue(xslAsString, vcContext);
TransformerFactory tfactory = TransformerFactory.newInstance();
XMLReader reader;
try {
reader = XMLReaderFactory.createXMLReader();
reader.setEntityResolver(er);
Source xsltsource = new SAXSource(reader, new InputSource(new StringReader(replacedOutput)));
templates = tfactory.newTemplates(xsltsource);
} catch (SAXException e) {
throw new OLATRuntimeException("Could not initialize transformer!", e);
} catch (TransformerConfigurationException e) {
throw new OLATRuntimeException("Could not initialize transformer (wrong config)!", e);
}
}
use of org.apache.velocity.context.Context in project OpenOLAT by OpenOLAT.
the class CourseCreationConfiguration method getSinglePageText.
/**
* @param translator
* @return single page content
*/
public String getSinglePageText(Translator translator) {
VelocityContainer vc = new VelocityContainer("singlePageTemplate", CourseCreationHelper.class, "singlePageTemplate", translator, null);
vc.contextPut("coursetitle", courseTitle);
// prepare rendering of velocity page for the content of the single page node
GlobalSettings globalSettings = new GlobalSettings() {
public int getFontSize() {
return 100;
}
public AJAXFlags getAjaxFlags() {
return new EmptyAJAXFlags();
}
public boolean isIdDivsForced() {
return false;
}
};
Context context = vc.getContext();
Renderer fr = Renderer.getInstance(vc, translator, null, new RenderResult(), globalSettings);
StringOutput wOut = new StringOutput(10000);
VelocityRenderDecorator vrdec = new VelocityRenderDecorator(fr, vc, wOut);
context.put("r", vrdec);
VelocityHelper.getInstance().mergeContent(vc.getPage(), context, wOut, null);
// free the decorator
context.remove("r");
IOUtils.closeQuietly(vrdec);
return WysiwygFactory.createXHtmlFileContent(wOut.toString(), courseTitle);
}
use of org.apache.velocity.context.Context in project OpenOLAT by OpenOLAT.
the class TaskFolderCallback method getTaskDeletedMailBody.
private String getTaskDeletedMailBody(UserRequest ureq, String fileName, String courseName, String courseLink) {
// grab standard text
String confirmation = translate("task.deleted.body");
Context c = new VelocityContext();
Identity identity = ureq.getIdentity();
c.put("first", identity.getUser().getProperty(UserConstants.FIRSTNAME, getLocale()));
c.put("last", identity.getUser().getProperty(UserConstants.LASTNAME, getLocale()));
c.put("email", UserManager.getInstance().getUserDisplayEmail(identity, ureq.getLocale()));
c.put("filename", fileName);
c.put("coursename", courseName);
c.put("courselink", courseLink);
return VelocityHelper.getInstance().evaluateVTL(confirmation, c);
}
use of org.apache.velocity.context.Context in project OpenOLAT by OpenOLAT.
the class AssessmentObjectComponentRenderer method renderInteraction.
/**
* Render the interaction or the PositionStageObject
* @param renderer
* @param sb
* @param interaction
* @param assessmentItem
* @param itemSessionState
* @param component
* @param ubu
* @param translator
*/
private void renderInteraction(AssessmentRenderer renderer, StringOutput sb, FlowInteraction interaction, ResolvedAssessmentItem resolvedAssessmentItem, ItemSessionState itemSessionState, AssessmentObjectComponent component, URLBuilder ubu, Translator translator) {
Context ctx = new VelocityContext();
ctx.put("interaction", interaction);
String page = getInteractionTemplate(interaction);
renderVelocity(renderer, sb, interaction, ctx, page, resolvedAssessmentItem, itemSessionState, component, ubu, translator);
}
Aggregations