| 1 | |
package org.webslinger.servlet; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import javax.servlet.ServletException; |
| 5 | |
|
| 6 | |
import org.webslinger.collections.CollectionUtil; |
| 7 | |
|
| 8 | |
public final class ServletUtil { |
| 9 | 0 | private ServletUtil() { |
| 10 | 0 | } |
| 11 | |
|
| 12 | |
public static final <T> T checkException(Throwable t) throws IOException, ServletException { |
| 13 | 0 | if (t instanceof IOException) throw (IOException) t; |
| 14 | 0 | if (t instanceof ServletException) throw (ServletException) t; |
| 15 | 0 | if (t instanceof RuntimeException) throw (RuntimeException) t; |
| 16 | 0 | if (t instanceof Error) throw (Error) t; |
| 17 | 0 | throw (ServletException) new ServletException(t.getMessage()).initCause(t); |
| 18 | |
} |
| 19 | |
|
| 20 | |
public static String embedParameter(String path, String name, String value) { |
| 21 | 0 | int question = path.indexOf("?"); |
| 22 | 0 | if (question == -1) return path + "?" + name + "=" + value; |
| 23 | 0 | String[] params = path.substring(question + 1).split("\\&"); |
| 24 | 0 | String startMatch = name + "="; |
| 25 | 0 | for (int i = 0; i < params.length; i++) { |
| 26 | 0 | String param = params[i]; |
| 27 | 0 | if (param.startsWith(name) && param.length() > name.length() && param.charAt(name.length()) == '=') { |
| 28 | 0 | params[i] = name + "=" + value; |
| 29 | 0 | return path.substring(0, question) + "?" + CollectionUtil.join(params, "&"); |
| 30 | |
} |
| 31 | |
} |
| 32 | 0 | return path + "&" + name + "=" + value; |
| 33 | |
} |
| 34 | |
} |