com.jcabi.http.response.JsonResponse Java Examples

The following examples show how to use com.jcabi.http.response.JsonResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InitializrSpringCloudInfoServiceTests.java    From spring-cloud-release-tools with Apache License 2.0 6 votes vote down vote up
@Test
public void getSpringCloudVersionsTest() throws Exception {
	RestTemplate rest = mock(RestTemplate.class);
	Github github = mock(Github.class);
	GithubPomReader githubPomReader = mock(GithubPomReader.class);
	Response response = mock(Response.class);
	Request request = mock(Request.class);
	RequestURI requestURI = mock(RequestURI.class);
	JsonResponse jsonResponse = new JsonResponse(new DefaultResponse(request, 200, "",
			new Array<>(),
			IOUtils.toByteArray(new ClassPathResource("spring-cloud-versions.json")
					.getInputStream())));
	doReturn(request).when(requestURI).back();
	doReturn(requestURI).when(requestURI).path(eq(SPRING_CLOUD_RELEASE_TAGS_PATH));
	doReturn(requestURI).when(request).uri();
	doReturn(jsonResponse).when(response).as(eq(JsonResponse.class));
	doReturn(response).when(request).fetch();
	doReturn(request).when(github).entry();
	InitializrSpringCloudInfoService service = spy(
			new InitializrSpringCloudInfoService(rest, github, githubPomReader));
	assertThat(service.getSpringCloudVersions(),
			Matchers.equalTo(SpringCloudInfoTestData.springCloudVersions.stream()
					.map(v -> v.replaceFirst("v", "")).collect(Collectors.toList())));
}
 
Example #2
Source File: PsLinkedin.java    From takes with MIT License 6 votes vote down vote up
/**
 * 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");
}
 
Example #3
Source File: PsTwitter.java    From takes with MIT License 6 votes vote down vote up
/**
 * Get user name from Twitter, with the token provided.
 * @param tkn Twitter access token
 * @return The user found in Twitter
 * @throws IOException If fails
 */
private Identity identity(final String tkn) throws IOException {
    return parse(
        this.user
            .uri()
            .set(
                URI.create(
                    new Href(PsTwitter.VERIFY_URL)
                        .with(PsTwitter.ACCESS_TOKEN, tkn)
                        .toString()
                )
            )
            .back()
            .header("accept", "application/json")
            .fetch().as(RestResponse.class)
            .assertStatus(HttpURLConnection.HTTP_OK)
            .as(JsonResponse.class)
            .json()
            .readObject()
    );
}
 
Example #4
Source File: PsTwitter.java    From takes with MIT License 6 votes vote down vote up
/**
 * Retrieve Twitter access token.
 * @return The Twitter access token
 * @throws IOException If failed
 */
private String fetch() throws IOException {
    return this.token
        .method("POST")
        .header(
            "Content-Type",
            "application/x-www-form-urlencoded;charset=UTF-8"
        )
        .header(
            "Authorization",
            String.format(
                "Basic %s", DatatypeConverter.printBase64Binary(
                    new UncheckedBytes(
                        new BytesOf(
                            String.format("%s:%s", this.app, this.key)
                        )
                    ).asBytes()
                )
            )
        )
        .fetch().as(RestResponse.class)
        .assertStatus(HttpURLConnection.HTTP_OK)
        .as(JsonResponse.class)
        .json().readObject().getString(PsTwitter.ACCESS_TOKEN);
}
 
Example #5
Source File: PsGoogle.java    From takes with MIT License 6 votes vote down vote up
/**
 * Get user name from Google, with the token provided.
 * @param token Google access token
 * @return The user found in Google
 * @throws IOException If fails
 */
private Identity fetch(final String token) throws IOException {
    // @checkstyle LineLength (1 line)
    final String uri = new Href(this.gapi).path("plus").path("v1")
        .path("people")
        .path("me")
        .with(PsGoogle.ACCESS_TOKEN, token)
        .toString();
    final JsonObject json = new JdkRequest(uri).fetch()
        .as(JsonResponse.class).json()
        .readObject();
    if (json.containsKey(PsGoogle.ERROR)) {
        throw new HttpException(
            HttpURLConnection.HTTP_BAD_REQUEST,
            String.format(
                "could not retrieve id from Google, possible cause: %s.",
                json.getJsonObject(PsGoogle.ERROR).get("message")
            )
        );
    }
    return PsGoogle.parse(json);
}
 
Example #6
Source File: PsGoogle.java    From takes with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example #7
Source File: SpringCloudRelease.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
@Override
@Cacheable("springCloudVersions")
public Collection<String> getSpringCloudVersions() throws IOException {
	List<String> releaseVersions = new ArrayList<>();
	JsonReader reader = github.entry().uri().path(SPRING_CLOUD_RELEASE_TAGS_PATH)
			.back().fetch().as(JsonResponse.class).json();
	JsonArray tags = reader.readArray();
	reader.close();
	List<JsonObject> tagsList = tags.getValuesAs(JsonObject.class);
	for (JsonObject obj : tagsList) {
		releaseVersions.add(obj.getString("name").replaceFirst("v", ""));
	}
	return releaseVersions;
}
 
Example #8
Source File: Command.java    From charles-rest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the commands' author org membership.
 * @return Json membership as described
 * <a href="https://developer.github.com/v3/orgs/members/#get-organization-membership">here</a>
 * or an empty Json object if the repo owner is not an organization.
 * @throws IOException If something goes wrong.
 */
public JsonObject authorOrgMembership() throws IOException {
    if(this.repo().isOwnedByOrganization()) {
        Request req = this.issue.repo().github().entry()
            .uri().path("/orgs/")
            .path(this.repo().ownerLogin())
            .path("/memberships/")
            .path(this.authorLogin()).back();
        return req.fetch().as(JsonResponse.class).json().readObject();
    } else {
        return Json.createObjectBuilder().build();
    }
        
}
 
Example #9
Source File: PsLinkedin.java    From takes with MIT License 5 votes vote down vote up
/**
 * Get user name from Linkedin, with the token provided.
 * @param token PsLinkedin access token
 * @return The user found in PsLinkedin
 * @throws IOException If fails
 */
private Identity fetch(final String token) throws IOException {
    // @checkstyle LineLength (1 line)
    final String uri = this.apihref
        .with("oauth2_access_token", token)
        .with("format", "json")
        .toString();
    return PsLinkedin.parse(
        new JdkRequest(uri)
            .header("accept", "application/json")
            .fetch().as(RestResponse.class)
            .assertStatus(HttpURLConnection.HTTP_OK)
            .as(JsonResponse.class).json().readObject()
    );
}
 
Example #10
Source File: PsGithub.java    From takes with MIT License 5 votes vote down vote up
/**
 * Get user name from Github, with the token provided.
 * @param token Github access token
 * @return The user found in Github
 * @throws IOException If fails
 */
private Identity fetch(final String token) throws IOException {
    final String uri = new Href(this.api).path("user")
        .with(PsGithub.ACCESS_TOKEN, token).toString();
    return PsGithub.parse(
        new JdkRequest(uri)
            .header("accept", "application/json")
            .fetch().as(RestResponse.class)
            .assertStatus(HttpURLConnection.HTTP_OK)
            .as(JsonResponse.class).json().readObject()
    );
}