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; |
| 19 | import java.time.OffsetDateTime; |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 20 | import java.util.Objects; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 21 | import java.util.Set; |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame] | 22 | import javax.annotation.CheckForNull; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 23 | import javax.inject.Inject; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 24 | import javax.json.JsonObject; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 25 | import javax.transaction.Transactional; |
| 26 | import javax.validation.constraints.NotEmpty; |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 27 | import javax.validation.constraints.NotNull; |
Matthias Andreas Benkard | 593765d | 2020-04-18 20:44:07 +0200 | [diff] [blame^] | 28 | import javax.ws.rs.Consumes; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 29 | import javax.ws.rs.FormParam; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 30 | import javax.ws.rs.GET; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 31 | import javax.ws.rs.POST; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 32 | import javax.ws.rs.Path; |
| 33 | import javax.ws.rs.Produces; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 34 | import javax.ws.rs.QueryParam; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 35 | import javax.ws.rs.core.Response; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 36 | import org.jsoup.Jsoup; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 37 | |
| 38 | @Path("/bookmarks") |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame] | 39 | public class BookmarkResource extends PostResource { |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 40 | |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame] | 41 | @ResourcePath("benki/bookmarks/newBookmark.html") |
| 42 | @Inject |
| 43 | Template newBookmark; |
| 44 | |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame] | 45 | public BookmarkResource() { |
| 46 | super(PostFilter.BOOKMARKS_ONLY, "Bookmarks"); |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 49 | @POST |
| 50 | @Transactional |
Matthias Andreas Benkard | f20e9b9 | 2020-02-10 21:14:57 +0100 | [diff] [blame] | 51 | @Authenticated |
Matthias Andreas Benkard | 593765d | 2020-04-18 20:44:07 +0200 | [diff] [blame^] | 52 | @Produces(WILDCARD) |
| 53 | @Consumes({APPLICATION_FORM_URLENCODED, MULTIPART_FORM_DATA}) |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 54 | public Response postBookmark( |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 55 | @FormParam("uri") @NotNull URI uri, |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 56 | @FormParam("title") @NotEmpty String title, |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 57 | @FormParam("description") @CheckForNull String description, |
| 58 | @FormParam("visibility") @NotNull Post.Visibility visibility) |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 59 | throws URISyntaxException { |
| 60 | |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 61 | var user = Objects.requireNonNull(getCurrentUser()); |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 62 | |
| 63 | var bookmark = new Bookmark(); |
| 64 | bookmark.uri = uri.toString(); |
| 65 | bookmark.title = title; |
| 66 | bookmark.tags = Set.of(); |
Matthias Andreas Benkard | 1e7674c | 2020-04-18 20:28:51 +0200 | [diff] [blame] | 67 | bookmark.description = description; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 68 | bookmark.owner = user; |
| 69 | bookmark.date = OffsetDateTime.now(); |
| 70 | |
Matthias Andreas Benkard | 06e6c81 | 2020-04-13 17:01:35 +0200 | [diff] [blame] | 71 | assignPostTargets(visibility, user, bookmark); |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 72 | |
| 73 | bookmark.persistAndFlush(); |
| 74 | |
| 75 | return Response.seeOther(new URI("/bookmarks")).build(); |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 78 | @GET |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame] | 79 | @Authenticated |
| 80 | @Path("new") |
| 81 | @Produces(TEXT_HTML) |
| 82 | public TemplateInstance getNewBookmarkForm( |
| 83 | @QueryParam("uri") @CheckForNull String uri, |
| 84 | @QueryParam("title") @CheckForNull String title, |
| 85 | @QueryParam("description") @CheckForNull String description) { |
| 86 | return newBookmark.data("uri", uri).data("title", title).data("description", description); |
| 87 | } |
| 88 | |
| 89 | @GET |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 90 | @Path("page-info") |
| 91 | @Authenticated |
| 92 | @Produces(APPLICATION_JSON) |
| 93 | public JsonObject getPageInfo(@QueryParam("uri") URI uri) throws IOException { |
| 94 | var document = Jsoup.connect(uri.toString()).get(); |
| 95 | return jsonProvider.createObjectBuilder().add("title", document.title()).build(); |
| 96 | } |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 97 | } |