/** * Read the body as JSON. * * @return JSON reader. */ public JsonReader json() { return new JsonReader( this.binary() ); }
/** * Follow link by REL. * @param rel Relation name * @return The same object * @throws IOException If fails */ public Request follow(final String rel) throws IOException { final WebLinkingResponse.Link link = this.links().get(rel); if (link == null) { throw new IOException( String.format( "Link with rel=\"%s\" doesn't exist, use #hasLink()", rel ) ); } return new RestResponse(this).jump(link.uri()); }
/** * Follow XML link. * @param query XPath query to fetch new URI * @return New request */ public Request rel(final String query) { this.assertXPath(query); return new RestResponse(this).jump( URI.create(this.xml().xpath(query).get(0)) ); }
/** * 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 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()"); }
/** * 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 void remove() throws IOException { this.request.method(Request.DELETE).fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); }
/** * Register this new namespace. * @param prefix Prefix to use * @param uri Namespace URI * @return This object */ public XmlResponse registerNs(final String prefix, final String uri) { return new XmlResponse(this, this.namespaces.with(prefix, uri)); }
/** * Verifies HTTP header against provided matcher, and throws * {@link AssertionError} in case of mismatch. * @param name Name of the header to match * @param value The value to expect in one of the headers * @return This object * @since 0.9 */ public RestResponse assertHeader(final String name, final String value) { return this.assertHeader(name, Matchers.hasItems(value)); }
@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 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); }
@Override public void unstar() throws IOException { this.request .method(Request.DELETE) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); } }
@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 void remove() throws IOException { this.request.method(Request.DELETE).fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); }
@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 void remove() throws IOException { this.request.method(Request.DELETE).fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); }
@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 delete() throws IOException { this.request.method(Request.DELETE).fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); }
@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"); }