@Override public RequestBody formParams(final Map<String, String> params) { RequestBody body = this; for (final Map.Entry<String, String> param : params.entrySet()) { body = body.formParam(param.getKey(), param.getValue()); } return body; } }
@Override public RequestBody formParams(final Map<String, String> params) { RequestBody body = this; for (final Map.Entry<String, String> param : params.entrySet()) { body = body.formParam(param.getKey(), param.getValue()); } return body; }
/** * 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"); }
@Override public boolean lock(final String label) throws IOException { final long start = System.currentTimeMillis(); final Response rsp = this.front("lock") .body().formParam("label", label) .formParam("name", this.lck).back() .method(Request.POST) .fetch(); final boolean locked = rsp.status() == HttpURLConnection.HTTP_SEE_OTHER; Logger.info( this, "lock of \"%s\" is %B in %[ms]s: %s", this.lck, locked, System.currentTimeMillis() - start, rsp.body() ); return locked; }
/** * Retrieve PsLinkedin access token. * @param home Home of this page * @param code PsLinkedin "authorization code" * @return The token * @throws IOException If failed */ private String token(final String home, final String code) throws IOException { final String uri = this.tkhref.toString(); return new JdkRequest(uri) .method("POST") .header("Accept", "application/xml") .body() .formParam("grant_type", "authorization_code") .formParam("client_id", this.app) .formParam("redirect_uri", home) .formParam("client_secret", this.key) .formParam(PsLinkedin.CODE, code) .back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class) .json().readObject().getString("access_token"); }
/** * Retrieve PsLinkedin access token. * @param home Home of this page * @param code PsLinkedin "authorization code" * @return The token * @throws IOException If failed */ private String token(final String home, final String code) throws IOException { final String uri = this.tkhref.toString(); return new JdkRequest(uri) .method("POST") .header("Accept", "application/xml") .body() .formParam("grant_type", "authorization_code") .formParam("client_id", this.app) .formParam("redirect_uri", home) .formParam("client_secret", this.key) .formParam(PsLinkedin.CODE, code) .back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class) .json().readObject().getString("access_token"); }
/** * Retrieve Google access token. * @param code Google "authorization code" * @return The token * @throws IOException If failed */ private String token(final String code) throws IOException { return new JdkRequest( new Href(this.gauth).path("o").path("oauth2").path("token") .toString() ).body() .formParam("client_id", this.app) .formParam("redirect_uri", this.redir) .formParam("client_secret", this.key) .formParam("grant_type", "authorization_code") .formParam(PsGoogle.CODE, code) .back() .header("Content-Type", "application/x-www-form-urlencoded") .method(com.jcabi.http.Request.POST) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class).json() .readObject() .getString(PsGoogle.ACCESS_TOKEN); }
/** * Retrieve Google access token. * @param code Google "authorization code" * @return The token * @throws IOException If failed */ private String token(final String code) throws IOException { return new JdkRequest( new Href(this.gauth).path("o").path("oauth2").path("token") .toString() ).body() .formParam("client_id", this.app) .formParam("redirect_uri", this.redir) .formParam("client_secret", this.key) .formParam("grant_type", "authorization_code") .formParam(PsGoogle.CODE, code) .back() .header("Content-Type", "application/x-www-form-urlencoded") .method(com.jcabi.http.Request.POST) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class).json() .readObject() .getString(PsGoogle.ACCESS_TOKEN); }
/** * Retrieve Github access token. * @param home Home of this page * @param code Github "authorization code" * @return The token * @throws IOException If failed */ private String token(final String home, final String code) throws IOException { final String uri = new Href(this.github) .path(PsGithub.LOGIN).path("oauth").path(PsGithub.ACCESS_TOKEN) .toString(); return new JdkRequest(uri) .method("POST") .header("Accept", "application/xml") .body() .formParam("client_id", this.app) .formParam("redirect_uri", home) .formParam("client_secret", this.key) .formParam(PsGithub.CODE, code) .back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .assertXPath("/OAuth/access_token") .xml() .xpath("/OAuth/access_token/text()") .get(0); }
/** * Retrieve Github access token. * @param home Home of this page * @param code Github "authorization code" * @return The token * @throws IOException If failed */ private String token(final String home, final String code) throws IOException { final String uri = new Href(this.github) .path(PsGithub.LOGIN).path("oauth").path(PsGithub.ACCESS_TOKEN) .toString(); return new JdkRequest(uri) .method("POST") .header("Accept", "application/xml") .body() .formParam("client_id", this.app) .formParam("redirect_uri", home) .formParam("client_secret", this.key) .formParam(PsGithub.CODE, code) .back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .assertXPath("/OAuth/access_token") .xml() .xpath("/OAuth/access_token/text()") .get(0); }
@Override public Counter create(final String name) throws IOException { final long start = System.currentTimeMillis(); this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel("/page/links/link[@rel='add']/@href") .method(Request.POST) .body().formParam("name", name).back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_SEE_OTHER); Logger.info( this, "counter \"%s\" created in %[ms]s", name, System.currentTimeMillis() - start ); return this.get(name); }
@Override public void post(final String text) throws IOException { this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel("/page/links/link[@rel='post']/@href") .method(Request.POST) .body().formParam("text", text).back() .fetch(); Logger.info(this, "message posted"); }
@Override public void email(final String email) throws IOException { this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel("/page/links/link[@rel='account']/@href") .fetch() .as(XmlResponse.class) .rel("/page/links/link[@rel='save-email']/@href") .method(Request.POST) .body().formParam("email", email).back() .fetch(); Logger.info(this, "email changed"); }
@Override public void create(final String name) throws IOException { this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel("/page/links/link[@rel='create']/@href") .method(Request.POST) .body().formParam("name", name).back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_SEE_OTHER); Logger.info(this, "attachment '%s' created", name); }
@Override public void rename(final String text) throws IOException { this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel("/page/links/link[@rel='rename']/@href") .method(Request.POST) .body().formParam("title", text).back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_SEE_OTHER); Logger.info(this, "bout #%d renamed", this.num); }
@Override public void invite(final String friend) throws IOException { final RestResponse response = this.request .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel("/page/links/link[@rel='invite']/@href") .method(Request.POST) .body().formParam("name", friend).back() .fetch() .as(RestResponse.class); if (response.status() == HttpURLConnection.HTTP_MOVED_PERM && response.cookie(RtFriends.COOKIE_RS_FLASH).getValue() .startsWith("incorrect+alias") ) { throw new Friends.UnknownAliasException( response.cookie(RtFriends.COOKIE_RS_FLASH).getValue() ); } response.assertStatus(HttpURLConnection.HTTP_SEE_OTHER); Logger.info(this, "friend '%s' invited", friend); }