X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2FAbstractCmsEntryPoint.java;h=311a8ff7cb8160a284b3db6d7d336194f89612e0;hb=1dd501c67d25472240084ea7c54ae1a3d5f70bd8;hp=d27ed006e6f282065264a4c2b57fa13db510b1dc;hpb=86db10fcb2299ebf71d5599a80dc54444b26f893;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/AbstractCmsEntryPoint.java b/org.argeo.cms/src/org/argeo/cms/AbstractCmsEntryPoint.java index d27ed006e..311a8ff7c 100644 --- a/org.argeo.cms/src/org/argeo/cms/AbstractCmsEntryPoint.java +++ b/org.argeo.cms/src/org/argeo/cms/AbstractCmsEntryPoint.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.Map; import javax.jcr.Node; +import javax.jcr.PathNotFoundException; import javax.jcr.Property; import javax.jcr.Repository; import javax.jcr.RepositoryException; @@ -14,6 +15,7 @@ import javax.security.auth.Subject; import javax.security.auth.login.CredentialNotFoundException; import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; +import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -34,8 +36,7 @@ import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** Manages history and navigation */ -public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint - implements CmsView { +public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint implements CmsView { private final Log log = LogFactory.getLog(AbstractCmsEntryPoint.class); private final Subject subject; @@ -49,6 +50,7 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint // Current state private Session session; private Node node; + private String nodePath;// useful when changing auth private String state; private String page; private Throwable exception; @@ -57,8 +59,8 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint private final JavaScriptExecutor jsExecutor; private final BrowserNavigation browserNavigation; - public AbstractCmsEntryPoint(Repository repository, String workspace, - String defaultPath, Map factoryProperties) { + public AbstractCmsEntryPoint(Repository repository, String workspace, String defaultPath, + Map factoryProperties) { this.repository = repository; this.workspace = workspace; this.defaultPath = defaultPath; @@ -67,14 +69,12 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint // Initial login try { - loginContext = new LoginContext(AuthConstants.LOGIN_CONTEXT_USER, - subject, new HttpRequestCallbackHandler( - UiContext.getHttpRequest())); + loginContext = new LoginContext(AuthConstants.LOGIN_CONTEXT_USER, subject, + new HttpRequestCallbackHandler(UiContext.getHttpRequest())); loginContext.login(); } catch (CredentialNotFoundException e) { try { - loginContext = new LoginContext( - AuthConstants.LOGIN_CONTEXT_ANONYMOUS, subject); + loginContext = new LoginContext(AuthConstants.LOGIN_CONTEXT_ANONYMOUS, subject); loginContext.login(); } catch (LoginException e1) { throw new ArgeoException("Cannot log as anonymous", e); @@ -87,8 +87,7 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint jsExecutor = RWT.getClient().getService(JavaScriptExecutor.class); browserNavigation = RWT.getClient().getService(BrowserNavigation.class); if (browserNavigation != null) - browserNavigation - .addBrowserNavigationListener(new CmsNavigationListener()); + browserNavigation.addBrowserNavigationListener(new CmsNavigationListener()); } @Override @@ -116,8 +115,7 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint try { initUi(parent); } catch (Exception e) { - throw new CmsException("Cannot create entrypoint contents", - e); + throw new CmsException("Cannot create entrypoint contents", e); } return null; } @@ -158,18 +156,17 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint } @Override - public Subject getSubject() { + public synchronized Subject getSubject() { return subject; } @Override - public void logout() { + public synchronized void logout() { if (loginContext == null) throw new CmsException("Login context should not be null"); try { loginContext.logout(); - LoginContext anonymousLc = new LoginContext( - AuthConstants.LOGIN_CONTEXT_ANONYMOUS, subject); + LoginContext anonymousLc = new LoginContext(AuthConstants.LOGIN_CONTEXT_ANONYMOUS, subject); anonymousLc.login(); authChange(anonymousLc); } catch (LoginException e) { @@ -178,29 +175,25 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint } @Override - public void authChange(LoginContext loginContext) { + public synchronized void authChange(LoginContext loginContext) { if (loginContext == null) throw new CmsException("Login context cannot be null"); this.loginContext = loginContext; - Subject.doAs(subject, new PrivilegedAction() { + Subject.doAs(loginContext.getSubject(), new PrivilegedAction() { @Override public Void run() { try { - String currentPath = null; - if (node != null) - currentPath = node.getPath(); JcrUtils.logoutQuietly(session); - session = repository.login(workspace); - if (currentPath != null) + if (nodePath != null) try { - node = session.getNode(currentPath); - } catch (Exception e) { - logout(); - session = repository.login(workspace); + node = session.getNode(nodePath); + } catch (PathNotFoundException e) { + // logout(); + // session = repository.login(workspace); navigateTo("~"); - throw e; + // throw e; } // refresh UI @@ -222,7 +215,7 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint doRefresh(); } - protected void doRefresh() { + protected synchronized void doRefresh() { Subject.doAs(subject, new PrivilegedAction() { @Override public Void run() { @@ -232,23 +225,11 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint }); } - // @Override - // public Object local(Msg msg) { - // String key = msg.getId(); - // int lastDot = key.lastIndexOf('.'); - // String className = key.substring(0, lastDot); - // String fieldName = key.substring(lastDot + 1); - // Locale locale = RWT.getLocale(); - // ResourceBundle rb = ResourceBundle.getBundle(className, locale, - // msg.getClassLoader()); - // return rb.getString(fieldName); - // } - /** Sets the state of the entry point and retrieve the related JCR node. */ protected synchronized String setState(String newState) { String previousState = this.state; - node = null; + Node node = null; page = null; this.state = newState; if (newState.equals("~")) @@ -274,20 +255,11 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint node = getDefaultNode(session); page = state; } - - // Title - String title; - if (node.isNodeType(NodeType.MIX_TITLE) - && node.hasProperty(Property.JCR_TITLE)) - title = node.getProperty(Property.JCR_TITLE).getString() - + " - " + getBaseTitle(); - else - title = getBaseTitle(); - jsExecutor.execute("document.title = \"" + title + "\""); + setNode(node); + String title = publishMetaData(node); if (log.isTraceEnabled()) - log.trace("node=" + node + ", state=" + state + " (page=" - + page + ", title=" + title + ")"); + log.trace("node=" + node + ", state=" + state + " (page=" + page + ")"); return title; } catch (Exception e) { @@ -295,15 +267,44 @@ public abstract class AbstractCmsEntryPoint extends AbstractEntryPoint if (previousState.equals("")) previousState = "~"; navigateTo(previousState); - throw new CmsException("Unexpected issue when accessing #" - + newState, e); + throw new CmsException("Unexpected issue when accessing #" + newState, e); } } - protected Node getNode() { + private String publishMetaData(Node node) throws RepositoryException { + // Title + String title; + if (node.isNodeType(NodeType.MIX_TITLE) && node.hasProperty(Property.JCR_TITLE)) + title = node.getProperty(Property.JCR_TITLE).getString() + " - " + getBaseTitle(); + else + title = getBaseTitle(); + + HttpServletRequest request = UiContext.getHttpRequest(); + if (request == null) + return null; + + StringBuilder js = new StringBuilder(); + title = title.replace("'", "\\'");// sanitize + js.append("document.title = '" + title + "';"); + jsExecutor.execute(js.toString()); + return title; + } + + // Simply remove some illegal character + // private String clean(String stringToClean) { + // return stringToClean.replaceAll("'", "").replaceAll("\\n", "") + // .replaceAll("\\t", ""); + // } + + protected synchronized Node getNode() { return node; } + private synchronized void setNode(Node node) throws RepositoryException { + this.node = node; + this.nodePath = node == null ? null : node.getPath(); + } + protected String getState() { return state; }