final String response = new JdkRequest(uri) .fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .body(); final String[] sectors = response.split("&"); String token = null;
/** * 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 String read(final String file) throws IOException { final Response response = this.request.fetch(); final String url = response .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .as(JsonResponse.class) .json().readObject().getJsonObject("files") .getJsonObject(file).getString("raw_url"); return response .as(RestResponse.class) .jump(URI.create(url)) .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .body(); }
/** * Follow LOCATION header. * @return New request */ public Request follow() { this.assertHeader( HttpHeaders.LOCATION, Matchers.not(Matchers.emptyIterableOf(String.class)) ); return this.jump( URI.create(this.headers().get(HttpHeaders.LOCATION).get(0)) ); }
/** * 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)) ); }
@Override public boolean starred() throws IOException { return this.request .fetch().as(RestResponse.class) .assertStatus( Matchers.isOneOf( HttpURLConnection.HTTP_NO_CONTENT, HttpURLConnection.HTTP_NOT_FOUND ) ).status() == HttpURLConnection.HTTP_NO_CONTENT; }
@Override public void merge( final String msg) throws IOException { final JsonStructure json = Json.createObjectBuilder() .add("commit_message", msg) .build(); this.merge(json).assertStatus(HttpURLConnection.HTTP_OK); }
@Override public String render( final JsonObject json) throws IOException { final StringWriter output = new StringWriter(); Json.createWriter(output).writeObject(json); return this.request .body() .set(output.toString()) .back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK) .assertHeader( HttpHeaders.CONTENT_TYPE, Matchers.everyItem( Matchers.startsWith(MediaType.TEXT_HTML) ) ) .body(); }
/** * Fetch the next portion, if available. * @throws IOException If there is any I/O problem */ @SuppressWarnings("unchecked") private void fetch() throws IOException { final RestResponse response = this.request.fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK); final WebLinkingResponse.Link link = response .as(WebLinkingResponse.class) .links() .get("next"); if (link == null) { this.more = false; } else { this.request = response.jump(link.uri()); } final JsonArray arr = response.as(JsonResponse.class).json() .readArray(); final Queue<P> list = new LinkedList<P>(); for (final JsonValue value : arr) { list.add((P) value); } this.objects = list; } }
@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); }
/** * Verifies HTTP response body content against provided matcher, * and throws {@link AssertionError} in case of mismatch. * @param matcher The matcher to use * @return This object */ public RestResponse assertBody(final Matcher<String> matcher) { MatcherAssert.assertThat( String.format( "HTTP response body content is not valid:%n%s", this ), this.body(), matcher ); return this; }
@Override public InputStream raw() throws IOException { return new ByteArrayInputStream( this.request.reset(HttpHeaders.ACCEPT) .header( HttpHeaders.ACCEPT, "application/vnd.github.v3.raw" ).fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_OK).binary() ); } }
@Override public boolean isLocked() { boolean locked = false; try { locked ^= this.request.method(Request.PUT).uri().path("/lock").back() .fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_NO_CONTENT).back().body() .get().isEmpty(); } catch (final IOException error) { locked = false; } return locked; }
/** * Verifies HTTP response status code against the provided matcher, * and throws {@link AssertionError} in case of mismatch. * @param matcher Matcher to validate status code * @return This object */ public RestResponse assertStatus(final Matcher<Integer> matcher) { MatcherAssert.assertThat( String.format( "HTTP response status is not the one expected:%n%s", this ), this.status(), matcher ); return this; }
/** * Jump to a new location. * @param uri Destination to jump to * @return New request */ @SuppressWarnings("PMD.UseConcurrentHashMap") public Request jump(final URI uri) { Request req = this.back().uri() .set(this.back().uri().get().resolve(uri)) .back(); final Map<String, List<String>> headers = this.headers(); if (headers.containsKey(HttpHeaders.SET_COOKIE)) { for (final String header : headers.get(HttpHeaders.SET_COOKIE)) { for (final HttpCookie cookie : HttpCookie.parse(header)) { req = req.header( HttpHeaders.COOKIE, String.format( "%s=%s", cookie.getName(), cookie.getValue() ) ); } } } return req; }
/** * Verifies HTTP response body content against provided matcher, * and throws {@link AssertionError} in case of mismatch. * @param matcher The matcher to use * @return This object */ public RestResponse assertBinary(final Matcher<byte[]> matcher) { MatcherAssert.assertThat( String.format( "HTTP response binary content is not valid:%n%s", this ), this.binary(), matcher ); return this; }
/** * 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 void remove() throws IOException { this.request.method(Request.DELETE).fetch() .as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); }
@Override public MergeState merge( final String msg, final String sha) throws IOException { final JsonObjectBuilder builder = Json.createObjectBuilder() .add("commit_message", msg).add("sha", sha); final RestResponse response = this.merge(builder.build()) .assertStatus( Matchers.isOneOf( HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_BAD_METHOD, HttpURLConnection.HTTP_CONFLICT ) ); final MergeState mergeState; switch (response.status()) { case HttpURLConnection.HTTP_OK: mergeState = MergeState.SUCCESS; break; case HttpURLConnection.HTTP_BAD_METHOD: mergeState = MergeState.NOT_MERGEABLE; break; default: mergeState = MergeState.BAD_HEAD; break; } return mergeState; }