@POST @Path("/{id}/comment") @Template(name = "/post/showPosts.ftl") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String addComment(@PathParam("id") String fPostId, @FormParam("message") String fCommentBody) throws IOException { User authUser = (User) securityContext.getUserPrincipal(); Comment comment = new Comment(); comment.setPostId(Integer.parseInt(fPostId)); comment.setMessage(fCommentBody); comment.setUserId((int) authUser.getId()); comment.saveIt(); response.sendRedirect("/post/" + fPostId); return ""; }
/** * Creates enhancing methods for given resource. * * @param resourceClass resource class for which enhancing methods should be created. * @param resourceInstance resource instance for which enhancing methods should be created. May be {@code null}. * @param newMethods list to store new methods into. */ private void createEnhancingMethods(final Class<?> resourceClass, final Object resourceInstance, final List<ModelProcessorUtil.Method> newMethods) { final Template template = resourceClass.getAnnotation(Template.class); if (template != null) { final Class<?> annotatedResourceClass = ModelHelper.getAnnotatedResourceClass(resourceClass); final List<MediaType> produces = MediaTypes .createQualitySourceMediaTypes(annotatedResourceClass.getAnnotation(Produces.class)); final List<MediaType> consumes = MediaTypes.createFrom(annotatedResourceClass.getAnnotation(Consumes.class)); final TemplateInflectorImpl inflector = new TemplateInflectorImpl(template.name(), resourceClass, resourceInstance); newMethods.add(new ModelProcessorUtil.Method(HttpMethod.GET, consumes, produces, inflector)); newMethods.add(new ModelProcessorUtil.Method(IMPLICIT_VIEW_PATH_PARAMETER_TEMPLATE, HttpMethod.GET, consumes, produces, inflector)); } }
@GET @Template(name = "/post/list") public ViewData listAction(@DefaultValue("1") @QueryParam("page") int page) { int postsPerPage = Configuration.POSTS_PER_PAGE; Paginator p = new Paginator(Post.class, postsPerPage, "*").orderBy("created_at desc"); int pageCount = Math.max((int) p.pageCount(), 1); int pageNumber = (page > pageCount) ? pageCount : Math.max(1, page); LazyList posts = p.getPage(pageNumber); ViewData.set("model", posts); HashMap<String, Object> pagination = new HashMap<>(); pagination.put("currentPage", pageNumber); pagination.put("totalPages", pageCount); pagination.put("linkUrl", RESOURCE_PATH); ViewData.set("pagination", pagination); return ViewData; }
@POST @Path("/{id}/edit") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Template(name= "/post/single") public Response editPostAction(@PathParam("id") int id, @FormParam("title") @Pattern(regexp = "^(?!\\s*$).+", message = "empty string") String title, @FormParam("content") @Pattern(regexp = "^(?!\\s*$).+", message = "empty string") String content){ Post post=Post.findById(id); post.setTitle(title); post.setContent(content); post.saveIt(); URI targetURIForRedirection = URI.create(RESOURCE_PATH + post.getId()); return Response.seeOther(targetURIForRedirection).build(); }
@POST @AuthenticationRequired @Path("/edit/{id}") @Template(name = "/post/showPost.ftl") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Post editPost(@PathParam("id") int id, @FormParam("title") String title, @FormParam("body") String body) { Post post = Post.findById(id); if (post != null) { post.setTitle(title); post.setBody(body); post.save(); } return post; }
@GET @Template @RolesAllowed({"user"}) @Path("/secure") public SecureDto allUsers(@Context SecurityContext context) { SecureDto secureDto = new SecureDto(); secureDto.setUsers(dao.all()); secureDto.setCurrentUser((User) context.getUserPrincipal()); return secureDto; }
@GET @Path("/absolute") @Template(name = "/hello.vm") public TreeMap<String, String> getByLocation() { return new TreeMap<String, String>() {{ put("name", "absolute"); }}; }
@GET @Path("/withoutSuffix") @Template(name = "/hello") public TreeMap<String, String> getHelloByName() { return new TreeMap<String, String>() {{ put("name", "withoutSuffix"); }}; }
@GET @Path("/register") @Template(name = "/auth/register.ftl") public ViewData showRegisterForm() { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); return view; }
@GET @Path("/") @Template(name = "/post/showPosts.ftl") public ViewData showIndex() { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); view.posts = Post.findAll(); return view; }
@GET @Path("/{id}") @Template(name = "/user/profile.ftl") public ViewData showUser(@PathParam("id") int id) { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); view.profile = User.findById(id); return view; }
@GET @Path("/{id}/allposts") @Template(name = "/user/posts.ftl") public ViewData showUserposts(@PathParam("id") int id) { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); view.posts = Post.findAll(); view.profile = User.findById(id); return view; }
@GET @Path("/all") @Template(name = "/user/showUsers.ftl") public ViewData showUsers() { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); view.users = User.findAll(); return view; }
@GET @Path("/editprofile") @Template(name = "/user/editprofile.ftl") public ViewData showRegisterForm() { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); return view; }
@GET @Path("/{id}") @Template(name = "/post/showPost.ftl") public ViewData showPost(@PathParam("id") int id) { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); view.post = Post.findById(id); return view; }
@GET @Path("/all") @Template(name = "/post/showPosts.ftl") public ViewData showPosts() { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); view.posts = Post.findAll(); return view; }
@GET @Path("/new") @Template(name = "/post/newPost.ftl") public ViewData newPost() { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); view.post = new Post(); return view; }
@GET @Path("/edit/{id}") @Template(name = "/post/editPost.ftl") public ViewData editPost(@PathParam("id") int id) { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); view.post = Post.findById(id); return view; }
@POST @Path("/") @Template(name = "/post/showPost.ftl") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Post createPost(@FormParam("title") String title, @FormParam("body") String body) { Post post = new Post(); post.setTitle(title); post.setBody(body); post.saveIt(); return post; }
/** * {@inheritDoc} */ @Override public void aroundWriteTo(final WriterInterceptorContext context) throws IOException, WebApplicationException { final Object entity = context.getEntity(); if (!(entity instanceof Viewable) && resourceInfoProvider.get().getResourceMethod() != null) { final Template template = TemplateHelper.getTemplateAnnotation(context.getAnnotations()); if (template != null) { context.setType(Viewable.class); context.setEntity(new Viewable(template.name(), entity)); } } context.proceed(); }
/** * Extract {@link org.glassfish.jersey.server.mvc.Template template} annotation from given list. * * @param annotations list of annotations. * @return {@link org.glassfish.jersey.server.mvc.Template template} annotation or {@code null} if this annotation is not present. */ public static Template getTemplateAnnotation(final Annotation[] annotations) { if (annotations != null && annotations.length > 0) { for (Annotation annotation : annotations) { if (annotation instanceof Template) { return (Template) annotation; } } } return null; }
@GET @Path("/register") @Template(name = "/auth/register") public ViewData showRegisterForm() throws IOException { HttpSession session = request.getSession(true); if (session.getAttribute("userId") != null) { response.sendRedirect("/users/" + (int) session.getAttribute("userId")); } this.flushError(); return ViewData; }
@GET @Path("/signin") @Template(name = "/auth/login") public ViewData showLoginForm() throws IOException { HttpSession session = request.getSession(true); if (session.getAttribute("userId") != null) { response.sendRedirect("/users/" + (int) session.getAttribute("userId")); return ViewData; } this.flushError(); return ViewData; }
@GET @Template(name = "/index/index") public ViewData indexAction() { int postsPerPage = Configuration.POSTS_PER_PAGE; ViewData.set("model", Post.findAll().limit(postsPerPage).orderBy("created_at desc")); return ViewData; }
@GET @Path("/profile") @Template(name = "/user/profile") public ViewData showProfileAction() throws IOException { HttpSession session = request.getSession(true); if (session.getAttribute("userId") == null) { response.sendRedirect("/signin"); } ViewData.set("profile", (User) securityContext.getUserPrincipal()); return ViewData; }
@GET @Template(name = "/user/list") public ViewData showProfilesListAction() { this.ViewData.set("authUser", securityContext.getUserPrincipal()); this.ViewData.set("users", User.findAll().limit(3).orderBy("created_at desc")); return this.ViewData; }
@GET @Path("/{id}") @Template(name = "/user/profile") public Response showUserProfileAction(@PathParam("id") int id) { User user = User.findById(id); if (user == null) { return Response.status(Response.Status.NOT_FOUND).build(); } ViewData.set("profile", user); return Response.ok(ViewData).build(); }
@GET @Path("/{id}") @Template(name = "/post/single") public Response singleAction(@PathParam("id") int id) { Post post = Post.findById(id); if (post == null) { return Response.status(Response.Status.NOT_FOUND).build(); } ViewData.set("model", post); return Response.ok(ViewData).build(); }
@GET @Path("/new") @Template(name = "/post/form") public Response formAction() { ViewData.set("model", new Post()); return Response.ok(ViewData).build(); }
@POST @Path("/new") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Template(name = "/post/single") public Response saveAction(@FormParam("title") @Pattern(regexp = "^(?!\\s*$).+", message = "empty string") String title ,@FormParam("content") @Pattern(regexp = "^(?!\\s*$).+", message = "empty string") String content) { Post post = new Post(); post.setTitle(title); post.setContent(content); post.saveIt(); URI targetURIForRedirection = URI.create(RESOURCE_PATH + post.getId()); return Response.seeOther(targetURIForRedirection).build(); }
@POST @Path("/{id}/delete") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Template(name= "/post/single") public Response deletePostAction(@PathParam("id") @NotNull(message = "not null") int id){ Post post=Post.findById(id); post.delete(); URI targetURIForRedirection = URI.create("/"); return Response.seeOther(targetURIForRedirection).build(); }
@POST @Path("/{id}/comment") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Template(name = "/post/single") public Response commentAction(@PathParam("id") @NotNull(message = "not null") int id ,@FormParam("content") @Pattern(regexp = "^(?!\\s*$).+", message = "empty string") String commentContent) { Post post = Post.findById(id); Comment comment = new Comment(); comment.setContent(commentContent); post.add(comment); post.saveIt(); URI targetURIForRedirection= URI.create("/posts/"+id); return Response.seeOther(targetURIForRedirection).build(); }
@GET @Path("/register/error") @Template(name = "/partials/register/error.ftl") public ViewData showRegisterError() { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); return view; }
@GET @Path("/register/userExists") @Template(name = "/partials/register/userExistsError.ftl") public ViewData showUserExistsError() { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); return new ViewData(); }
@GET @Path("/404") @Template(name = "/404.ftl") public ViewData notFound() throws IOException { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); return view; }
@GET @Path("/{id}") @Template(name = "/post/showPost.ftl") public ViewData showPost(@PathParam("id") int id) throws IOException { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); view.post = Post.findById(id); if ( view.post == null ) { response.sendRedirect("/404" ); } return view; }
@GET @Path("/new") @Template(name = "/post/newPost.ftl") public ViewData newPost() { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); view.post = new Post(); view.post.setTitle(""); view.post.setBody(""); return view; }
@GET @Path("/error") @Template(name = "/post/postError.ftl") public ViewData postError(@PathParam("id") int id) { ViewData view = new ViewData(); view.authUser = (User) securityContext.getUserPrincipal(); view.post = Post.findById(id); return view; }