/connect-web/src/main/java/org/osforce/connect/web/interceptor/ObjectExposeInterceptor.java

http://focus-sns.googlecode.com/ · Java · 198 lines · 156 code · 22 blank · 20 comment · 17 complexity · 34765b18651294b5bbe8efc018b9dcc3 MD5 · raw file

  1. package org.osforce.connect.web.interceptor;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.apache.commons.lang.StringUtils;
  5. import org.apache.commons.lang.math.NumberUtils;
  6. import org.osforce.connect.entity.system.Project;
  7. import org.osforce.connect.entity.system.ProjectCategory;
  8. import org.osforce.connect.entity.system.Site;
  9. import org.osforce.connect.entity.system.User;
  10. import org.osforce.connect.entity.team.TeamMember;
  11. import org.osforce.connect.service.system.ProjectCategoryService;
  12. import org.osforce.connect.service.system.ProjectService;
  13. import org.osforce.connect.service.system.SiteService;
  14. import org.osforce.connect.service.system.UserService;
  15. import org.osforce.connect.service.team.MemberService;
  16. import org.osforce.connect.web.AttributeKeys;
  17. import org.osforce.connect.web.route.RouteController;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.web.servlet.ModelAndView;
  20. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  21. /**
  22. * Expose object to web context
  23. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  24. * @since 1.1.0
  25. * @create May 18, 2011 - 2:22:31 PM
  26. * <a href="http://www.opensourceforce.org">????</a>
  27. */
  28. public class ObjectExposeInterceptor extends HandlerInterceptorAdapter {
  29. private SiteService siteService;
  30. private UserService userService;
  31. private ProjectService projectService;
  32. private MemberService memberService;
  33. private ProjectCategoryService categoryService;
  34. public ObjectExposeInterceptor() {
  35. }
  36. @Autowired
  37. public void setSiteService(SiteService siteService) {
  38. this.siteService = siteService;
  39. }
  40. @Autowired
  41. public void setUserService(UserService userService) {
  42. this.userService = userService;
  43. }
  44. @Autowired
  45. public void setProjectService(ProjectService projectService) {
  46. this.projectService = projectService;
  47. }
  48. @Autowired
  49. public void setMemberService(MemberService memberService) {
  50. this.memberService = memberService;
  51. }
  52. @Autowired
  53. public void setCategoryService(ProjectCategoryService categoryService) {
  54. this.categoryService = categoryService;
  55. }
  56. @Override
  57. public boolean preHandle(HttpServletRequest request,
  58. HttpServletResponse response, Object handler) throws Exception {
  59. //
  60. exposeContextPath(request);
  61. //
  62. exposeQueryString(request);
  63. //
  64. exposeRequestPath(request);
  65. //
  66. exposeSite(request);
  67. //
  68. exposeTheme(request);
  69. //
  70. exposeProject(request);
  71. //
  72. exposeUser(request);
  73. //
  74. exposeMember(request);
  75. //
  76. return true;
  77. }
  78. @Override
  79. public void postHandle(HttpServletRequest request,
  80. HttpServletResponse response, Object handler,
  81. ModelAndView modelAndView) throws Exception {
  82. //
  83. exposeQualifier(request, handler);
  84. //
  85. exposeProjectCategory(request, handler);
  86. }
  87. protected void exposeContextPath(HttpServletRequest request) {
  88. request.setAttribute(AttributeKeys.BASE_KEY_READABLE, request.getContextPath());
  89. }
  90. protected void exposeQueryString(HttpServletRequest request) {
  91. request.setAttribute(AttributeKeys.QUERY_STRING_KEY_READABLE, request.getQueryString());
  92. }
  93. protected void exposeRequestPath(HttpServletRequest request) {
  94. request.setAttribute(AttributeKeys.REQUEST_PATH_KEY_READABLE, request.getRequestURI());
  95. }
  96. protected void exposeSite(HttpServletRequest request) {
  97. if(request.getAttribute(AttributeKeys.SITE_KEY)==null) {
  98. String domain = request.getServerName();
  99. Site site = siteService.getSite(domain);
  100. request.setAttribute(AttributeKeys.SITE_KEY, site);
  101. request.setAttribute(AttributeKeys.SITE_KEY_READABLE, site);
  102. request.setAttribute(AttributeKeys.SITE_DOMAIN_KEY_READABLE, domain);
  103. }
  104. }
  105. // TODO FIXME
  106. protected void exposeTheme(HttpServletRequest request) {
  107. request.setAttribute(AttributeKeys.THEME_KEY_READABLE, "default");
  108. }
  109. protected void exposeUser(HttpServletRequest request) {
  110. if(request.getSession().getAttribute(AttributeKeys.USER_ID_KEY)!=null) {
  111. Long userId = (Long) request.getSession().getAttribute(AttributeKeys.USER_ID_KEY);
  112. User user = userService.getUser(userId);
  113. request.setAttribute(AttributeKeys.USER_KEY, user);
  114. request.setAttribute(AttributeKeys.USER_KEY_READABLE, user);
  115. }
  116. }
  117. protected void exposeProject(HttpServletRequest request) {
  118. String uniqueId = (String) request.getAttribute(AttributeKeys.PROJECT_UNIQUE_KEY_READABLE);
  119. if(uniqueId==null) {
  120. uniqueId = request.getParameter(AttributeKeys.PROJECT_UNIQUE_KEY_READABLE);
  121. }
  122. if(StringUtils.isNotBlank(uniqueId)) {
  123. Project project = projectService.getProject(uniqueId);
  124. request.setAttribute(AttributeKeys.PROJECT_KEY, project);
  125. request.setAttribute(AttributeKeys.PROJECT_KEY_READABLE, project);
  126. }
  127. }
  128. protected void exposeProjectCategory(HttpServletRequest request, Object handler) {
  129. if(handler instanceof RouteController) {
  130. String categoryIdStr = request.getParameter("categoryId");
  131. Project project = (Project) request.getAttribute(AttributeKeys.PROJECT_KEY);
  132. Site site = (Site) request.getAttribute(AttributeKeys.SITE_KEY);
  133. String categoryCode = (String) request.getAttribute("categoryCode");
  134. ProjectCategory currentCategory = null;
  135. if(StringUtils.isNotBlank(categoryIdStr)) {
  136. currentCategory = categoryService.getProjectCategory(NumberUtils.createLong(categoryIdStr));
  137. } else if(StringUtils.isNotBlank(categoryCode)) {
  138. currentCategory = categoryService.getProjectCategory(site, categoryCode);
  139. } else if(project!=null) {
  140. currentCategory = project.getCategory();
  141. }
  142. request.setAttribute(AttributeKeys.PROJECT_CATEGORY_CURRENT_KEY_READABLE, currentCategory);
  143. }
  144. }
  145. protected void exposeMember(HttpServletRequest request) {
  146. Project project = (Project) request.getAttribute(AttributeKeys.PROJECT_KEY);
  147. User user = (User) request.getAttribute(AttributeKeys.USER_KEY);
  148. if(project!=null && user!=null) {
  149. TeamMember member = memberService.getMember(project, user, Boolean.TRUE);
  150. request.setAttribute(AttributeKeys.TEAM_MEMBER_KEY, member);
  151. request.setAttribute(AttributeKeys.TEAM_MEMBER_KEY_READABLE, member);
  152. }
  153. }
  154. protected void exposeQualifier(HttpServletRequest request, Object handler) {
  155. if(handler instanceof RouteController) {
  156. // expose qualifier
  157. String uniqueId = (String) request.getAttribute(AttributeKeys.PROJECT_UNIQUE_KEY_READABLE);
  158. if(uniqueId==null) {
  159. uniqueId = request.getParameter(AttributeKeys.PROJECT_UNIQUE_KEY_READABLE);
  160. }
  161. if(StringUtils.isNotBlank(uniqueId)) {
  162. Project project = projectService.getProject(uniqueId);
  163. request.setAttribute("qualifier", project.getCategory().getCode());
  164. }
  165. String categoryCode = (String) request.getAttribute("categoryCode");
  166. if(StringUtils.isNotBlank(categoryCode)) {
  167. request.setAttribute("qualifier", categoryCode);
  168. }
  169. String categoryIdStr = request.getParameter("categoryId");
  170. if(StringUtils.isNotBlank(categoryIdStr)) {
  171. ProjectCategory category = categoryService.getProjectCategory(NumberUtils.createLong(categoryIdStr));
  172. request.setAttribute("qualifier", category.getCode());
  173. }
  174. }
  175. }
  176. }