19 package org.turro.action;
21 import java.net.URLDecoder;
22 import java.net.URLEncoder;
23 import java.nio.charset.Charset;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Base64;
26 import java.util.HashMap;
28 import org.turro.collections.KeyValueMap;
29 import org.turro.collections.parser.ParserException;
30 import org.turro.log.WebLoggers;
31 import org.turro.string.Strings;
32 import org.turro.util.Converter;
40 public String
get(String key) {
41 return parameters.get(key);
44 public <T> T
get(String key, Class<T> javaClass) {
45 return Converter.STANDARD.convert(parameters.get(key), javaClass);
49 parameters.put(key, Converter.STANDARD.convert(value, String.class));
62 public String
raw(String separator) {
63 StringBuilder sb =
new StringBuilder();
64 if(!Strings.isBlank(url)) {
65 sb.append(url).append(
"?");
67 for(Map.Entry<String, String> entry : parameters.entrySet()) {
68 if(!sb.isEmpty()) sb.append(separator);
69 sb.append(entry.getKey()).append(
"=").append(entry.getValue());
78 public String
plain(String separator) {
79 StringBuilder sb =
new StringBuilder();
80 if(!Strings.isBlank(url)) {
81 sb.append(url).append(
"?");
83 for(Map.Entry<String, String> entry : parameters.entrySet()) {
84 if(!sb.isEmpty()) sb.append(separator);
85 sb.append(entry.getKey()).append(
"=").append(URLEncoder.encode(entry.getValue(), charset));
91 StringBuilder sb =
new StringBuilder();
92 if(!Strings.isBlank(url)) {
93 sb.append(url).append(
"?");
95 return sb.append(ENCODED).append(
"=")
96 .append(Base64.getUrlEncoder().encodeToString(
raw(
";").getBytes(charset)))
101 return new KeyValueMap(parameters);
106 private static String decode(String value) {
107 return new String(Base64.getUrlDecoder().decode(value), charset);
110 private void addAll(KeyValueMap kvm) {
111 parameters.putAll(kvm);
122 instance.parameters.putAll(pars.parameters);
132 if(parameterMap !=
null) {
133 parameterMap.keySet().forEach((key) -> {
134 if(ENCODED.equals(key)) {
136 KeyValueMap kvm = new KeyValueMap(decode(parameterMap.get(key)[0]));
137 if(kvm != null) instance.addAll(kvm);
138 } catch (ParserException ex) {
142 instance.
add(key, URLDecoder.decode(parameterMap.get(key)[0], charset));
149 private static final Charset charset = StandardCharsets.UTF_8;
150 private static final String ENCODED =
"el-encx";
152 private final HashMap<String, String> parameters;
156 private Parameters() {
157 parameters =
new HashMap<>();
Parameters url(String url)
static Parameters from(Map< String, String[]> parameterMap)
String raw(String separator)
static Parameters copy(Parameters pars)
Parameters add(String key, Object value)
String plain(String separator)
static Parameters path(String path)
static Parameters instance()
WebLoggers exception(Throwable throwable)
static WebLoggers info(Object entity)