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 | |
| 6 | import eu.mulk.mulkcms2.benki.accesscontrol.Role; |
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 eu.mulk.mulkcms2.benki.users.User; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 10 | import io.quarkus.qute.Template; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 11 | import io.quarkus.qute.TemplateInstance; |
| 12 | import io.quarkus.qute.api.ResourcePath; |
Matthias Andreas Benkard | f20e9b9 | 2020-02-10 21:14:57 +0100 | [diff] [blame] | 13 | import io.quarkus.security.Authenticated; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 14 | import java.io.IOException; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 15 | import java.net.URI; |
| 16 | import java.net.URISyntaxException; |
| 17 | import java.time.OffsetDateTime; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 18 | import java.util.Set; |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame] | 19 | import javax.annotation.CheckForNull; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 20 | import javax.inject.Inject; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 21 | import javax.json.JsonObject; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 22 | import javax.transaction.Transactional; |
| 23 | import javax.validation.constraints.NotEmpty; |
| 24 | import javax.validation.constraints.NotNull; |
| 25 | import javax.validation.constraints.Pattern; |
| 26 | import javax.ws.rs.BadRequestException; |
| 27 | import javax.ws.rs.FormParam; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 28 | import javax.ws.rs.GET; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 29 | import javax.ws.rs.POST; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.Produces; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 32 | import javax.ws.rs.QueryParam; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 33 | import javax.ws.rs.core.Response; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 34 | import org.jsoup.Jsoup; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 35 | |
| 36 | @Path("/bookmarks") |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame^] | 37 | public class BookmarkResource extends PostResource { |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 38 | |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame] | 39 | @ResourcePath("benki/bookmarks/newBookmark.html") |
| 40 | @Inject |
| 41 | Template newBookmark; |
| 42 | |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame^] | 43 | public BookmarkResource() { |
| 44 | super(PostFilter.BOOKMARKS_ONLY, "Bookmarks"); |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 47 | @POST |
| 48 | @Transactional |
Matthias Andreas Benkard | f20e9b9 | 2020-02-10 21:14:57 +0100 | [diff] [blame] | 49 | @Authenticated |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 50 | public Response postBookmark( |
| 51 | @FormParam("uri") URI uri, |
| 52 | @FormParam("title") @NotEmpty String title, |
| 53 | @FormParam("description") String description, |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 54 | @FormParam("visibility") @NotNull @Pattern(regexp = "public|semiprivate|private") |
| 55 | String visibility) |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 56 | throws URISyntaxException { |
| 57 | |
| 58 | var userName = identity.getPrincipal().getName(); |
Matthias Andreas Benkard | 088a21f | 2020-02-22 10:52:11 +0100 | [diff] [blame] | 59 | var user = User.findByNickname(userName); |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 60 | |
| 61 | var bookmark = new Bookmark(); |
| 62 | bookmark.uri = uri.toString(); |
| 63 | bookmark.title = title; |
| 64 | bookmark.tags = Set.of(); |
| 65 | bookmark.description = description != null ? description : ""; |
| 66 | bookmark.owner = user; |
| 67 | bookmark.date = OffsetDateTime.now(); |
| 68 | |
| 69 | if (visibility.equals("public")) { |
| 70 | Role world = Role.find("from Role r join r.tags tag where tag = 'world'").singleResult(); |
| 71 | bookmark.targets = Set.of(world); |
| 72 | } else if (visibility.equals("semiprivate")) { |
| 73 | bookmark.targets = Set.copyOf(user.defaultTargets); |
| 74 | } else if (!visibility.equals("private")) { |
| 75 | throw new BadRequestException(String.format("invalid visibility ā%sā", visibility)); |
| 76 | } |
| 77 | |
| 78 | bookmark.persistAndFlush(); |
| 79 | |
| 80 | return Response.seeOther(new URI("/bookmarks")).build(); |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 83 | @GET |
Matthias Andreas Benkard | 4940b29 | 2020-03-29 18:41:07 +0200 | [diff] [blame^] | 84 | @Authenticated |
| 85 | @Path("new") |
| 86 | @Produces(TEXT_HTML) |
| 87 | public TemplateInstance getNewBookmarkForm( |
| 88 | @QueryParam("uri") @CheckForNull String uri, |
| 89 | @QueryParam("title") @CheckForNull String title, |
| 90 | @QueryParam("description") @CheckForNull String description) { |
| 91 | return newBookmark.data("uri", uri).data("title", title).data("description", description); |
| 92 | } |
| 93 | |
| 94 | @GET |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 95 | @Path("page-info") |
| 96 | @Authenticated |
| 97 | @Produces(APPLICATION_JSON) |
| 98 | public JsonObject getPageInfo(@QueryParam("uri") URI uri) throws IOException { |
| 99 | var document = Jsoup.connect(uri.toString()).get(); |
| 100 | return jsonProvider.createObjectBuilder().add("title", document.title()).build(); |
| 101 | } |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 102 | } |