-
Notifications
You must be signed in to change notification settings - Fork 3
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
Luke Sikina
committed
Oct 31, 2023
1 parent
ea5ef2b
commit f0a500f
Showing
5 changed files
with
184 additions
and
5 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
70 changes: 70 additions & 0 deletions
70
...pic-sure-resource-api/src/main/java/edu/harvard/dbmi/avillach/service/ProxyWebClient.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,70 @@ | ||
package edu.harvard.dbmi.avillach.service; | ||
|
||
import edu.harvard.dbmi.avillach.util.HttpClientUtil; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.client.methods.HttpRequestBase; | ||
import org.apache.http.client.utils.URIBuilder; | ||
import org.apache.http.entity.StringEntity; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.core.Response; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
public class ProxyWebClient { | ||
private static final Logger LOG = LoggerFactory.getLogger(ProxyWebClient.class); | ||
private final HttpClient client; | ||
|
||
public ProxyWebClient() { | ||
this(HttpClientUtil.getConfiguredHttpClient()); | ||
} | ||
|
||
public ProxyWebClient(HttpClient client) { | ||
this.client = client; | ||
} | ||
|
||
public Response postProxy(String containerId, String path, String body) { | ||
try { | ||
URI uri = new URIBuilder() | ||
.setScheme("http") | ||
.setHost(containerId) | ||
.setPath(path) | ||
.build(); | ||
HttpPost request = new HttpPost(uri); | ||
request.setEntity(new StringEntity(body)); | ||
return getResponse(request); | ||
} catch (URISyntaxException e) { | ||
LOG.warn("Failed to construct URI. Container: {} Path: {}", containerId, path); | ||
throw new RuntimeException(e); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public Response getProxy(String containerId, String path) { | ||
try { | ||
URI uri = new URIBuilder() | ||
.setScheme("http") | ||
.setHost(containerId) | ||
.setPath(path) | ||
.build(); | ||
HttpGet request = new HttpGet(uri); | ||
return getResponse(request); | ||
} catch (URISyntaxException e) { | ||
LOG.warn("Failed to construct URI. Container: {} Path: {}", containerId, path); | ||
throw new RuntimeException(e); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private Response getResponse(HttpRequestBase request) throws IOException { | ||
HttpResponse response = client.execute(request); | ||
return Response.ok(response.getEntity().getContent()).build(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...sure-resource-api/src/test/java/edu/harvard/dbmi/avillach/service/ProxyWebClientTest.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,64 @@ | ||
package edu.harvard.dbmi.avillach.service; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import javax.ws.rs.core.Response; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ProxyWebClientTest { | ||
|
||
@Mock | ||
private HttpClient client; | ||
|
||
@Mock | ||
private HttpResponse response; | ||
|
||
@Mock | ||
private HttpEntity entity; | ||
|
||
@InjectMocks | ||
private ProxyWebClient subject; | ||
|
||
@Test | ||
public void shouldPostToProxy() throws IOException { | ||
Mockito.when(client.execute(Mockito.any(HttpPost.class))) | ||
.thenReturn(response); | ||
Mockito.when(response.getEntity()) | ||
.thenReturn(entity); | ||
Mockito.when(entity.getContent()) | ||
.thenReturn(new ByteArrayInputStream("{}".getBytes())); | ||
|
||
Response actual = subject.postProxy("foo", "/my/cool/path", "{}"); | ||
|
||
Assert.assertEquals(200, actual.getStatus()); | ||
} | ||
|
||
@Test | ||
public void shouldGetToProxy() throws IOException { | ||
Mockito.when(client.execute(Mockito.any(HttpGet.class))) | ||
.thenReturn(response); | ||
Mockito.when(response.getEntity()) | ||
.thenReturn(entity); | ||
Mockito.when(entity.getContent()) | ||
.thenReturn(new ByteArrayInputStream("{}".getBytes())); | ||
|
||
Response actual = subject.getProxy("bar", "/my/cool/path"); | ||
|
||
Assert.assertEquals(200, actual.getStatus()); | ||
} | ||
} |
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