/** * Make a bundle. * @param level Logging level * @param msg Message * @return JaxbBundle injectable into the page * @since 0.4.16 */ public static JaxbBundle bundle(final Level level, final String msg) { return FlashInset.bundle(level, msg, 0L); }
/** * Throw an exception that will forward to the page with an error message. * @param uri The URI to forward to * @param cause The cause of this problem * @return The exception to throw */ public static WebApplicationException forward(@NotNull final URI uri, @NotNull final Exception cause) { return FlashInset.forward(uri, cause.getMessage(), Level.SEVERE); }
/** * Flash. * @return The inset with flash */ @Inset.Runtime public final FlashInset flash() { return new FlashInset(this); }
/** * Create an exception that will redirect to the page with an error message. * * <p>The difference between this method and a static * {@link #forward(URI,Exception)} is in its awareness of a resource, * which is forwarding. Key benefit is that a non-static method adds extra * value to the cookie, which is time consumed by the resource until the * redirect happened. * * @param uri The URI to forward to * @param cause The cause of this problem * @return The exception to throw */ public WebApplicationException redirect(@NotNull final URI uri, @NotNull final Exception cause) { return this.redirect(uri, cause.getMessage(), Level.SEVERE); }
@Override public void render(@NotNull final BasePage<?, ?> page, @NotNull final Response.ResponseBuilder builder) { if (this.resource.httpHeaders().getCookies() .containsKey(FlashInset.COOKIE)) { final FlashInset.Flash cookie = new FlashInset.Flash( this.resource.httpHeaders().getCookies().get(FlashInset.COOKIE) ); page.append( FlashInset.bundle( cookie.level(), cookie.message(), cookie.msec() ) ); builder.cookie( new CookieBuilder(this.resource.uriInfo().getBaseUri()) .name(FlashInset.COOKIE) .build() ); } }
/** * Version inset. * @return The inset */ @Inset.Runtime public final Inset version() { return new VersionInset( Manifests.read("Stateful-Version"), Manifests.read("Stateful-Revision"), Manifests.read("Stateful-Date") ); }
/** * Create an exception that will forward to the page with an error message. * @param uri The URI to forward to * @param message The message to show as error * @param level Message level * @return The exception to throw */ public static WebApplicationException forward(@NotNull final URI uri, @NotNull final String message, @NotNull final Level level) { return new FlashException( Response.status(HttpURLConnection.HTTP_SEE_OTHER) .location(uri) .cookie(new FlashInset.Flash(uri, message, level)) .header(FlashInset.HEADER, message) .entity(message) .build(), message, level ); }
/** * Public ctor, from exact values. * @param base Base URI where we're using it * @param message The message * @param level The level * @param msec Milliseconds consumed * @checkstyle ParameterNumber (5 lines) */ public Flash(@NotNull final URI base, @NotNull final String message, @NotNull final Level level, final long msec) { super( new CookieBuilder(base) .name(FlashInset.COOKIE) .value(FlashInset.Flash.encode(message, level, msec)) .temporary() .build() ); Validate.notBlank(message, "flash message can't be empty"); this.msg = message; this.lvl = level; Validate.isTrue( msec >= 0L || msec == -1L, "milliseconds can either be positive or equal to -1" ); this.millis = msec; } /**
/** * Refresh the token. * @throws IOException If fails due to IO problem */ @GET @Path("/refresh") public void index() throws IOException { this.user().refresh(); throw this.flash().redirect( this.uriInfo().getBaseUriBuilder() .clone() .path(IndexRs.class) .build(), "security token successfully refreshed", Level.INFO ); }
final String[] parts = header.split("\\s+"); if (!"Basic".equals(parts[0])) { throw FlashInset.forward( this.resource.uriInfo().getBaseUri(), String.format( final String[] tokens = StringUtils.splitPreserveAllTokens(body, ':'); if (tokens.length != 2) { throw FlashInset.forward( this.resource.uriInfo().getBaseUri(), String.format(
/** * Flash. * @return The inset with flash */ @Inset.Runtime public final FlashInset flash() { return new FlashInset(this); }
/** * Inset with a version of the product. * @return The inset */ @Inset.Runtime public final Inset insetVersion() { return new VersionInset( Manifests.read("Aintshy-Version"), Manifests.read("Aintshy-Revision"), Manifests.read("Aintshy-Date") ); }
return new FlashException( Response.status(HttpURLConnection.HTTP_SEE_OTHER) .location(uri) .cookie( new FlashInset.Flash( uri, message, level, System.currentTimeMillis() - this.resource.started()
/** * Post a question. * @param text Text to post * @throws IOException If fails */ @POST @Path("/post") public void post(@FormParam("text") final String text) throws IOException { final Human human = new SafeHuman(this.human(), this); human.ask(text); throw this.flash().redirect( this.uriInfo().getBaseUri(), "thanks for the question", Level.INFO ); }
/** * Get current user. * @return User */ protected final User user() { final Identity identity = this.auth().identity(); if (identity.equals(Identity.ANONYMOUS)) { throw FlashInset.forward( this.uriInfo().getBaseUriBuilder().clone() .path(IndexRs.class) .build(), "please login first", Level.SEVERE ); } return this.base().user(identity.urn()); }
/** * Delete a counter. * @param name Name of the counter * @throws IOException If fails due to IO problem */ @GET @Path("/delete") public void delete(@QueryParam(CountersRs.PARAM) final String name) throws IOException { this.user().counters().delete(name); throw this.flash().redirect( this.uriInfo().getBaseUriBuilder() .clone() .path(CountersRs.class) .build(), String.format("counter %s deleted successfully", name), Level.INFO ); }
/** * Answer the talk. * @param text Text of the answer * @throws IOException If fails */ @POST @Path("/post") public void answer( @FormParam("text") final String text) throws IOException { final Talk talk = this.talk(); talk.messages().post(talk.asker().equals(this.human()), text); throw this.flash().redirect( this.uriInfo().getBaseUri(), "thanks for the message", Level.INFO ); }
/** * Confirm email. * @param code Code * @throws IOException If fails */ @POST @Path("/confirm") public void confirm(@FormParam("code") final String code) throws IOException { final Profile profile = this.human().profile(); try { profile.confirm(code); } catch (final Profile.ConfirmCodeException ex) { throw this.flash().redirect(this.uriInfo().getBaseUri(), ex); } throw this.flash().redirect( this.uriInfo().getBaseUri(), String.format("your email %s is confirmed", profile.email()), Level.INFO ); }
/** * Remind by email. * @param email Email * @throws IOException If fails */ @POST @Path("/remind") public void resend(@FormParam("email") final String email) throws IOException { this.base().remind( email, new SmtpPocket( email, "password remind", "Hey, here is your password: %s", Postman.class.cast( this.servletContext().getAttribute( Postman.class.getName() ) ) ) ); throw this.flash().redirect( this.uriInfo().getBaseUri(), "check your inbox", Level.INFO ); }
/** * Resend code by email. * @throws IOException If fails */ @POST @Path("/resend") public void resend() throws IOException { final Profile profile = this.human().profile(); profile.resend( new SmtpPocket( profile.email(), "confirmation code (resend)", "Hey, here is your code again: %s", Postman.class.cast( this.servletContext().getAttribute( Postman.class.getName() ) ) ) ); throw this.flash().redirect( this.uriInfo().getBaseUri(), "check your inbox", Level.INFO ); }