Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,13 @@
package com.baeldung.sparkjava;
import static spark.Spark.*;
public class HelloWorldService {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello, Baeldung");
get("/hello/:name", (req, res) -> {
return "Hello: " + req.params(":name");
});
}
}
@@ -0,0 +1,64 @@
package com.baeldung.sparkjava;
import static spark.Spark.delete;
import static spark.Spark.get;
import static spark.Spark.options;
import static spark.Spark.post;
import static spark.Spark.put;
import com.google.gson.Gson;
public class SparkRestExample {
public static void main(String[] args) {
final UserService userService = new UserServiceMapImpl();
post("/users", (request, response) -> {
response.type("application/json");
User user = new Gson().fromJson(request.body(), User.class);
userService.addUser(user);
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS));
});
get("/users", (request, response) -> {
response.type("application/json");
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, new Gson().toJsonTree(userService.getUsers())));
});
get("/users/:id", (request, response) -> {
response.type("application/json");
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, new Gson().toJsonTree(userService.getUser(request.params(":id")))));
});
put("/users/:id", (request, response) -> {
response.type("application/json");
User toEdit = new Gson().fromJson(request.body(), User.class);
User editedUser = userService.editUser(toEdit);
if (editedUser != null) {
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, new Gson().toJsonTree(editedUser)));
} else {
return new Gson().toJson(new StandardResponse(StatusResponse.ERROR, new Gson().toJson("User not found or error in edit")));
}
});
delete("/users/:id", (request, response) -> {
response.type("application/json");
userService.deleteUser(request.params(":id"));
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, "user deleted"));
});
options("/users/:id", (request, response) -> {
response.type("application/json");
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, (userService.userExist(request.params(":id"))) ? "User exists" : "User does not exists"));
});
}
}
@@ -0,0 +1,47 @@
package com.baeldung.sparkjava;
import com.google.gson.JsonElement;
public class StandardResponse {
private StatusResponse status;
private String message;
private JsonElement data;
public StandardResponse(StatusResponse status) {
this.status = status;
}
public StandardResponse(StatusResponse status, String message) {
this.status = status;
this.message = message;
}
public StandardResponse(StatusResponse status, JsonElement data) {
this.status = status;
this.data = data;
}
public StatusResponse getStatus() {
return status;
}
public void setStatus(StatusResponse status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public JsonElement getData() {
return data;
}
public void setData(JsonElement data) {
this.data = data;
}
}
@@ -0,0 +1,16 @@
package com.baeldung.sparkjava;
public enum StatusResponse {
SUCCESS("Success"), ERROR("Error");
final private String status;
StatusResponse(String status) {
this.status = status;
}
public String getStatus() {
return status;
}
}
@@ -0,0 +1,59 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.baeldung.sparkjava;
class User {
private String id;
private String firstName;
private String lastName;
private String email;
public User(String id, String firstName, String lastName, String email) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return new StringBuffer().append(getEmail()).toString();
}
}
@@ -0,0 +1,13 @@
package com.baeldung.sparkjava;
public class UserException extends Exception {
public UserException() {
super();
}
public UserException(String message) {
super(message);
}
}
@@ -0,0 +1,17 @@
package com.baeldung.sparkjava;
import java.util.Collection;
public interface UserService {
public void addUser(User user);
public Collection<User> getUsers();
public User getUser(String id);
public User editUser(User user) throws UserException;
public void deleteUser(String id);
public boolean userExist(String id);
}
@@ -0,0 +1,68 @@
package com.baeldung.sparkjava;
import java.util.Collection;
import java.util.HashMap;
public class UserServiceMapImpl implements UserService {
private HashMap<String, User> userMap;
public UserServiceMapImpl() {
userMap = new HashMap<>();
}
@Override
public void addUser(User user) {
userMap.put(user.getId(), user);
}
@Override
public Collection<User> getUsers() {
return userMap.values();
}
@Override
public User getUser(String id) {
return userMap.get(id);
}
@Override
public User editUser(User forEdit) throws UserException {
try {
if (forEdit.getId() == null)
throw new UserException("ID cannot be blank");
User toEdit = userMap.get(forEdit.getId());
if (toEdit == null)
throw new UserException("User not found");
if (forEdit.getEmail() != null) {
toEdit.setEmail(forEdit.getEmail());
}
if (forEdit.getFirstName() != null) {
toEdit.setFirstName(forEdit.getFirstName());
}
if (forEdit.getLastName() != null) {
toEdit.setLastName(forEdit.getLastName());
}
if (forEdit.getId() != null) {
toEdit.setId(forEdit.getId());
}
return toEdit;
} catch (Exception ex) {
throw new UserException(ex.getMessage());
}
}
@Override
public void deleteUser(String id) {
userMap.remove(id);
}
@Override
public boolean userExist(String id) {
return userMap.containsKey(id);
}
}