/** * Get front request. * @param label Label * @return Request * @throws IOException If fails */ private Request front(final String label) throws IOException { return this.request .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel(String.format("/page/links/link[@rel='%s']/@href", label)); }
@Override public boolean subscription() throws IOException { return Boolean.valueOf( this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .xml() .xpath("/page/bout/subscription/text()") .get(0) ); }
@Override public String title() throws IOException { return this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .xml() .xpath("/page/bout/title/text()") .get(0); }
/** * Fetch JSON object. * @return JSON object * @throws IOException If fails */ public JsonObject fetch() throws IOException { return this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class) .json().readObject(); }
@Override public Counters counters() throws IOException { return new RtCounters( this.request .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel("/page/links/link[@rel='menu:counters']/@href") ); }
@Override public Locks locks() throws IOException { return new RtLocks( this.request .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel("/page/links/link[@rel='menu:locks']/@href") ); } }
@Override public Iterable<String> names() throws IOException { return this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .xml() .xpath("/page/counters/counter/name/text()"); }
@Override public boolean unseen() throws IOException { return Boolean.parseBoolean( this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .xml() .xpath(this.xpath("unseen/text()")) .get(0) ); }
@Override public String etag() throws IOException { return this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .xml() .xpath(this.xpath("etag/text()")) .get(0); }
@Override public String author() throws IOException { return this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .xml() .xpath(this.xpath("author/text()")) .get(0); }
@Override public String ctype() throws IOException { return this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .xml() .xpath(this.xpath("ctype/text()")) .get(0); }
@Override public Iterable<String> iterate() throws IOException { final RestResponse response = this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK); final List<JsonString> list = response.as(JsonResponse.class) .json().readArray().getValuesAs(JsonString.class); final List<String> templates = new ArrayList<String>(list.size()); for (final JsonString value : list) { templates.add(value.getString()); } return templates; }
@Override public InputStream read() throws IOException { return new ByteArrayInputStream( this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel(this.xpath("links/link[@rel='download']/@href")) .fetch().binary() ); }
@Override public void subscribe(final boolean subs) throws IOException { this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(XmlResponse.class) .rel("/page/links/link[@rel='subscribe']/@href") .method(Request.GET) .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_SEE_OTHER); Logger.info(this, "bout #%d subscription changed", this.num); }
@Override public Iterable<String> iterate() throws IOException { final List<JsonObject> array = this.request.method(Request.GET) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class) .json().readArray().getValuesAs(JsonObject.class); final Collection<String> emails = new ArrayList<String>(array.size()); for (final JsonObject obj : array) { // @checkstyle MultipleStringLiterals (1 line) emails.add(obj.getString("email")); } return emails; }
@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 Gist fork() throws IOException { return new RtGist( this.ghub, this.entry, this.request.uri().path("/forks").back() .method(Request.POST) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_CREATED) .as(JsonResponse.class) .json().readObject().getString("id") ); }
@Override public Commit create( final JsonObject params ) throws IOException { return this.get( this.request.method(Request.POST) .body().set(params).back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_CREATED) .as(JsonResponse.class) .json().readObject().getString("sha") ); }
@Override public Tree create( final JsonObject params ) throws IOException { return this.get( this.request.method(Request.POST) .body().set(params).back() .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_CREATED) .as(JsonResponse.class) .json().readObject().getString("sha") ); } }
@Override public void add(final Iterable<String> labels) throws IOException { JsonArrayBuilder builder = Json.createArrayBuilder(); for (final String label : labels) { builder = builder.add(label); } final JsonStructure json = builder.build(); this.request.method(Request.POST) .body().set(json).back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class) .json().readArray(); }