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