new
Brought to you by:
yagupoli
package yl.yljwl; import yl.ylcommons.YLGenericBean; import yl.ylcommons.YLGenericPair; import yl.ylcommons.YLUtils; import java.io.IOException; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; /** * Created by yl on Apr 24 2019 */ class JWLSessionContext implements Serializable { private TreeMap<String, YLGenericBean> tabScopeBeans = new TreeMap<>(); YLGenericPair<String, YLGenericBean> getTabScopeBean(boolean multiTab, String tabId, boolean toCreate) { final YLGenericPair<String, YLGenericBean> ret = new YLGenericPair<>(); if(multiTab) { ret.setValue2(tabScopeBeans.get(tabId)); } else { ret.setValue1(null); } //YLGenericBean ret = tabScopeBeans.get(multiTab ? tabId : String.valueOf(null)); return ret; } private void writeObject(java.io.ObjectOutputStream out) throws ClassNotFoundException, IOException { YLUtils.serializeObject(this, out); } private void readObject(java.io.ObjectInputStream in) throws ClassNotFoundException, IOException { final JWLSessionContext o = YLUtils.deserializeObject(getClass(), in); tabScopeBeans = o.tabScopeBeans; } } package yl.yljwl; import yl.ylcommons.YLGenericPair; import yl.ylcommons.YLMsg; import yl.ylcommons.YLUtils; import yl.ylwebcommons.YLWebUtils; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.http.*; import java.io.IOException; import java.math.BigDecimal; import java.nio.charset.Charset; /** * Created by yl on 22/11/17. */ @MultipartConfig public class JWLBootstrapServlet extends HttpServlet { private JWLApp app; @Override public void init() throws ServletException { try { final String appClass = getServletConfig().getInitParameter(JWLConstants.JWLBootstrapServletInitParam_APP_CLASS); final Class clazz = Class.forName(appClass); if(!YLUtils.isRelated(JWLApp.class, clazz)) { throw new JWLException(YLUtils.formatMsg("class {} should extend class {}", YLUtils.fqn(clazz), YLUtils.fqn(JWLApp.class))); } app = (JWLApp) YLUtils.newInstance(clazz); app.init(getServletConfig()); } catch(Exception e) { YLUtils.<ServletException>reThrow(e, ServletException.class); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req, resp); } private void process(HttpServletRequest req, HttpServletResponse resp) { final YLGenericPair<String, String> pagePath = getPagePath(req); final JWLPageContext pageContext = new JWLPageContext(req, resp, pagePath.getValue1()); try { req.setCharacterEncoding(Charset.forName("UTF-8").name()); resp.setCharacterEncoding(Charset.forName("UTF-8").name()); boolean redirect = false; if (app.isMultiTabApp()) { //if(YLUtils.compare(pagePath.getValue1(), "*") == 0) { // pagePath.setValue1(app.getTabScopeBean(null, session, true).getValue1()); // redirect = true; //} //else { // pagePath.setValue1(app.getTabScopeBean(pagePath.getValue1(), session, false).getValue1()); //} } else { redirect = YLUtils.compare(pagePath.getValue1(), "*") != 0; } final JWLPageResolver pageResolver = app.getPageResolver(); if (pagePath.getValue2() == null || redirect) { if(app.isMultiTabApp()) { } else { pageContext.setTabId("*"); } JWLPageType pageType = pagePath.getValue2() == null ? pageResolver.getHomePage(app) : pageResolver.getPageType(app); pageType.setUrlParameters(pageContext.getUrlQueryParams()); if(pagePath.getValue2() == null) { pageType = pageResolver.resolveClass2Path(app, pageType); } else { pageType.setPath(pagePath.getValue2()); pageType = pageResolver.resolvePath2Class(app, pageType); } YLWebUtils.sendRedirect(resp, JWLAbstractPage.getPageURL(pageType, pageContext)); return; } } catch(Exception e) { log(new YLMsg(YLMsg.MsgType.ERR, getClass(), "process", e).toString()); e.printStackTrace(); if(resp.getStatus() != HttpServletResponse.SC_FOUND) { JWLAbstractPage.sendError(app, pageContext, e, req, resp); } } // final HttpSession session = req.getSession(); // // try { // synchronized(session) { // final String sessionCookie = YLWebUtils.getCookie(JWLConstants.COOKIE_PREFIX, JWLConstants.getSysName(JWLBootstrapServlet.class, "sessionCookie"), req); // // // if(session.isNew()) { // // if(sessionCookie != null) { // // } // // //if(appContext == null) { // // YLUtils.wait(session, -1); // //} // } // } // } // finally { // // } } private static YLGenericPair<String, String> getPagePath(HttpServletRequest req) { if(!req.getServletPath().endsWith(JWLConstants.JWLBootstrapServlet_URL_PATTERN.substring(1)) || YLUtils.compare(req.getServletPath(), "/" + JWLConstants.JWLBootstrapServlet_URL_PATTERN.substring(1)) == 0) { return new YLGenericPair<>(); } final int ndx = req.getServletPath().lastIndexOf("/"); String tabId = req.getServletPath().substring(ndx + 1); tabId = tabId.substring(0, tabId.indexOf(JWLConstants.JWLBootstrapServlet_URL_PATTERN.substring(1))); return new YLGenericPair<>(tabId, ndx == 0 ? null : req.getServletPath().substring(1, ndx)); } } package yl.yljwl; import yl.ylcommons.YLGenericBean; import yl.ylcommons.YLGenericBeanImpl; import yl.ylcommons.YLGenericPair; import yl.ylcommons.YLUtils; import javax.naming.InitialContext; import javax.servlet.ServletConfig; import javax.servlet.http.HttpSession; import java.math.BigDecimal; import java.net.URL; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; /** * Created by yl on Nov 22 2017 */ public abstract class JWLApp { private ServletConfig servletConfig; private final YLGenericBean appScopeBean = new YLGenericBeanImpl(); private boolean multiTabApp; private final Map<String, String> contextParams = new HashMap<>(); private String ajaxProgressIndicatorCssClass; private int ajaxTimeoutInMilliseconds; void init(ServletConfig servletConfig) throws JWLException { this.servletConfig = servletConfig; final String sMultiTabApp = servletConfig.getInitParameter(JWLConstants.JWLBootstrapServletInitParam_MULTI_TAB_APP); if(!YLUtils.isEmpty(sMultiTabApp) && YLUtils.compare(Boolean.TRUE.toString().toLowerCase(), sMultiTabApp.toLowerCase()) == 0) { multiTabApp = true; } for(Enumeration<String> en = servletConfig.getServletContext().getInitParameterNames(); en.hasMoreElements(); ) { final String name = en.nextElement(); contextParams.put(name, servletConfig.getServletContext().getInitParameter(name)); } String s = servletConfig.getInitParameter(JWLConstants.JWLBootstrapServletInitParam_AJAX_PROGRESS_INDICATOR_CSS_CLASS); ajaxProgressIndicatorCssClass = YLUtils.isEmpty(s) ? "JWLAjaxProgressIndicator" : s; final BigDecimal n = YLUtils.parseNumber(servletConfig.getInitParameter(JWLConstants.JWLBootstrapServletInitParam_AJAX_TIMEOUT_IN_MILLISECONDS)); if(n == null || n.intValue() <= 0) { ajaxTimeoutInMilliseconds = Integer.MAX_VALUE; } else { ajaxTimeoutInMilliseconds = n.intValue(); } onInit(); } public boolean isMultiTabApp() { return multiTabApp; } public Map<String, String> getContextParams() { return contextParams; } public int getAjaxTimeoutInMilliseconds() { return ajaxTimeoutInMilliseconds; } public String getAjaxProgressIndicatorCssClass() { return ajaxProgressIndicatorCssClass; } public Object lookup(String name) throws JWLException { try { return InitialContext.doLookup(name); } catch(Exception e) { YLUtils.<JWLException>reThrow(e, JWLException.class); return null; } } public URL getResourceURL(String path) throws JWLException { final String p = path.startsWith("/") ? path : "/" + path; try { URL url = servletConfig.getServletContext().getResource(p); if(url == null) { url = YLUtils.getURL(path, Thread.currentThread().getContextClassLoader()); } return url; } catch(Exception e) { YLUtils.<JWLException>reThrow(e, JWLException.class); return null; } } public URL getResourceURL(Class parent, String name) throws JWLException { String s = parent.getPackage().getName().replace('.', '/'); if(!YLUtils.isEmpty(s)) { s += '/'; } s += name; return getResourceURL(s); } /** * The returned bean should be protected by synchronized * @return */ public YLGenericBean getAppScopeBean() { return appScopeBean; } public YLGenericPair<String, YLGenericBean> getTabScopeBean(String tabId, HttpSession session, boolean create) { JWLSessionContext sessionContext; if(session.isNew()) { sessionContext = new JWLSessionContext(); session.setAttribute(JWLConstants.getSysName(JWLApp.class, "sessionContext"), sessionContext); } else { sessionContext = (JWLSessionContext) session.getAttribute(JWLConstants.getSysName(JWLApp.class, "sessionContext")); } return sessionContext.getTabScopeBean(isMultiTabApp(), tabId, create); } //QQQQQQQQQQQQQQ public YLGenericBean getTabScopeBean() { return null; } public abstract JWLPageResolver getPageResolver(); protected abstract void onInit() throws JWLException; //QQQQQQQQQQQQQQ YLGenericBean getSysScopeBean() { return null; } } package yl.yljwl; import org.json.JSONObject; import yl.ylcommons.YLGenericBean; import yl.ylcommons.YLGenericWrapper; import yl.ylcommons.YLMsg; import yl.ylcommons.YLUtils; import yl.yljwl.components.*; import yl.yljwl.events.JWLAbstractEvent; import yl.ylwebcommons.YLWebPrintWriter; import yl.ylwebcommons.YLWebUtils; import javax.servlet.http.*; import javax.servlet.jsp.PageContext; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.Charset; import java.time.Instant; import java.util.*; import java.util.stream.Collectors; /** * Created by yl on Nov 22 2017 */ public abstract class JWLAbstractPage { transient JWLPageContext pageContext; private transient JWLApp app; private transient JWLChangeLog changeLog; protected abstract void onInit() throws JWLException; protected abstract void onDestroy() throws JWLException; protected abstract void onLoad() throws JWLException; protected abstract void onUpdate() throws JWLException; protected void onSessionDestroyed() throws JWLException { throw new JWLExceptionSessionTimeout(); } protected void loadCommonResources() throws JWLException { getPageHeader().add(new JWLCss()).setPath(pageContext.getContextPath() + "/jweblet/res/css/jquery-ui.min.css"); getPageHeader().add(new JWLCss()).setPath(pageContext.getContextPath() + "/jweblet/res/css/jquery-ui-timepicker-addon.css"); getPageHeader().add(new JWLCss()).setPath(pageContext.getContextPath() + "/jweblet/res/css/quill.snow.css"); getPageHeader().add(new JWLCss()).setPath(pageContext.getContextPath() + "/jweblet/res/css/jweblet.css?ts=" + Instant.now().getEpochSecond()); getPageHeader().add(new JWLCss()).setPath(pageContext.getContextPath() + "/jweblet/res/css/jweblet-ext.css?ts=" + Instant.now().getEpochSecond()); getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/jquery-3.2.1.min.js"); getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/jquery-ui.min.js"); getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/jquery-ui-i18n.min.js"); getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/jquery-ui-timepicker-addon.js"); getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/jquery-ui-timepicker-addon-i18n.min.js"); //getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/jquery.disablescroll.min.js"); //getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/jquery.scrollintoview.min.js"); getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/quill.min.js"); getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/purify.min.js"); getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/jweblet.js?ts=" + Instant.now().getEpochSecond()); getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/jweblet-ext.js?ts=" + Instant.now().getEpochSecond()); getPageHeader().add(new JWLScript()).setPath(pageContext.getContextPath() + "/jweblet/res/js/jweblet-ext-i18.js?ts=" + Instant.now().getEpochSecond()); } protected final JWLPageContext getPageContext() { return pageContext; } public YLGenericBean getPageScopeBean() { return pageContext.getPageScopeBean(); } public YLGenericBean getTabScopeBean() { final JWLPageContext pageContext = getPageContext(); // return getApp().getTabScopeBean(pageContext.getTabId(),pageContext.getHttpServletRequest().getSession()); return null; } public String getContextPath() { return pageContext.getContextPath(); } public <T extends JWLAbstractComponent> T getComponentById(String id) { if(getPageBody() != null) { final JWLAbstractComponent ret = JWLAbstractComponent.getComponentById(id, getPageBody().getChildren()); if(ret != null) { return (T) ret; } } if(getPageHeader() != null) { final JWLAbstractComponent ret = JWLAbstractComponent.getComponentById(id, getPageHeader().getChildren()); if(ret != null) { return (T) ret; } } return null; } public <T extends JWLApp> T getApp() { return (T) app; } public Map<String, String[]> getUrlQueryParams() { return pageContext.getUrlQueryParams(); } public JWLPageBody getPageBody() { return pageContext.getPageBody(); } public JWLPageHeader getPageHeader() { return pageContext.getPageHeader(); } public int getUpdateIntervalInMilliseconds() { return pageContext.getUpdateIntervalInMilliseconds(); } public void setUpdateIntervalInMilliseconds(int updateIntervalInMilliseconds) { pageContext.setUpdateIntervalInMilliseconds(updateIntervalInMilliseconds); } public void redirect(String path) throws JWLException { try { if(!YLWebUtils.isAbsoluteURL(path)) { if(!path.startsWith("/")) { path = "/" + path; } if(!path.startsWith(pageContext.getContextPath() + "/")) { path = pageContext.getContextPath() + path; } } YLWebUtils.sendRedirect(pageContext.getHttpServletResponse(), path); } catch(Exception e) { YLUtils.<JWLException>reThrow(e, JWLException.class); } } public JWLChangeLog getChangeLog() { return changeLog; } void setPageContext(JWLPageContext pageContext) { this.pageContext = pageContext; this.pageContext.getPageHeader().setPage(this); this.pageContext.getPageBody().setPage(this); } void setApp(JWLApp app) { this.app = app; } public void setFocusedComponentId(String focusedComponentId) { pageContext.setFocusedComponentId(focusedComponentId); } public String getFocusedComponentId() { return pageContext.getFocusedComponentId(); } public void setCookie(String name, String value) { YLWebUtils.setCookie(JWLConstants.COOKIE_PREFIX,name,value,true,getPageContext().getHttpServletResponse()); } public String getCookie(String name) { return YLWebUtils.getCookie(JWLConstants.COOKIE_PREFIX,name,getPageContext().getHttpServletRequest()); } public Iterable<String> getCookieNames() { return YLWebUtils.getCookieNames(JWLConstants.COOKIE_PREFIX,getPageContext().getHttpServletRequest()); } public void removeCookie(String name) { YLWebUtils.removeCookie(JWLConstants.COOKIE_PREFIX,name,getPageContext().getHttpServletRequest(),getPageContext().getHttpServletResponse()); } protected static String getPageURL(JWLPageType pageType, JWLPageContext pageContext) throws JWLException { try { String p = pageType.getPath(); if(!p.startsWith("/")) { p = "/" + p; } if(!p.endsWith("/")) { p += "/"; } return pageContext.getContextPath() + p + pageContext.getTabId() + JWLConstants.JWLBootstrapServlet_URL_PATTERN.substring(1) + YLWebUtils.formatURLQueryString(pageType.getURLParameters()); } catch(Exception e) { YLUtils.<JWLException>reThrow(e, JWLException.class); return null; } } static void sendError(JWLApp app, JWLPageContext pageContext, Exception e, HttpServletRequest req, HttpServletResponse resp) { try { final JWLPageType pageType = app.getPageResolver().getErrorPage(app); pageType.setUrlParameters(pageContext.getUrlQueryParams()); app.getPageResolver().resolveClass2Path(app, pageType); if(e != null) { JWLAbstractErrorPage.setException(e, app.getSysScopeBean()); } if(e instanceof JWLExceptionAjax) { pageType.setUrlParameters(Collections.EMPTY_MAP); YLWebUtils.sendRedirect(resp, JWLAbstractPage.getPageURL(pageType, pageContext)); } else if(YLUtils.isEmpty(req.getHeader(JWLConstants.JSConstants.AJAX_HEADER_FLAG.getValue()))) { YLWebUtils.sendRedirect(resp, JWLAbstractPage.getPageURL(pageType, pageContext)); } else { resp.setContentType("application/json;charset=" + Charset.forName("UTF-8").name()); final JSONObject ret = new JSONObject(); ret.put(JWLConstants.JSConstants.AJAX_RESP_REDIRECTED_URL.getValue(), JWLAbstractPage.getPageURL(pageType, pageContext)); resp.getWriter().write(ret.toString()); } } catch(Exception ignore) { } } void process(HttpServletRequest req, HttpServletResponse resp) { for(Cookie cookie : req.getCookies()) { if(cookie.getName().startsWith(JWLConstants.COOKIE_PREFIX)) { cookie.setMaxAge(Integer.MAX_VALUE); resp.addCookie(cookie); } } Exception excp = null; try { onInit(); if(!YLUtils.isEmpty(req.getHeader(JWLConstants.JSConstants.AJAX_HEADER_FLAG.getValue()))) { pageUpdate(req, resp, false); } else if(!YLUtils.isEmpty(req.getHeader(JWLConstants.JSConstants.FORM_SUBMITTED_AJAX.getValue()))) { pageUpdate(req, resp, true); } else if(YLUtils.compare("POST", req.getMethod().toUpperCase()) == 0) { pageSubmit(req, resp); } else { final YLGenericBean sysScopeBean = getApp().getSysScopeBean(); if(sysScopeBean.get(JWLConstants.getSysName(JWLAbstractPage.class, "submitted")).getValue1()) { pageLoadSubmitted(req, resp); } else { pageLoad(req, resp); } } } catch(Exception e) { excp = e; } finally { Exception ee = null; try { onDestroy(); } catch(Exception e) { ee = e; } getApp().getSysScopeBean().set(JWLConstants.getSysName(JWLAbstractPage.class, "cachedPage"), excp == null && ee == null ? pageContext : null); if(excp != null) { YLUtils.reThrow(excp, JWLException.class); } if(ee != null) { YLUtils.reThrow(ee, JWLException.class); } } } private void pageUpdate(HttpServletRequest req, HttpServletResponse resp, boolean formSubmitted) { HttpServletResponse origResp = null; changeLog = new JWLChangeLog(); try { getPageContext(req, resp); origResp = pageContext.getHttpServletResponse(); final YLGenericWrapper<String> redirect = new YLGenericWrapper<>(); final HttpServletResponseWrapper myResp = new HttpServletResponseWrapper(resp) { @Override public void sendRedirect(String location) throws IOException { redirect.setObject(location); } }; pageContext.setHttpServletRequestResponse(req,myResp); if(formSubmitted) { final String fileUploadBoxId = req.getParameter(JWLConstants.JSConstants.FORM_SUBMITTED_FILE_UPLOAD.getValue()); if(fileUploadBoxId != null) { final JWLAbstractFileUploadBox fileUploadBox = getComponentById(fileUploadBoxId); if(fileUploadBox == null) { System.err.print(new YLMsg(YLMsg.MsgType.ERR, getClass(), "pageUpdate", "ajax update occured after new page was loaded! Cannot retrieve component with id: " + fileUploadBoxId).toString()); resp.setContentType("application/json;charset=" + Charset.forName("UTF-8").name()); final JSONObject ret = new JSONObject(); resp.getWriter().write(ret.toString()); return; } for(Part part : req.getParts()) { if(YLUtils.compare(part.getName(), fileUploadBoxId) == 0) { fileUploadBox.processUIValue(part); break; } } } else { final String formId = req.getParameter(JWLConstants.JSConstants.FORM_NAME.getValue()); if(formId == null) { throw new JWLExceptionBug("no form id has been passed with submitted data! This is the bug"); } final JWLAbstractForm form = getComponentById(formId); if(form == null) { System.err.print(new YLMsg(YLMsg.MsgType.ERR, getClass(), "pageUpdate", "ajax update occured after new page was loaded! Cannot retrieve component with id: " + formId).toString()); resp.setContentType("application/json;charset=" + Charset.forName("UTF-8").name()); final JSONObject ret = new JSONObject(); resp.getWriter().write(ret.toString()); return; } final String btnId = req.getParameter(JWLConstants.JSConstants.FORM_SUBMITTED_BUTTON.getValue()); if(btnId == null) { throw new JWLExceptionBug("form id=" + formId + " has been submitted with no submit button id! This is the bug"); } final JWLAbstractComponent btn = getComponentById(btnId); final JWLSubmitButton subBtn = (JWLSubmitButton) (btn instanceof JWLDialog.PushButton ? ((JWLDialog.PushButton) btn).getDelegate() : btn); if(subBtn == null) { System.err.print(new YLMsg(YLMsg.MsgType.ERR, getClass(), "pageUpdate", "ajax update occurred after new page was loaded! Cannot retrieve component with id: " + btnId).toString()); resp.setContentType("application/json;charset=" + Charset.forName("UTF-8").name()); final JSONObject ret = new JSONObject(); resp.getWriter().write(ret.toString()); return; } final Map<String, String[]> params = new LinkedHashMap<>(req.getParameterMap()); params.remove(JWLConstants.JSConstants.FORM_NAME.getValue()); params.remove(JWLConstants.JSConstants.FORM_SUBMITTED_BUTTON.getValue()); getPageContext().getUrlQueryParams().keySet().forEach(params::remove); form.submit(params, req.getParts(), subBtn); } } else { final JSONObject eventData = new JSONObject(req.getReader().lines().collect(Collectors.joining())); final String id = eventData.optString("id"); if(YLUtils.isEmpty(id)) { onUpdate(); } else { final JWLAbstractComponent src = getComponentById(id); if(src == null) { //throw new JWLExceptionBug("severe error occur. Cannot retrieve component with id: " + id + " this is the bug"); System.err.print(new YLMsg(YLMsg.MsgType.ERR, getClass(), "pageUpdate", "ajax update occured after new page was loaded! Cannot retrieve component with id: " + id).toString()); resp.setContentType("application/json;charset=" + Charset.forName("UTF-8").name()); final JSONObject ret = new JSONObject(); resp.getWriter().write(ret.toString()); return; } else { final JWLAbstractEvent event = src.resolveJSON2Event(eventData.optString("action"), eventData.optJSONObject("data")); if(event == null) { throw new JWLExceptionBug("severe error occur. Cannot resolve ajaxEvent from json: " + eventData.toString() + " this is the bug"); } src.fireEvent(event); } } } resp.setContentType("application/json;charset=" + Charset.forName("UTF-8").name()); final JSONObject ret = new JSONObject(); if(redirect.getObject() != null) { ret.put(JWLConstants.JSConstants.AJAX_RESP_REDIRECTED_URL.getValue(), redirect.getObject()); } else { ret.put(JWLConstants.JSConstants.AJAX_PAGE_UPDATE_INTERVAL.getValue(), getUpdateIntervalInMilliseconds()); String js = getChangeLog().toJS(); if(!YLUtils.isEmpty(pageContext.getFocusedComponentId())) { js = YLWebUtils.addSemicolon(js); js += YLUtils.formatMsg("YLJWeblet__.setFocus(\"{}\");", pageContext.getFocusedComponentId() ); pageContext.setFocusedComponentId(null); } if(!YLUtils.isEmpty(js)) { ret.put(JWLConstants.JSConstants.CHANGE_LOG_CHANGES.getValue(), js); } } resp.getWriter().write(ret.toString()); } catch(Exception e) { YLUtils.reThrow(e, JWLException.class); } finally { changeLog = null; if(origResp != null) { pageContext.setHttpServletRequestResponse(req,origResp); } } } private void pageSubmit(HttpServletRequest req, HttpServletResponse resp) throws JWLException { getPageContext(req,resp); final String formId = req.getParameter(JWLConstants.JSConstants.FORM_NAME.getValue()); if(formId == null) { throw new JWLException("no form id has been passed with submitted data"); } final JWLAbstractForm form = getComponentById(formId); if(form == null) { throw new JWLException("unknown form has been submitted! id=" + formId); } final String btnId = req.getParameter(JWLConstants.JSConstants.FORM_SUBMITTED_BUTTON.getValue()); if(btnId == null) { throw new JWLException("form id=" + formId + " has been submitted with no submit button id"); } final JWLSubmitButton subBtn = getComponentById(btnId); if(subBtn == null) { throw new JWLException("form id=" + formId + " has been submitted with unknown submit button id=" + btnId); } final Map<String, String[]> params = new LinkedHashMap<>(req.getParameterMap()); params.remove(JWLConstants.JSConstants.FORM_NAME.getValue()); params.remove(JWLConstants.JSConstants.FORM_SUBMITTED_BUTTON.getValue()); getPageContext().getUrlQueryParams().keySet().forEach(params::remove); try { form.submit(params, req.getParts(), subBtn); } catch(Exception e) { YLUtils.reThrow(e, JWLException.class); } getApp().getSysScopeBean().set(JWLConstants.getSysName(JWLAbstractPage.class, "submitted"), true); final JWLPageType pageType = getApp().getPageResolver().getPageType(getApp()); pageType.setUrlParameters(pageContext.getUrlQueryParams()); pageType.setType(getClass()); JWLPageType pt = getApp().getPageResolver().resolveClass2Path(getApp(), pageType); redirect(getPageURL(pt, pageContext)); } private void pageLoadSubmitted(HttpServletRequest req, HttpServletResponse resp) throws JWLException { getPageContext(req, resp); getApp().getSysScopeBean().remove(JWLConstants.getSysName(JWLAbstractPage.class, "submitted")); render(); } void pageLoad(HttpServletRequest req, HttpServletResponse resp) throws JWLException { onLoad(); if(resp.getStatus() == HttpServletResponse.SC_FOUND) { return; } loadCommonResources(); render(); } private void getPageContext(HttpServletRequest req, HttpServletResponse resp) throws JWLException { final JWLPageContext pageContext = (JWLPageContext) getApp().getSysScopeBean().get(JWLConstants.getSysName(JWLAbstractPage.class, "cachedPage")).getValue2(); if(pageContext == null) { throw new JWLExceptionSessionTimeout("Session timeout! Cannot retrieve cached page. Page class: " + YLUtils.fqn(getClass())); } pageContext.setHttpServletRequestResponse(req, resp); setPageContext(pageContext); } private void render() throws JWLException { try(YLWebPrintWriter pwBodyHtml = new YLWebPrintWriter(); YLWebPrintWriter pwBodyJs = new YLWebPrintWriter(); YLWebPrintWriter pwHeader = new YLWebPrintWriter()) { try { pageContext.getPageBody().render(pwBodyHtml, pwBodyJs); } finally { pwBodyHtml.flush(); if(!YLUtils.isEmpty(pageContext.getFocusedComponentId())) { pwBodyJs.addSemicolon(); pwBodyJs.write(YLUtils.formatMsg("YLJWeblet__.setFocus(\"{}\");", pageContext.getFocusedComponentId() )); pageContext.setFocusedComponentId(null); } pwBodyJs.flush(); } if(!YLUtils.isEmpty(pageContext.getOnLoadJSId())) { final JWLAbstractComponent c = getComponentById(pageContext.getOnLoadJSId()); if(c != null) { c.remove(); } } pwBodyJs.addSemicolon(); pwBodyJs.write(YLUtils.formatMsg("YLJWeblet__.pageUpdateTimer({});", getUpdateIntervalInMilliseconds() )); final String onLoad = YLUtils.formatMsg("YLJWeblet__.init({},\"{}\",{});{}", JWLConstants.JSConstants.toJSON(), getApp().getAjaxProgressIndicatorCssClass(), getApp().getAjaxTimeoutInMilliseconds(), pwBodyJs.getOutWriter().toString() ); final JWLScriptInline onLoadJS = new JWLScriptInline(); onLoadJS.setScript("$(function(){" + onLoad + "})"); pageContext.setOnLoadJSId(pageContext.getPageHeader().addChild(onLoadJS).getId()); try { pageContext.getPageHeader().render(pwHeader, null); } finally { pwHeader.flush(); } final PrintWriter pw = pageContext.getHttpServletResponse().getWriter(); pw.write("<!doctype html>"); pw.write("<html>"); pw.write(pwHeader.getOutWriter().toString()); pw.write(pwBodyHtml.getOutWriter().toString()); pw.write("</html>"); } catch(Exception e) { YLUtils.<JWLException>reThrow(e, JWLException.class); } finally { final YLGenericBean sysScopeBean = getApp().getSysScopeBean(); sysScopeBean.set(JWLConstants.getSysName(JWLAbstractPage.class, "cachedPage"), null); if(!(this instanceof JWLAbstractErrorPage)) { JWLAbstractErrorPage.setException(null, sysScopeBean); } } } } package yl.yljwl; import yl.ylcommons.YLGenericBean; import yl.ylcommons.YLGenericBeanImpl; import yl.ylcommons.YLUtils; import yl.ylwebcommons.YLWebUtils; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.Serializable; import java.util.LinkedHashMap; import java.util.Map; /** * Created by yl on 27/11/17. */ class JWLPageContext implements Serializable { private Map<String, String[]> urlQueryParams; private String contextPath; private String tabId; private transient HttpServletRequest httpServletRequest; private transient HttpServletResponse httpServletResponse; private JWLPageBody pageBody = new JWLPageBody(); private JWLPageHeader pageHeader = new JWLPageHeader(); private String focusedComponentId; private String onLoadJSId; private int updateIntervalInMilliseconds; private YLGenericBean pageScopeBean = new YLGenericBeanImpl(); JWLPageContext(HttpServletRequest req, HttpServletResponse resp, String tabId) { urlQueryParams = YLWebUtils.parseURLQueryString(req.getQueryString()); contextPath = req.getContextPath(); this.tabId = tabId; httpServletRequest = req; httpServletResponse = resp; } Map<String, String[]> getUrlQueryParams() { return urlQueryParams; } String getContextPath() { return contextPath; } String getTabId() { return tabId; } void setTabId(String tabId) { this.tabId = tabId; } HttpServletRequest getHttpServletRequest() { return httpServletRequest; } HttpServletResponse getHttpServletResponse() { return httpServletResponse; } JWLPageBody getPageBody() { return pageBody; } JWLPageHeader getPageHeader() { return pageHeader; } String getFocusedComponentId() { return focusedComponentId; } void setFocusedComponentId(String focusedComponentId) { this.focusedComponentId = focusedComponentId; } void setHttpServletRequestResponse(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { this.httpServletRequest = httpServletRequest; this.httpServletResponse = httpServletResponse; } String getOnLoadJSId() { return onLoadJSId; } void setOnLoadJSId(String onLoadJSId) { this.onLoadJSId = onLoadJSId; } int getUpdateIntervalInMilliseconds() { return updateIntervalInMilliseconds; } void setUpdateIntervalInMilliseconds(int updateIntervalInMilliseconds) { this.updateIntervalInMilliseconds = updateIntervalInMilliseconds; } YLGenericBean getPageScopeBean() { return pageScopeBean; } private void writeObject(java.io.ObjectOutputStream out) throws ClassNotFoundException, IOException { YLUtils.serializeObject(this, out); } private void readObject(java.io.ObjectInputStream in) throws ClassNotFoundException, IOException { final JWLPageContext o = YLUtils.deserializeObject(getClass(), in); urlQueryParams = new LinkedHashMap<>(o.urlQueryParams); contextPath = o.contextPath; tabId = o.tabId; pageBody = o.pageBody; pageHeader = o.pageHeader; focusedComponentId = o.focusedComponentId; onLoadJSId = o.onLoadJSId; updateIntervalInMilliseconds = o.updateIntervalInMilliseconds; pageScopeBean = o.pageScopeBean; } }
Diff: