Commit a50f5cef authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

checkstyle og spotbugs

parent 33291bf7
......@@ -43,7 +43,7 @@ public class TodoModel implements Iterable<TodoList> {
/**
* Replaces an existing TodoList with the same name, or adds it.
*
* @param todoList
* @param todoList the TodoList
* @return the replaced TodoList, or null
*/
public TodoList putTodoList(TodoList todoList) {
......
package todolist.ui;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import com.fasterxml.jackson.databind.ObjectMapper;
import todolist.core.TodoList;
import todolist.core.TodoModel;
import todolist.json.TodoModule;
......@@ -35,9 +35,9 @@ public class RemoteTodoModelAccess implements TodoModelAccess {
private TodoModel getTodoModel() {
if (todoModel == null) {
HttpRequest request = HttpRequest.newBuilder(endpointBaseUri)
.header("Accept", "application/json")
.GET()
.build();
.header("Accept", "application/json")
.GET()
.build();
try {
final HttpResponse<String> response =
HttpClient.newBuilder().build().send(request, HttpResponse.BodyHandlers.ofString());
......@@ -62,6 +62,14 @@ public class RemoteTodoModelAccess implements TodoModelAccess {
return allNames;
}
private String uriParam(String s) {
return URLEncoder.encode(s, StandardCharsets.UTF_8);
}
private URI todoListUri(String name) {
return endpointBaseUri.resolve(uriParam(name));
}
/**
* Gets the TodoList with the given name.
*
......@@ -73,8 +81,8 @@ public class RemoteTodoModelAccess implements TodoModelAccess {
// if existing list has no todo items, try to (re)load
if (oldTodoList == null || (!oldTodoList.iterator().hasNext())) {
HttpRequest request =
HttpRequest.newBuilder(endpointBaseUri.resolve(URLEncoder.encode(name, StandardCharsets.UTF_8)))
.header("Accept", "application/json").GET().build();
HttpRequest.newBuilder(todoListUri(name))
.header("Accept", "application/json").GET().build();
try {
final HttpResponse<String> response =
HttpClient.newBuilder().build().send(request, HttpResponse.BodyHandlers.ofString());
......@@ -93,11 +101,11 @@ public class RemoteTodoModelAccess implements TodoModelAccess {
private void putTodoList(TodoList todoList) {
try {
String json = objectMapper.writeValueAsString(todoList);
HttpRequest request = HttpRequest.newBuilder(endpointBaseUri.resolve(URLEncoder.encode(todoList.getName(), StandardCharsets.UTF_8)))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.PUT(BodyPublishers.ofString(json))
.build();
HttpRequest request = HttpRequest.newBuilder(todoListUri(todoList.getName()))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.PUT(BodyPublishers.ofString(json))
.build();
final HttpResponse<String> response =
HttpClient.newBuilder().build().send(request, HttpResponse.BodyHandlers.ofString());
String responseString = response.body();
......@@ -127,10 +135,10 @@ public class RemoteTodoModelAccess implements TodoModelAccess {
*/
public void removeTodoList(String name) {
try {
HttpRequest request = HttpRequest.newBuilder(endpointBaseUri.resolve(URLEncoder.encode(name, StandardCharsets.UTF_8)))
.header("Accept", "application/json")
.DELETE()
.build();
HttpRequest request = HttpRequest.newBuilder(todoListUri(name))
.header("Accept", "application/json")
.DELETE()
.build();
final HttpResponse<String> response =
HttpClient.newBuilder().build().send(request, HttpResponse.BodyHandlers.ofString());
String responseString = response.body();
......@@ -151,11 +159,11 @@ public class RemoteTodoModelAccess implements TodoModelAccess {
*/
public void renameTodoList(String oldName, String newName) {
try {
HttpRequest request = HttpRequest.newBuilder(endpointBaseUri.resolve(URLEncoder.encode(oldName, StandardCharsets.UTF_8)))
.header("Accept", "application/json")
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(BodyPublishers.ofString("newName=" + URLEncoder.encode(newName, StandardCharsets.UTF_8)))
.build();
HttpRequest request = HttpRequest.newBuilder(todoListUri(oldName))
.header("Accept", "application/json")
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(BodyPublishers.ofString("newName=" + uriParam(newName)))
.build();
final HttpResponse<String> response =
HttpClient.newBuilder().build().send(request, HttpResponse.BodyHandlers.ofString());
String responseString = response.body();
......
......@@ -40,8 +40,8 @@ public class TodoAppController {
// try to read file from home folder first
if (userTodoModelPath != null) {
try {
reader = new FileReader(Paths.get(System.getProperty("user.home"), userTodoModelPath).toFile(),
StandardCharsets.UTF_8);
reader = new FileReader(Paths.get(System.getProperty("user.home"),
userTodoModelPath).toFile(), StandardCharsets.UTF_8);
} catch (IOException ioex) {
System.err.println("Fant ingen " + userTodoModelPath + " på hjemmeområdet");
}
......@@ -80,7 +80,8 @@ public class TodoAppController {
}
if (todoModel == null) {
todoModel = new TodoModel();
TodoList todoList = new TodoList("Helgehandling", new TodoItem().text("Øl"), new TodoItem().text("Pizza"));
TodoList todoList = new TodoList("Helgehandling", new TodoItem().text("Øl"),
new TodoItem().text("Pizza"));
todoModel.addTodoList(todoList);
}
return todoModel;
......
......@@ -56,7 +56,7 @@ public class TodoModelController {
}
});
todoListsView.getSelectionModel().selectedIndexProperty().addListener((prop, oldIndex, newIndex)
-> {
-> {
if (newIndex.intValue() == 0) {
todoListsView.setValue("");
} else {
......
......@@ -41,6 +41,11 @@ public class TodoListResource {
}
}
/**
* Gets the corresponding TodoList.
*
* @return the corresponding TodoList
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public TodoList getTodoList() {
......
......@@ -3,6 +3,8 @@ package todolist.restserver;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.jackson.JacksonFeature;
......@@ -51,11 +53,14 @@ public class TodoConfig extends ResourceConfig {
private static TodoModel createDefaultTodoModel() {
TodoPersistence todoPersistence = new TodoPersistence();
try (InputStream input = TodoConfig.class.getResourceAsStream("default-todomodel.json")) {
return todoPersistence.readTodoModel(new InputStreamReader(input, StandardCharsets.UTF_8));
} catch (IOException e) {
System.out.println("Couldn't read default-todomodel.json, so rigging TodoModel manually ("
+ e + ")");
URL url = TodoConfig.class.getResource("default-todomodel.json");
if (url != null) {
try (Reader reader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) {
return todoPersistence.readTodoModel(reader);
} catch (IOException e) {
System.out.println("Couldn't read default-todomodel.json, so rigging TodoModel manually ("
+ e + ")");
}
}
TodoModel todoModel = new TodoModel();
todoModel.addTodoList(new TodoList("todo1"));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment