Repository: cxf Updated Branches: refs/heads/master e0a8c1656 -> 6d8303541
Adding a grant MBW Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/6d830354 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/6d830354 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/6d830354 Branch: refs/heads/master Commit: 6d830354143f91f317d9faf24bbbb63c9ceeab44 Parents: e0a8c16 Author: Sergey Beryozkin <[email protected]> Authored: Fri Jun 19 17:49:15 2015 +0100 Committer: Sergey Beryozkin <[email protected]> Committed: Fri Jun 19 17:49:15 2015 +0100 ---------------------------------------------------------------------- .../java/demo/jaxrs/server/BigQueryServer.java | 17 ++++-- .../oauth2/client/AccessTokenGrantWriter.java | 58 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/6d830354/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 index 373ddae..9b368b8 100644 --- 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 @@ -22,9 +22,12 @@ import java.io.FileInputStream; import java.io.InputStream; import java.security.KeyStore; import java.security.PrivateKey; +import java.util.Arrays; import java.util.Collections; import java.util.List; +import javax.ws.rs.core.MediaType; + import org.apache.cxf.jaxrs.client.WebClient; import org.apache.cxf.jaxrs.provider.json.JsonMapObjectProvider; import org.apache.cxf.rs.security.jose.JoseType; @@ -33,9 +36,10 @@ 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.client.AccessTokenGrantWriter; 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.provider.OAuthJSONProvider; import org.apache.cxf.rs.security.oauth2.utils.OAuthUtils; @@ -54,8 +58,8 @@ public class BigQueryServer { 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"); + bigQueryClient.accept(MediaType.APPLICATION_JSON); + bigQueryClient.type(MediaType.APPLICATION_JSON); List<ShakespeareText> texts = BigQueryService.getMatchingTexts(bigQueryClient, accessToken, "brave", "10"); @@ -82,8 +86,11 @@ public class BigQueryServer { JwtBearerGrant grant = new JwtBearerGrant(base64UrlAssertion); - WebClient accessTokenService = WebClient.create("https://www.googleapis.com/oauth2/v3/token"); - return OAuthClientUtils.getAccessToken(accessTokenService, grant); + WebClient accessTokenService = WebClient.create("https://www.googleapis.com/oauth2/v3/token", + Arrays.asList(new OAuthJSONProvider(), new AccessTokenGrantWriter())); + accessTokenService.type(MediaType.APPLICATION_FORM_URLENCODED); + accessTokenService.accept(MediaType.APPLICATION_JSON); + return accessTokenService.post(grant, ClientAccessToken.class); } private static PrivateKey loadPrivateKey(String p12File, String password) throws Exception { http://git-wip-us.apache.org/repos/asf/cxf/blob/6d830354/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/AccessTokenGrantWriter.java ---------------------------------------------------------------------- diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/AccessTokenGrantWriter.java b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/AccessTokenGrantWriter.java new file mode 100644 index 0000000..5654a71 --- /dev/null +++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/AccessTokenGrantWriter.java @@ -0,0 +1,58 @@ +/** + * 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 org.apache.cxf.rs.security.oauth2.client; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Form; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; + +import org.apache.cxf.jaxrs.utils.FormUtils; +import org.apache.cxf.rs.security.oauth2.common.AccessTokenGrant; + +@Produces(MediaType.APPLICATION_FORM_URLENCODED) +public class AccessTokenGrantWriter implements MessageBodyWriter<AccessTokenGrant> { + + @Override + public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return true; + } + + @Override + public long getSize(AccessTokenGrant t, Class<?> type, Type genericType, Annotation[] annotations, + MediaType mediaType) { + return -1; + } + + @Override + public void writeTo(AccessTokenGrant t, Class<?> type, Type genericType, Annotation[] annotations, + MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, + OutputStream entityStream) throws IOException, WebApplicationException { + String form = FormUtils.formToString(new Form(t.toMap())); + entityStream.write(form.getBytes()); + } + +}
