Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit a09e7eb

Browse files
author
Petr Bouda
committed
J-484: Throw ResponseProcessingException during a processing of the Response on client
Change-Id: I0fbbf8f6c54bbbc8ea5cc73ff164f7a68c439520
1 parent dc607ba commit a09e7eb

File tree

10 files changed

+243
-71
lines changed

10 files changed

+243
-71
lines changed

core-client/src/main/java/org/glassfish/jersey/client/ClientFilteringStages.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import javax.ws.rs.ProcessingException;
4646
import javax.ws.rs.client.ClientRequestFilter;
4747
import javax.ws.rs.client.ClientResponseFilter;
48+
import javax.ws.rs.client.ResponseProcessingException;
4849
import javax.ws.rs.core.Response;
4950

5051
import org.glassfish.jersey.internal.inject.Providers;
@@ -139,7 +140,8 @@ public Continuation<ClientResponse> apply(ClientResponse responseContext) {
139140
filter.filter(responseContext.getRequestContext(), responseContext);
140141
}
141142
} catch (IOException ex) {
142-
throw new ProcessingException(ex);
143+
InboundJaxrsResponse response = new InboundJaxrsResponse(responseContext, null);
144+
throw new ResponseProcessingException(response, ex);
143145
}
144146

145147
return Continuation.of(responseContext, getDefaultNext());

core-client/src/main/java/org/glassfish/jersey/client/ClientRuntime.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.logging.Logger;
4949

5050
import javax.ws.rs.ProcessingException;
51+
import javax.ws.rs.client.ResponseProcessingException;
5152
import javax.ws.rs.core.HttpHeaders;
5253
import javax.ws.rs.core.MultivaluedMap;
5354

@@ -257,8 +258,8 @@ public ClientResponse invoke(final ClientRequest request) {
257258
}
258259

259260
return Stages.process(response, responseProcessingRoot);
260-
} catch (final ProcessingException ex) {
261-
throw ex;
261+
} catch (final ProcessingException pe) {
262+
throw pe;
262263
} catch (final Throwable t) {
263264
throw new ProcessingException(t.getMessage(), t);
264265
}

core-client/src/main/java/org/glassfish/jersey/client/JerseyInvocation.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import javax.ws.rs.client.Entity;
6666
import javax.ws.rs.client.Invocation;
6767
import javax.ws.rs.client.InvocationCallback;
68+
import javax.ws.rs.client.ResponseProcessingException;
6869
import javax.ws.rs.core.CacheControl;
6970
import javax.ws.rs.core.Cookie;
7071
import javax.ws.rs.core.GenericType;
@@ -801,11 +802,15 @@ private <T> T translate(final ClientResponse response, final RequestScope scope,
801802
try {
802803
return response.readEntity(responseType);
803804
} catch (final ProcessingException ex) {
804-
throw ex;
805+
if (ex.getClass() == ProcessingException.class) {
806+
throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), ex.getCause());
807+
}
808+
throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), ex);
805809
} catch (final WebApplicationException ex) {
806-
throw new ProcessingException(ex);
810+
throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), ex);
807811
} catch (final Exception ex) {
808-
throw new ProcessingException(LocalizationMessages.UNEXPECTED_ERROR_RESPONSE_PROCESSING(), ex);
812+
throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope),
813+
LocalizationMessages.UNEXPECTED_ERROR_RESPONSE_PROCESSING(), ex);
809814
}
810815
} else {
811816
throw convertToException(new InboundJaxrsResponse(response, scope));
@@ -861,11 +866,12 @@ private <T> T translate(final ClientResponse response, final RequestScope scope,
861866
try {
862867
return response.readEntity(responseType);
863868
} catch (final ProcessingException ex) {
864-
throw ex;
869+
throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), ex.getCause());
865870
} catch (final WebApplicationException ex) {
866-
throw new ProcessingException(ex);
871+
throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope), ex);
867872
} catch (final Exception ex) {
868-
throw new ProcessingException(LocalizationMessages.UNEXPECTED_ERROR_RESPONSE_PROCESSING(), ex);
873+
throw new ResponseProcessingException(new InboundJaxrsResponse(response, scope),
874+
LocalizationMessages.UNEXPECTED_ERROR_RESPONSE_PROCESSING(), ex);
869875
}
870876
} else {
871877
throw convertToException(new InboundJaxrsResponse(response, scope));
@@ -1022,9 +1028,9 @@ private ProcessingException convertToException(final Response response) {
10221028
}
10231029
}
10241030

