Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 1 | package eu.mulk.mulkcms2.benki.bookmarks; |
| 2 | |
Matthias Andreas Benkard | 593765d | 2020-04-18 20:44:07 +0200 | [diff] [blame] | 3 | import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 4 | import static javax.ws.rs.core.MediaType.APPLICATION_JSON; |
Matthias Andreas Benkard | 593765d | 2020-04-18 20:44:07 +0200 | [diff] [blame] | 5 | import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 6 | import static javax.ws.rs.core.MediaType.TEXT_HTML; |
Matthias Andreas Benkard | 593765d | 2020-04-18 20:44:07 +0200 | [diff] [blame] | 7 | import static javax.ws.rs.core.MediaType.WILDCARD; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 8 | |
Matthias Andreas Benkard | 06e6c81 | 2020-04-13 17:01:35 +0200 | [diff] [blame] | 9 | import eu.mulk.mulkcms2.benki.posts.Post; |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame] | 10 | import eu.mulk.mulkcms2.benki.posts.PostFilter; |
| 11 | import eu.mulk.mulkcms2.benki.posts.PostResource; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 12 | import io.quarkus.qute.Template; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 13 | import io.quarkus.qute.TemplateInstance; |
| 14 | import io.quarkus.qute.api.ResourcePath; |
Matthias Andreas Benkard | f20e9b9 | 2020-02-10 21:14:57 +0100 | [diff] [blame] | 15 | import io.quarkus.security.Authenticated; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 16 | import java.io.IOException; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 17 | import java.net.URI; |
| 18 | import java.net.URISyntaxException; |
Matthias Andreas Benkard | cf0fe88 | 2020-04-19 18:33:37 +0200 | [diff] [blame] | 19 | import java.security.NoSuchAlgorithmException; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 20 | import java.time.OffsetDateTime; |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 21 | import java.util.Objects; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 22 | import java.util.Set; |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame] | 23 | import javax.annotation.CheckForNull; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 24 | import javax.inject.Inject; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 25 | import javax.json.JsonObject; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 26 | import javax.transaction.Transactional; |
| 27 | import javax.validation.constraints.NotEmpty; |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 28 | import javax.validation.constraints.NotNull; |
Matthias Andreas Benkard | 593765d | 2020-04-18 20:44:07 +0200 | [diff] [blame] | 29 | import javax.ws.rs.Consumes; |
Matthias Andreas Benkard | bbac0c7 | 2020-04-19 19:38:49 +0200 | [diff] [blame^] | 30 | import javax.ws.rs.ForbiddenException; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 31 | import javax.ws.rs.FormParam; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 32 | import javax.ws.rs.GET; |
Matthias Andreas Benkard | bbac0c7 | 2020-04-19 19:38:49 +0200 | [diff] [blame^] | 33 | import javax.ws.rs.NotFoundException; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 34 | import javax.ws.rs.POST; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 35 | import javax.ws.rs.Path; |
Matthias Andreas Benkard | bbac0c7 | 2020-04-19 19:38:49 +0200 | [diff] [blame^] | 36 | import javax.ws.rs.PathParam; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 37 | import javax.ws.rs.Produces; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 38 | import javax.ws.rs.QueryParam; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 39 | import javax.ws.rs.core.Response; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 40 | import org.jsoup.Jsoup; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 41 | |
| 42 | @Path("/bookmarks") |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame] | 43 | public class BookmarkResource extends PostResource { |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 44 | |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame] | 45 | @ResourcePath("benki/bookmarks/newBookmark.html") |
| 46 | @Inject |
| 47 | Template newBookmark; |
| 48 | |
Matthias Andreas Benkard | cf0fe88 | 2020-04-19 18:33:37 +0200 | [diff] [blame] | 49 | public BookmarkResource() throws NoSuchAlgorithmException { |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame] | 50 | super(PostFilter.BOOKMARKS_ONLY, "Bookmarks"); |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 53 | @POST |
| 54 | @Transactional |
Matthias Andreas Benkard | f20e9b9 | 2020-02-10 21:14:57 +0100 | [diff] [blame] | 55 | @Authenticated |
Matthias Andreas Benkard | 593765d | 2020-04-18 20:44:07 +0200 | [diff] [blame] | 56 | @Produces(WILDCARD) |
| 57 | @Consumes({APPLICATION_FORM_URLENCODED, MULTIPART_FORM_DATA}) |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 58 | public Response postBookmark( |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 59 | @FormParam("uri") @NotNull URI uri, |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 60 | @FormParam("title") @NotEmpty String title, |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 61 | @FormParam("description") @CheckForNull String description, |
| 62 | @FormParam("visibility") @NotNull Post.Visibility visibility) |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 63 | throws URISyntaxException { |
| 64 | |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 65 | var user = Objects.requireNonNull(getCurrentUser()); |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 66 | |
| 67 | var bookmark = new Bookmark(); |
| 68 | bookmark.uri = uri.toString(); |
| 69 | bookmark.title = title; |
| 70 | bookmark.tags = Set.of(); |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 71 | bookmark.description = description; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 72 | bookmark.owner = user; |
| 73 | bookmark.date = OffsetDateTime.now(); |
| 74 | |
Matthias Andreas Benkard | 06e6c81 | 2020-04-13 17:01:35 +0200 | [diff] [blame] | 75 | assignPostTargets(visibility, user, bookmark); |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 76 | |
| 77 | bookmark.persistAndFlush(); |
| 78 | |
| 79 | return Response.seeOther(new URI("/bookmarks")).build(); |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Matthias Andreas Benkard | bbac0c7 | 2020-04-19 19:38:49 +0200 | [diff] [blame^] | 82 | @POST |
| 83 | @Transactional |
| 84 | @Authenticated |
| 85 | @Produces(WILDCARD) |
| 86 | @Consumes({APPLICATION_FORM_URLENCODED, MULTIPART_FORM_DATA}) |
| 87 | @Path("{id}/edit") |
| 88 | public Response patchMessage( |
| 89 | @PathParam("id") int id, |
| 90 | @FormParam("uri") @NotNull URI uri, |
| 91 | @FormParam("title") @NotEmpty String title, |
| 92 | @FormParam("description") @CheckForNull String description, |
| 93 | @FormParam("visibility") Post.Visibility visibility) |
| 94 | throws URISyntaxException { |
| 95 | |
| 96 | var user = Objects.requireNonNull(getCurrentUser()); |
| 97 | |
| 98 | var bookmark = getSession().byId(Bookmark.class).load(id); |
| 99 | |
| 100 | if (bookmark == null) { |
| 101 | throw new NotFoundException(); |
| 102 | } |
| 103 | |
| 104 | if (bookmark.owner == null || !Objects.equals(bookmark.owner.id, user.id)) { |
| 105 | throw new ForbiddenException(); |
| 106 | } |
| 107 | |
| 108 | bookmark.uri = uri.toString(); |
| 109 | bookmark.title = title; |
| 110 | bookmark.tags = Set.of(); |
| 111 | bookmark.description = description; |
| 112 | bookmark.owner = user; |
| 113 | |
| 114 | assignPostTargets(visibility, user, bookmark); |
| 115 | |
| 116 | return Response.seeOther(new URI("/bookmarks")).build(); |
| 117 | } |
| 118 | |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 119 | @GET |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame] | 120 | @Authenticated |
| 121 | @Path("new") |
| 122 | @Produces(TEXT_HTML) |
| 123 | public TemplateInstance getNewBookmarkForm( |
| 124 | @QueryParam("uri") @CheckForNull String uri, |
| 125 | @QueryParam("title") @CheckForNull String title, |
| 126 | @QueryParam("description") @CheckForNull String description) { |
| 127 | return newBookmark.data("uri", uri).data("title", title).data("description", description); |
| 128 | } |
| 129 | |
| 130 | @GET |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 131 | @Path("page-info") |
| 132 | @Authenticated |
| 133 | @Produces(APPLICATION_JSON) |
| 134 | public JsonObject getPageInfo(@QueryParam("uri") URI uri) throws IOException { |
| 135 | var document = Jsoup.connect(uri.toString()).get(); |
| 136 | return jsonProvider.createObjectBuilder().add("title", document.title()).build(); |
| 137 | } |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 138 | } |