From b1ef0bed2b6da9d715506fe4d524d70d6f42d406 Mon Sep 17 00:00:00 2001 From: Dmitry Danileyko Date: Mon, 8 Sep 2014 09:17:09 +0400 Subject: [PATCH 1/2] Added lesson 34 - Spring MVC --- Lesson34/.classpath | 31 ++++++ Lesson34/.project | 23 ++++ Lesson34/.settings/org.eclipse.jdt.core.prefs | 5 + Lesson34/.settings/org.eclipse.m2e.core.prefs | 4 + Lesson34/pom.xml | 104 ++++++++++++++++++ Lesson34/src/main/java/lesson34/Stock.java | 26 +++++ .../main/java/lesson34/StockQuoteService.java | 42 +++++++ .../src/main/java/lesson34/config/Config.java | 31 ++++++ .../java/lesson34/controller/Controller.java | 24 ++++ .../main/java/lesson34/init/Initializer.java | 29 +++++ .../src/main/webapp/WEB-INF/views/home.jsp | 33 ++++++ Lesson34/src/main/webapp/resources/style.css | 0 12 files changed, 352 insertions(+) create mode 100644 Lesson34/.classpath create mode 100644 Lesson34/.project create mode 100644 Lesson34/.settings/org.eclipse.jdt.core.prefs create mode 100644 Lesson34/.settings/org.eclipse.m2e.core.prefs create mode 100644 Lesson34/pom.xml create mode 100644 Lesson34/src/main/java/lesson34/Stock.java create mode 100644 Lesson34/src/main/java/lesson34/StockQuoteService.java create mode 100644 Lesson34/src/main/java/lesson34/config/Config.java create mode 100644 Lesson34/src/main/java/lesson34/controller/Controller.java create mode 100644 Lesson34/src/main/java/lesson34/init/Initializer.java create mode 100644 Lesson34/src/main/webapp/WEB-INF/views/home.jsp create mode 100644 Lesson34/src/main/webapp/resources/style.css diff --git a/Lesson34/.classpath b/Lesson34/.classpath new file mode 100644 index 0000000..6d7587a --- /dev/null +++ b/Lesson34/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Lesson34/.project b/Lesson34/.project new file mode 100644 index 0000000..e48b5e3 --- /dev/null +++ b/Lesson34/.project @@ -0,0 +1,23 @@ + + + quote + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/Lesson34/.settings/org.eclipse.jdt.core.prefs b/Lesson34/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..714351a --- /dev/null +++ b/Lesson34/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/Lesson34/.settings/org.eclipse.m2e.core.prefs b/Lesson34/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/Lesson34/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/Lesson34/pom.xml b/Lesson34/pom.xml new file mode 100644 index 0000000..00b9f29 --- /dev/null +++ b/Lesson34/pom.xml @@ -0,0 +1,104 @@ + + + 4.0.0 + + lesson34 + quote + 1.0-SNAPSHOT + war + + quote + http://maven.apache.org + + + 1.6 + 3.1.0.RELEASE + 2.2.2 + + + + + + + + + + + org.springframework + spring-core + 4.0.6.RELEASE + jar + + + commons-logging + commons-logging + + + + + org.springframework + spring-webmvc + 4.0.6.RELEASE + + + aopalliance + aopalliance + + + + + commons-logging + commons-logging + 1.2 + + + org.springframework + spring-context + 4.0.6.RELEASE + jar + + + javax + javaee-api + 7.0 + + + javax.activation + activation + + + + + + + + springsource-milestones + SpringSource Milestones Proxy + https://oss.sonatype.org/content/repositories/springsource-milestones + + + + + quote + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + false + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-war-plugin + 2.3 + + false + + + + + diff --git a/Lesson34/src/main/java/lesson34/Stock.java b/Lesson34/src/main/java/lesson34/Stock.java new file mode 100644 index 0000000..603af5c --- /dev/null +++ b/Lesson34/src/main/java/lesson34/Stock.java @@ -0,0 +1,26 @@ +package lesson34; +public class Stock { + + public Stock(String symbol, String quote) { + this.symbol = symbol; + this.quote = quote; + } + private String symbol; + private String quote; + + public String getSymbol() { + return symbol; + } + + public void setSymbol(String symbol) { + this.symbol = symbol; + } + + public String getQuote() { + return quote; + } + + public void setQuote(String quote) { + this.quote = quote; + } +} diff --git a/Lesson34/src/main/java/lesson34/StockQuoteService.java b/Lesson34/src/main/java/lesson34/StockQuoteService.java new file mode 100644 index 0000000..580aa13 --- /dev/null +++ b/Lesson34/src/main/java/lesson34/StockQuoteService.java @@ -0,0 +1,42 @@ +package lesson34; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.StringTokenizer; +import java.util.concurrent.Future; + +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.AsyncResult; +import org.springframework.stereotype.Service; + +@Service +public class StockQuoteService { + + @Async + public Future getStockQuote(String symbol) throws InterruptedException { + InputStream stream = null; + try { + stream = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + + symbol + "&f=sl1d1t1c1ohgv&e=.csv").openStream(); + } catch (IOException ioe) { + ioe.printStackTrace(); + } + String ticker = null, price = null; + try (InputStreamReader inStream = new InputStreamReader(stream); + BufferedReader buff = new BufferedReader(inStream);) { + String csvString = buff.readLine(); + StringTokenizer tokenizer = new StringTokenizer(csvString, ","); + ticker = tokenizer.nextToken(); + price = tokenizer.nextToken(); + } catch (MalformedURLException e) { + System.out.println("Please check the spelling of the URL: " + e.toString()); + } catch (IOException e1) { + System.out.println("Can't read from the Internet: " + e1.toString()); + } finally { + return new AsyncResult<>(new Stock(ticker, price)); + } + } +} diff --git a/Lesson34/src/main/java/lesson34/config/Config.java b/Lesson34/src/main/java/lesson34/config/Config.java new file mode 100644 index 0000000..d68003c --- /dev/null +++ b/Lesson34/src/main/java/lesson34/config/Config.java @@ -0,0 +1,31 @@ +package lesson34.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +@Configuration +@ComponentScan(basePackages="lesson34") +@EnableWebMvc +public class Config extends WebMvcConfigurerAdapter{ + + @Bean + public ViewResolver getViewResolver(){ + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); + resolver.setPrefix("/WEB-INF/views/"); + resolver.setSuffix(".jsp"); + return resolver; + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + + +} diff --git a/Lesson34/src/main/java/lesson34/controller/Controller.java b/Lesson34/src/main/java/lesson34/controller/Controller.java new file mode 100644 index 0000000..fd612ba --- /dev/null +++ b/Lesson34/src/main/java/lesson34/controller/Controller.java @@ -0,0 +1,24 @@ +package lesson34.controller; + +import lesson34.*; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import javax.servlet.http.HttpServletResponse; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.ModelAndView; + +@RestController +public class Controller { + @RequestMapping(value="/", method = RequestMethod.GET) + public ModelAndView rootView(HttpServletResponse response) throws IOException{ + return new ModelAndView("home"); + } + @RequestMapping(value="/quote", method = RequestMethod.GET) + public @ResponseBody Stock rootView(@RequestParam(value = "symbol", required = false) String symbol, HttpServletResponse response) throws IOException, InterruptedException, ExecutionException{ + return new StockQuoteService().getStockQuote(symbol).get(); + } +} diff --git a/Lesson34/src/main/java/lesson34/init/Initializer.java b/Lesson34/src/main/java/lesson34/init/Initializer.java new file mode 100644 index 0000000..54fe46d --- /dev/null +++ b/Lesson34/src/main/java/lesson34/init/Initializer.java @@ -0,0 +1,29 @@ +package lesson34.init; + +import lesson34.config.*; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.ContextLoaderListener; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +public class Initializer implements WebApplicationInitializer { + @Override + public void onStartup(ServletContext servletContext) throws ServletException { + WebApplicationContext webAppContext = getContext(); + servletContext.addListener(new ContextLoaderListener(webAppContext)); + ServletRegistration.Dynamic dispatcher = servletContext.addServlet("stockquote", new DispatcherServlet(webAppContext)); + dispatcher.setLoadOnStartup(1); + dispatcher.addMapping("/"); + } + + private AnnotationConfigWebApplicationContext getContext() { + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.register(Config.class); + return context; + } + +} diff --git a/Lesson34/src/main/webapp/WEB-INF/views/home.jsp b/Lesson34/src/main/webapp/WEB-INF/views/home.jsp new file mode 100644 index 0000000..b33cec8 --- /dev/null +++ b/Lesson34/src/main/webapp/WEB-INF/views/home.jsp @@ -0,0 +1,33 @@ + + + + Start Page + + + + + +

