Refactoring: project and java package

This commit is contained in:
fecaille
2016-04-06 13:38:50 +02:00
parent 9c27e23174
commit f792354df6
8 changed files with 17 additions and 17 deletions

View File

@@ -0,0 +1,25 @@
package fr.pavnay.demo.springboot.react;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
//@EnableOAuth2Sso
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode("LEGACYHTML5");
resolver.setCacheable(false);
return resolver;
}
}

View File

@@ -0,0 +1,23 @@
package fr.pavnay.demo.springboot.react.domain;
public class Comment {
private final Long id;
private final String author;
private final String text;
public Comment(Long id, String author, String text) {
this.id = id;
this.author = author;
this.text = text;
}
public Long getId() {
return id;
}
public String getAuthor() {
return author;
}
public String getText() {
return text;
}
}

View File

@@ -0,0 +1,40 @@
package fr.pavnay.demo.springboot.react.rest;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
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 fr.pavnay.demo.springboot.react.domain.Comment;
import fr.pavnay.demo.springboot.react.service.CommentService;
@RestController
public class CommentController {
@Autowired
private CommentService service;
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
@RequestMapping(value="/api/comments", method=RequestMethod.GET)
public @ResponseBody List<Comment> comments() {
return service.getAll();
}
@RequestMapping(value="/api/comments", method=RequestMethod.POST)
public @ResponseBody List<Comment> comments(
@RequestParam(value="id", required=true) Long id,
@RequestParam(value="author", required=true) String author,
@RequestParam(value="text", required=true) String text) {
service.add(new Comment(id, author, text));
return service.getAll();
}
}

View File

@@ -0,0 +1,31 @@
package fr.pavnay.demo.springboot.react.service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;
import fr.pavnay.demo.springboot.react.domain.Comment;
@Component
public class CommentService {
private List<Comment> comments = new ArrayList<Comment>();
@PostConstruct
void init() {
comments.addAll(Arrays.asList(new Comment(1L, "Pete Hunt", "This is one comment"),
new Comment(2L, "Jordan Walke", "This is *another* comment")));
}
public void add(Comment comment) {
comments.add(comment);
}
public List<Comment> getAll() {
return comments;
}
}

View File

@@ -0,0 +1,76 @@
package fr.pavnay.demo.springboot.react.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class JavaScriptEngine {
private final ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
public JavaScriptEngine polyfillToNashorn() {
this.loadFromClassPath("static/js/nashorn-polyfill.js");
return this;
}
public JavaScriptEngine eval(String script) {
try {
this.scriptEngine.eval(script);
} catch (ScriptException e) {
throw new IllegalStateException("Failed to eval " + script + "!", e);
}
return this;
}
public JavaScriptEngine loadFromClassPath(String file) {
try {
this.scriptEngine.eval(readFromClassPath(file));
} catch (ScriptException e) {
throw new IllegalStateException("Failed to loadFromClassPath " + file + "!", e);
}
return this;
}
public Object invokeFunction(String functionName, Object... args) {
try {
return ((Invocable) this.scriptEngine).invokeFunction(functionName, args);
} catch (ScriptException | NoSuchMethodException e) {
throw new IllegalArgumentException("Failed to invoke " + functionName, e);
}
}
public <T> T invokeFunction(String functionName, Function<Object, T> converter, Object... args) {
return converter.apply(invokeFunction(functionName, args));
}
private String readFromClassPath(String path) {
try (InputStream in = getClass().getClassLoader().getResourceAsStream(path)) {
if (in == null) {
throw new IllegalArgumentException(path + " is not found!");
}
return copyToString(in, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException("Failed to read " + path, e);
}
}
private static String copyToString(InputStream in, Charset charset) throws IOException {
StringBuilder out = new StringBuilder();
try (InputStreamReader reader = new InputStreamReader(in, charset);) {
char[] buffer = new char[4096];
int bytesRead = -1;
while ((bytesRead = reader.read(buffer)) != -1) {
out.append(buffer, 0, bytesRead);
}
return out.toString();
}
}
}

View File

@@ -0,0 +1,53 @@
package fr.pavnay.demo.springboot.react.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.pavnay.demo.springboot.react.domain.Comment;
import fr.pavnay.demo.springboot.react.service.CommentService;
import fr.pavnay.demo.springboot.react.util.JavaScriptEngine;
@Controller
public class ViewController {
@Autowired
private CommentService service;
@Bean
JavaScriptEngine nashornEngine() {
return new JavaScriptEngine().polyfillToNashorn()
.loadFromClassPath("static/js/vendors.bundle.js")
.loadFromClassPath("static/js/app.bundle.js");
}
@Autowired
ObjectMapper objectMapper;
@Autowired
JavaScriptEngine nashorn;
@RequestMapping("/greeting")
public String greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name,
Model model) {
model.addAttribute("name", name);
return "greeting";
}
@RequestMapping("/index")
public String index(Model model) throws JsonProcessingException {
List<Comment> comments = service.getAll();
String markup = nashorn.invokeFunction("renderServer", String::valueOf, comments);
String data = objectMapper.writeValueAsString(comments);
model.addAttribute("markup", markup);
model.addAttribute("data", data);
return "index";
}
}