1
15
16 package software.amazon.awssdk.protocols.core;
17
18 import software.amazon.awssdk.annotations.SdkProtectedApi;
19 import software.amazon.awssdk.utils.Validate;
20 import software.amazon.awssdk.utils.http.SdkHttpUtils;
21
22 @SdkProtectedApi
23 public abstract class PathMarshaller {
24
25
28 public static final PathMarshaller NON_GREEDY = new NonGreedyPathMarshaller();
29
30
33 public static final PathMarshaller GREEDY = new GreedyPathMarshaller();
34
35
39 public static final PathMarshaller GREEDY_WITH_SLASHES = new GreedyLeadingSlashPathMarshaller();
40
41 private PathMarshaller() {
42 }
43
44 private static String trimLeadingSlash(String value) {
45 if (value.startsWith("/")) {
46 return value.replaceFirst("/", "");
47 }
48 return value;
49 }
50
51
57 public abstract String marshall(String resourcePath, String paramName, String pathValue);
58
59 private static class NonGreedyPathMarshaller extends PathMarshaller {
60 @Override
61 public String marshall(String resourcePath, String paramName, String pathValue) {
62 Validate.notEmpty(pathValue, "%s cannot be empty.", paramName);
63 return resourcePath.replace(String.format("{%s}", paramName), SdkHttpUtils.urlEncode(pathValue));
64 }
65 }
66
67 private static class GreedyPathMarshaller extends PathMarshaller {
68
69 @Override
70 public String marshall(String resourcePath, String paramName, String pathValue) {
71 Validate.notEmpty(pathValue, "%s cannot be empty.", paramName);
72 return resourcePath.replace(String.format("{%s+}", paramName),
73 SdkHttpUtils.urlEncodeIgnoreSlashes(trimLeadingSlash(pathValue)));
74 }
75 }
76
77 private static class GreedyLeadingSlashPathMarshaller extends PathMarshaller {
78
79 @Override
80 public String marshall(String resourcePath, String paramName, String pathValue) {
81 Validate.notEmpty(pathValue, "%s cannot be empty.", paramName);
82 return resourcePath.replace(String.format("{%s+}", paramName),
83 SdkHttpUtils.urlEncodeIgnoreSlashes(pathValue));
84 }
85 }
86
87 }
88