PageRenderTime 108ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/b3log-solo/core/src/main/java/org/b3log/solo/processor/UserTemplateProcessor.java

http://b3log-solo.googlecode.com/
Java | 137 lines | 75 code | 12 blank | 50 comment | 2 complexity | 6daf07f3aeaf0e0638b26d5eac34a372 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. /*
  2. * Copyright (c) 2009, 2010, 2011, 2012, B3log Team
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.b3log.solo.processor;
  17. import freemarker.template.Template;
  18. import java.io.IOException;
  19. import java.util.Map;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.b3log.latke.Keys;
  26. import org.b3log.latke.action.AbstractCacheablePageAction;
  27. import org.b3log.latke.annotation.RequestProcessing;
  28. import org.b3log.latke.annotation.RequestProcessor;
  29. import org.b3log.latke.service.LangPropsService;
  30. import org.b3log.latke.servlet.HTTPRequestContext;
  31. import org.b3log.latke.servlet.HTTPRequestMethod;
  32. import org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer;
  33. import org.b3log.latke.util.Locales;
  34. import org.b3log.latke.util.freemarker.Templates;
  35. import org.b3log.solo.model.PageTypes;
  36. import org.b3log.solo.model.Preference;
  37. import org.b3log.solo.processor.renderer.FrontRenderer;
  38. import org.b3log.solo.processor.util.Filler;
  39. import org.b3log.solo.service.PreferenceQueryService;
  40. import org.b3log.solo.util.Skins;
  41. import org.json.JSONObject;
  42. /**
  43. * User template processor.
  44. *
  45. * <p>
  46. * User can add a template (for example "links.ftl") then visits the page ("links.html").
  47. * </p>
  48. *
  49. * <p>
  50. * See <a href="https://code.google.com/p/b3log-solo/issues/detail?id=409">issue 409</a> for more details.
  51. * </p>
  52. *
  53. * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
  54. * @version 1.0.0.2, May 28, 2012
  55. * @since 0.4.5
  56. */
  57. @RequestProcessor
  58. public final class UserTemplateProcessor {
  59. /**
  60. * Logger.
  61. */
  62. private static final Logger LOGGER = Logger.getLogger(ArticleProcessor.class.getName());
  63. /**
  64. * Filler.
  65. */
  66. private Filler filler = Filler.getInstance();
  67. /**
  68. * Preference query service.
  69. */
  70. private PreferenceQueryService preferenceQueryService = PreferenceQueryService.getInstance();
  71. /**
  72. * Language service.
  73. */
  74. private LangPropsService langPropsService = LangPropsService.getInstance();
  75. /**
  76. * Shows the user template page.
  77. *
  78. * @param context the specified context
  79. * @param request the specified HTTP servlet request
  80. * @param response the specified HTTP servlet response
  81. * @throws IOException io exception
  82. */
  83. @RequestProcessing(value = "/*.html", method = HTTPRequestMethod.GET)
  84. public void showPage(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response)
  85. throws IOException {
  86. final String requestURI = request.getRequestURI();
  87. String templateName = StringUtils.substringAfterLast(requestURI, "/");
  88. templateName = StringUtils.substringBefore(templateName, ".") + ".ftl";
  89. LOGGER.log(Level.FINE, "Shows page[requestURI={0}, templateName={1}]", new Object[]{requestURI, templateName});
  90. final AbstractFreeMarkerRenderer renderer = new FrontRenderer();
  91. context.setRenderer(renderer);
  92. renderer.setTemplateName(templateName);
  93. final Map<String, Object> dataModel = renderer.getDataModel();
  94. final Template template = Templates.getTemplate((String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), templateName);
  95. if (null == template) {
  96. try {
  97. response.sendError(HttpServletResponse.SC_NOT_FOUND);
  98. return;
  99. } catch (final IOException ex) {
  100. LOGGER.severe(ex.getMessage());
  101. }
  102. }
  103. try {
  104. final Map<String, String> langs = langPropsService.getAll(Locales.getLocale(request));
  105. dataModel.putAll(langs);
  106. final JSONObject preference = preferenceQueryService.getPreference();
  107. filler.fillBlogHeader(request, dataModel, preference);
  108. filler.fillUserTemplate(template, dataModel, preference);
  109. filler.fillBlogFooter(dataModel, preference);
  110. Skins.fillSkinLangs(preference.optString(Preference.LOCALE_STRING),
  111. (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
  112. request.setAttribute(AbstractCacheablePageAction.CACHED_OID, "No id");
  113. request.setAttribute(AbstractCacheablePageAction.CACHED_TITLE, requestURI);
  114. request.setAttribute(AbstractCacheablePageAction.CACHED_TYPE, langs.get(PageTypes.USER_TEMPLATE_PAGE));
  115. request.setAttribute(AbstractCacheablePageAction.CACHED_LINK, requestURI);
  116. } catch (final Exception e) {
  117. LOGGER.log(Level.SEVERE, e.getMessage(), e);
  118. try {
  119. response.sendError(HttpServletResponse.SC_NOT_FOUND);
  120. } catch (final IOException ex) {
  121. LOGGER.severe(ex.getMessage());
  122. }
  123. }
  124. }
  125. }