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; |
| 7 | import eu.mulk.mulkcms2.benki.users.User; |
| 8 | import io.quarkus.panache.common.Sort; |
| 9 | import io.quarkus.qute.Template; |
| 10 | import io.quarkus.qute.TemplateExtension; |
| 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 | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 14 | import io.quarkus.security.identity.SecurityIdentity; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 15 | import java.io.IOException; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 16 | import java.net.URI; |
| 17 | import java.net.URISyntaxException; |
| 18 | import java.time.OffsetDateTime; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 19 | import java.time.format.DateTimeFormatter; |
| 20 | import java.time.format.FormatStyle; |
| 21 | import java.time.temporal.TemporalAccessor; |
| 22 | import java.util.List; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 23 | import java.util.Set; |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame^] | 24 | import javax.annotation.CheckForNull; |
| 25 | import javax.annotation.Nullable; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 26 | import javax.inject.Inject; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 27 | import javax.json.JsonObject; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 28 | import javax.json.spi.JsonProvider; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 29 | import javax.transaction.Transactional; |
| 30 | import javax.validation.constraints.NotEmpty; |
| 31 | import javax.validation.constraints.NotNull; |
| 32 | import javax.validation.constraints.Pattern; |
| 33 | import javax.ws.rs.BadRequestException; |
| 34 | import javax.ws.rs.FormParam; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 35 | import javax.ws.rs.GET; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 36 | import javax.ws.rs.POST; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 37 | import javax.ws.rs.Path; |
| 38 | import javax.ws.rs.Produces; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 39 | import javax.ws.rs.QueryParam; |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 40 | import javax.ws.rs.core.Response; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 41 | import org.jboss.logging.Logger; |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 42 | import org.jsoup.Jsoup; |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 43 | |
| 44 | @Path("/bookmarks") |
| 45 | public class BookmarkResource { |
| 46 | |
| 47 | private static Logger log = Logger.getLogger(BookmarkResource.class); |
| 48 | |
| 49 | private static DateTimeFormatter htmlDateFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; |
| 50 | |
| 51 | private static DateTimeFormatter humanDateFormatter = |
| 52 | DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT); |
| 53 | |
| 54 | private static JsonProvider jsonProvider = JsonProvider.provider(); |
| 55 | |
| 56 | @ResourcePath("benki/bookmarks/bookmarkList.html") |
| 57 | @Inject |
| 58 | Template bookmarkList; |
| 59 | |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame^] | 60 | @ResourcePath("benki/bookmarks/newBookmark.html") |
| 61 | @Inject |
| 62 | Template newBookmark; |
| 63 | |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 64 | @Inject SecurityIdentity identity; |
| 65 | |
| 66 | @GET |
| 67 | @Produces(TEXT_HTML) |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 68 | public TemplateInstance getIndex() { |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 69 | List<Bookmark> bookmarks; |
| 70 | if (identity.isAnonymous()) { |
| 71 | Role world = Role.find("from Role r join r.tags tag where tag = 'world'").singleResult(); |
| 72 | bookmarks = |
| 73 | Bookmark.find( |
| 74 | "select bm from Bookmark bm join bm.targets target left join fetch bm.owner where target = ?1", |
| 75 | Sort.by("date").descending(), |
| 76 | world) |
| 77 | .list(); |
| 78 | } else { |
| 79 | var userName = identity.getPrincipal().getName(); |
| 80 | User user = |
| 81 | User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult(); |
| 82 | bookmarks = |
| 83 | Bookmark.find( |
| 84 | "select bm from BenkiUser u inner join u.visibleBookmarks bm left join fetch bm.owner where u.id = ?1", |
| 85 | Sort.by("date").descending(), |
| 86 | user.id) |
| 87 | .list(); |
| 88 | } |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 89 | return bookmarkList.data("bookmarks", bookmarks).data("authenticated", !identity.isAnonymous()); |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Matthias Andreas Benkard | fc427e2 | 2020-02-13 05:00:54 +0100 | [diff] [blame^] | 92 | @GET |
| 93 | @Authenticated |
| 94 | @Path("new") |
| 95 | @Produces(TEXT_HTML) |
| 96 | public TemplateInstance getNewBookmarkForm( |
| 97 | @QueryParam("uri") @CheckForNull String uri, |
| 98 | @QueryParam("title") @CheckForNull String title, |
| 99 | @QueryParam("description") @CheckForNull String description) { |
| 100 | return newBookmark.data("uri", uri).data("title", title).data("description", description); |
| 101 | } |
| 102 | |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 103 | @POST |
| 104 | @Transactional |
Matthias Andreas Benkard | f20e9b9 | 2020-02-10 21:14:57 +0100 | [diff] [blame] | 105 | @Authenticated |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 106 | public Response postBookmark( |
| 107 | @FormParam("uri") URI uri, |
| 108 | @FormParam("title") @NotEmpty String title, |
| 109 | @FormParam("description") String description, |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 110 | @FormParam("visibility") @NotNull @Pattern(regexp = "public|semiprivate|private") |
| 111 | String visibility) |
Matthias Andreas Benkard | bca9461 | 2020-02-10 20:58:13 +0100 | [diff] [blame] | 112 | throws URISyntaxException { |
| 113 | |
| 114 | var userName = identity.getPrincipal().getName(); |
| 115 | User user = |
| 116 | User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult(); |
| 117 | |
| 118 | var bookmark = new Bookmark(); |
| 119 | bookmark.uri = uri.toString(); |
| 120 | bookmark.title = title; |
| 121 | bookmark.tags = Set.of(); |
| 122 | bookmark.description = description != null ? description : ""; |
| 123 | bookmark.owner = user; |
| 124 | bookmark.date = OffsetDateTime.now(); |
| 125 | |
| 126 | if (visibility.equals("public")) { |
| 127 | Role world = Role.find("from Role r join r.tags tag where tag = 'world'").singleResult(); |
| 128 | bookmark.targets = Set.of(world); |
| 129 | } else if (visibility.equals("semiprivate")) { |
| 130 | bookmark.targets = Set.copyOf(user.defaultTargets); |
| 131 | } else if (!visibility.equals("private")) { |
| 132 | throw new BadRequestException(String.format("invalid visibility ā%sā", visibility)); |
| 133 | } |
| 134 | |
| 135 | bookmark.persistAndFlush(); |
| 136 | |
| 137 | return Response.seeOther(new URI("/bookmarks")).build(); |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Matthias Andreas Benkard | 35e14e4 | 2020-02-12 06:20:05 +0100 | [diff] [blame] | 140 | @GET |
| 141 | @Path("page-info") |
| 142 | @Authenticated |
| 143 | @Produces(APPLICATION_JSON) |
| 144 | public JsonObject getPageInfo(@QueryParam("uri") URI uri) throws IOException { |
| 145 | var document = Jsoup.connect(uri.toString()).get(); |
| 146 | return jsonProvider.createObjectBuilder().add("title", document.title()).build(); |
| 147 | } |
| 148 | |
Matthias Andreas Benkard | 2d4f92e | 2020-02-09 16:15:07 +0100 | [diff] [blame] | 149 | @TemplateExtension |
| 150 | static String humanDateTime(TemporalAccessor x) { |
| 151 | return humanDateFormatter.format(x); |
| 152 | } |
| 153 | |
| 154 | @TemplateExtension |
| 155 | static String htmlDateTime(TemporalAccessor x) { |
| 156 | return htmlDateFormatter.format(x); |
| 157 | } |
| 158 | } |