import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.utils.URIBuilder;
import com.netflix.util.Pair;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;
public class CookieAndRedirectUtil {
public static void addCookies(HttpServletResponse response, Map<String, String> cookies) {
for (Map.Entry<String, String> entry : cookies.entrySet()) {
Cookie cookie = new Cookie(entry.getKey(), entry.getValue());
response.addCookie(cookie);
}
}
public static void redirectToPath(HttpServletRequest request, HttpServletResponse response, String path) throws URISyntaxException {
URIBuilder builder = new URIBuilder(path);
Map<String, String[]> parameterMap = request.getParameterMap();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
builder.setParameter(entry.getKey(), entry.getValue()[0]);
}
URI uri = builder.build();
response.sendRedirect(uri.toString());
}
public static void removeCookies(HttpServletRequest request, HttpServletResponse response, List<String> cookiesToRemove) {
for (String cookieName : cookiesToRemove) {
Cookie cookie = new Cookie(cookieName, null);
cookie.setMaxAge(0);
cookie.setPath("/");
response.addCookie(cookie);
}
}
public static Pair<String, String> getRedirectUrl(HttpServletRequest request, String path) throws URISyntaxException {
URIBuilder builder = new URIBuilder(path);
Map<String, String[]> parameterMap = request.getParameterMap();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
builder.setParameter(entry.getKey(), entry.getValue()[0]);
}
return new Pair<>(request.getScheme(), builder.build().toString());
}
public static void appendOrReplaceQueryParam(HttpServletRequest request, HttpServletResponse response, String paramName, String paramValue) throws URISyntaxException {
URIBuilder builder = new URIBuilder(request.getRequestURL().toString());
String query = request.getQueryString();
if (StringUtils.isNotBlank(query)) {
St
评论已关闭