Skip to content

Commit

Permalink
jboot v1.4.4 release
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfuhai committed Mar 26, 2018
1 parent ff3cc53 commit d1a3723
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 58 deletions.
22 changes: 2 additions & 20 deletions src/main/java/io/jboot/web/flashmessage/FlashMessageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,13 @@ public class FlashMessageManager {

private static final String FLASH_SESSION_ATTR = "_JFM_"; // JFM : jboot flash message

private static final String FLASH_COOKIE_ATTR = "_JFM_"; // JFM : jboot flash message
private static final String FLASH_COOKIE_VALUE = "true";


private static final FlashMessageManager ME = new FlashMessageManager();

public static FlashMessageManager me() {
return ME;
}

public void renderTo(Controller controller) {
if (!hasFlashMessage(controller)) {
return;
}
HashMap<String, Object> flash = controller.getSessionAttr(FLASH_SESSION_ATTR);
if (flash == null || flash.isEmpty()) {
return;
Expand All @@ -63,25 +56,14 @@ public void init(Controller controller) {
return;
}
controller.setSessionAttr(FLASH_SESSION_ATTR, flash);
controller.setCookie(FLASH_COOKIE_ATTR, FLASH_COOKIE_VALUE, 60);
}


public void release(Controller controller) {
if (!hasFlashMessage(controller)) {
if (controller.getSessionAttr(FLASH_SESSION_ATTR) == null) {
return;
}
controller.removeSessionAttr(FLASH_SESSION_ATTR);
controller.removeCookie(FLASH_COOKIE_ATTR);
}

/**
* 查看是否有 FlashMessage
* 因为对 FlashMessage操作是有"代价的",需要去操作redis 等缓存
* 所以应该先通过 cookie来判断,再执行
*
* @return
*/
private boolean hasFlashMessage(Controller controller) {
return FLASH_COOKIE_VALUE.equals(controller.getCookie(FLASH_COOKIE_ATTR));
}
}
4 changes: 3 additions & 1 deletion src/main/java/io/jboot/web/handler/JbootActionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public void handle(String target, HttpServletRequest request, HttpServletRespons

boolean isRedirect = render instanceof RedirectRender;




/**
* 如果当前是redirect
* 尝试设置 FlashMessage 数据到 session
Expand All @@ -126,7 +129,6 @@ public void handle(String target, HttpServletRequest request, HttpServletRespons

render.setContext(request, response, action.getViewPath()).render();


/**
* 如果当前不是redirect,那么尝试去清空FlashMessage数据
*/
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/jboot/web/handler/JbootHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void handle(String target, HttpServletRequest request, HttpServletRespons
* 通过 JbootRequestContext 去保存 request,然后可以在当前线程的任何地方
* 通过 JbootRequestContext.getRequest() 去获取。
*/
JbootServletRequestWrapper jbootServletRequest = new JbootServletRequestWrapper(request);
JbootServletRequestWrapper jbootServletRequest = new JbootServletRequestWrapper(request,response);
JbootRequestContext.handle(jbootServletRequest, response);


Expand All @@ -65,7 +65,7 @@ public void handle(String target, HttpServletRequest request, HttpServletRespons
JbootExceptionHolder.release();
context.shutdown();
JbootRequestContext.release();
jbootServletRequest.finish();
jbootServletRequest.refreshSession();
}

}
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/io/jboot/web/render/JbootErrorRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,12 @@ public void render() {
return;
}

PrintWriter writer = null;
try {
response.setContentType(contentType);
writer = response.getWriter();
PrintWriter writer = response.getWriter();
writer.write(getErrorHtml());
writer.flush();
} catch (IOException e) {
throw new RenderException(e);
} finally {
if (writer != null)
writer.close();
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/main/java/io/jboot/web/render/JbootJsonRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,16 @@ public void render() {

RenderHelpler.actionCacheExec(jsonText, forIE ? contentTypeForIE : contentType);

PrintWriter writer = null;
try {
response.setHeader("Pragma", "no-cache"); // HTTP/1.0 caches might not implement Cache-Control and might only implement Pragma: no-cache
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);

response.setContentType(forIE ? contentTypeForIE : contentType);
writer = response.getWriter();
PrintWriter writer = response.getWriter();
writer.write(jsonText);
writer.flush();
} catch (IOException e) {
throw new RenderException(e);
} finally {
if (writer != null) {
writer.close();
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/jboot/web/render/JbootRenderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,5 @@ public Render getTemplateRender(String view) {
public Render getXmlRender(String view) {
return new JbootXmlRender(view);
}

}
4 changes: 1 addition & 3 deletions src/main/java/io/jboot/web/render/JbootTextRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public JbootTextRender(String text, ContentType contentType) {
}

public void render() {
PrintWriter writer = null;
try {

RenderHelpler.actionCacheExec(text, contentType);
Expand All @@ -66,9 +65,8 @@ public void render() {
response.setContentType(contentType);
response.setCharacterEncoding(getEncoding()); // 与 contentType 分开设置

writer = response.getWriter();
PrintWriter writer = response.getWriter();
writer.write(text);
writer.flush();
} catch (IOException e) {
throw new RenderException(e);
}
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/io/jboot/web/render/RenderHelpler.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,11 @@ public static void actionCacheExec(String html, String contentType) {

public static void renderHtml(HttpServletResponse response, String html, String contentType) {
response.setContentType(contentType);
PrintWriter responseWriter = null;
try {
responseWriter = response.getWriter();
PrintWriter responseWriter = response.getWriter();
responseWriter.write(html);
responseWriter.flush();
} catch (Exception e) {
throw new RenderException(e);
} finally {
if (responseWriter != null)
responseWriter.close();
}
}

Expand Down
21 changes: 17 additions & 4 deletions src/main/java/io/jboot/web/session/JbootHttpSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ public class JbootHttpSession implements HttpSession {

private volatile boolean invalid;
private volatile boolean dataChanged;
private volatile boolean empty;

public JbootHttpSession(String id, ServletContext servletContext, Map<String, Object> sessionStore) {
this.id = id;
this.servletContext = servletContext;
this.sessionStore = sessionStore;
this.createdAt = System.currentTimeMillis();
this.lastAccessedAt = createdAt;
this.empty = sessionStore.isEmpty();
}

@Override
Expand Down Expand Up @@ -128,11 +130,11 @@ public void setAttribute(String name, Object value) {
if (value != null) {
newAttributes.put(name, value);
deleteAttribute.remove(name);
empty = false;
dataChanged = true;
} else {
deleteAttribute.add(name);
newAttributes.remove(name);
removeAttribute(name);
}
dataChanged = true;
}

@Override
Expand All @@ -143,6 +145,14 @@ public void putValue(String name, Object value) {
@Override
public void removeAttribute(String name) {
checkValid();
if (empty && newAttributes.isEmpty()) {
return;
}

if (!newAttributes.containsKey(name) && !sessionStore.containsKey(name)){
return;
}

deleteAttribute.add(name);
newAttributes.remove(name);
dataChanged = true;
Expand All @@ -151,7 +161,6 @@ public void removeAttribute(String name) {
@Override
public void removeValue(String name) {
removeAttribute(name);
dataChanged = true;
}

@Override
Expand Down Expand Up @@ -189,4 +198,8 @@ protected void checkValid() throws IllegalStateException {
throw new IllegalStateException("http session has invalidate");
}
}

public boolean isEmpty() {
return empty;
}
}
23 changes: 14 additions & 9 deletions src/main/java/io/jboot/web/session/JbootServletRequestWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
import io.jboot.core.cache.JbootCacheManager;
import io.jboot.web.JbootRequestContext;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;
import javax.servlet.http.*;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;


public class JbootServletRequestWrapper extends HttpServletRequestWrapper {

private HttpServletResponse response;
private JbootHttpSession httpSession;
private JbootCache jbootCache;

Expand All @@ -42,8 +40,9 @@ public class JbootServletRequestWrapper extends HttpServletRequestWrapper {
private String cacheType = JbootSessionConfig.get().getCacheType();


public JbootServletRequestWrapper(HttpServletRequest request) {
public JbootServletRequestWrapper(HttpServletRequest request, HttpServletResponse response) {
super(request);
this.response = response;
this.jbootCache = JbootCacheManager.me().getCache(cacheType);
}

Expand All @@ -60,7 +59,7 @@ public HttpSession getSession(boolean create) {
}

String sessionId = getCookie(cookieName);
if (sessionId != null) {
if (sessionId != null ) {
httpSession = new JbootHttpSession(sessionId, getRequest().getServletContext(), createSessionStore(sessionId));
} else if (create) {
sessionId = UUID.randomUUID().toString().replace("-", "");
Expand All @@ -82,7 +81,7 @@ private Map<String, Object> createSessionStore(String sessionId) {
/**
* http请求技术时,更新session信息,包括:刷新session的存储时间,更新session数据,清空session数据等
*/
public void finish() {
public void refreshSession() {
if (httpSession == null) {
return;
}
Expand All @@ -92,6 +91,13 @@ public void finish() {
jbootCache.remove(cacheName, httpSession.getId());
setCookie(cookieName, null, 0);
}

// 空的httpSession数据
else if (httpSession.isEmpty()) {
jbootCache.remove(cacheName, httpSession.getId());
setCookie(cookieName, null, 0);
}

//session 已经被修改(session数据的增删改查)
else if (httpSession.isDataChanged()) {
Map<String, Object> snapshot = httpSession.snapshot();
Expand Down Expand Up @@ -143,9 +149,8 @@ private void setCookie(String name, String value, int maxAgeInSeconds) {
if (cookieDomain != null) {
cookie.setDomain(cookieDomain);
}
cookie.setMaxAge(cookieMaxAge);
cookie.setHttpOnly(true);
JbootRequestContext.getResponse().addCookie(cookie);
response.addCookie(cookie);
}

}

0 comments on commit d1a3723

Please sign in to comment.