1025-
return new ProcessingException(webAppException);
1031+
return new ResponseProcessingException(response, webAppException);
10261032
} catch (final Throwable t) {
1027-
return new ProcessingException(LocalizationMessages.RESPONSE_TO_EXCEPTION_CONVERSION_FAILED(), t);
1033+
return new ResponseProcessingException(response, LocalizationMessages.RESPONSE_TO_EXCEPTION_CONVERSION_FAILED(), t);
10281034
}
10291035
}
10301036

core-client/src/main/java/org/glassfish/jersey/client/authentication/BasicAuthenticator.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
package org.glassfish.jersey.client.authentication;
4242

43-
import javax.ws.rs.ProcessingException;
4443
import javax.ws.rs.client.ClientRequestContext;
4544
import javax.ws.rs.client.ClientResponseContext;
4645
import javax.ws.rs.core.HttpHeaders;
@@ -56,6 +55,7 @@
5655
* @author Craig McClanahan
5756
*/
5857
final class BasicAuthenticator {
58+
5959
private final HttpAuthenticationFilter.Credentials defaultCredentials;
6060

6161
/**
@@ -68,7 +68,6 @@ final class BasicAuthenticator {
6868
this.defaultCredentials = defaultCredentials;
6969
}
7070

71-
7271
private String calculateAuthentication(HttpAuthenticationFilter.Credentials credentials) {
7372
String username = credentials.getUsername();
7473
byte[] password = credentials.getPassword();
@@ -89,28 +88,30 @@ private String calculateAuthentication(HttpAuthenticationFilter.Credentials cred
8988
return "Basic " + Base64.encodeAsString(usernamePassword);
9089
}
9190

92-
9391
/**
9492
* Adds authentication information to the request.
9593
*
9694
* @param request Request context.
95+
* @throws RequestAuthenticationException in case that basic credentials missing or are in invalid format
9796
*/
98-
public void filterRequest(ClientRequestContext request) {
97+
public void filterRequest(ClientRequestContext request) throws RequestAuthenticationException {
9998
HttpAuthenticationFilter.Credentials credentials = HttpAuthenticationFilter.getCredentials(request,
10099
defaultCredentials, HttpAuthenticationFilter.Type.BASIC);
101100
if (credentials == null) {
102-
throw new ProcessingException(LocalizationMessages.AUTHENTICATION_CREDENTIALS_MISSING_BASIC());
101+
throw new RequestAuthenticationException(LocalizationMessages.AUTHENTICATION_CREDENTIALS_MISSING_BASIC());
103102
}
104103
request.getHeaders().add(HttpHeaders.AUTHORIZATION, calculateAuthentication(credentials));
105104
}
106105

107106
/**
108107
* Checks the response and if basic authentication is required then performs a new request
109108
* with basic authentication.
110-
* @param request Request context.
109+
*
110+
* @param request Request context.
111111
* @param response Response context (will be updated with newest response data if the request was repeated).
112112
* @return {@code true} if response does not require authentication or if authentication is required,
113-
* new request was done with digest authentication information and authentication was successful.
113+
* new request was done with digest authentication information and authentication was successful.
114+
* @throws ResponseAuthenticationException in case that basic credentials missing or are in invalid format
114115
*/
115116
public boolean filterResponseAndAuthenticate(ClientRequestContext request, ClientResponseContext response) {
116117
final String authenticate = response.getHeaders().getFirst(HttpHeaders.WWW_AUTHENTICATE);
@@ -119,7 +120,7 @@ public boolean filterResponseAndAuthenticate(ClientRequestContext request, Clien
119120
.getCredentials(request, defaultCredentials, HttpAuthenticationFilter.Type.BASIC);
120121

121122
if (credentials == null) {
122-
throw new ProcessingException(LocalizationMessages.AUTHENTICATION_CREDENTIALS_MISSING_BASIC());
123+
throw new ResponseAuthenticationException(null, LocalizationMessages.AUTHENTICATION_CREDENTIALS_MISSING_BASIC());
123124
}
124125

125126
return HttpAuthenticationFilter.repeatRequest(request, response, calculateAuthentication(credentials));

core-client/src/main/java/org/glassfish/jersey/client/authentication/DigestAuthenticator.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import java.util.regex.Matcher;
5353
import java.util.regex.Pattern;
5454

55-
import javax.ws.rs.ProcessingException;
5655
import javax.ws.rs.client.ClientRequestContext;
5756
import javax.ws.rs.client.ClientResponseContext;
5857
import javax.ws.rs.core.HttpHeaders;
@@ -84,26 +83,26 @@ final class DigestAuthenticator {
8483
* Create a new instance initialized from credentials and configuration.
8584
*
8685
* @param credentials Credentials. Can be {@code null} if there are no default credentials.
87-
* @param limit Maximum number of URIs that should be kept in the cache containing URIs and their
88-
* {@link org.glassfish.jersey.client.authentication.DigestAuthenticator.DigestScheme}.
86+
* @param limit Maximum number of URIs that should be kept in the cache containing URIs and their
87+
* {@link org.glassfish.jersey.client.authentication.DigestAuthenticator.DigestScheme}.
8988
*/
9089
DigestAuthenticator(final HttpAuthenticationFilter.Credentials credentials, final int limit) {
9190
this.credentials = credentials;
9291

9392
digestCache = Collections.synchronizedMap(new LinkedHashMap<URI, DigestScheme>(limit) {
94-
// use id as it is an anonymous inner class with changed behaviour
95-
private static final long serialVersionUID = 2546245625L;
93+
// use id as it is an anonymous inner class with changed behaviour
94+
private static final long serialVersionUID = 2546245625L;
9695

97-
@Override
98-
protected boolean removeEldestEntry(final Map.Entry eldest) {
99-
return size() > limit;
100-
}
101-
});
96+
@Override
97+
protected boolean removeEldestEntry(final Map.Entry eldest) {
98+
return size() > limit;
99+
}
100+
});
102101

103102
try {
104103
randomGenerator = SecureRandom.getInstance("SHA1PRNG");
105104
} catch (final NoSuchAlgorithmException e) {
106-
throw new ProcessingException(LocalizationMessages.ERROR_DIGEST_FILTER_GENERATOR(), e);
105+
throw new RequestAuthenticationException(LocalizationMessages.ERROR_DIGEST_FILTER_GENERATOR(), e);
107106
}
108107
}
109108

@@ -131,7 +130,7 @@ boolean filterRequest(final ClientRequestContext request) throws IOException {
131130
* Process response and repeat the request if digest authentication is requested. When request is repeated
132131
* the response will be modified to contain new response information.
133132
*
134-
* @param request Request context.
133+
* @param request Request context.
135134
* @param response Response context (will be updated with newest response data if the request was repeated).
136135
* @return {@code true} if response does not require authentication or if authentication is required,
137136
* new request was done with digest authentication information and authentication was successful.
@@ -150,7 +149,8 @@ public boolean filterResponse(final ClientRequestContext request, final ClientRe
150149
final HttpAuthenticationFilter.Credentials cred = HttpAuthenticationFilter.getCredentials(request,
151150
this.credentials, HttpAuthenticationFilter.Type.DIGEST);
152151
if (cred == null) {
153-
throw new RuntimeException(LocalizationMessages.AUTHENTICATION_CREDENTIALS_MISSING_DIGEST());
152+
153+
throw new ResponseAuthenticationException(null, LocalizationMessages.AUTHENTICATION_CREDENTIALS_MISSING_DIGEST());
154154
}
155155

156156
final boolean success = HttpAuthenticationFilter.repeatRequest(request, response, createNextAuthToken(digestScheme,
@@ -231,7 +231,7 @@ private DigestScheme parseAuthHeaders(final List<?> headers) throws IOException
231231
/**
232232
* Creates digest string including counter.
233233
*
234-
* @param ds DigestScheme instance
234+
* @param ds DigestScheme instance
235235
* @param requestContext client request context
236236
* @return digest authentication token string
237237
* @throws IOException
@@ -279,9 +279,9 @@ private String createNextAuthToken(final DigestScheme ds, final ClientRequestCon
279279
/**
280280
* Append comma separated key=value token
281281
*
282-
* @param sb string builder instance
283-
* @param key key string
284-
* @param value value string
282+
* @param sb string builder instance
283+
* @param key key string
284+
* @param value value string
285285
* @param useQuote true if value needs to be enclosed in quotes
286286
*/
287287
private static void append(final StringBuilder sb, final String key, final String value, final boolean useQuote) {
@@ -309,8 +309,8 @@ private static void append(final StringBuilder sb, final String key, final Strin
309309
* Append comma separated key=value token. The value gets enclosed in
310310
* quotes.
311311
*
312-
* @param sb string builder instance
313-
* @param key key string
312+
* @param sb string builder instance
313+
* @param key key string
314314
* @param value value string
315315
*/
316316
private static void append(final StringBuilder sb, final String key, final String value) {

core-client/src/main/java/org/glassfish/jersey/client/authentication/HttpAuthenticationFilter.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import java.util.Map;
5151

5252
import javax.ws.rs.Priorities;
53-
import javax.ws.rs.ProcessingException;
5453
import javax.ws.rs.client.Client;
5554
import javax.ws.rs.client.ClientBuilder;
5655
import javax.ws.rs.client.ClientRequestContext;
@@ -120,12 +119,12 @@ static enum Type {
120119
/**
121120
* Create a new filter instance.
122121
*
123-
* @param mode Mode.
124-
* @param basicCredentials Basic credentials (can be {@code null} if this filter does not work in the
125-
* basic mode or if no default credentials are defined).
122+
* @param mode Mode.
123+
* @param basicCredentials Basic credentials (can be {@code null} if this filter does not work in the
124+
* basic mode or if no default credentials are defined).
126125
* @param digestCredentials Digest credentials (can be {@code null} if this filter does not work in the
127-
* digest mode or if no default credentials are defined).
128-
* @param configuration Configuration (non-{@code null}).
126+
* digest mode or if no default credentials are defined).
127+
* @param configuration Configuration (non-{@code null}).
129128
*/
130129
HttpAuthenticationFilter(HttpAuthenticationFeature.Mode mode, Credentials basicCredentials,
131130
Credentials digestCredentials, Configuration configuration) {
@@ -288,13 +287,11 @@ private void updateCache(ClientRequestContext request, boolean success, Type ope
288287
* Repeat the {@code request} with provided {@code newAuthorizationHeader}
289288
* and update the {@code response} with newest response data.
290289
*
291-
* @param request Request context.
292-
* @param response Response context (will be updated with the new response data).
290+
* @param request Request context.
291+
* @param response Response context (will be updated with the new response data).
293292
* @param newAuthorizationHeader {@code Authorization} header that should be added to the new request.
294-
*
295293
* @return {@code true} is the authentication was successful ({@code true} if 401 response code was not returned;
296294
* {@code false} otherwise).
297-
*
298295
*/
299296
static boolean repeatRequest(ClientRequestContext request, ClientResponseContext response, String newAuthorizationHeader) {
300297
Client client = ClientBuilder.newClient(request.getConfiguration());
@@ -350,6 +347,7 @@ static class Credentials {
350347

351348
/**
352349
* Create a new credentials from username and password as byte array.
350+
*
353351
* @param username Username.
354352
* @param password Password as byte array.
355353
*/
@@ -360,6 +358,7 @@ static class Credentials {
360358

361359
/**
362360
* Create a new credentials from username and password as {@link String}.
361+
*
363362
* @param username Username.
364363
* @param password {@code String} password.
365364
*/
@@ -410,7 +409,8 @@ private static Credentials extractCredentials(ClientRequestContext request, Type
410409
} else if (password instanceof String) {
411410
pwdBytes = ((String) password).getBytes(CHARACTER_SET);
412411
} else {
413-
throw new ProcessingException(LocalizationMessages.AUTHENTICATION_CREDENTIALS_REQUEST_PASSWORD_UNSUPPORTED());
412+
throw new RequestAuthenticationException(
413+
LocalizationMessages.AUTHENTICATION_CREDENTIALS_REQUEST_PASSWORD_UNSUPPORTED());
414414
}
415415
return new Credentials(userName, pwdBytes);
416416
}
@@ -420,16 +420,16 @@ private static Credentials extractCredentials(ClientRequestContext request, Type
420420
/**
421421
* Get credentials actual for the current request. Priorities in credentials selection are the following:
422422
* <ol>
423-
* <li>Basic/digest specific credentials defined in the request properties</li>
424-
* <li>Common credentials defined in the request properties</li>
425-
* <li>{@code defaultCredentials}</li>
423+
* <li>Basic/digest specific credentials defined in the request properties</li>
424+
* <li>Common credentials defined in the request properties</li>
425+
* <li>{@code defaultCredentials}</li>
426426
* </ol>
427427
*
428-
* @param request Request from which credentials should be extracted.
428+
* @param request Request from which credentials should be extracted.
429429
* @param defaultCredentials Default credentials (can be {@code null}).
430-
* @param type Type of requested credentials.
431-
*
430+
* @param type Type of requested credentials.
432431
* @return Credentials or {@code null} if no credentials are found and {@code defaultCredentials} are {@code null}.
432+
* @throws RequestAuthenticationException in case the {@code username} or {@code password} is invalid.
433433
*/
434434
static Credentials getCredentials(ClientRequestContext request, Credentials defaultCredentials, Type type) {
435435
Credentials commonCredentials = extractCredentials(request, type);

0 commit comments

Comments
 (0)