This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix NPE issue with http HEAD (#1809)
* feign can't handle empty/null reponse body :-( * move DB truncation into the same transaction as the insert, otherwise we might run into inconsistency! * RetentionPolicyDccRevocationTest * add DccRevocationClientDelegatorTest
- Loading branch information
Showing
13 changed files
with
133 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...in/java/app/coronawarn/server/services/distribution/dcc/DccRevocationClientDelegator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package app.coronawarn.server.services.distribution.dcc; | ||
|
||
import feign.Client; | ||
import feign.Request; | ||
import feign.Request.Options; | ||
import feign.Response; | ||
import feign.Response.Body; | ||
import feign.httpclient.ApacheHttpClient; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class DccRevocationClientDelegator implements Client { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DccRevocationClientDelegator.class); | ||
|
||
private final ApacheHttpClient apacheHttpClient; | ||
|
||
public DccRevocationClientDelegator(final ApacheHttpClient apacheHttpClient) { | ||
this.apacheHttpClient = apacheHttpClient; | ||
} | ||
|
||
@Override | ||
public Response execute(final Request request, final Options options) throws IOException { | ||
final Response response = apacheHttpClient.execute(request, options); | ||
|
||
// in case of http HEAD the response is NULL! | ||
final Body body = response.body(); | ||
if (body != null) { | ||
return response; | ||
} else { | ||
logger.info("response body is null for '{}'", request); | ||
} | ||
|
||
return Response.builder() | ||
.status(response.status()) | ||
.reason(response.reason()) | ||
.headers(response.headers()) | ||
.request(response.request()) | ||
.body("", StandardCharsets.UTF_8).build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ava/app/coronawarn/server/services/distribution/dcc/DccRevocationClientDelegatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package app.coronawarn.server.services.distribution.dcc; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import feign.Request; | ||
import feign.Request.Body; | ||
import feign.Request.HttpMethod; | ||
import feign.Request.Options; | ||
import feign.RequestTemplate; | ||
import feign.Response; | ||
import feign.httpclient.ApacheHttpClient; | ||
import java.util.Collections; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DccRevocationClientDelegatorTest { | ||
|
||
@Test | ||
void nullBodyShouldBeTurnedIntoEmptyString() throws Exception { | ||
final ApacheHttpClient client = mock(ApacheHttpClient.class); | ||
final Request request = Request.create(HttpMethod.GET, "http://localhost", Collections.emptyMap(), Body.empty(), | ||
(RequestTemplate) null); | ||
final Response mockResponse = Response.builder().request(request).body((Response.Body) null).build(); | ||
when(client.execute(any(), any())).thenReturn(mockResponse); | ||
final DccRevocationClientDelegator fixture = new DccRevocationClientDelegator(client); | ||
assertNull(mockResponse.body()); | ||
final Response response = fixture.execute(request, new Options()); | ||
assertNotNull(response.body()); | ||
} | ||
|
||
@Test | ||
void responseIsNotChangedIfBodyIsNotNull() throws Exception { | ||
final ApacheHttpClient client = mock(ApacheHttpClient.class); | ||
final Request request = Request.create(HttpMethod.GET, "http://localhost", Collections.emptyMap(), Body.empty(), | ||
(RequestTemplate) null); | ||
final Response mockResponse = Response.builder().request(request).body("foo".getBytes()).build(); | ||
when(client.execute(any(), any())).thenReturn(mockResponse); | ||
final DccRevocationClientDelegator fixture = new DccRevocationClientDelegator(client); | ||
assertNotNull(mockResponse.body()); | ||
final Response response = fixture.execute(request, new Options()); | ||
assertEquals(mockResponse, response); | ||
} | ||
|
||
@Test | ||
void testDccRevocationClientDelegator() { | ||
final DccRevocationClientDelegator fixture = new DccRevocationClientDelegator(new ApacheHttpClient()); | ||
assertNotNull(fixture); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters