/
BookStoreMinimal.java
155 lines (136 loc) · 4.63 KB
/
BookStoreMinimal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package org.jboss.resteasy.links.test;
import org.jboss.resteasy.links.AddLinks;
import org.jboss.resteasy.links.LinkResource;
import org.jboss.resteasy.links.LinkResources;
import org.jboss.resteasy.links.ParamBinding;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Path("/")
public class BookStoreMinimal {
private Map<String,Book> books = new HashMap<String,Book>();
{
Book book = new Book("foo", "bar");
book.addComment(0, "great book");
book.addComment(1, "terrible book");
books.put(book.getTitle(), book);
}
@Produces({"application/xml", "application/json"})
@AddLinks
@LinkResource(value = Book.class)
@GET
@Path("books")
public Collection<Book> getBooks(){
return books.values();
}
@Consumes({"application/xml", "application/json"})
@LinkResource()
@POST
@Path("books")
public void addBook(Book book){
books.put(book.getTitle(), book);
}
@Produces({"application/xml", "application/json"})
@AddLinks
@LinkResource()
@GET
@Path("book/{id}")
public Book getBook(@PathParam("id") String id){
return books.get(id);
}
@Consumes({"application/xml", "application/json"})
@LinkResource()
@PUT
@Path("book/{id}")
public void updateBook(@PathParam("id") String id, Book book){
books.put(id, book);
}
@LinkResource(value = Book.class)
@DELETE
@Path("book/{id}")
public void deleteBook(@PathParam("id") String id){
books.remove(id);
}
// comments
@Produces({"application/xml", "application/json"})
@AddLinks
@LinkResources({
@LinkResource(value = Book.class, rel = "comments"),
@LinkResource(value = Comment.class)
})
@GET
@Path("book/{id}/comments")
public Collection<Comment> getComments(@PathParam("id") String bookId){
return books.get(bookId).getComments();
}
@Produces({"application/xml", "application/json"})
@AddLinks
@LinkResources({
@LinkResource(value = Book.class, rel="comment-collection"),
@LinkResource(value = Comment.class, rel="collection"),
@LinkResource(value = ScrollableCollection.class, rel = "prev", constraint = "${this.start - this.limit >= 0}", queryParameters = {
@ParamBinding(name = "start", value = "${this.start - this.limit}"),
@ParamBinding(name = "limit", value = "${this.limit}") }),
@LinkResource(value = ScrollableCollection.class, rel = "next", constraint = "${this.start + this.limit < this.totalRecords}", queryParameters = {
@ParamBinding(name = "start", value = "${this.start + this.limit}"),
@ParamBinding(name = "limit", value = "${this.limit}")
}, matrixParameters = {@ParamBinding(name = "query", value = "${this.query}")})
})
@GET
@Path("book/{id}/comment-collection")
public ScrollableCollection getScrollableComments(@PathParam("id") String id, @QueryParam("start") int start, @QueryParam("limit") @DefaultValue("1") int limit, @MatrixParam("query") String query){
List<Comment> comments = new ArrayList<Comment>();
for (Comment comment : books.get(id).getComments()) {
if (comment.getText().contains(query)) {
comments.add(comment);
}
}
start = start < 0 ? 0 : start;
limit = limit < 1 ? 1 : limit;
limit = (start + limit) > comments.size() ? comments.size() - start : limit;
return new ScrollableCollection(id, start, limit, comments.size(), comments.subList(start, start + limit), query);
}
@Produces({"application/xml", "application/json"})
@AddLinks
@LinkResource()
@GET
@Path("book/{id}/comment/{cid}")
public Comment getComment(@PathParam("id") String bookId, @PathParam("cid") int commentId){
return books.get(bookId).getComment(commentId);
}
@Consumes({"application/xml", "application/json"})
@LinkResource()
@POST
@Path("book/{id}/comments")
public void addComment(@PathParam("id") String bookId, Comment comment){
books.get(bookId).getComments().add(comment);
}
@Consumes({"application/xml", "application/json"})
@LinkResource()
@PUT
@Path("book/{id}/comment/{cid}")
public void updateComment(@PathParam("id") String bookId, @PathParam("cid") int commentId, Comment comment){
books.get(bookId).getComment(commentId).setText(comment.getText());
}
@LinkResource(Comment.class)
@DELETE
@Path("book/{id}/comment/{cid}")
public void deleteComment(@PathParam("id") String bookId, @PathParam("cid") int commentId){
Book book = books.get(bookId);
Comment c = book.getComment(commentId);
book.getComments().remove(c);
}
}