-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
251 additions
and
176 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
felles/klient/src/main/java/no/nav/vedtak/klient/http/BaseHttpClient.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,111 @@ | ||
package no.nav.vedtak.klient.http; | ||
|
||
import no.nav.vedtak.exception.IntegrasjonException; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.Set; | ||
|
||
public class BaseHttpClient { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(BaseHttpClient.class); | ||
private static final int RETRIES = 2; // 1 attempt, the n retries | ||
|
||
private final HttpClient httpClient; | ||
|
||
protected BaseHttpClient(HttpClient httpClient) { | ||
this.httpClient = httpClient; | ||
} | ||
|
||
public String send(HttpClientRequest request) { | ||
var httpRequest = request.request(); | ||
return ResponseHandler.handleResponse(doSendExpectStringRetry(httpRequest), httpRequest.uri(), Set.of()); | ||
} | ||
|
||
public String send(HttpClientRequest request, Set<Integer> acceptStatus) { | ||
var httpRequest = request.request(); | ||
return ResponseHandler.handleResponse(doSendExpectStringRetry(httpRequest), httpRequest.uri(), acceptStatus); | ||
} | ||
|
||
public byte[] sendReturnByteArray(HttpClientRequest request) { | ||
var httpRequest = request.request(); | ||
return ResponseHandler.handleResponse(doSendExpectBytearrayRetry(httpRequest), httpRequest.uri(), Set.of()); | ||
} | ||
|
||
public HttpResponse<String> sendReturnResponse(HttpClientRequest request) { | ||
var httpRequest = request.request(); | ||
return ResponseHandler.handleRawResponse(doSendExpectStringRetry(httpRequest), httpRequest.uri(), Set.of()); | ||
} | ||
|
||
public HttpResponse<String> sendReturnResponse(HttpClientRequest request, Set<Integer> acceptStatus) { | ||
var httpRequest = request.request(); | ||
return ResponseHandler.handleRawResponse(doSendExpectStringRetry(httpRequest), httpRequest.uri(), acceptStatus); | ||
} | ||
|
||
public HttpResponse<byte[]> sendReturnResponseByteArray(HttpClientRequest request) { | ||
var httpRequest = request.request(); | ||
return ResponseHandler.handleRawResponse(doSendExpectBytearrayRetry(httpRequest), httpRequest.uri(), Set.of()); | ||
} | ||
|
||
/** | ||
* Raw response, not checked for status codes 4nn or 5nn - please ensure that any usage avoids "quiet errors" | ||
*/ | ||
public HttpResponse<String> sendReturnUnhandled(HttpClientRequest request) { | ||
return doSendExpectStringRetry(request.request()); | ||
} | ||
|
||
public HttpResponse<String> sendReturnUnhandledNoRetry(HttpClientRequest request) { | ||
return doSendExpectString(request.request()); | ||
} | ||
|
||
private HttpResponse<byte[]> doSendExpectBytearrayRetry(HttpRequest httpRequest) { | ||
int i = RETRIES; | ||
while (i-- > 0) { | ||
try { | ||
return doSendExpectBytearray(httpRequest); | ||
} catch (IntegrasjonException e) { | ||
LOG.trace("F-157390 IntegrasjonException ved kall {} til endepunkt {}", RETRIES - i, e); | ||
} | ||
} | ||
return doSendExpectBytearray(httpRequest); | ||
} | ||
|
||
private HttpResponse<String> doSendExpectStringRetry(HttpRequest httpRequest) { | ||
int i = RETRIES; | ||
while (i-- > 0) { | ||
try { | ||
return doSendExpectString(httpRequest); | ||
} catch (IntegrasjonException e) { | ||
LOG.trace("F-157390 IntegrasjonException ved kall {} til endepunkt {}", RETRIES - i, e); | ||
} | ||
} | ||
return doSendExpectString(httpRequest); | ||
} | ||
|
||
private HttpResponse<byte[]> doSendExpectBytearray(HttpRequest request) { | ||
try { | ||
return httpClient.send(request, HttpResponse.BodyHandlers.ofByteArray()); | ||
} catch (IOException e) { | ||
throw new IntegrasjonException("F-157391", "Uventet IO-exception mot endepunkt", e); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new IntegrasjonException("F-157392", "InterruptedException ved kall mot endepunkt", e); | ||
} | ||
} | ||
|
||
private HttpResponse<String> doSendExpectString(HttpRequest request) { | ||
try { | ||
return httpClient.send(request, HttpResponse.BodyHandlers.ofString()); | ||
} catch (IOException e) { | ||
throw new IntegrasjonException("F-157391", "Uventet IO-exception mot endepunkt", e); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new IntegrasjonException("F-157392", "InterruptedException ved kall mot endepunkt", e); | ||
} | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
felles/klient/src/main/java/no/nav/vedtak/klient/http/ProxyHttpClient.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,42 @@ | ||
package no.nav.vedtak.klient.http; | ||
|
||
import no.nav.foreldrepenger.konfig.Environment; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.net.ProxySelector; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.time.Duration; | ||
import java.util.Optional; | ||
|
||
public final class ProxyHttpClient extends BaseHttpClient { | ||
private static final Environment ENV = Environment.current(); | ||
|
||
private static final String AZURE_HTTP_PROXY = "azure.http.proxy"; | ||
private static final String PROXY_KEY = "proxy.url"; | ||
private static final String DEFAULT_PROXY_URL = "http://webproxy.nais:8088"; | ||
|
||
private static ProxyHttpClient CLIENT; | ||
|
||
private ProxyHttpClient() { | ||
super(HttpClient.newBuilder() | ||
.connectTimeout(Duration.ofSeconds(15)) | ||
.proxy(Optional.ofNullable(ENV.isFss() ? URI.create(ENV.getProperty(AZURE_HTTP_PROXY, getDefaultProxy())) : null) | ||
.map(p -> new InetSocketAddress(p.getHost(), p.getPort())) | ||
.map(ProxySelector::of) | ||
.orElse(HttpClient.Builder.NO_PROXY)).build()); | ||
} | ||
|
||
public static synchronized ProxyHttpClient client() { | ||
var inst = CLIENT; | ||
if (inst == null) { | ||
inst = new ProxyHttpClient(); | ||
CLIENT = inst; | ||
} | ||
return inst; | ||
} | ||
|
||
private static String getDefaultProxy() { | ||
return ENV.getProperty(PROXY_KEY, DEFAULT_PROXY_URL); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...asjon/rest-klient/src/main/java/no/nav/vedtak/felles/integrasjon/rest/BaseRestClient.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,79 @@ | ||
package no.nav.vedtak.felles.integrasjon.rest; | ||
|
||
import no.nav.vedtak.klient.http.BaseHttpClient; | ||
import no.nav.vedtak.mapper.json.DefaultJsonMapper; | ||
|
||
import java.net.HttpURLConnection; | ||
import java.net.http.HttpResponse; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
class BaseRestClient { | ||
|
||
private BaseHttpClient httpklient; | ||
|
||
public BaseRestClient(BaseHttpClient httpClient) { | ||
this.httpklient = httpClient; | ||
} | ||
|
||
public <T> T send(RestRequest request, Class<T> clazz) { | ||
var response = httpklient.send(request); | ||
return mapResponse(response, s -> false, clazz); | ||
} | ||
|
||
public <T> T sendExpectConflict(RestRequest request, Class<T> clazz) { | ||
var response = httpklient.send(request, Set.of(HttpURLConnection.HTTP_CONFLICT)); | ||
return mapResponse(response, s -> false, clazz); | ||
} | ||
|
||
public <T> Optional<T> sendReturnOptional(RestRequest request, Class<T> clazz) { | ||
var response = httpklient.send(request); | ||
return Optional.ofNullable(mapResponse(response, String::isEmpty, clazz)); | ||
} | ||
|
||
public <T> List<T> sendReturnList(RestRequest request, Class<T> clazz) { | ||
var response = httpklient.send(request); | ||
return DefaultJsonMapper.listFromJson(response, clazz); | ||
} | ||
|
||
public <T> Map<String, T> sendReturnMap(RestRequest request, Class<T> clazz) { | ||
var response = httpklient.send(request); | ||
return DefaultJsonMapper.mapFromJson(response, clazz); | ||
} | ||
|
||
public byte[] sendReturnByteArray(RestRequest request) { | ||
return httpklient.sendReturnByteArray(request); | ||
} | ||
|
||
public HttpResponse<String> sendReturnResponseString(RestRequest request) { | ||
return httpklient.sendReturnResponse(request); | ||
} | ||
|
||
public HttpResponse<byte[]> sendReturnResponseByteArray(RestRequest request) { | ||
return httpklient.sendReturnResponseByteArray(request); | ||
} | ||
|
||
/** | ||
* Raw response, not checked for status codes 4nn or 5nn - please ensure that any usage avoids "quiet errors" | ||
*/ | ||
public HttpResponse<String> sendReturnUnhandled(RestRequest request) { | ||
return httpklient.sendReturnUnhandled(request); | ||
} | ||
|
||
public HttpResponse<String> sendReturnUnhandledNoRetry(RestRequest request) { | ||
return httpklient.sendReturnUnhandledNoRetry(request); | ||
} | ||
|
||
private <T> T mapResponse(String response, Predicate<String> filterOut, Class<T> clazz) { | ||
if (response == null || filterOut.test(response)) { | ||
return null; | ||
} | ||
if (clazz.isAssignableFrom(String.class)) { | ||
return clazz.cast(response); | ||
} | ||
return DefaultJsonMapper.fromJson(response, clazz); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...sjon/rest-klient/src/main/java/no/nav/vedtak/felles/integrasjon/rest/ProxyRestClient.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,15 @@ | ||
package no.nav.vedtak.felles.integrasjon.rest; | ||
|
||
import no.nav.vedtak.klient.http.ProxyHttpClient; | ||
|
||
public final class ProxyRestClient extends BaseRestClient { | ||
|
||
private ProxyRestClient() { | ||
super(ProxyHttpClient.client()); | ||
} | ||
|
||
public static ProxyRestClient client() { | ||
return new ProxyRestClient(); | ||
} | ||
|
||
} |
Oops, something went wrong.