-
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.
[ALS-5065] Create proxy endpoint to enable dockerized services
- Create endpoint to proxy requests
- Loading branch information
1 parent
097ff66
commit 519ecc2
Showing
5 changed files
with
186 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.Header; | ||
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.enterprise.context.ApplicationScoped; | ||
import javax.ws.rs.core.Response; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
@ApplicationScoped | ||
public class ProxyWebClient { | ||
private static final Logger LOG = LoggerFactory.getLogger(ProxyWebClient.class); | ||
HttpClient client; | ||
|
||
public ProxyWebClient() { | ||
client = HttpClientUtil.getConfiguredHttpClient(); | ||
} | ||
|
||
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)); | ||
request.addHeader("Content-Type", "application/json"); | ||
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(); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...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,66 @@ | ||
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())); | ||
subject.client = client; | ||
|
||
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())); | ||
subject.client = client; | ||
|
||
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