Please input Symbol (e.g. MSFT, AMZN)

+ + +
+
+ + diff --git a/Lesson34/src/main/webapp/resources/style.css b/Lesson34/src/main/webapp/resources/style.css new file mode 100644 index 0000000..e69de29 From 753aa3e9f1a4e400166b707776176577e67701fb Mon Sep 17 00:00:00 2001 From: Dmitry Danileyko Date: Wed, 24 Sep 2014 00:20:08 +0400 Subject: [PATCH 2/2] Lesson 35 added --- Lesson35/.classpath | 31 +++ Lesson35/.project | 23 +++ Lesson35/.settings/org.eclipse.jdt.core.prefs | 5 + Lesson35/.settings/org.eclipse.m2e.core.prefs | 4 + Lesson35/pom.xml | 188 ++++++++++++++++++ Lesson35/src/main/java/lesson35/Stock.java | 27 +++ .../main/java/lesson35/StockQuoteService.java | 42 ++++ .../main/java/lesson35/config/MvcConfig.java | 30 +++ .../main/java/lesson35/config/RootConfig.java | 10 + .../java/lesson35/config/SecurityConfig.java | 33 +++ .../java/lesson35/controller/Controller.java | 42 ++++ .../main/java/lesson35/init/Initializer.java | 23 +++ .../lesson35/init/SecurityInitializer.java | 7 + .../src/main/webapp/WEB-INF/glassfish-web.xml | 11 + .../src/main/webapp/WEB-INF/views/index.jsp | 43 ++++ .../src/main/webapp/WEB-INF/views/login.jsp | 16 ++ Lesson35/src/main/webapp/resources/style.css | 0 17 files changed, 535 insertions(+) create mode 100644 Lesson35/.classpath create mode 100644 Lesson35/.project create mode 100644 Lesson35/.settings/org.eclipse.jdt.core.prefs create mode 100644 Lesson35/.settings/org.eclipse.m2e.core.prefs create mode 100644 Lesson35/pom.xml create mode 100644 Lesson35/src/main/java/lesson35/Stock.java create mode 100644 Lesson35/src/main/java/lesson35/StockQuoteService.java create mode 100644 Lesson35/src/main/java/lesson35/config/MvcConfig.java create mode 100644 Lesson35/src/main/java/lesson35/config/RootConfig.java create mode 100644 Lesson35/src/main/java/lesson35/config/SecurityConfig.java create mode 100644 Lesson35/src/main/java/lesson35/controller/Controller.java create mode 100644 Lesson35/src/main/java/lesson35/init/Initializer.java create mode 100644 Lesson35/src/main/java/lesson35/init/SecurityInitializer.java create mode 100644 Lesson35/src/main/webapp/WEB-INF/glassfish-web.xml create mode 100644 Lesson35/src/main/webapp/WEB-INF/views/index.jsp create mode 100644 Lesson35/src/main/webapp/WEB-INF/views/login.jsp create mode 100644 Lesson35/src/main/webapp/resources/style.css diff --git a/Lesson35/.classpath b/Lesson35/.classpath new file mode 100644 index 0000000..6d7587a --- /dev/null +++ b/Lesson35/.classpath @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Lesson35/.project b/Lesson35/.project new file mode 100644 index 0000000..4f3c368 --- /dev/null +++ b/Lesson35/.project @@ -0,0 +1,23 @@ + + + lesson34 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/Lesson35/.settings/org.eclipse.jdt.core.prefs b/Lesson35/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..714351a --- /dev/null +++ b/Lesson35/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/Lesson35/.settings/org.eclipse.m2e.core.prefs b/Lesson35/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/Lesson35/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/Lesson35/pom.xml b/Lesson35/pom.xml new file mode 100644 index 0000000..2954d0f --- /dev/null +++ b/Lesson35/pom.xml @@ -0,0 +1,188 @@ + + + 4.0.0 + + lesson35 + lesson35 + 1.0-SNAPSHOT + war + + spring_security + http://maven.apache.org + + + 1.6 + 4.0.6.RELEASE + 2.2.2 + + + + + springsource-milestones + SpringSource Milestones Proxy + https://oss.sonatype.org/content/repositories/springsource-milestones + + + + + lesson35 + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + false + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-war-plugin + 2.3 + + false + + + + + + + org.springframework.security + spring-security-core + 3.2.5.RELEASE + jar + + + org.springframework + spring-aop + + + org.springframework + spring-beans + + + org.springframework + spring-context + + + org.springframework + spring-core + + + org.springframework + spring-expression + + + + + org.springframework + spring-web + 4.0.6.RELEASE + jar + + + org.springframework + spring-aop + + + org.springframework + spring-beans + + + org.springframework + spring-context + + + org.springframework + spring-core + + + + + org.springframework + spring-webmvc + 4.0.6.RELEASE + jar + + + org.springframework + spring-beans + + + org.springframework + spring-context + + + org.springframework + spring-core + + + org.springframework + spring-expression + + + + + org.springframework.security + spring-security-config + 3.2.5.RELEASE + jar + + + org.springframework + spring-aop + + + org.springframework + spring-beans + + + org.springframework + spring-context + + + org.springframework + spring-core + + + + + org.springframework.security + spring-security-web + 3.2.5.RELEASE + jar + + + org.springframework + spring-beans + + + org.springframework + spring-context + + + org.springframework + spring-core + + + org.springframework + spring-expression + + + + + org.springframework + spring-context + 4.0.6.RELEASE + jar + + + javax + javaee-api + 7.0 + jar + + + diff --git a/Lesson35/src/main/java/lesson35/Stock.java b/Lesson35/src/main/java/lesson35/Stock.java new file mode 100644 index 0000000..5e5507c --- /dev/null +++ b/Lesson35/src/main/java/lesson35/Stock.java @@ -0,0 +1,27 @@ +package lesson35; + +public class Stock { + + public Stock(String symbol, String quote) { + this.symbol = symbol; + this.quote = quote; + } + private String symbol; + private String quote; + + public String getSymbol() { + return symbol; + } + + public void setSymbol(String symbol) { + this.symbol = symbol; + } + + public String getQuote() { + return quote; + } + + public void setQuote(String quote) { + this.quote = quote; + } +} diff --git a/Lesson35/src/main/java/lesson35/StockQuoteService.java b/Lesson35/src/main/java/lesson35/StockQuoteService.java new file mode 100644 index 0000000..77415a8 --- /dev/null +++ b/Lesson35/src/main/java/lesson35/StockQuoteService.java @@ -0,0 +1,42 @@ +package lesson35; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.StringTokenizer; +import java.util.concurrent.Future; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.AsyncResult; +import org.springframework.stereotype.Service; + +@Service +public class StockQuoteService { + + @Async + public Future getStockQuote(String symbol) throws InterruptedException { + InputStream stream = null; + try { + stream = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + + symbol + "&f=sl1d1t1c1ohgv&e=.csv").openStream(); + } catch (IOException ioe) { + ioe.printStackTrace(); + } + String ticker = null, price = null; + try (InputStreamReader inStream = new InputStreamReader(stream); + BufferedReader buff = new BufferedReader(inStream);) { + String csvString = buff.readLine(); + StringTokenizer tokenizer = new StringTokenizer(csvString, ","); + ticker = tokenizer.nextToken(); + price = tokenizer.nextToken(); + } catch (MalformedURLException e) { + System.out.println("Please check the spelling of " + "the URL: " + e.toString()); + } catch (IOException e1) { + System.out.println("Can't read from the Internet: " + e1.toString()); + } finally { + return new AsyncResult<>(new Stock(ticker, price)); + } + } +} diff --git a/Lesson35/src/main/java/lesson35/config/MvcConfig.java b/Lesson35/src/main/java/lesson35/config/MvcConfig.java new file mode 100644 index 0000000..cd5e4ad --- /dev/null +++ b/Lesson35/src/main/java/lesson35/config/MvcConfig.java @@ -0,0 +1,30 @@ +package lesson35.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +@Configuration +@ComponentScan(basePackages="lesson35") +@EnableWebMvc +public class MvcConfig extends WebMvcConfigurerAdapter{ + + @Bean + public ViewResolver getViewResolver(){ + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); + resolver.setPrefix("/WEB-INF/views/"); + resolver.setSuffix(".jsp"); + return resolver; + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); + } + +} diff --git a/Lesson35/src/main/java/lesson35/config/RootConfig.java b/Lesson35/src/main/java/lesson35/config/RootConfig.java new file mode 100644 index 0000000..d199053 --- /dev/null +++ b/Lesson35/src/main/java/lesson35/config/RootConfig.java @@ -0,0 +1,10 @@ +package lesson35.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan +public class RootConfig { + +} diff --git a/Lesson35/src/main/java/lesson35/config/SecurityConfig.java b/Lesson35/src/main/java/lesson35/config/SecurityConfig.java new file mode 100644 index 0000000..4302c41 --- /dev/null +++ b/Lesson35/src/main/java/lesson35/config/SecurityConfig.java @@ -0,0 +1,33 @@ +package lesson35.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; + +@Configuration +@EnableWebMvcSecurity +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + public void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().withUser("user").password("password").roles("USER").and() + .withUser("admin").password("adminpassword").roles("ADMIN", "USER"); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http. + csrf().disable(). + authorizeRequests(). + antMatchers("/quote", "/").hasRole("USER"). + antMatchers("/hiddenpage").hasRole("ADMIN"). + antMatchers("/resources/**").permitAll(). + and(). + formLogin().loginPage("/login").permitAll(). + and(). + logout().permitAll().logoutSuccessUrl("/login?logout"); + } + +} diff --git a/Lesson35/src/main/java/lesson35/controller/Controller.java b/Lesson35/src/main/java/lesson35/controller/Controller.java new file mode 100644 index 0000000..8f6ecb5 --- /dev/null +++ b/Lesson35/src/main/java/lesson35/controller/Controller.java @@ -0,0 +1,42 @@ +package lesson35.controller; + +import lesson35.Stock; +import lesson35.StockQuoteService; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import javax.servlet.http.HttpServletResponse; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.ModelAndView; + +@RestController +public class Controller { + @RequestMapping(value = "/", method = RequestMethod.GET) + public ModelAndView mainView(HttpServletResponse response) throws IOException { + ModelAndView mv = new ModelAndView("index"); + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + mv.addObject("currentUser", auth.getName()); + return mv; + } + + @RequestMapping(value = "/quote", method = RequestMethod.GET) + public @ResponseBody + Stock quoteView(@RequestParam(value = "symbol", required = false) String symbol, HttpServletResponse response) throws IOException, InterruptedException, ExecutionException { + return new StockQuoteService().getStockQuote(symbol).get(); + } + + @RequestMapping(value = "/login", method = RequestMethod.GET) + public ModelAndView loginView(HttpServletResponse response) throws IOException { + return new ModelAndView("login"); + } + + @RequestMapping(value = "/hiddenpage", method = RequestMethod.GET) + public String hiddenView(HttpServletResponse response) throws IOException { + return "Hidden Page"; + } +} diff --git a/Lesson35/src/main/java/lesson35/init/Initializer.java b/Lesson35/src/main/java/lesson35/init/Initializer.java new file mode 100644 index 0000000..ccb8fcd --- /dev/null +++ b/Lesson35/src/main/java/lesson35/init/Initializer.java @@ -0,0 +1,23 @@ +package lesson35.init; + +import lesson35.config.MvcConfig; +import lesson35.config.RootConfig; +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer{ + + @Override + protected Class[] getRootConfigClasses() { + return new Class[] {RootConfig.class}; + } + + @Override + protected Class[] getServletConfigClasses() { + return new Class[] {MvcConfig.class}; + } + + @Override + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} diff --git a/Lesson35/src/main/java/lesson35/init/SecurityInitializer.java b/Lesson35/src/main/java/lesson35/init/SecurityInitializer.java new file mode 100644 index 0000000..c7c2ed8 --- /dev/null +++ b/Lesson35/src/main/java/lesson35/init/SecurityInitializer.java @@ -0,0 +1,7 @@ +package lesson35.init; + +import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; + +public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer{ + +} diff --git a/Lesson35/src/main/webapp/WEB-INF/glassfish-web.xml b/Lesson35/src/main/webapp/WEB-INF/glassfish-web.xml new file mode 100644 index 0000000..cf3f93f --- /dev/null +++ b/Lesson35/src/main/webapp/WEB-INF/glassfish-web.xml @@ -0,0 +1,11 @@ + + + + /lesson35 + + + + Keep a copy of the generated servlet class' java code. + + + diff --git a/Lesson35/src/main/webapp/WEB-INF/views/index.jsp b/Lesson35/src/main/webapp/WEB-INF/views/index.jsp new file mode 100644 index 0000000..8c11f29 --- /dev/null +++ b/Lesson35/src/main/webapp/WEB-INF/views/index.jsp @@ -0,0 +1,43 @@ + + + + Start Page + + + + + +

Current user: ${currentUser}. Press to Logout

+

Please input Symbol (e.g. MSFT, AMZN)

+ + +
+
+ + diff --git a/Lesson35/src/main/webapp/WEB-INF/views/login.jsp b/Lesson35/src/main/webapp/WEB-INF/views/login.jsp new file mode 100644 index 0000000..7a712e1 --- /dev/null +++ b/Lesson35/src/main/webapp/WEB-INF/views/login.jsp @@ -0,0 +1,16 @@ + + + + Login Page + + + +
+

Please input login

+ +

Please input password

+ + +
+ + diff --git a/Lesson35/src/main/webapp/resources/style.css b/Lesson35/src/main/webapp/resources/style.css new file mode 100644 index 0000000..e69de29