-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add uri templating/parameters to client requests
- Fix for entities/chunked streaming in responses
- Loading branch information
Showing
9 changed files
with
163 additions
and
11 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
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
39 changes: 39 additions & 0 deletions
39
core/src/main/java/org/chodavarapu/datamill/http/impl/TemplateBasedUriBuilder.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,39 @@ | ||
package org.chodavarapu.datamill.http.impl; | ||
|
||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
public class TemplateBasedUriBuilder { | ||
private static final Pattern placeholderPattern = Pattern.compile("\\{([a-z]+)}"); | ||
|
||
public String build(String uri, Map<String, String> uriParameters) { | ||
StringBuilder composedUri = new StringBuilder(); | ||
|
||
java.util.regex.Matcher matcher = placeholderPattern.matcher(uri); | ||
int searchIx = 0; | ||
while (matcher.find(searchIx)) { | ||
composedUri.append(uri.substring(searchIx, matcher.start())); | ||
|
||
String parameterName = matcher.group(1); | ||
if (parameterName != null && parameterName.length() > 1) { | ||
String value = uriParameters.get(parameterName); | ||
if (value != null) { | ||
composedUri.append(value); | ||
} else { | ||
composedUri.append(matcher.group()); | ||
} | ||
} | ||
|
||
searchIx = matcher.end(); | ||
} | ||
|
||
if (searchIx < uri.length()) { | ||
composedUri.append(uri.substring(searchIx)); | ||
} | ||
|
||
return composedUri.toString(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
core/src/test/java/org/chodavarapu/datamill/http/impl/InputStreamEntityTest.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,35 @@ | ||
package org.chodavarapu.datamill.http.impl; | ||
|
||
import org.apache.commons.io.output.ByteArrayOutputStream; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
public class InputStreamEntityTest { | ||
@Test | ||
public void entityStreaming() { | ||
assertEquals("test", new InputStreamEntity(new ByteArrayInputStream("test".getBytes())) | ||
.asString().toBlocking().last()); | ||
assertEquals("test", new String(new InputStreamEntity(new ByteArrayInputStream("test".getBytes())) | ||
.asBytes().toBlocking().last())); | ||
assertEquals("test", new String(new InputStreamEntity(new ByteArrayInputStream("test".getBytes())) | ||
.asChunks() | ||
.collect( | ||
() -> new ByteArrayOutputStream(), | ||
(stream, chunk) -> { | ||
try { | ||
stream.write(chunk); | ||
} catch (IOException e) { | ||
} | ||
}) | ||
.map(stream -> stream.toByteArray()).toBlocking().last())); | ||
assertEquals("value", new InputStreamEntity(new ByteArrayInputStream("{\"name\":\"value\"}".getBytes())) | ||
.asJson().toBlocking().last().get("name").asString()); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
core/src/test/java/org/chodavarapu/datamill/http/impl/TemplateBasedUriBuilderTest.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,32 @@ | ||
package org.chodavarapu.datamill.http.impl; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
public class TemplateBasedUriBuilderTest { | ||
@Test | ||
public void templateBasedUriBuilding() { | ||
TemplateBasedUriBuilder builder = new TemplateBasedUriBuilder(); | ||
|
||
assertEquals("http://localhost:9080/users/0/repositories", | ||
builder.build("http://{host}:{port}/users/{id}/repositories", | ||
ImmutableMap.of("host", "localhost", "port", "9080", "id", "0"))); | ||
assertEquals("https://localhost:9080/users/0/repositories", | ||
builder.build("{scheme}://{host}:{port}/users/{id}/repositories", | ||
ImmutableMap.of("host", "localhost", "port", "9080", "id", "0", "scheme", "https"))); | ||
assertEquals("http://localhost:9080/users/0/repositories", | ||
builder.build("http://{host}:{port}/users/{id}/{association}", | ||
ImmutableMap.of("host", "localhost", "port", "9080", "id", "0", "association", "repositories"))); | ||
assertEquals("http://localhost:9080/users/0/repositories/{unmatched}", | ||
builder.build("http://{host}:{port}/users/{id}/{association}/{unmatched}", | ||
ImmutableMap.of("host", "localhost", "port", "9080", "id", "0", "association", "repositories"))); | ||
assertEquals("http://localhost:9080/users", | ||
builder.build("{scheme}{host}:{port}{path}", | ||
ImmutableMap.of("scheme", "http://", "host", "localhost", "port", "9080", "path", "/users"))); | ||
} | ||
} |