Skip to content

Commit

Permalink
HomePage interceptor and some refactoring done.
Browse files Browse the repository at this point in the history
  • Loading branch information
amaron committed Aug 12, 2012
1 parent 2292625 commit 31abc90
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 231 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TwitMini.controller;
package TwitMini.configuration;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
Expand All @@ -20,7 +20,7 @@


@Configuration
public class AppConfig {
public class MintyConfig {

@Bean
public JdbcTemplate jdbcTemplate() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/TwitMini/controller/HomePageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public ModelAndView allTweets(){

@RequestMapping(value="/getMorePublicTweets.json", method= RequestMethod.GET)
@ResponseBody
public List<TweetData> getMorePublicTweets(@RequestParam final int offset, @RequestParam final int limit){
return tweetStore.listAllTweets(offset,limit);
public List<TweetData> getMorePublicTweets(@RequestParam final int offset){
return tweetStore.listAllTweets(offset,10);

}

Expand Down
22 changes: 10 additions & 12 deletions src/main/java/TwitMini/controller/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,26 @@ public LoginController(JdbcTemplate db, UserService userService, Logger logger)
}

@RequestMapping("/")
public ModelAndView index(HttpSession Session) {
String userName = (String) Session.getAttribute("userName");
if(userName==null)
public ModelAndView index() {

return new ModelAndView("index"); // will change it to specific register page with only register
else return new ModelAndView("redirect:/home");


}

@RequestMapping("/index")
public ModelAndView indexHome(HttpSession Session) {
String userName = (String) Session.getAttribute("userName");
if(userName==null)
public ModelAndView indexHome() {

return new ModelAndView("index"); // will change it to specific register page with only register
else return new ModelAndView("redirect:/home");


}

@RequestMapping(value = "/user/login", method = RequestMethod.GET)
public ModelAndView loginForm(HttpSession Session) {
String userName = (String) Session.getAttribute("userName");
if(userName==null)
public ModelAndView loginForm() {

return new ModelAndView("login"); // will change it to specific register page with only register
else return new ModelAndView("redirect:/home");

}


Expand Down
7 changes: 3 additions & 4 deletions src/main/java/TwitMini/controller/SignupController.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ public String checkCaptcha( @RequestParam String challenge,


@RequestMapping(value = "/user/register", method = RequestMethod.GET)
public ModelAndView RegistrationForm(HttpSession Session) {
String userName = (String) Session.getAttribute("userName");
if(userName==null)
public ModelAndView RegistrationForm() {

return new ModelAndView("register"); // will change it to specific register page with only register
else return new ModelAndView("redirect:/home");

}

@RequestMapping(value = "/user/register/usrchk/{username}.json", method = RequestMethod.POST)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TwitMini.controller;
package TwitMini.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/TwitMini/interceptor/HomeInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package TwitMini.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class HomeInterceptor extends HandlerInterceptorAdapter {

private final ThreadLocal<Long> userID;



@Autowired
public HomeInterceptor(@Qualifier("userID") ThreadLocal<Long> userID) {
this.userID = userID;
}

@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {

HttpSession session = request.getSession(false);
if (session != null) {
String userName = (String) session.getAttribute("userName");
if (userName != null) {
userID.set((Long) session.getAttribute("userID"));
System.out.println("checking if homeinterceptor works, user id is "+ userID + request.getRequestURL()+" with value:"+userID.get());
response.sendRedirect("/home");
return false;
}
}

return true;
}
}
93 changes: 50 additions & 43 deletions src/main/webapp/WEB-INF/dispatcher-servlet.xml
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven/>


<context:component-scan base-package="TwitMini"/>

<context:component-scan base-package="TwitMini.controller"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/home/*"/>
<mvc:mapping path="/home"/>
<mvc:mapping path="/tweet"/>
<mvc:mapping path="/tweet/*"/>
<mvc:mapping path="/user/follow/*"/>
<mvc:mapping path='/user/updates.json'/>
<mvc:mapping path='/user/tweet/create.json'/>
<mvc:mapping path='/user/{handle}/tweet/{id}/reply'/>
<mvc:mapping path="/user/unfollow/*"/>
<mvc:mapping path="/user/retweet/{id}"/>

<bean class="TwitMini.controller.AuthInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>




<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<mvc:annotation-driven/>


<context:component-scan base-package="TwitMini"/>

<context:component-scan base-package="TwitMini.controller"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/home/*"/>
<mvc:mapping path="/home"/>
<mvc:mapping path="/tweet"/>
<mvc:mapping path="/tweet/*"/>
<mvc:mapping path="/user/follow/*"/>
<mvc:mapping path='/user/updates.json'/>
<mvc:mapping path='/user/tweet/create.json'/>
<mvc:mapping path='/user/{handle}/tweet/{id}/reply'/>
<mvc:mapping path="/user/unfollow/*"/>
<mvc:mapping path="/user/retweet/{id}"/>

<bean class="TwitMini.interceptor.AuthInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/index"/>
<mvc:mapping path="/"/>
<mvc:mapping path="/user/login"/>
<mvc:mapping path="/user/register"/>
<bean class="TwitMini.interceptor.HomeInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>




<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4 changes: 2 additions & 2 deletions src/main/webapp/WEB-INF/jsp/profileview.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
<script type="text/javascript">
var cur_offset_tweets=10;
var cur_handle='${User.username}';
function getMoreMentions(){
function getMoreUserTweets(){
$.ajax({
type: 'GET',
Expand Down Expand Up @@ -315,7 +315,7 @@
</div>

<div class="span9">
<button class="btn btn-success" id="moreTweetsBtn" onclick="getMoreMentions();return false">load more...</button>
<button class="btn btn-success" id="moreTweetsBtn" onclick="getMoreUserTweets();return false">load more...</button>
</div>


Expand Down
Loading

0 comments on commit 31abc90

Please sign in to comment.