Repository: cxf Updated Branches: refs/heads/master 72f81630f -> 4bc00dc9e
[CXF-6165] Prototyping a command line client Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/4bc00dc9 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/4bc00dc9 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/4bc00dc9 Branch: refs/heads/master Commit: 4bc00dc9e621b2d61764bfbe24c82042a784d083 Parents: 72f8163 Author: Sergey Beryozkin <[email protected]> Authored: Fri Jun 19 17:09:48 2015 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Fri Jun 19 17:09:48 2015 +0100 ---------------------------------------------------------------------- .../release/samples/jax_rs/big_query/pom.xml | 16 ++++ .../java/demo/jaxrs/server/BigQueryServer.java | 96 ++++++++++++++++++++ .../java/demo/jaxrs/server/BigQueryService.java | 28 ++++-- 3 files changed, 130 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/4bc00dc9/distribution/src/main/release/samples/jax_rs/big_query/pom.xml ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/big_query/pom.xml b/distribution/src/main/release/samples/jax_rs/big_query/pom.xml index 19196af..22d273a 100644 --- a/distribution/src/main/release/samples/jax_rs/big_query/pom.xml +++ b/distribution/src/main/release/samples/jax_rs/big_query/pom.xml @@ -109,6 +109,22 @@ </webResources> </configuration> </plugin> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>exec-maven-plugin</artifactId> + <executions> + <execution> + <goals> + <goal>exec</goal> + </goals> + </execution> + </executions> + <configuration> + <executable>java</executable> + <mainClass>demo.jaxrs.server.BigQueryServer</mainClass> + </configuration> + </plugin> </plugins> </build> + </project> http://git-wip-us.apache.org/repos/asf/cxf/blob/4bc00dc9/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryServer.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryServer.java b/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryServer.java new file mode 100644 index 0000000..373ddae --- /dev/null +++ b/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryServer.java @@ -0,0 +1,96 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package demo.jaxrs.server; + +import java.io.FileInputStream; +import java.io.InputStream; +import java.security.KeyStore; +import java.security.PrivateKey; +import java.util.Collections; +import java.util.List; + +import org.apache.cxf.jaxrs.client.WebClient; +import org.apache.cxf.jaxrs.provider.json.JsonMapObjectProvider; +import org.apache.cxf.rs.security.jose.JoseType; +import org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm; +import org.apache.cxf.rs.security.jose.jws.JwsHeaders; +import org.apache.cxf.rs.security.jose.jws.JwsJwtCompactProducer; +import org.apache.cxf.rs.security.jose.jwt.JwtClaims; +import org.apache.cxf.rs.security.jose.jwt.JwtToken; +import org.apache.cxf.rs.security.oauth2.client.OAuthClientUtils; +import org.apache.cxf.rs.security.oauth2.common.ClientAccessToken; +import org.apache.cxf.rs.security.oauth2.grants.jwt.JwtBearerGrant; +import org.apache.cxf.rs.security.oauth2.utils.OAuthUtils; + + +public class BigQueryServer { + public static void main(String[] args) throws Exception { + final String pc12File = args[0]; + final String keySecret = args[1]; + final String issuer = args[2]; + final String projectId = args[3]; + + PrivateKey privateKey = loadPrivateKey(pc12File, keySecret); + + + ClientAccessToken accessToken = getAccessToken(privateKey, issuer); + + WebClient bigQueryClient = WebClient.create("https://www.googleapis.com/bigquery/v2/projects/" + + projectId + "/queries", + Collections.singletonList(new JsonMapObjectProvider())); + bigQueryClient.accept("application/json"); + bigQueryClient.type("application/json"); + + List<ShakespeareText> texts = BigQueryService.getMatchingTexts(bigQueryClient, accessToken, "brave", "10"); + + System.out.println("Matching texts:"); + for (ShakespeareText text : texts) { + System.out.println(text.getText() + ":" + text.getDate()); + } + } + + private static ClientAccessToken getAccessToken(PrivateKey privateKey, String issuer) { + JwsHeaders headers = new JwsHeaders(JoseType.JWT, SignatureAlgorithm.RS256); + JwtClaims claims = new JwtClaims(); + claims.setIssuer(issuer); + claims.setAudience("https://www.googleapis.com/oauth2/v3/token"); + + long issuedAt = OAuthUtils.getIssuedAt(); + claims.setIssuedAt(issuedAt); + claims.setExpiryTime(issuedAt + 60 * 60); + claims.setProperty("scope", "https://www.googleapis.com/auth/bigquery.readonly"); + + JwtToken token = new JwtToken(headers, claims); + JwsJwtCompactProducer p = new JwsJwtCompactProducer(token); + String base64UrlAssertion = p.signWith(privateKey); + + JwtBearerGrant grant = new JwtBearerGrant(base64UrlAssertion); + + WebClient accessTokenService = WebClient.create("https://www.googleapis.com/oauth2/v3/token"); + return OAuthClientUtils.getAccessToken(accessTokenService, grant); + } + + private static PrivateKey loadPrivateKey(String p12File, String password) throws Exception { + InputStream is = new FileInputStream(p12File); + KeyStore store = KeyStore.getInstance("PKCS12"); + store.load(is, password.toCharArray()); + return (PrivateKey)store.getKey("privateKey", password.toCharArray()); + } + +} http://git-wip-us.apache.org/repos/asf/cxf/blob/4bc00dc9/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryService.java ---------------------------------------------------------------------- diff --git a/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryService.java b/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryService.java index 49ab839..51f440e 100644 --- a/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryService.java +++ b/distribution/src/main/release/samples/jax_rs/big_query/src/main/java/demo/jaxrs/server/BigQueryService.java @@ -18,6 +18,7 @@ */ package demo.jaxrs.server; +import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -52,33 +53,40 @@ public class BigQueryService { public BigQueryResponse completeBigQuery(@Context OidcClientTokenContext context) { ClientAccessToken accessToken = context.getToken(); - bigQueryClient.authorization(accessToken); MultivaluedMap<String, String> state = context.getState(); String searchWord = state.getFirst("word"); String maxResults = state.getFirst("maxResults"); + BigQueryResponse bigQueryResponse = new BigQueryResponse(context.getUserInfo().getName(), + searchWord); + bigQueryResponse.setTexts(getMatchingTexts(bigQueryClient, accessToken, searchWord, maxResults)); + return bigQueryResponse; + } + + public void setBigQueryClient(WebClient bigQueryClient) { + this.bigQueryClient = bigQueryClient; + } + + static List<ShakespeareText> getMatchingTexts(WebClient bqClient, ClientAccessToken accessToken, + String searchWord, String maxResults) { + bqClient.authorization(accessToken); String bigQuerySelect = String.format(BQ_SELECT, searchWord); String bigQueryRequest = String.format(BQ_REQUEST, bigQuerySelect, Integer.parseInt(maxResults)); - JsonMapObject jsonMap = bigQueryClient.post(bigQueryRequest, JsonMapObject.class); - BigQueryResponse bigQueryResponse = new BigQueryResponse(context.getUserInfo().getName(), - searchWord); + JsonMapObject jsonMap = bqClient.post(bigQueryRequest, JsonMapObject.class); + List<ShakespeareText> texts = new LinkedList<ShakespeareText>(); List<Map<String, Object>> rows = CastUtils.cast((List<?>)jsonMap.getProperty("rows")); if (rows != null) { for (Map<String, Object> row : rows) { List<Map<String, Object>> fields = CastUtils.cast((List<?>)row.get("f")); ShakespeareText text = new ShakespeareText((String)fields.get(0).get("v"), (String)fields.get(1).get("v")); - bigQueryResponse.getTexts().add(text); + texts.add(text); } } - return bigQueryResponse; - } - - public void setBigQueryClient(WebClient bigQueryClient) { - this.bigQueryClient = bigQueryClient; + return texts; } }
