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

Commit f874edc

Browse files
Marek PotociarGerrit Code Review
authored andcommitted
Merge "Findbugs + improved fix for J-484 (JERSEY-2468)"
2 parents 9f0e149 + 3a5aa7b commit f874edc

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ class InboundJaxrsResponse extends Response {
8585
public InboundJaxrsResponse(final ClientResponse context, final RequestScope scope) {
8686
this.context = context;
8787
this.scope = scope;
88-
this.scopeInstance = scope.referenceCurrent();
88+
if (this.scope != null) {
89+
this.scopeInstance = scope.referenceCurrent();
90+
} else {
91+
this.scopeInstance = null;
92+
}
8993
}
9094

9195
@Override
@@ -105,7 +109,7 @@ public Object getEntity() throws IllegalStateException {
105109

106110
@Override
107111
public <T> T readEntity(final Class<T> entityType) throws ProcessingException, IllegalStateException {
108-
return scope.runInScope(scopeInstance, new Producer<T>() {
112+
return runInScopeIfPossible(new Producer<T>() {
109113
@Override
110114
public T call() {
111115
return context.readEntity(entityType);
@@ -116,7 +120,7 @@ public T call() {
116120
@Override
117121
@SuppressWarnings("unchecked")
118122
public <T> T readEntity(final GenericType<T> entityType) throws ProcessingException, IllegalStateException {
119-
return scope.runInScope(scopeInstance, new Producer<T>() {
123+
return runInScopeIfPossible(new Producer<T>() {
120124
@Override
121125
public T call() {
122126
return context.readEntity(entityType);
@@ -127,7 +131,7 @@ public T call() {
127131
@Override
128132
public <T> T readEntity(final Class<T> entityType, final Annotation[] annotations)
129133
throws ProcessingException, IllegalStateException {
130-
return scope.runInScope(scopeInstance, new Producer<T>() {
134+
return runInScopeIfPossible(new Producer<T>() {
131135
@Override
132136
public T call() {
133137
return context.readEntity(entityType, annotations);
@@ -139,7 +143,7 @@ public T call() {
139143
@SuppressWarnings("unchecked")
140144
public <T> T readEntity(final GenericType<T> entityType, final Annotation[] annotations)
141145
throws ProcessingException, IllegalStateException {
142-
return scope.runInScope(scopeInstance, new Producer<T>() {
146+
return runInScopeIfPossible(new Producer<T>() {
143147
@Override
144148
public T call() {
145149
return context.readEntity(entityType, annotations);
@@ -162,7 +166,9 @@ public void close() throws ProcessingException {
162166
try {
163167
context.close();
164168
} finally {
165-
scopeInstance.release();
169+
if (scopeInstance != null) {
170+
scopeInstance.release();
171+
}
166172
}
167173
}
168174

@@ -255,4 +261,12 @@ public String toString() {
255261
.add("context", context)
256262
.toString();
257263
}
264+
265+
private <T> T runInScopeIfPossible(Producer<T> producer) {
266+
if (scope != null && scopeInstance != null) {
267+
return scope.runInScope(scopeInstance, producer);
268+
} else {
269+
return producer.call();
270+
}
271+
}
258272
}

core-server/src/main/java/org/glassfish/jersey/server/filter/EncodingFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public boolean equals(Object obj) {
231231
@Override
232232
public int compareTo(ContentEncoding o) {
233233
// higher q goes first (i.e. descending order)
234-
return Integer.valueOf(o.q).compareTo(q);
234+
return Integer.compare(o.q, q);
235235
}
236236
}
237237

tests/e2e/src/test/java/org/glassfish/jersey/tests/e2e/client/ClientFilterTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
import javax.ws.rs.Path;
4646
import javax.ws.rs.client.ClientRequestContext;
4747
import javax.ws.rs.client.ClientRequestFilter;
48+
import javax.ws.rs.client.ClientResponseContext;
49+
import javax.ws.rs.client.ClientResponseFilter;
50+
import javax.ws.rs.client.ResponseProcessingException;
4851
import javax.ws.rs.client.WebTarget;
4952
import javax.ws.rs.core.Application;
5053
import javax.ws.rs.core.CacheControl;
@@ -60,6 +63,7 @@
6063

6164
import org.junit.Test;
6265
import static org.junit.Assert.assertEquals;
66+
import static org.junit.Assert.assertNotNull;
6367
import static org.junit.Assert.assertTrue;
6468

6569
/**
@@ -122,4 +126,33 @@ public void filterHeaderProvidersTest() {
122126
assertTrue(entity.contains("private"));
123127
assertTrue(entity.contains("cookie-value"));
124128
}
129+
130+
public static class IOExceptionResponseFilter implements ClientResponseFilter {
131+
132+
@Override
133+
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws
134+
IOException {
135+
throw new IOException(IOExceptionResponseFilter.class.getName());
136+
137+
}
138+
}
139+
140+
@Test
141+
public void ioExceptionResponseFilterTest() {
142+
final WebTarget target = target();
143+
target.register(IOExceptionResponseFilter.class).register(LoggingFilter.class);
144+
145+
boolean caught = false;
146+
147+
try {
148+
target.path("test").request().get(Response.class);
149+
} catch (ResponseProcessingException e) {
150+
caught = true;
151+
assertNotNull(e.getCause());
152+
assertEquals(IOException.class, e.getCause().getClass());
153+
assertEquals(IOExceptionResponseFilter.class.getName(), e.getCause().getMessage());
154+
}
155+
156+
assertTrue(caught);
157+
}
125158
}

0 commit comments

Comments
 (0)