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