/** * Build a FreeMarker template model for the given model Map. * <p>The default implementation builds a {@link AllHttpScopesHashModel}. * @param model the model to use for rendering * @param request current HTTP request * @param response current servlet response * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof */ protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request); fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory); fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel); fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response)); fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper())); fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request)); fmModel.putAll(model); return fmModel; }
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); }
public ContentType(String httpHeaderValue) { this(httpHeaderValue, contentTypeContainsCharset(httpHeaderValue)); }
protected HttpRequestParametersHashModel createRequestParametersHashModel(HttpServletRequest request) { return new HttpRequestParametersHashModel(request); }
/** * Build a FreeMarker {@link HttpSessionHashModel} for the given request, * detecting whether a session already exists and reacting accordingly. * @param request current HTTP request * @param response current servlet response * @return the FreeMarker HttpSessionHashModel */ private HttpSessionHashModel buildSessionModel(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(false); if (session != null) { return new HttpSessionHashModel(session, getObjectWrapper()); } else { return new HttpSessionHashModel(null, request, response, getObjectWrapper()); } }
/** * Create the template loader. The default implementation will create a {@link ClassTemplateLoader} if the template * path starts with {@code "class://"}, a {@link FileTemplateLoader} if the template path starts with * {@code "file://"}, and a {@link WebappTemplateLoader} otherwise. Also, if * {@link Configuration#Configuration(freemarker.template.Version) incompatible_improvements} is 2.3.22 or higher, * it will create a {@link MultiTemplateLoader} if the template path starts with {@code "["}. * * @param templatePath * the template path to create a loader for * @return a newly created template loader */ protected TemplateLoader createTemplateLoader(String templatePath) throws IOException { return InitParamParser.createTemplateLoader(templatePath, getConfiguration(), getClass(), getServletContext()); }
private ContentType getTemplateSpecificContentType(final Template template) { Object contentTypeAttr = template.getCustomAttribute("content_type"); if (contentTypeAttr != null) { // Converted with toString() for backward compatibility. return new ContentType(contentTypeAttr.toString()); } String outputFormatMimeType = template.getOutputFormat().getMimeType(); if (outputFormatMimeType != null) { if (responseCharacterEncoding == ResponseCharacterEncoding.LEGACY) { // In legacy mode we won't call serlvetResponse.setCharacterEncoding(...), so: return new ContentType(outputFormatMimeType + "; charset=" + getTemplateSpecificOutputEncoding(template), true); } else { return new ContentType(outputFormatMimeType, false); } } return null; }
public int size() { return getKeys().size(); }
public boolean isEmpty() throws TemplateModelException { checkSessionExistence(); return session == null || !session.getAttributeNames().hasMoreElements(); } }
/** * Creates a new instance of AllHttpScopesHashModel for handling a single * HTTP servlet request. * @param wrapper the object wrapper to use * @param context the servlet context of the web application * @param request the HTTP servlet request being processed */ public AllHttpScopesHashModel(ObjectWrapper wrapper, ServletContext context, HttpServletRequest request) { setObjectWrapper(wrapper); this.context = context; this.request = request; }
void initializeSessionAndInstallModel(HttpServletRequest request, HttpServletResponse response, HttpSessionHashModel sessionModel, HttpSession session) throws ServletException, IOException { session.setAttribute(ATTR_SESSION_MODEL, sessionModel); initializeSession(request, response); }
private void checkSessionExistence() throws TemplateModelException { if (session == null && request != null) { session = request.getSession(false); if (session != null && servlet != null) { try { servlet.initializeSessionAndInstallModel(request, response, this, session); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new TemplateModelException(e); } } } }
static List parseCommaSeparatedPatterns(String value) throws ParseException { List/*<String>*/ values = parseCommaSeparatedList(value); List/*<Pattern>*/ patterns = new ArrayList(values.size()); for (int i = 0; i < values.size(); i++) { patterns.add(Pattern.compile((String) values.get(i))); } return patterns; }
@Override public void process(Object model, Writer writer) throws TemplateException, IOException { assertEquals(Locale.US, locale); assertTrue(model instanceof AllHttpScopesHashModel); AllHttpScopesHashModel fmModel = (AllHttpScopesHashModel) model; assertEquals("myvalue", fmModel.get("myattr").toString()); } };
/** * Build a FreeMarker template model for the given model Map. * <p>The default implementation builds a {@link AllHttpScopesHashModel}. * @param model the model to use for rendering * @param request current HTTP request * @param response current servlet response * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof */ protected SimpleHash buildTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { AllHttpScopesHashModel fmModel = new AllHttpScopesHashModel(getObjectWrapper(), getServletContext(), request); fmModel.put(FreemarkerServlet.KEY_JSP_TAGLIBS, this.taglibFactory); fmModel.put(FreemarkerServlet.KEY_APPLICATION, this.servletContextHashModel); fmModel.put(FreemarkerServlet.KEY_SESSION, buildSessionModel(request, response)); fmModel.put(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request, response, getObjectWrapper())); fmModel.put(FreemarkerServlet.KEY_REQUEST_PARAMETERS, new HttpRequestParametersHashModel(request)); fmModel.putAll(model); return fmModel; }
/** * Build a FreeMarker {@link HttpSessionHashModel} for the given request, * detecting whether a session already exists and reacting accordingly. * @param request current HTTP request * @param response current servlet response * @return the FreeMarker HttpSessionHashModel */ private HttpSessionHashModel buildSessionModel(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(false); if (session != null) { return new HttpSessionHashModel(session, getObjectWrapper()); } else { return new HttpSessionHashModel(null, request, response, getObjectWrapper()); } }
public TemplateCollectionModel keys() { return new SimpleCollection(getKeys().iterator()); }
@Override public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); }
public TemplateModel get(String key) throws TemplateModelException { checkSessionExistence(); return wrapper.wrap(session != null ? session.getAttribute(key) : null); }
public TemplateCollectionModel values() { final Iterator iter = getKeys().iterator(); return new SimpleCollection( new Iterator() { public boolean hasNext() { return iter.hasNext(); } public Object next() { return request.getParameter((String) iter.next()); } public void remove() { throw new UnsupportedOperationException(); } }); }