001    package com.khubla.pragmatach.framework.controller.impl.template;
002    
003    import java.io.ByteArrayInputStream;
004    import java.io.ByteArrayOutputStream;
005    import java.io.InputStream;
006    import java.io.OutputStream;
007    import java.util.Map;
008    import java.util.Map.Entry;
009    
010    import javax.servlet.http.HttpServletResponse;
011    
012    import org.apache.commons.io.IOUtils;
013    import org.apache.commons.lang.StringUtils;
014    
015    import com.khubla.pragmatach.framework.api.PragmatachException;
016    import com.khubla.pragmatach.framework.controller.impl.AbstractResponse;
017    
018    /**
019     * @author tome
020     */
021    public class SimpleTemplateResponse extends AbstractResponse {
022            /**
023             * InputStream
024             */
025            private final InputStream resourceInputStream;
026            /**
027             * parameters
028             */
029            private final Map<String, String> parameters;
030            private static final String ESCAPE = "$";
031    
032            public static void template(InputStream templateInputStream,
033                            OutputStream outputStream, Map<String, String> substitutions)
034                            throws PragmatachException {
035                    try {
036                            /*
037                             * read the entire InputStream
038                             */
039                            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
040                            IOUtils.copy(templateInputStream, baos);
041                            /*
042                             * do substitutions
043                             */
044                            String renderedTemplate = baos.toString("UTF-8");
045                            if (null != substitutions) {
046                                    for (final Entry<String, String> entry : substitutions
047                                                    .entrySet()) {
048                                            renderedTemplate = StringUtils.replace(renderedTemplate,
049                                                            ESCAPE + entry.getKey(), entry.getValue());
050                                    }
051                            }
052                            /*
053                             * write out
054                             */
055                            final ByteArrayInputStream bais = new ByteArrayInputStream(
056                                            renderedTemplate.getBytes("UTF-8"));
057                            IOUtils.copy(bais, outputStream);
058                    } catch (final Exception e) {
059                            throw new PragmatachException("Exception in template", e);
060                    }
061            }
062    
063            /**
064             * ctor
065             */
066            public SimpleTemplateResponse(Map<String, String> cacheHeaders,
067                            String template, Map<String, String> parameters) {
068                    super(cacheHeaders);
069                    this.parameters = parameters;
070                    if (null != template) {
071                            /*
072                             * ok, go for it
073                             */
074                            resourceInputStream = getClass()
075                                            .getResourceAsStream("/" + template);
076                    } else {
077                            resourceInputStream = null;
078                    }
079            }
080    
081            @Override
082            public String getContentType() throws PragmatachException {
083                    return CONTENT_TYPE_HTML;
084            }
085    
086            @Override
087            public Map<String, String> getHeaders() throws PragmatachException {
088                    return getCacheHeaders();
089            }
090    
091            @Override
092            public void render(HttpServletResponse httpServletResponse)
093                            throws PragmatachException {
094                    try {
095                            template(resourceInputStream,
096                                            httpServletResponse.getOutputStream(), parameters);
097                    } catch (final Exception e) {
098                            throw new PragmatachException("Exception in render", e);
099                    }
100            }
101    }