BrightSide Workbench Full Report + Source Code
Parameters.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.action;
20 
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;
27 import java.util.Map;
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;
33 
38 public class Parameters {
39 
40  public String get(String key) {
41  return parameters.get(key);
42  }
43 
44  public <T> T get(String key, Class<T> javaClass) {
45  return Converter.STANDARD.convert(parameters.get(key), javaClass);
46  }
47 
48  public Parameters add(String key, Object value) {
49  parameters.put(key, Converter.STANDARD.convert(value, String.class));
50  return this;
51  }
52 
53  public Parameters url(String url) {
54  this.url = url;
55  return this;
56  }
57 
58  public String raw() {
59  return raw("&");
60  }
61 
62  public String raw(String separator) {
63  StringBuilder sb = new StringBuilder();
64  if(!Strings.isBlank(url)) {
65  sb.append(url).append("?");
66  }
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());
70  }
71  return sb.toString();
72  }
73 
74  public String plain() {
75  return plain("&");
76  }
77 
78  public String plain(String separator) {
79  StringBuilder sb = new StringBuilder();
80  if(!Strings.isBlank(url)) {
81  sb.append(url).append("?");
82  }
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));
86  }
87  return sb.toString();
88  }
89 
90  public String encoded() {
91  StringBuilder sb = new StringBuilder();
92  if(!Strings.isBlank(url)) {
93  sb.append(url).append("?");
94  }
95  return sb.append(ENCODED).append("=")
96  .append(Base64.getUrlEncoder().encodeToString(raw(";").getBytes(charset)))
97  .toString();
98  }
99 
100  public KeyValueMap asMap() {
101  return new KeyValueMap(parameters);
102  }
103 
104  /* Utils */
105 
106  private static String decode(String value) {
107  return new String(Base64.getUrlDecoder().decode(value), charset);
108  }
109 
110  private void addAll(KeyValueMap kvm) {
111  parameters.putAll(kvm);
112  }
113 
114  /* Factory */
115 
116  public static Parameters instance() {
117  return new Parameters();
118  }
119 
120  public static Parameters copy(Parameters pars) {
122  instance.parameters.putAll(pars.parameters);
123  return instance;
124  }
125 
126  public static Parameters path(String path) {
127  return instance().url(path);
128  }
129 
130  public static Parameters from(Map<String, String[]> parameterMap) {
132  if(parameterMap != null) {
133  parameterMap.keySet().forEach((key) -> {
134  if(ENCODED.equals(key)) {
135  try {
136  KeyValueMap kvm = new KeyValueMap(decode(parameterMap.get(key)[0]));
137  if(kvm != null) instance.addAll(kvm);
138  } catch (ParserException ex) {
139  WebLoggers.info(Parameters.class).exception(ex).log();
140  }
141  } else {
142  instance.add(key, URLDecoder.decode(parameterMap.get(key)[0], charset));
143  }
144  });
145  }
146  return instance;
147  }
148 
149  private static final Charset charset = StandardCharsets.UTF_8;
150  private static final String ENCODED = "el-encx";
151 
152  private final HashMap<String, String> parameters;
153 
154  private String url;
155 
156  private Parameters() {
157  parameters = new HashMap<>();
158  }
159 
160 }
Parameters url(String url)
Definition: Parameters.java:53
static Parameters from(Map< String, String[]> parameterMap)
String raw(String separator)
Definition: Parameters.java:62
static Parameters copy(Parameters pars)
Parameters add(String key, Object value)
Definition: Parameters.java:48
String plain(String separator)
Definition: Parameters.java:78
static Parameters path(String path)
static Parameters instance()
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
static WebLoggers info(Object entity)
Definition: WebLoggers.java:43