/** * With this {@link UriInfo}. * @param info The {@link UriInfo} object * @return This object */ public ResourceMocker withUriInfo(final UriInfo info) { Mockito.doReturn(info).when(this.resource).uriInfo(); return this; }
/** * Redirect URI. * @return The URI */ private URI redirectUri() { return this.resource.uriInfo().getRequestUriBuilder() .clone() .queryParam(Facebook.FLAG, "") .build(); }
/** * Redirect URI. * @return The URI */ private URI redirectUri() { return this.resource.uriInfo().getRequestUriBuilder() .clone() .queryParam(Amazon.FLAG, "") .build(); }
/** * Redirect URI. * @return The URI */ private URI redirectUri() { return this.resource.uriInfo().getRequestUriBuilder() .clone() .queryParam(Github.FLAG, "") .build(); }
/** * Redirect URI. * @return The URI */ private URI redirectUri() { return this.resource.uriInfo().getRequestUriBuilder() .clone() .queryParam(LinkedIn.FLAG, "") .build(); }
@Override public Identity identity() throws IOException { Identity identity = Identity.ANONYMOUS; final boolean authed = this.resource.uriInfo() .getQueryParameters().containsKey(Google.STATE) && this.resource.uriInfo().getQueryParameters() .getFirst(Google.STATE).equals(Google.FLAG); if (authed) { final List<String> code = this.resource.uriInfo() // @checkstyle MultipleStringLiterals (1 line) .getQueryParameters().get("code"); if (code == null || code.isEmpty()) { throw new WebApplicationException( new IllegalArgumentException( "HTTP query parameter 'code' is mandatory" ), HttpURLConnection.HTTP_BAD_REQUEST ); } identity = this.fetch(this.token(code.get(0))); } return identity; }
@Override public Identity identity() throws IOException { Identity identity = Identity.ANONYMOUS; if (this.resource.uriInfo().getQueryParameters() .containsKey(LinkedIn.FLAG)) { final List<String> code = this.resource.uriInfo() // @checkstyle MultipleStringLiterals (1 line) .getQueryParameters().get("code"); if (code == null || code.isEmpty()) { throw new WebApplicationException( new IllegalArgumentException( "HTTP query parameter 'code' is mandatory" ), HttpURLConnection.HTTP_BAD_REQUEST ); } identity = this.fetch(this.token(code.get(0))); } return identity; }
@Override public Identity identity() throws IOException { Identity identity = Identity.ANONYMOUS; if (this.resource.uriInfo().getQueryParameters() .containsKey(Github.FLAG)) { final List<String> code = this.resource.uriInfo() // @checkstyle MultipleStringLiterals (1 line) .getQueryParameters().get("code"); if (code == null || code.isEmpty()) { throw new WebApplicationException( new IllegalArgumentException( "HTTP query parameter 'code' is mandatory" ), HttpURLConnection.HTTP_BAD_REQUEST ); } identity = this.fetch(this.token(code.get(0))); } return identity; }
@Override public Identity identity() throws IOException { Identity identity = Identity.ANONYMOUS; if (this.resource.uriInfo().getQueryParameters() .containsKey(Amazon.FLAG)) { final List<String> code = this.resource.uriInfo() // @checkstyle MultipleStringLiterals (1 line) .getQueryParameters().get("code"); if (code == null || code.isEmpty()) { throw new WebApplicationException( new IllegalArgumentException( "HTTP query parameter 'code' is mandatory" ), HttpURLConnection.HTTP_BAD_REQUEST ); } identity = this.fetch(this.token(code.get(0))); } return identity; }
/** * Attach to this resource and make {@code HREF} attribute * absolute, using the URI information of the resource. * @param res The resource to attach to */ public void attachTo(@NotNull @Valid final Resource res) { synchronized (this.elements) { if (!this.href.isAbsolute()) { if (this.href.getPath().charAt(0) == '.') { this.href = Link.combine( res.uriInfo().getRequestUri(), this.href.getPath().substring(1), this.href.getQuery() ); } else if (this.href.getPath().charAt(0) == '/') { this.href = Link.combine( res.uriInfo().getBaseUri(), this.href.getPath(), this.href.getQuery() ); } } } }
@Override public Identity identity() throws IOException { Identity identity = Identity.ANONYMOUS; if (this.resource.uriInfo().getQueryParameters() .containsKey(Facebook.FLAG)) { final List<String> code = this.resource.uriInfo() // @checkstyle MultipleStringLiterals (1 line) .getQueryParameters().get("code"); if (code == null || code.isEmpty()) { throw new WebApplicationException( new IllegalArgumentException( "HTTP query parameter 'code' is mandatory" ), HttpURLConnection.HTTP_BAD_REQUEST ); } final User fbuser = this.fetch(this.token(code.get(0))); identity = new Identity.Simple( URN.create(String.format("urn:facebook:%s", fbuser.getId())), fbuser.getName(), UriBuilder.fromUri("https://graph.facebook.com/") .path("/{id}/picture") .build(fbuser.getId()) ); } return identity; }
/** * Ctor. * @param hmn Human * @param base Base resource * @throws IOException If fails */ SafeHuman(final Human hmn, final Resource base) throws IOException { if (!hmn.profile().confirmed()) { throw new WebApplicationException( Response.seeOther( base.uriInfo().getBaseUriBuilder() .path(SetupRs.class) .path(SetupRs.class, "notConfirmed") .build() ).build() ); } if (hmn.profile().year() == 0) { throw new WebApplicationException( Response.seeOther( base.uriInfo().getBaseUriBuilder() .path(SetupRs.class) .path(SetupRs.class, "noDetails") .build() ).build() ); } this.human = hmn; }
if (!"Basic".equals(parts[0])) { throw FlashInset.forward( this.resource.uriInfo().getBaseUri(), String.format( "Invalid authentication scheme '%s'", if (tokens.length != 2) { throw FlashInset.forward( this.resource.uriInfo().getBaseUri(), String.format( "Authentication body '%s' has %d part(s), two expected",
.status(HttpURLConnection.HTTP_SEE_OTHER) .location( this.resource.uriInfo().getRequestUriBuilder() .replaceQuery("") .build()
/** * Logout authentication cookie. * * <p>Use this cookie to log user out of the system, for example: * * <pre> if (you_are_not_allowed()) { * throw new AuthException( * Response.seeOther(this.uriInfo().getBaseUri()) * .cookie(this.auth().logout()) * .build() * ); * } * </pre> * * @return The cookie */ @Cacheable.FlushAfter public NewCookie logout() { return new CookieBuilder(this.resource.uriInfo().getBaseUri()) .name(AuthInset.AUTH_COOKIE) .build(); }
/** * Build an instance of provided class. * @param type Type of resource * @return The resource * @param <T> Type of it * @since 0.4.11 */ public <T extends BaseResource> T mock(final Class<T> type) { final T rest; try { rest = type.newInstance(); } catch (final InstantiationException ex) { throw new IllegalArgumentException(ex); } catch (final IllegalAccessException ex) { throw new IllegalArgumentException(ex); } rest.setHttpHeaders(this.resource.httpHeaders()); rest.setHttpServletRequest(this.resource.httpServletRequest()); rest.setProviders(this.resource.providers()); rest.setSecurityContext(this.resource.securityContext()); rest.setServletContext(this.resource.servletContext()); rest.setUriInfo(this.resource.uriInfo()); return rest; }
@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() ); } }
new Link( "rexsl:logout", this.resource.uriInfo().getRequestUriBuilder() .clone() .queryParam(AuthInset.LOGOUT_FLAG, true) builder.header("X-Rexsl-Identity", identity.urn()); if (this.resource.uriInfo().getQueryParameters() .containsKey(AuthInset.LOGOUT_FLAG)) { throw new AuthException( Response.status(HttpURLConnection.HTTP_SEE_OTHER).location( this.resource.uriInfo().getRequestUriBuilder() .replaceQuery("") .build()
/** * Authentication cookie. * @param identity The identity to wrap into the cookie * @return The cookie */ public NewCookie cookie(final Identity identity) { return new CookieBuilder(this.resource.uriInfo().getBaseUri()) .name(AuthInset.AUTH_COOKIE) .value(new Encrypted(identity, this.key).cookie()) .temporary() .build(); }
/** * Retrieve Google access token. * @param code Google "authorization code" * @return The token * @throws IOException If fails */ private String token(final String code) throws IOException { return new JdkRequest("https://accounts.google.com/o/oauth2/token") .body() .formParam("client_id", this.app) .formParam("redirect_uri", this.resource.uriInfo().getBaseUri()) .formParam("client_secret", this.secret) .formParam("grant_type", "authorization_code") .formParam("code", code) .back() .header( HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED ) .method(Request.POST) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class).json() .readObject() // @checkstyle MultipleStringLiterals (1 line) .getString("access_token"); }