cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: Mostly finalizing ClientCodeRequestFilter improvements for now
Date Wed, 13 May 2015 13:20:32 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 06bb31c09 -> 73b44127f


Mostly finalizing ClientCodeRequestFilter improvements for now


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/73b44127
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/73b44127
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/73b44127

Branch: refs/heads/3.0.x-fixes
Commit: 73b44127f9c0148413dfa950ea226614d83e49bb
Parents: 06bb31c
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Wed May 13 14:18:58 2015 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Wed May 13 14:20:12 2015 +0100

----------------------------------------------------------------------
 .../apache/cxf/jaxrs/client/AbstractClient.java |  9 ++
 .../org/apache/cxf/jaxrs/client/Client.java     | 47 ++++++----
 .../org/apache/cxf/jaxrs/client/WebClient.java  |  4 +-
 .../oauth2/client/BearerAuthSupplier.java       |  5 +-
 .../oauth2/client/ClientCodeRequestFilter.java  | 97 +++++++++++++++-----
 5 files changed, 116 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/73b44127/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
----------------------------------------------------------------------
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
index bf3dc6d..dec951b 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
@@ -197,6 +197,15 @@ public abstract class AbstractClient implements Client {
     /**
      * {@inheritDoc}
      */
+    public Client authorization(Object auth) {
+        String value = convertParamValue(auth, null);
+        state.getRequestHeaders().putSingle(HttpHeaders.AUTHORIZATION, value);
+        return this;
+    }
+    
+    /**
+     * {@inheritDoc}
+     */
     public Client modified(Date date, boolean ifNot) {
         SimpleDateFormat dateFormat = HttpUtils.getHttpDateFormat();
         String hName = ifNot ? HttpHeaders.IF_UNMODIFIED_SINCE : HttpHeaders.IF_MODIFIED_SINCE;

http://git-wip-us.apache.org/repos/asf/cxf/blob/73b44127/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/Client.java
----------------------------------------------------------------------
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/Client.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/Client.java
index 6b74e61..ad5f0c5 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/Client.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/Client.java
@@ -34,63 +34,63 @@ import javax.ws.rs.core.Response;
 public interface Client {
     
     /**
-     * sets HTTP Content-Type header
+     * Set HTTP Content-Type header
      * @param ct JAXRS MediaType representing Content-Type value  
      * @return the updated Client
      */
     Client type(MediaType ct);
     
     /**
-     * sets HTTP Content-Type header
+     * Set HTTP Content-Type header
      * @param type Content-Type value  
      * @return the updated Client
      */
     Client type(String type);
     
     /**
-     * sets HTTP Accept header
+     * Set HTTP Accept header
      * @param types list of JAXRS MediaTypes representing Accept header values  
      * @return the updated Client
      */
     Client accept(MediaType... types);
     
     /**
-     * sets HTTP Accept header
+     * Set HTTP Accept header
      * @param types list of Accept header values  
      * @return the updated Client
      */
     Client accept(String... types);
     
     /**
-     * sets HTTP Content-Language header 
+     * Set HTTP Content-Language header 
      * @param language Content-Language header value  
      * @return the updated Client
      */    
     Client language(String language);
     
     /**
-     * sets HTTP Accept-Language header 
+     * Set HTTP Accept-Language header 
      * @param languages list of Accept-Language header values  
      * @return the updated Client
      */
     Client acceptLanguage(String ...languages);
     
     /**
-     * sets HTTP Content-Encoding header 
+     * Set HTTP Content-Encoding header 
      * @param encoding Content-Encoding header value  
      * @return the updated Client
      */
     Client encoding(String encoding);
     
     /**
-     * sets HTTP Accept-Encoding header 
+     * Set HTTP Accept-Encoding header 
      * @param encodings list of Accept-Encoding header value  
      * @return the updated Client
      */
     Client acceptEncoding(String ...encodings);
     
     /**
-     * sets HTTP If-Match or If-None-Match header
+     * Set HTTP If-Match or If-None-Match header
      * @param tag ETag value
      * @param ifNot if true then If-None-Match is set, If-Match otherwise  
      * @return the updated Client
@@ -98,7 +98,7 @@ public interface Client {
     Client match(EntityTag tag, boolean ifNot);
     
     /**
-     * sets HTTP If-Modified-Since or If-Unmodified-Since header
+     * Set HTTP If-Modified-Since or If-Unmodified-Since header
      * @param date Date value, will be formated as "EEE, dd MMM yyyy HH:mm:ss zzz" 
      * @param ifNot if true then If-Unmodified-Since is set, If-Modified-Since otherwise
 
      * @return the updated Client
@@ -106,14 +106,21 @@ public interface Client {
     Client modified(Date date, boolean ifNot);
     
     /**
-     * sets HTTP Cookie header 
+     * Set HTTP Cookie header 
      * @param cookie Cookie value  
      * @return the updated Client
      */
     Client cookie(Cookie cookie);
     
     /**
-     * Updates the current URI query parameters
+     * Set HTTP Authorization header 
+     * @param auth Authorization value  
+     * @return the updated Client
+     */
+    Client authorization(Object auth);
+    
+    /**
+     * Update the current URI query parameters
      * @param name query name
      * @param values query values
      * @return updated WebClient
@@ -121,7 +128,7 @@ public interface Client {
     Client query(String name, Object ...values);
     
     /**
-     * Sets arbitrary HTTP Header
+     * Set arbitrary HTTP Header
      * @param name header name
      * @param values list of header values
      * @return the updated Client
@@ -129,44 +136,44 @@ public interface Client {
     Client header(String name, Object... values);
     
     /**
-     * Sets HTTP Headers
+     * Set HTTP Headers
      * @param map headers
      * @return the updated Client
      */
     Client headers(MultivaluedMap<String, String> map);
 
     /**
-     * Resets the headers and response state if any
+     * Reset the headers and response state if any
      * @return  the updated Client
      */
     Client reset();
     
     /**
-     * Gets the copy of request headers
+     * Get the copy of request headers
      * @return request headers
      */
     MultivaluedMap<String, String> getHeaders();
     
     /**
-     * Gets the base URI this Client has been intialized with
+     * Get the base URI this Client has been intialized with
      * @return base URI
      */
     URI getBaseURI();
     
     /**
-     * Gets the current URI this Client is working with
+     * Get the current URI this Client is working with
      * @return current URI
      */
     URI getCurrentURI();
     
     /**
-     * Gets the response state if any
+     * Get the response state if any
      * @return JAXRS Response response
      */
     Response getResponse();
     
     /**
-     * Releases the internal state and configuration associated with this client 
+     * Release the internal state and configuration associated with this client 
      */
     void close();
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/73b44127/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
----------------------------------------------------------------------
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
index 259e769..3c3015a 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
@@ -766,11 +766,11 @@ public class WebClient extends AbstractClient {
      * @param value new values, null is equivalent to removing the header
      * @return updated WebClient
      */
-    public WebClient replaceHeader(String headerName, String value) {
+    public WebClient replaceHeader(String headerName, Object value) {
         MultivaluedMap<String, String> headers = getState().getRequestHeaders();
         headers.remove(headerName);
         if (value != null) {
-            headers.add(headerName, value);
+            super.header(headerName, value);
         }
         return this;
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/73b44127/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/BearerAuthSupplier.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/BearerAuthSupplier.java
b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/BearerAuthSupplier.java
index 475c2cb..6b1f60c 100644
--- a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/BearerAuthSupplier.java
+++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/BearerAuthSupplier.java
@@ -69,9 +69,8 @@ public class BearerAuthSupplier extends AbstractAuthSupplier implements
HttpAuth
     }
     private void refreshAccessTokenIfExpired(AuthorizationPolicy authPolicy) {
         ClientAccessToken at = getClientAccessToken();
-        if (at.getExpiresIn() != -1 
-            && OAuthUtils.isExpired(at.getIssuedAt(), 
-                                    at.getExpiresIn())) {
+        if (OAuthUtils.isExpired(at.getIssuedAt(), 
+                                 at.getExpiresIn())) {
             refreshAccessToken(authPolicy);
         }
         

http://git-wip-us.apache.org/repos/asf/cxf/blob/73b44127/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/ClientCodeRequestFilter.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/ClientCodeRequestFilter.java
b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/ClientCodeRequestFilter.java
index 603d2da..2a2d298 100644
--- a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/ClientCodeRequestFilter.java
+++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/ClientCodeRequestFilter.java
@@ -44,6 +44,7 @@ import org.apache.cxf.rs.security.oauth2.common.AccessTokenGrant;
 import org.apache.cxf.rs.security.oauth2.common.ClientAccessToken;
 import org.apache.cxf.rs.security.oauth2.grants.code.AuthorizationCodeGrant;
 import org.apache.cxf.rs.security.oauth2.utils.OAuthConstants;
+import org.apache.cxf.rs.security.oauth2.utils.OAuthUtils;
 
 @PreMatching
 @Priority(Priorities.AUTHENTICATION + 1)
@@ -52,7 +53,7 @@ public class ClientCodeRequestFilter implements ContainerRequestFilter {
     private MessageContext mc;
     
     private String scopes;
-    private String relRedirectUri;
+    private String completeUri;
     private String startUri;
     private String authorizationServiceUri;
     private OAuthClientUtils.Consumer consumer;
@@ -60,6 +61,7 @@ public class ClientCodeRequestFilter implements ContainerRequestFilter {
     private ClientTokenContextManager clientTokenContextManager;
     private WebClient accessTokenService;
     private boolean decodeRequestParameters;
+    private long expiryThreshold;
     
     @Override
     public void filter(ContainerRequestContext rc) throws IOException {
@@ -68,18 +70,29 @@ public class ClientCodeRequestFilter implements ContainerRequestFilter
{
             throw ExceptionUtils.toNotAuthorizedException(null, null);
         }
         UriInfo ui = rc.getUriInfo();
-        if (ui.getPath().endsWith(startUri)) {
-            if (clientTokenContextManager != null) {
-                ClientTokenContext request = clientTokenContextManager.getClientTokenContext(mc);
-                if (request != null) {
-                    setClientCodeRequest(request);
-                    rc.setRequestUri(URI.create(relRedirectUri));
-                    return;
+        String absoluteRequestUri = ui.getAbsolutePath().toString();
+        
+        boolean sameUriRedirect = false;
+        if (completeUri == null) {
+            String referer = rc.getHeaderString("Referer");
+            if (referer != null && referer.startsWith(authorizationServiceUri)) {
+                completeUri = absoluteRequestUri;
+                sameUriRedirect = true;
+            }
+        }
+        
+        if (!sameUriRedirect && absoluteRequestUri.endsWith(startUri)) {
+            ClientTokenContext request = getClientTokenContext();
+            if (request != null) {
+                setClientCodeRequest(request);
+                if (completeUri != null) {
+                    rc.setRequestUri(URI.create(completeUri));
                 }
+                return;
             }
             Response codeResponse = createCodeResponse(rc, sc, ui);
             rc.abortWith(codeResponse);
-        } else if (ui.getPath().endsWith(relRedirectUri)) {
+        } else if (absoluteRequestUri.endsWith(completeUri)) {
             processCodeResponse(rc, sc, ui);
         }
     }
@@ -97,7 +110,12 @@ public class ClientCodeRequestFilter implements ContainerRequestFilter
{
     }
 
     private URI getAbsoluteRedirectUri(UriInfo ui) {
-        return ui.getBaseUriBuilder().path(relRedirectUri).build();
+        if (completeUri != null) {
+            return completeUri.startsWith("http") ? URI.create(completeUri) 
+                : ui.getBaseUriBuilder().path(completeUri).build();
+        } else {
+            return ui.getAbsolutePath();
+        }
     }
     protected void processCodeResponse(ContainerRequestContext rc, SecurityContext sc, UriInfo
ui) {
         MultivaluedMap<String, String> params = toRequestState(rc, ui);
@@ -107,18 +125,26 @@ public class ClientCodeRequestFilter implements ContainerRequestFilter
{
             AccessTokenGrant grant = new AuthorizationCodeGrant(codeParam, getAbsoluteRedirectUri(ui));
             at = OAuthClientUtils.getAccessToken(accessTokenService, consumer, grant);
         }
-        ClientTokenContext request = createTokenContext(at);
-        ((ClientTokenContextImpl)request).setToken(at);
-        if (clientStateManager != null) {
-            MultivaluedMap<String, String> state = clientStateManager.fromRedirectState(mc,
params);
-            ((ClientTokenContextImpl)request).setState(state);
-        }
+        ClientTokenContext tokenContext = initializeClientTokenContext(at, params);
         if (at != null && clientTokenContextManager != null) {
-            clientTokenContextManager.setClientTokenContext(mc, request);
+            clientTokenContextManager.setClientTokenContext(mc, tokenContext);
         }
-        setClientCodeRequest(request);
+        setClientCodeRequest(tokenContext);
     }
     
+    private ClientTokenContext initializeClientTokenContext(ClientAccessToken at, 
+                                                            MultivaluedMap<String, String>
params) {
+        ClientTokenContext tokenContext = createTokenContext(at);
+        ((ClientTokenContextImpl)tokenContext).setToken(at);
+        if (clientStateManager != null) {
+            MultivaluedMap<String, String> state = clientStateManager.fromRedirectState(mc,
params);
+            ((ClientTokenContextImpl)tokenContext).setState(state);
+        }
+        
+        return tokenContext;
+        
+    }
+
     protected ClientTokenContext createTokenContext(ClientAccessToken at) {
         return new ClientTokenContextImpl();
     }
@@ -160,7 +186,7 @@ public class ClientCodeRequestFilter implements ContainerRequestFilter
{
         this.scopes = scopesString;
     }
 
-    public void setRelativeStartUri(String relStartUri) {
+    public void setStartUri(String relStartUri) {
         this.startUri = relStartUri;
     }
 
@@ -168,8 +194,8 @@ public class ClientCodeRequestFilter implements ContainerRequestFilter
{
         this.authorizationServiceUri = authorizationServiceUri;
     }
 
-    public void setRelativeCompleteUri(String completeUri) {
-        this.relRedirectUri = completeUri;
+    public void setCompleteUri(String completeUri) {
+        this.completeUri = completeUri;
     }
 
     public void setAccessTokenService(WebClient accessTokenService) {
@@ -195,4 +221,33 @@ public class ClientCodeRequestFilter implements ContainerRequestFilter
{
         this.decodeRequestParameters = decodeRequestParameters;
     }
 
+    private ClientTokenContext getClientTokenContext() {
+        ClientTokenContext ctx = null;
+        if (clientTokenContextManager != null) {
+            ctx = clientTokenContextManager.getClientTokenContext(mc);
+            if (ctx != null) {
+                ClientAccessToken newAt = refreshAccessTokenIfExpired(ctx.getToken());
+                if (newAt != null) {
+                    clientTokenContextManager.removeClientTokenContext(mc, ctx);
+                    ClientTokenContext newCtx = initializeClientTokenContext(newAt, ctx.getState());
           
+                    clientTokenContextManager.setClientTokenContext(mc, newCtx);
+                    ctx = newCtx;
+                }
+            }
+        }
+        return ctx;
+    }
+    
+    private ClientAccessToken refreshAccessTokenIfExpired(ClientAccessToken at) {
+        if (at.getRefreshToken() != null
+            && ((expiryThreshold > 0 && OAuthUtils.isExpired(at.getIssuedAt(),
at.getExpiresIn() - expiryThreshold))
+            || OAuthUtils.isExpired(at.getIssuedAt(), at.getExpiresIn()))) {
+            return OAuthClientUtils.refreshAccessToken(accessTokenService, consumer, at);
+        }
+        return null;
+    }
+
+    public void setExpiryThreshold(long expiryThreshold) {
+        this.expiryThreshold = expiryThreshold;
+    }
 }


Mime
View raw message