Last active
September 3, 2024 18:10
-
-
Save mwoodiupui/1a308774c1030fc8deb9b7a416f13cc0 to your computer and use it in GitHub Desktop.
Augmenting a URL's path using URIBuilder
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
package net.wood; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.util.Collections; | |
import java.util.Objects; | |
import java.util.List; | |
import org.apache.http.client.utils.URIBuilder; | |
public class App { | |
private static final String token = "axolotl"; | |
private static String base; | |
public static void main(String[] argv) | |
throws URISyntaxException { | |
if (argv.length <= 0) { | |
base = "https://example.com/path/"; | |
} else { | |
base = argv[]; | |
} | |
URIBuilder uriBuilder = new URIBuilder(base); | |
List<String> pathSegments = uriBuilder.getPathSegments(); | |
while(pathSegments.remove("")); | |
printList("before", pathSegments); | |
System.out.println(); | |
Collections.addAll(pathSegments, | |
"request-a-copy", | |
token); | |
printList("after", pathSegments); | |
System.out.println(); | |
URI uri = uriBuilder.setPathSegments(pathSegments).build(); | |
System.out.println(uri.toString()); | |
} | |
private static void printList(String title, List<String> theList) { | |
System.out.println(title); | |
for (String element : theList) { | |
System.out.print(null == element ? "null" : element.getClass()); | |
System.out.print(": "); | |
System.out.format("'%s'\n", element); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment