-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 34f54af
Showing
28 changed files
with
1,054 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>leyou</artifactId> | ||
<groupId>cn.lollipop</groupId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>ly-common</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-logging</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
17 changes: 17 additions & 0 deletions
17
ly-common/src/main/java/cn/lollipop/common/CommonExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.lollipop.common; | ||
|
||
import cn.lollipop.common.exception.LyException; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import vo.ExceptionResult; | ||
|
||
@ControllerAdvice | ||
public class CommonExceptionHandler { | ||
|
||
@ExceptionHandler(LyException.class) | ||
public ResponseEntity<ExceptionResult> handleException(LyException e) { | ||
ExceptionResult result = new ExceptionResult(e.getExceptionConstant()); | ||
return ResponseEntity.status(result.getStatus()).body(result); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
ly-common/src/main/java/cn/lollipop/common/ExceptionConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cn.lollipop.common; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public enum ExceptionConstant { | ||
PRICE_CANNOT_BE_NULL(400, "价格不能为空!") | ||
; | ||
|
||
private int code; | ||
private String msg; | ||
} |
13 changes: 13 additions & 0 deletions
13
ly-common/src/main/java/cn/lollipop/common/exception/LyException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.lollipop.common.exception; | ||
|
||
import cn.lollipop.common.ExceptionConstant; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
public class LyException extends RuntimeException { | ||
private ExceptionConstant exceptionConstant; | ||
} |
215 changes: 215 additions & 0 deletions
215
ly-common/src/main/java/cn/lollipop/common/util/CookieUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package cn.lollipop.common.util; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.servlet.http.Cookie; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLDecoder; | ||
import java.net.URLEncoder; | ||
|
||
/** | ||
* | ||
* Cookie 工具类 | ||
* | ||
*/ | ||
public final class CookieUtils { | ||
|
||
protected static final Logger logger = LoggerFactory.getLogger(CookieUtils.class); | ||
|
||
/** | ||
* 得到Cookie的值, 不编码 | ||
* | ||
* @param request | ||
* @param cookieName | ||
* @return | ||
*/ | ||
public static String getCookieValue(HttpServletRequest request, String cookieName) { | ||
return getCookieValue(request, cookieName, false); | ||
} | ||
|
||
/** | ||
* 得到Cookie的值, | ||
* | ||
* @param request | ||
* @param cookieName | ||
* @return | ||
*/ | ||
public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) { | ||
Cookie[] cookieList = request.getCookies(); | ||
if (cookieList == null || cookieName == null){ | ||
return null; | ||
} | ||
String retValue = null; | ||
try { | ||
for (int i = 0; i < cookieList.length; i++) { | ||
if (cookieList[i].getName().equals(cookieName)) { | ||
if (isDecoder) { | ||
retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8"); | ||
} else { | ||
retValue = cookieList[i].getValue(); | ||
} | ||
break; | ||
} | ||
} | ||
} catch (UnsupportedEncodingException e) { | ||
logger.error("Cookie Decode Error.", e); | ||
} | ||
return retValue; | ||
} | ||
|
||
/** | ||
* 得到Cookie的值, | ||
* | ||
* @param request | ||
* @param cookieName | ||
* @return | ||
*/ | ||
public static String getCookieValue(HttpServletRequest request, String cookieName, String encodeString) { | ||
Cookie[] cookieList = request.getCookies(); | ||
if (cookieList == null || cookieName == null){ | ||
return null; | ||
} | ||
String retValue = null; | ||
try { | ||
for (int i = 0; i < cookieList.length; i++) { | ||
if (cookieList[i].getName().equals(cookieName)) { | ||
retValue = URLDecoder.decode(cookieList[i].getValue(), encodeString); | ||
break; | ||
} | ||
} | ||
} catch (UnsupportedEncodingException e) { | ||
logger.error("Cookie Decode Error.", e); | ||
} | ||
return retValue; | ||
} | ||
|
||
/** | ||
* 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码 | ||
*/ | ||
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue) { | ||
setCookie(request, response, cookieName, cookieValue, -1); | ||
} | ||
|
||
/** | ||
* 设置Cookie的值 在指定时间内生效,但不编码 | ||
*/ | ||
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage) { | ||
setCookie(request, response, cookieName, cookieValue, cookieMaxage, false); | ||
} | ||
|
||
/** | ||
* 设置Cookie的值 不设置生效时间,但编码 | ||
*/ | ||
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, boolean isEncode) { | ||
setCookie(request, response, cookieName, cookieValue, -1, isEncode); | ||
} | ||
|
||
/** | ||
* 设置Cookie的值 在指定时间内生效, 编码参数 | ||
*/ | ||
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) { | ||
doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, isEncode); | ||
} | ||
|
||
/** | ||
* 设置Cookie的值 在指定时间内生效, 编码参数(指定编码) | ||
*/ | ||
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, String encodeString) { | ||
doSetCookie(request, response, cookieName, cookieValue, cookieMaxage, encodeString); | ||
} | ||
|
||
/** | ||
* 删除Cookie带cookie域名 | ||
*/ | ||
public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) { | ||
doSetCookie(request, response, cookieName, "", -1, false); | ||
} | ||
|
||
/** | ||
* 设置Cookie的值,并使其在指定时间内生效 | ||
* | ||
* @param cookieMaxage | ||
* cookie生效的最大秒数 | ||
*/ | ||
private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) { | ||
try { | ||
if (cookieValue == null) { | ||
cookieValue = ""; | ||
} else if (isEncode) { | ||
cookieValue = URLEncoder.encode(cookieValue, "utf-8"); | ||
} | ||
Cookie cookie = new Cookie(cookieName, cookieValue); | ||
if (cookieMaxage > 0) | ||
cookie.setMaxAge(cookieMaxage); | ||
if (null != request)// 设置域名的cookie | ||
cookie.setDomain(getDomainName(request)); | ||
cookie.setPath("/"); | ||
response.addCookie(cookie); | ||
} catch (Exception e) { | ||
logger.error("Cookie Encode Error.", e); | ||
} | ||
} | ||
|
||
/** | ||
* 设置Cookie的值,并使其在指定时间内生效 | ||
* | ||
* @param cookieMaxage | ||
* cookie生效的最大秒数 | ||
*/ | ||
private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String cookieValue, int cookieMaxage, String encodeString) { | ||
try { | ||
if (cookieValue == null) { | ||
cookieValue = ""; | ||
} else { | ||
cookieValue = URLEncoder.encode(cookieValue, encodeString); | ||
} | ||
Cookie cookie = new Cookie(cookieName, cookieValue); | ||
if (cookieMaxage > 0) | ||
cookie.setMaxAge(cookieMaxage); | ||
if (null != request)// 设置域名的cookie | ||
cookie.setDomain(getDomainName(request)); | ||
cookie.setPath("/"); | ||
response.addCookie(cookie); | ||
} catch (Exception e) { | ||
logger.error("Cookie Encode Error.", e); | ||
} | ||
} | ||
|
||
/** | ||
* 得到cookie的域名 | ||
*/ | ||
private static final String getDomainName(HttpServletRequest request) { | ||
String domainName = null; | ||
|
||
String serverName = request.getRequestURL().toString(); | ||
if (serverName == null || serverName.equals("")) { | ||
domainName = ""; | ||
} else { | ||
serverName = serverName.toLowerCase(); | ||
serverName = serverName.substring(7); | ||
final int end = serverName.indexOf("/"); | ||
serverName = serverName.substring(0, end); | ||
final String[] domains = serverName.split("\\."); | ||
int len = domains.length; | ||
if (len > 3) { | ||
// www.xxx.com.cn | ||
domainName = domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1]; | ||
} else if (len <= 3 && len > 1) { | ||
// xxx.com or xxx.cn | ||
domainName = domains[len - 2] + "." + domains[len - 1]; | ||
} else { | ||
domainName = serverName; | ||
} | ||
} | ||
|
||
if (domainName != null && domainName.indexOf(":") > 0) { | ||
String[] ary = domainName.split("\\:"); | ||
domainName = ary[0]; | ||
} | ||
return domainName; | ||
} | ||
|
||
} |
Oops, something went wrong.