Search in sources :

Example 1 with Chunk

use of com.x5.template.Chunk in project camel by apache.

the class ChunkEndpoint method createChunk.

/**
     * Create a Chunk template
     *
     * @param resourceReader Reader used to get template
     * @param theme The theme
     * @return Chunk
     */
private Chunk createChunk(Reader resourceReader, Theme theme, boolean fromTemplate) throws IOException {
    ClassLoader oldcl = Thread.currentThread().getContextClassLoader();
    try {
        ClassLoader apcl = getCamelContext().getApplicationContextClassLoader();
        if (apcl != null) {
            Thread.currentThread().setContextClassLoader(apcl);
        }
        Chunk newChunk;
        if (fromTemplate) {
            newChunk = theme.makeChunk();
            String targetString = IOUtils.toString(resourceReader);
            newChunk.append(targetString);
        } else {
            String targetString = IOUtils.toString(resourceReader);
            newChunk = theme.makeChunk(targetString);
        }
        return newChunk;
    } finally {
        resourceReader.close();
        if (oldcl != null) {
            Thread.currentThread().setContextClassLoader(oldcl);
        }
    }
}
Also used : Chunk(com.x5.template.Chunk)

Example 2 with Chunk

use of com.x5.template.Chunk in project camel by apache.

the class ChunkEndpoint method onExchange.

@Override
protected void onExchange(Exchange exchange) throws Exception {
    boolean fromTemplate;
    String newResourceUri = exchange.getIn().getHeader(CHUNK_RESOURCE_URI, String.class);
    if (newResourceUri == null) {
        String newTemplate = exchange.getIn().getHeader(CHUNK_TEMPLATE, String.class);
        Chunk newChunk;
        if (newTemplate == null) {
            fromTemplate = false;
            newChunk = getOrCreateChunk(theme, fromTemplate);
        } else {
            fromTemplate = true;
            newChunk = createChunk(new StringReader(newTemplate), theme, fromTemplate);
            exchange.getIn().removeHeader(CHUNK_TEMPLATE);
        }
        // Execute Chunk
        Map<String, Object> variableMap = ExchangeHelper.createVariableMap(exchange);
        StringWriter writer = new StringWriter();
        newChunk.putAll(variableMap);
        newChunk.render(writer);
        writer.flush();
        // Fill out message
        Message out = exchange.getOut();
        out.setBody(newChunk.toString());
        out.setHeaders(exchange.getIn().getHeaders());
        out.setAttachments(exchange.getIn().getAttachments());
    } else {
        exchange.getIn().removeHeader(ChunkConstants.CHUNK_RESOURCE_URI);
        ChunkEndpoint newEndpoint = getCamelContext().getEndpoint(CHUNK_ENDPOINT_URI_PREFIX + newResourceUri, ChunkEndpoint.class);
        newEndpoint.onExchange(exchange);
    }
}
Also used : StringWriter(java.io.StringWriter) Message(org.apache.camel.Message) StringReader(java.io.StringReader) Chunk(com.x5.template.Chunk)

Aggregations

Chunk (com.x5.template.Chunk)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Message (org.apache.camel.Message)1