All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nervousync.utils.CookieUtils Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * Licensed to the Nervousync Studio (NSYC) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.nervousync.utils;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.nervousync.commons.Globals;

import java.util.Arrays;

/**
 * 

Cookie utilities

*

Cookie工具集

* * @author Steven Wee [email protected] * @version $Revision: 1.2.0 $ $Date: Jan 13, 2010 13:49:41 $ */ public final class CookieUtils { /** *

Private constructor for CookieUtils

*

Cookie工具集的私有构造方法

*/ private CookieUtils() { } /** *

Set cookie information

*

写入Cookie信息

* * @param cookieName Cookie name * Cookie名称 * @param cookieValue Cookie value * Cookie值 * @param request HttpServletRequest instance * Http请求实例对象 * @param response HttpServletResponse instance * Http响应实例对象 * * @return Process result * 操作结果 */ public static boolean setCookie(final String cookieName, final String cookieValue, final HttpServletRequest request, final HttpServletResponse response) { return setCookie(cookieName, cookieValue, Globals.DEFAULT_VALUE_STRING, Globals.DEFAULT_VALUE_STRING, request, response); } /** *

Set cookie information

*

写入Cookie信息

* * @param cookieName Cookie name * Cookie名称 * @param cookieValue Cookie value * Cookie值 * @param domainName Domain name * 所在域名 * @param request HttpServletRequest instance * Http请求实例对象 * @param response HttpServletResponse instance * Http响应实例对象 * * @return Process result * 操作结果 */ public static boolean setCookie(final String cookieName, final String cookieValue, final String domainName, final HttpServletRequest request, final HttpServletResponse response) { return setCookie(cookieName, cookieValue, domainName, Globals.DEFAULT_VALUE_STRING, null, request, response); } /** *

Set cookie information

*

写入Cookie信息

* * @param cookieName Cookie name * Cookie名称 * @param cookieValue Cookie value * Cookie值 * @param domainName Domain name * 所在域名 * @param cookiePath Cookie path * Cookie路径 * @param request HttpServletRequest instance * Http请求实例对象 * @param response HttpServletResponse instance * Http响应实例对象 * * @return Process result * 操作结果 */ public static boolean setCookie(final String cookieName, final String cookieValue, final String domainName, final String cookiePath, final HttpServletRequest request, final HttpServletResponse response) { return setCookie(cookieName, cookieValue, domainName, cookiePath, null, request, response); } /** *

Set cookie information

*

写入Cookie信息

* * @param cookieName Cookie name * Cookie名称 * @param cookieValue Cookie value * Cookie值 * @param lifeCycle Life cycle * 生存周期 * @param request HttpServletRequest instance * Http请求实例对象 * @param response HttpServletResponse instance * Http响应实例对象 * * @return Process result * 操作结果 */ public static boolean setCookie(final String cookieName, final String cookieValue, final Integer lifeCycle, final HttpServletRequest request, final HttpServletResponse response) { return setCookie(cookieName, cookieValue, Globals.DEFAULT_VALUE_STRING, Globals.DEFAULT_VALUE_STRING, lifeCycle, request, response); } /** *

Set cookie information

*

写入Cookie信息

* * @param cookieName Cookie name * Cookie名称 * @param cookieValue Cookie value * Cookie值 * @param domainName Domain name * 所在域名 * @param cookiePath Cookie path * Cookie路径 * @param lifeCycle Life cycle * 生存周期 * @param request HttpServletRequest instance * Http请求实例对象 * @param response HttpServletResponse instance * Http响应实例对象 * * @return Process result * 操作结果 */ public static boolean setCookie(final String cookieName, final String cookieValue, final String domainName, final String cookiePath, final Integer lifeCycle, final HttpServletRequest request, final HttpServletResponse response) { try { Cookie cookie = getCookie(cookieName, request); if (cookie == null) { cookie = new Cookie(cookieName, cookieValue); cookie.setPath(StringUtils.isEmpty(cookiePath) ? "/" : cookiePath); if (domainName != null) { cookie.setDomain("." + domainName); } if (lifeCycle != null && lifeCycle >= 0) { // Default Cookie Life Cycle cookie.setMaxAge(60 * 60 * lifeCycle); } } else { cookie.setValue(cookieValue); } response.addCookie(cookie); return true; } catch (Exception e) { return false; } } /** *

Read cookie instance

*

读取Cookie实例对象

* * @param cookieName Cookie name * Cookie名称 * @param request HttpServletRequest instance * Http请求实例对象 * * @return Cookie instance or null if not exists * Cookie实例对象,如果不存在则返回null */ public static Cookie getCookie(String cookieName, HttpServletRequest request) { return Arrays.stream(request.getCookies()) .filter(cookie -> cookie.getName().equalsIgnoreCase(cookieName)) .findFirst() .orElse(null); } /** *

Read cookie information

*

读取Cookie信息

* * @param cookieName Cookie name * Cookie名称 * @param request HttpServletRequest instance * Http请求实例对象 * * @return Cookie value or null if not exists * Cookie值,如果不存在则返回null */ public static String getCookieValue(String cookieName, HttpServletRequest request) { return Arrays.stream(request.getCookies()) .filter(cookie -> cookie.getName().equalsIgnoreCase(cookieName)) .findFirst() .map(Cookie::getValue) .orElse(Globals.DEFAULT_VALUE_STRING); } /** *

Remove cookie information

*

移除Cookie信息

* * @param cookieName Cookie name * Cookie名称 * @param request HttpServletRequest instance * Http请求实例对象 * @param response HttpServletResponse instance * Http响应实例对象 */ public static void delCookie(String cookieName, HttpServletRequest request, HttpServletResponse response) { delCookie(getCookie(cookieName, request), response); } /** *

Remove cookie information

*

移除Cookie信息

* * @param cookie Cookie instance * Cookie实例对象 * @param response HttpServletResponse instance * Http响应实例对象 */ public static void delCookie(Cookie cookie, HttpServletResponse response) { if (cookie != null) { cookie.setMaxAge(0); response.addCookie(cookie); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy