Author: angela Date: Thu Nov 10 15:05:42 2011 New Revision: 1200363 URL: http://svn.apache.org/viewvc?rev=1200363&view=rev Log: JCR-3144 : JSR 283 Workspace Management add support for - Workspace.createWorkspace(String) - Workspace.deleteWorkspace(String) Added: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/WorkspaceTest.java Modified: jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/jcr/JCRWebdavServer.java jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/JCRWebdavServerServlet.java jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/RootCollection.java jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/WorkspaceResourceImpl.java jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/RepositoryServiceImpl.java Modified: jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/jcr/JCRWebdavServer.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/jcr/JCRWebdavServer.java?rev=1200363&r1=1200362&r2=1200363&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/jcr/JCRWebdavServer.java (original) +++ jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/jcr/JCRWebdavServer.java Thu Nov 10 15:05:42 2011 @@ -18,6 +18,7 @@ package org.apache.jackrabbit.server.jcr import org.apache.jackrabbit.server.SessionProvider; import org.apache.jackrabbit.webdav.DavException; +import org.apache.jackrabbit.webdav.DavMethods; import org.apache.jackrabbit.webdav.DavSession; import org.apache.jackrabbit.webdav.DavSessionProvider; import org.apache.jackrabbit.webdav.WebdavRequest; @@ -320,7 +321,10 @@ public class JCRWebdavServer implements */ private Session getRepositorySession(WebdavRequest request) throws DavException { try { - String workspaceName = request.getRequestLocator().getWorkspaceName(); + String workspaceName = null; + if (DavMethods.DAV_MKWORKSPACE != DavMethods.getMethodCode(request.getMethod())) { + workspaceName = request.getRequestLocator().getWorkspaceName(); + } return sessionProvider.getSession(request, repository, workspaceName); } catch (LoginException e) { // LoginException results in UNAUTHORIZED, Modified: jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/JCRWebdavServerServlet.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/JCRWebdavServerServlet.java?rev=1200363&r1=1200362&r2=1200363&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/JCRWebdavServerServlet.java (original) +++ jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/JCRWebdavServerServlet.java Thu Nov 10 15:05:42 2011 @@ -23,6 +23,7 @@ import org.apache.jackrabbit.server.Sess import org.apache.jackrabbit.server.jcr.JCRWebdavServer; import org.apache.jackrabbit.webdav.DavException; import org.apache.jackrabbit.webdav.DavLocatorFactory; +import org.apache.jackrabbit.webdav.DavMethods; import org.apache.jackrabbit.webdav.DavResource; import org.apache.jackrabbit.webdav.DavResourceFactory; import org.apache.jackrabbit.webdav.DavServletResponse; @@ -131,8 +132,10 @@ public abstract class JCRWebdavServerSer Session repositorySesssion = JcrDavSession.getRepositorySession(request.getDavSession()); String reqWspName = resource.getLocator().getWorkspaceName(); String wsName = repositorySesssion.getWorkspace().getName(); - // compare workspace names if the requested resource isn't the root-collection. - if (reqWspName != null && !reqWspName.equals(wsName)) { + // compare workspace names if the requested resource isn't the + // root-collection and the request not MKWORKSPACE. + if (DavMethods.DAV_MKWORKSPACE != DavMethods.getMethodCode(request.getMethod()) && + reqWspName != null && !reqWspName.equals(wsName)) { return false; } } catch (DavException e) { Modified: jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/RootCollection.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/RootCollection.java?rev=1200363&r1=1200362&r2=1200363&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/RootCollection.java (original) +++ jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/RootCollection.java Thu Nov 10 15:05:42 2011 @@ -16,6 +16,7 @@ */ package org.apache.jackrabbit.webdav.jcr; +import org.apache.jackrabbit.util.Text; import org.apache.jackrabbit.webdav.DavException; import org.apache.jackrabbit.webdav.DavResource; import org.apache.jackrabbit.webdav.DavResourceFactory; @@ -33,6 +34,7 @@ import org.slf4j.LoggerFactory; import javax.jcr.RepositoryException; import javax.jcr.Session; import javax.jcr.Repository; +import javax.jcr.Workspace; import java.util.ArrayList; import java.util.List; import java.util.Date; @@ -66,6 +68,7 @@ public class RootCollection extends Abst initSupportedReports(); } + //--------------------------------------------------------< DavResource >--- /** * Returns a string listing the METHODS for this resource as it * is required for the "Allow" response header. @@ -74,7 +77,7 @@ public class RootCollection extends Abst * @see org.apache.jackrabbit.webdav.DavResource#getSupportedMethods() */ public String getSupportedMethods() { - StringBuffer sb = new StringBuffer(DavResource.METHODS); + StringBuilder sb = new StringBuilder(DavResource.METHODS); sb.append(", "); sb.append(DeltaVResource.METHODS_INCL_MKWORKSPACE); sb.append(", "); @@ -116,6 +119,7 @@ public class RootCollection extends Abst * Always returns 'now' * * @return + * @see org.apache.jackrabbit.webdav.DavResource#getModificationTime() */ public long getModificationTime() { return new Date().getTime(); @@ -126,6 +130,7 @@ public class RootCollection extends Abst * * @param outputContext * @throws IOException + * @see DavResource#spool(org.apache.jackrabbit.webdav.io.OutputContext) */ public void spool(OutputContext outputContext) throws IOException { if (outputContext.hasStream()) { @@ -136,7 +141,7 @@ public class RootCollection extends Abst String repVersion = rep.getDescriptor(Repository.REP_VERSION_DESC); String repostr = repName + " " + repVersion; - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append(""); sb.append(repostr); sb.append(""); @@ -213,11 +218,34 @@ public class RootCollection extends Abst } /** - * Throws exception: 403 Forbidden. + * Calls {@link Workspace#deleteWorkspace(String)} for the workspace with + * the name as indicated by the specified member. + * * @see DavResource#removeMember(org.apache.jackrabbit.webdav.DavResource) */ public void removeMember(DavResource member) throws DavException { - throw new DavException(DavServletResponse.SC_FORBIDDEN); + Workspace wsp = getRepositorySession().getWorkspace(); + String name = Text.getName(member.getResourcePath()); + try { + wsp.deleteWorkspace(name); + } catch (RepositoryException e) { + throw new JcrDavException(e); + } + } + + //-----------------------------------------------------< DeltaVResource >--- + /** + * @see DeltaVResource#addWorkspace(org.apache.jackrabbit.webdav.DavResource) + */ + @Override + public void addWorkspace(DavResource workspace) throws DavException { + Workspace wsp = getRepositorySession().getWorkspace(); + String name = workspace.getDisplayName(); + try { + wsp.createWorkspace(name); + } catch (RepositoryException e) { + throw new JcrDavException(e); + } } //-------------------------------------------------------------------------- Modified: jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/WorkspaceResourceImpl.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/WorkspaceResourceImpl.java?rev=1200363&r1=1200362&r2=1200363&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/WorkspaceResourceImpl.java (original) +++ jackrabbit/trunk/jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/jcr/WorkspaceResourceImpl.java Thu Nov 10 15:05:42 2011 @@ -70,6 +70,7 @@ import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -143,7 +144,7 @@ public class WorkspaceResourceImpl exten //--------------------------------------------------------< DavResource >--- public String getSupportedMethods() { - StringBuffer sb = new StringBuffer(DavResource.METHODS); + StringBuilder sb = new StringBuilder(DavResource.METHODS); sb.append(", "); sb.append(DeltaVResource.METHODS_INCL_MKWORKSPACE); sb.append(", "); @@ -155,10 +156,18 @@ public class WorkspaceResourceImpl exten } /** - * @return true + * @return true if the workspace name (see {@link #getDisplayName()} is + * present in the list of available workspace names such as exposed by + * the editing JCR session. */ public boolean exists() { - return true; + try { + List available = Arrays.asList(getRepositorySession().getWorkspace().getAccessibleWorkspaceNames()); + return available.contains(getDisplayName()); + } catch (RepositoryException e) { + log.warn(e.getMessage()); + return false; + } } /** @@ -203,7 +212,7 @@ public class WorkspaceResourceImpl exten String repVersion = rep.getDescriptor(Repository.REP_VERSION_DESC); String repostr = repName + " " + repVersion; - StringBuffer sb = new StringBuffer(); + StringBuilder sb = new StringBuilder(); sb.append(""); sb.append(repostr); sb.append(""); @@ -243,7 +252,7 @@ public class WorkspaceResourceImpl exten public DavResource getCollection() { DavResource collection = null; // create location with 'null' values for workspace-path and resource-path - DavResourceLocator parentLoc = getLocator().getFactory().createResourceLocator(getLocator().getPrefix(), null, null); + DavResourceLocator parentLoc = getLocator().getFactory().createResourceLocator(getLocator().getPrefix(), null, null, false); try { collection = createResourceFromLocator(parentLoc); } catch (DavException e) { Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java?rev=1200363&r1=1200362&r2=1200363&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java (original) +++ jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/TestAll.java Thu Nov 10 15:05:42 2011 @@ -104,6 +104,9 @@ public class TestAll extends TestCase { // observation suite.addTestSuite(ObservationTest.class); + // workspace mgt + suite.addTestSuite(WorkspaceTest.class); + return suite; } } \ No newline at end of file Added: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/WorkspaceTest.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/WorkspaceTest.java?rev=1200363&view=auto ============================================================================== --- jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/WorkspaceTest.java (added) +++ jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/WorkspaceTest.java Thu Nov 10 15:05:42 2011 @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) 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.apache.jackrabbit.jcr2spi; + +import org.apache.jackrabbit.test.AbstractJCRTest; +import org.apache.jackrabbit.test.NotExecutableException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.jcr.NoSuchWorkspaceException; +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.jcr.UnsupportedRepositoryOperationException; +import javax.jcr.Workspace; +import java.util.Arrays; +import java.util.List; + +/** + * WorkspaceTest... + */ +public class WorkspaceTest extends AbstractJCRTest { + + /** + * logger instance + */ + private static final Logger log = LoggerFactory.getLogger(WorkspaceTest.class); + + private static String getNewWorkspaceName(Workspace wsp) throws RepositoryException { + List names = Arrays.asList(wsp.getAccessibleWorkspaceNames()); + int index = 0; + while (names.contains("testWsp_" + index)) { + index++; + } + return "testWsp_" + index; + } + + public void testCreateWorkspace() throws Exception { + Session s = null; + try { + Workspace wsp = superuser.getWorkspace(); + String name = getNewWorkspaceName(wsp); + wsp.createWorkspace(name); + + List wsps = Arrays.asList(wsp.getAccessibleWorkspaceNames()); + assertTrue(wsps.contains(name)); + + s = getHelper().getSuperuserSession(name); + Workspace newW = s.getWorkspace(); + assertEquals(name, newW.getName()); + } catch (UnsupportedRepositoryOperationException e) { + throw new NotExecutableException(); + } catch (UnsupportedOperationException e) { + throw new NotExecutableException(); + } finally { + if (s != null) { + s.logout(); + } + } + } + + public void testCreateWorkspaceFromSource() throws Exception { + Session s = null; + try { + Workspace wsp = superuser.getWorkspace(); + String name = getNewWorkspaceName(wsp); + + wsp.createWorkspace(name, wsp.getName()); + + List wsps = Arrays.asList(wsp.getAccessibleWorkspaceNames()); + assertTrue(wsps.contains(name)); + + s = getHelper().getSuperuserSession(name); + Workspace newW = s.getWorkspace(); + assertEquals(name, newW.getName()); + } catch (UnsupportedRepositoryOperationException e) { + throw new NotExecutableException(); + } catch (UnsupportedOperationException e) { + throw new NotExecutableException(); + } finally { + if (s != null) { + s.logout(); + } + } + } + + public void testDeleteWorkspace() throws Exception { + try { + Workspace wsp = superuser.getWorkspace(); + String name = getNewWorkspaceName(wsp); + + wsp.createWorkspace(name, wsp.getName()); + + List wsps = Arrays.asList(wsp.getAccessibleWorkspaceNames()); + assertTrue(wsps.contains(name)); + + wsp.deleteWorkspace(name); + + wsps = Arrays.asList(wsp.getAccessibleWorkspaceNames()); + assertFalse(wsps.contains(name)); + + Session s = null; + try { + s = getHelper().getSuperuserSession(name); + fail(name + " has been deleted."); + } catch (NoSuchWorkspaceException e) { + // success + } finally { + if (s != null) { + s.logout(); + } + } + } catch (UnsupportedRepositoryOperationException e) { + throw new NotExecutableException(); + } catch (UnsupportedOperationException e) { + throw new NotExecutableException(); + } + } +} \ No newline at end of file Modified: jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/RepositoryServiceImpl.java URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/RepositoryServiceImpl.java?rev=1200363&r1=1200362&r2=1200363&view=diff ============================================================================== --- jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/RepositoryServiceImpl.java (original) +++ jackrabbit/trunk/jackrabbit-spi2dav/src/main/java/org/apache/jackrabbit/spi2dav/RepositoryServiceImpl.java Thu Nov 10 15:05:42 2011 @@ -147,6 +147,7 @@ import org.apache.jackrabbit.webdav.clie import org.apache.jackrabbit.webdav.client.methods.LockMethod; import org.apache.jackrabbit.webdav.client.methods.MergeMethod; import org.apache.jackrabbit.webdav.client.methods.MkColMethod; +import org.apache.jackrabbit.webdav.client.methods.MkWorkspaceMethod; import org.apache.jackrabbit.webdav.client.methods.MoveMethod; import org.apache.jackrabbit.webdav.client.methods.OptionsMethod; import org.apache.jackrabbit.webdav.client.methods.OrderPatchMethod; @@ -1009,7 +1010,7 @@ public class RepositoryServiceImpl imple } /** - * @see RepositoryService#getItemInfos(SessionInfo, NodeId) + * @see RepositoryService#getItemInfos(SessionInfo, ItemId) */ public Iterator getItemInfos(SessionInfo sessionInfo, ItemId itemId) throws RepositoryException { // TODO: implement batch read properly: @@ -2414,16 +2415,46 @@ public class RepositoryServiceImpl imple * {@inheritDoc} */ public void createWorkspace(SessionInfo sessionInfo, String name, String srcWorkspaceName) throws AccessDeniedException, UnsupportedRepositoryOperationException, NoSuchWorkspaceException, RepositoryException { - // TODO - throw new UnsupportedOperationException("JCR-2003. Implementation missing"); + if (srcWorkspaceName != null) { + throw new UnsupportedOperationException("JCR-2003. Implementation missing"); + } + + MkWorkspaceMethod method = null; + try { + method = new MkWorkspaceMethod(uriResolver.getWorkspaceUri(name)); + initMethod(method, sessionInfo, true); + getClient(sessionInfo).executeMethod(method); + method.checkSuccess(); + } catch (IOException e) { + throw new RepositoryException(e); + } catch (DavException e) { + throw ExceptionConverter.generate(e); + } finally { + if (method != null) { + method.releaseConnection(); + } + } } /** * {@inheritDoc} */ public void deleteWorkspace(SessionInfo sessionInfo, String name) throws AccessDeniedException, UnsupportedRepositoryOperationException, NoSuchWorkspaceException, RepositoryException { - // TODO - throw new UnsupportedOperationException("JCR-2003. Implementation missing"); + DeleteMethod method = null; + try { + method = new DeleteMethod(uriResolver.getWorkspaceUri(name)); + initMethod(method, sessionInfo, true); + getClient(sessionInfo).executeMethod(method); + method.checkSuccess(); + } catch (IOException e) { + throw new RepositoryException(e); + } catch (DavException e) { + throw ExceptionConverter.generate(e); + } finally { + if (method != null) { + method.releaseConnection(); + } + } } /**