Author: areshetnyak
Date: 2009-08-28 09:12:40 -0400 (Fri, 28 Aug 2009)
New Revision: 49
Added:
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/NamesList.java
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/RepositoryServiceConf.java
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/RestRepositoryService.java
jcr/trunk/component/ext/src/test/java/org/exoplatform/services/jcr/ext/repository/
jcr/trunk/component/ext/src/test/java/org/exoplatform/services/jcr/ext/repository/RestRepositoryServiceTest.java
Modified:
jcr/trunk/component/ext/src/test/java/conf/standalone/test-configuration.xml
Log:
EXOJCR-13 : Add RestRepositoryService and test for this service.
Added:
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/NamesList.java
===================================================================
---
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/NamesList.java
(rev 0)
+++
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/NamesList.java 2009-08-28
13:12:40 UTC (rev 49)
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.jcr.ext.repository;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>
+ * Date: 27.08.2009
+ *
+ * @author <a href="mailto:alex.reshetnyak@exoplatform.com.ua">Alex
Reshetnyak</a>
+ * @version $Id: NamesList.java 111 2008-11-11 11:11:11Z rainf0x $
+ */
+public class NamesList
+{
+
+ /**
+ * List with names.
+ */
+ protected List<String> names = new ArrayList<String>();
+
+ /**
+ * Empty constructor.
+ */
+ public NamesList()
+ {
+ }
+
+ /**
+ * Constructor.
+ *
+ * @param names
+ * List, the list with names
+ */
+ public NamesList(List<String> names)
+ {
+ this.names = names;
+ }
+
+ /**
+ * @return List
+ * return the list of name
+ */
+ public List<String> getNames()
+ {
+ return names;
+ }
+
+ /**
+ * @param names
+ * List, the list with names
+ */
+ public void setNames(List<String> names)
+ {
+ this.names = names;
+ }
+
+}
Property changes on:
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/NamesList.java
___________________________________________________________________
Name: svn:eol-style
+ native
Added:
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/RepositoryServiceConf.java
===================================================================
---
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/RepositoryServiceConf.java
(rev 0)
+++
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/RepositoryServiceConf.java 2009-08-28
13:12:40 UTC (rev 49)
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.jcr.ext.repository;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.exoplatform.services.jcr.config.AbstractRepositoryServiceConfiguration;
+import org.exoplatform.services.jcr.config.RepositoryEntry;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * @author <a href="mailto:vitaly.parfonov@gmail.com">Vitaly
Parfonov</a>
+ * @version $Id: $
+ */
+public class RepositoryServiceConf
+ extends AbstractRepositoryServiceConfiguration
+{
+
+ /**
+ * @param defaultRepositoryName
+ * String, the default repository name
+ * @param repositories
+ * List of RepositoryEntry-s
+ */
+ public RepositoryServiceConf(List<RepositoryEntry> repositories, String
defaultRepositoryName)
+ {
+ this.defaultRepositoryName = defaultRepositoryName;
+ this.repositoryConfigurations = repositories;
+ }
+
+ /**
+ * The empty constructor.
+ */
+ public RepositoryServiceConf()
+ {
+ this.repositoryConfigurations = new ArrayList<RepositoryEntry>();
+ }
+
+ /**
+ * @param repositories
+ * List of RepositoryEntry
+ */
+ public RepositoryServiceConf(List<RepositoryEntry> repositories)
+ {
+ this.repositoryConfigurations = repositories;
+ }
+
+ /**
+ * @return List
+ * return the list of RepositoryEntry
+ */
+ public List<RepositoryEntry> getRepositories()
+ {
+ return repositoryConfigurations;
+ }
+
+ /**
+ * @param repositories
+ * List of RepositoryEntry
+ */
+ public void setRepositories(List<RepositoryEntry> repositories)
+ {
+ this.repositoryConfigurations = repositories;
+ }
+
+ public RepositoryEntry getRepositoryEntry(String repositoryName)
+ {
+ for (RepositoryEntry repositoryEntry : this.repositoryConfigurations)
+ {
+ if (repositoryEntry.getName().equals(repositoryName))
+ return repositoryEntry;
+ }
+ return null;
+ }
+
+}
Property changes on:
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/RepositoryServiceConf.java
___________________________________________________________________
Name: svn:eol-style
+ native
Added:
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/RestRepositoryService.java
===================================================================
---
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/RestRepositoryService.java
(rev 0)
+++
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/RestRepositoryService.java 2009-08-28
13:12:40 UTC (rev 49)
@@ -0,0 +1,501 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.jcr.ext.repository;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.annotation.security.RolesAllowed;
+import javax.jcr.RepositoryException;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.CacheControl;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.core.Response.Status;
+
+import org.exoplatform.common.http.HTTPStatus;
+import org.exoplatform.services.jcr.RepositoryService;
+import org.exoplatform.services.jcr.config.RepositoryConfigurationException;
+import org.exoplatform.services.jcr.config.RepositoryEntry;
+import org.exoplatform.services.jcr.config.RepositoryServiceConfiguration;
+import org.exoplatform.services.jcr.config.WorkspaceEntry;
+import org.exoplatform.services.jcr.core.ManageableRepository;
+import org.exoplatform.services.jcr.impl.core.RepositoryImpl;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+import org.exoplatform.services.rest.resource.ResourceContainer;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>
+ * Date: 27.08.2009
+ *
+ * @author <a href="mailto:alex.reshetnyak@exoplatform.com.ua">Alex
Reshetnyak</a>
+ * @version $Id: NamesList.java 111 2008-11-11 11:11:11Z rainf0x $
+ */
+
+@Path("/jcr-service")
+public class RestRepositoryService
+ implements ResourceContainer
+{
+
+ /**
+ * Definition the constants.
+ */
+ public static final class Constants
+ {
+
+ /**
+ * The base path to this service.
+ */
+ public static final String BASE_URL = "/jcr-service";
+
+ /**
+ * Definition the operation types.
+ */
+ public static final class OperationType
+ {
+ /**
+ * Repository service configuration operation.
+ */
+ public static final String REPOSITORY_SERVICE_CONFIGURATION =
"/repository-service-configuration";
+
+ /**
+ * Default workspace configuration operations.
+ */
+ public static final String DEFAULT_WS_CONFIG = "/default-ws-config";
+
+ /**
+ * Create new repository operations.
+ */
+ public static final String NEW_REPOSITORY = "/new-repository";
+
+ /**
+ * Create new workspace.
+ */
+ public static final String NEW_WORKSPACE = "/new-workspace";
+
+ /**
+ * Remove repository operation.
+ */
+ public static final String REMOVE_REPOSITORY = "/remove-repository";
+
+ /**
+ * Remove workspace operation.
+ */
+ public static final String REMOVE_WORKSPACE = "/remove-workspace";
+
+ /**
+ * The list of repositories name operations.
+ */
+ public static final String REPOSITORIES_LIST = "/repositories";
+
+ /**
+ * The list of workspaces name operation.
+ */
+ public static final String WORKSPACES_LIST = "/workspaces";
+
+ /**
+ * Update workspace configuration operation.
+ */
+ public static final String UPDATE_WORKSPACE_CONFIG =
"/update-workspace-config";
+
+ /**
+ * OperationType private constructor.
+ */
+ private OperationType()
+ {
+ }
+ }
+
+ /**
+ * Constants private constructor.
+ */
+ private Constants()
+ {
+ }
+ }
+
+ /**
+ * Class logger.
+ */
+ private final Log log =
ExoLogger.getLogger("jcr.ext.RestRepositoryService");
+
+ /**
+ *
+ */
+ private final RepositoryService repositoryService;
+
+ /**
+ * To disable cache control.
+ */
+ private static final CacheControl NO_CACHE = new CacheControl();
+
+ static
+ {
+ // noCache = new CacheControl();
+ NO_CACHE.setNoCache(true);
+ NO_CACHE.setNoStore(true);
+ }
+
+ /**
+ * @param repositoryService
+ */
+ public RestRepositoryService(RepositoryService repositoryService)
+ {
+ this.repositoryService = repositoryService;
+ }
+
+ /**
+ * @return
+ * @throws RepositoryConfigurationException
+ * @throws RepositoryException
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @RolesAllowed("administrators")
+ @Path("/repository-service-configuration")
+ public Response getRepositoryServiceConfiguration()
+ {
+ RepositoryServiceConfiguration configuration = repositoryService.getConfig();
+ RepositoryServiceConf conf =
+ new RepositoryServiceConf(configuration.getRepositoryConfigurations(),
configuration
+ .getDefaultRepositoryName());
+ return Response.ok(conf,
MediaType.APPLICATION_JSON_TYPE).cacheControl(NO_CACHE).build();
+ }
+
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @RolesAllowed("administrators")
+ @Path("/default-ws-config/{repositoryName}")
+ public Response getDefaultWorkspaceConfig(@PathParam("repositoryName")
String repositoryName)
+ {
+ String errorMessage = new String();
+ Status status;
+
+ try
+ {
+ String defaultWorkspaceName =
+
repositoryService.getRepository(repositoryName).getConfiguration().getDefaultWorkspaceName();
+
+ for (WorkspaceEntry wEntry :
repositoryService.getRepository(repositoryName).getConfiguration()
+ .getWorkspaceEntries())
+ if (defaultWorkspaceName.equals(wEntry.getName()))
+ return Response.ok(wEntry).cacheControl(NO_CACHE).build();
+
+ return Response.status(Response.Status.NOT_FOUND).entity("Can not get
default workspace configuration.").type(
+ MediaType.TEXT_PLAIN).cacheControl(NO_CACHE).build();
+ }
+ catch (RepositoryException e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.NOT_FOUND;
+ }
+ catch (Throwable e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.INTERNAL_SERVER_ERROR;
+ }
+
+ return
Response.status(status).entity(errorMessage).type(MediaType.TEXT_PLAIN_TYPE).cacheControl(NO_CACHE)
+ .build();
+ }
+
+ /**
+ * @param uriInfo
+ * @param newRepository
+ * @return
+ * @throws URISyntaxException
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ @RolesAllowed("administrators")
+ @Path("/new-repository")
+ public Response createRepository(@Context UriInfo uriInfo, RepositoryEntry
newRepository) throws URISyntaxException
+ {
+ String errorMessage = new String();
+ Status status;
+ try
+ {
+ repositoryService.createRepository(newRepository);
+ URI location = new URI(uriInfo.getBaseUri().toString() + uriInfo.getPath() +
"/" + newRepository.getName());
+ return Response.created(location).cacheControl(NO_CACHE).build();
+ }
+ catch (RepositoryException e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.BAD_REQUEST;
+ }
+ catch (RepositoryConfigurationException e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.BAD_REQUEST;
+ }
+ catch (Throwable e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.INTERNAL_SERVER_ERROR;
+ }
+
+ return
Response.status(status).entity(errorMessage).type(MediaType.TEXT_PLAIN_TYPE).cacheControl(NO_CACHE)
+ .build();
+ }
+
+ /**
+ * @param uriInfo
+ * @param repositoryName
+ * @param newWorkspace
+ * @return
+ * @throws URISyntaxException
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @RolesAllowed("administrators")
+ @Path("/new-workspace/{repositoryName}")
+ public Response createWorkspace(@Context UriInfo uriInfo,
@PathParam("repositoryName") String repositoryName,
+ WorkspaceEntry newWorkspace) throws URISyntaxException
+ {
+ String errorMessage = new String();
+ Status status;
+ try
+ {
+ RepositoryImpl repository = (RepositoryImpl)
repositoryService.getRepository(repositoryName);
+ repository.configWorkspace(newWorkspace);
+ repository.createWorkspace(newWorkspace.getName());
+ URI location = new URI(uriInfo.getBaseUri().toString() + uriInfo.getPath() +
"/" + newWorkspace.getName());
+ return Response.created(location).build();
+ }
+ catch (RepositoryException e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.NOT_FOUND;
+ }
+ catch (RepositoryConfigurationException e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.BAD_REQUEST;
+ }
+ catch (Throwable e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.INTERNAL_SERVER_ERROR;
+ }
+ return
Response.status(status).entity(errorMessage).type(MediaType.TEXT_PLAIN_TYPE).cacheControl(NO_CACHE)
+ .build();
+ }
+
+ /**
+ * @param uriInfo
+ * @param repositoryName
+ * @return
+ * @throws URISyntaxException
+ */
+ @POST
+ @RolesAllowed("administrators")
+ @Path("/remove-repository/{repositoryName}")
+ public Response removeRepository(@Context UriInfo uriInfo,
@PathParam("repositoryName") String repositoryName)
+ {
+ String errorMessage = new String();
+ Status status;
+
+ try
+ {
+ if (repositoryService.canRemoveRepository(repositoryName))
+ {
+ repositoryService.removeRepository(repositoryName);
+ return Response.noContent().build();
+ }
+ return Response.status(HTTPStatus.CONFLICT).entity("Can't remove
repository " + repositoryName).cacheControl(
+ NO_CACHE).build();
+ }
+ catch (RepositoryException e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.NOT_FOUND;
+ }
+ catch (Throwable e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.INTERNAL_SERVER_ERROR;
+ }
+
+ return
Response.status(status).entity(errorMessage).type(MediaType.TEXT_PLAIN_TYPE).cacheControl(NO_CACHE)
+ .build();
+ }
+
+ /**
+ * @param uriInfo
+ * @param repositoryName
+ * @return
+ * @throws URISyntaxException
+ */
+ @POST
+ @RolesAllowed("administrators")
+ @Path("/remove-workspace/{repositoryName}/{workspaceName}")
+ public Response removeWorkspace(@Context UriInfo uriInfo,
@PathParam("repositoryName") String repositoryName,
+ @PathParam("workspaceName") String workspaceName)
+ {
+ String errorMessage = new String();
+ Status status;
+
+ try
+ {
+ ManageableRepository repository =
repositoryService.getRepository(repositoryName);
+ if (repository.canRemoveWorkspace(workspaceName))
+ {
+ repository.removeWorkspace(workspaceName);
+ return Response.noContent().build();
+ }
+ return Response.status(Status.CONFLICT).entity(
+ "Can't remove workspace " + workspaceName + " in
repository " + repositoryName)
+ .cacheControl(NO_CACHE).build();
+ }
+ catch (RepositoryException e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.NOT_FOUND;
+ }
+ catch (RepositoryConfigurationException e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.NOT_FOUND;
+ }
+ catch (Throwable e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.INTERNAL_SERVER_ERROR;
+ }
+
+ return
Response.status(status).entity(errorMessage).type(MediaType.TEXT_PLAIN_TYPE).cacheControl(NO_CACHE)
+ .build();
+ }
+
+ /**
+ * @return
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @RolesAllowed("administrators")
+ @Path("/repositories")
+ public Response getRepositoryNames()
+ {
+ List<String> repositories = new ArrayList<String>();
+
+ for (RepositoryEntry rEntry :
repositoryService.getConfig().getRepositoryConfigurations())
+ {
+ repositories.add(rEntry.getName());
+ }
+
+ return Response.ok(new NamesList(repositories)).cacheControl(NO_CACHE).build();
+ }
+
+ /**
+ * @param repositoryName
+ * @return
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @RolesAllowed("administrators")
+ @Path("/workspaces/{repositoryName}")
+ public Response getWorkspaceNames(@PathParam("repositoryName") String
repositoryName)
+ {
+ String errorMessage = new String();
+ Status status;
+
+ try
+ {
+ List<String> workspaces = new ArrayList<String>();
+
+ for (WorkspaceEntry wEntry :
repositoryService.getRepository(repositoryName).getConfiguration()
+ .getWorkspaceEntries())
+ {
+ workspaces.add(wEntry.getName());
+ }
+
+ return Response.ok(new NamesList(workspaces)).cacheControl(NO_CACHE).build();
+
+ }
+ catch (RepositoryException e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.NOT_FOUND;
+ }
+ catch (Throwable e)
+ {
+ if (log.isDebugEnabled())
+ log.error(e.getMessage(), e);
+ errorMessage = e.getMessage();
+ status = Status.INTERNAL_SERVER_ERROR;
+ }
+
+ return
Response.status(status).entity(errorMessage).type(MediaType.TEXT_PLAIN_TYPE).cacheControl(NO_CACHE)
+ .build();
+ }
+
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @RolesAllowed("administrators")
+ @Path("/update-workspace-config/{repositoryName}/{workspaceName}")
+ public Response updateWorkspaceConfiguration(@PathParam("repositoryName")
String repositoryName,
+ @PathParam("workspaceName") String workspaceName, WorkspaceEntry
workspaceEntry)
+ {
+ return Response.status(Status.OK).entity("The method /update-workspace-config
not implemented.").type(
+ MediaType.TEXT_PLAIN_TYPE).cacheControl(NO_CACHE).build();
+ }
+
+}
Property changes on:
jcr/trunk/component/ext/src/main/java/org/exoplatform/services/jcr/ext/repository/RestRepositoryService.java
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: jcr/trunk/component/ext/src/test/java/conf/standalone/test-configuration.xml
===================================================================
---
jcr/trunk/component/ext/src/test/java/conf/standalone/test-configuration.xml 2009-08-28
13:04:12 UTC (rev 48)
+++
jcr/trunk/component/ext/src/test/java/conf/standalone/test-configuration.xml 2009-08-28
13:12:40 UTC (rev 49)
@@ -120,6 +120,10 @@
</value-param>
</init-params>
</component>
+
+ <component>
+
<type>org.exoplatform.services.jcr.ext.repository.RestRepositoryService</type>
+ </component>
<!-- backup -->
<component>
Added:
jcr/trunk/component/ext/src/test/java/org/exoplatform/services/jcr/ext/repository/RestRepositoryServiceTest.java
===================================================================
---
jcr/trunk/component/ext/src/test/java/org/exoplatform/services/jcr/ext/repository/RestRepositoryServiceTest.java
(rev 0)
+++
jcr/trunk/component/ext/src/test/java/org/exoplatform/services/jcr/ext/repository/RestRepositoryServiceTest.java 2009-08-28
13:12:40 UTC (rev 49)
@@ -0,0 +1,281 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.jcr.ext.repository;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import javax.ws.rs.core.MultivaluedMap;
+
+import org.exoplatform.services.jcr.config.ContainerEntry;
+import org.exoplatform.services.jcr.config.QueryHandlerEntry;
+import org.exoplatform.services.jcr.config.RepositoryEntry;
+import org.exoplatform.services.jcr.config.SimpleParameterEntry;
+import org.exoplatform.services.jcr.config.WorkspaceEntry;
+import org.exoplatform.services.jcr.ext.BaseStandaloneTest;
+import org.exoplatform.services.jcr.ext.app.SessionProviderService;
+import org.exoplatform.services.jcr.ext.app.ThreadLocalSessionProviderService;
+import org.exoplatform.services.jcr.ext.backup.ContainerRequestUserRole;
+import org.exoplatform.services.jcr.ext.common.SessionProvider;
+import org.exoplatform.services.rest.RequestHandler;
+import org.exoplatform.services.rest.impl.ContainerResponse;
+import org.exoplatform.services.rest.impl.InputHeadersMap;
+import org.exoplatform.services.rest.impl.MultivaluedMapImpl;
+import org.exoplatform.services.rest.tools.ByteArrayContainerResponseWriter;
+import org.exoplatform.services.security.ConversationState;
+import org.exoplatform.services.security.Identity;
+import org.exoplatform.ws.frameworks.json.JsonHandler;
+import org.exoplatform.ws.frameworks.json.JsonParser;
+import org.exoplatform.ws.frameworks.json.impl.BeanBuilder;
+import org.exoplatform.ws.frameworks.json.impl.JsonDefaultHandler;
+import org.exoplatform.ws.frameworks.json.impl.JsonGeneratorImpl;
+import org.exoplatform.ws.frameworks.json.impl.JsonParserImpl;
+import org.exoplatform.ws.frameworks.json.value.JsonValue;
+
+/**
+ * Created by The eXo Platform SAS.
+ *
+ * <br/>
+ * Date: 26.08.2009
+ *
+ * @author <a href="mailto:alex.reshetnyak@exoplatform.com.ua">Alex
Reshetnyak</a>
+ * @version $Id: RestRepositoryServiceTest.java 111 2008-11-11 11:11:11Z rainf0x $
+ */
+public class RestRepositoryServiceTest
+ extends BaseStandaloneTest
+{
+ private String REST_REPOSITORY_SERVICE_PATH =
RestRepositoryService.Constants.BASE_URL;
+
+ private RequestHandler handler;
+
+ public void setUp() throws Exception
+ {
+ super.setUp();
+
+ handler = (RequestHandler)
container.getComponentInstanceOfType(RequestHandler.class);
+
+ SessionProviderService sessionProviderService =
+ (SessionProviderService)
container.getComponentInstanceOfType(ThreadLocalSessionProviderService.class);
+ assertNotNull(sessionProviderService);
+ sessionProviderService.setSessionProvider(null, new SessionProvider(new
ConversationState(new Identity("root"))));
+ }
+
+ public void testRepositoriesList() throws Exception
+ {
+ MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
+ ContainerRequestUserRole creq =
+ new ContainerRequestUserRole("GET", new
URI(REST_REPOSITORY_SERVICE_PATH
+ +
RestRepositoryService.Constants.OperationType.REPOSITORIES_LIST), new URI(""),
null,
+ new InputHeadersMap(headers));
+
+ ByteArrayContainerResponseWriter responseWriter = new
ByteArrayContainerResponseWriter();
+ ContainerResponse cres = new ContainerResponse(responseWriter);
+ handler.handleRequest(creq, cres);
+
+ assertEquals(200, cres.getStatus());
+
+ NamesList repositories = (NamesList) getObject(NamesList.class,
responseWriter.getBody());
+
+ assertNotNull(repositories);
+ assertEquals(repositoryService.getConfig().getRepositoryConfigurations().size(),
repositories.getNames().size());
+ }
+
+ public void testWorkspacesList() throws Exception
+ {
+ String repoName = repositoryService.getConfig().getDefaultRepositoryName();
+
+ MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
+ ContainerRequestUserRole creq =
+ new ContainerRequestUserRole("GET", new
URI(REST_REPOSITORY_SERVICE_PATH
+ + RestRepositoryService.Constants.OperationType.WORKSPACES_LIST +
"/" + repoName + "/"),
+ new URI(""), null, new InputHeadersMap(headers));
+
+ ByteArrayContainerResponseWriter responseWriter = new
ByteArrayContainerResponseWriter();
+ ContainerResponse cres = new ContainerResponse(responseWriter);
+ handler.handleRequest(creq, cres);
+
+ assertEquals(200, cres.getStatus());
+
+ NamesList workspaces = (NamesList) getObject(NamesList.class,
responseWriter.getBody());
+
+ assertNotNull(workspaces);
+
assertEquals(repositoryService.getConfig().getRepositoryConfiguration(repoName).getWorkspaceEntries().size(),
+ workspaces.getNames().size());
+ }
+
+ public void testGetDefaultWorkspaceConfig() throws Exception
+ {
+ String repoName = repositoryService.getConfig().getDefaultRepositoryName();
+
+ MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
+ ContainerRequestUserRole creq =
+ new ContainerRequestUserRole("GET", new
URI(REST_REPOSITORY_SERVICE_PATH
+ + RestRepositoryService.Constants.OperationType.DEFAULT_WS_CONFIG
+ "/" + repoName + "/"),
+ new URI(""), null, new InputHeadersMap(headers));
+
+ ByteArrayContainerResponseWriter responseWriter = new
ByteArrayContainerResponseWriter();
+ ContainerResponse cres = new ContainerResponse(responseWriter);
+ handler.handleRequest(creq, cres);
+
+ assertEquals(200, cres.getStatus());
+
+ WorkspaceEntry workspaceEntry = (WorkspaceEntry) getObject(WorkspaceEntry.class,
responseWriter.getBody());
+
+ assertNotNull(workspaceEntry);
+
assertEquals(repositoryService.getConfig().getRepositoryConfiguration(repoName).getDefaultWorkspaceName(),
+ workspaceEntry.getName());
+ }
+
+ public void testGetRepositoryServiceConfiguration() throws Exception
+ {
+ MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
+ ContainerRequestUserRole creq =
+ new ContainerRequestUserRole("GET", new
URI(REST_REPOSITORY_SERVICE_PATH
+ +
RestRepositoryService.Constants.OperationType.REPOSITORY_SERVICE_CONFIGURATION), new
URI(""),
+ null, new InputHeadersMap(headers));
+
+ ByteArrayContainerResponseWriter responseWriter = new
ByteArrayContainerResponseWriter();
+ ContainerResponse cres = new ContainerResponse(responseWriter);
+ handler.handleRequest(creq, cres);
+
+ assertEquals(200, cres.getStatus());
+
+ RepositoryServiceConf repositoryConf =
+ (RepositoryServiceConf) getObject(RepositoryServiceConf.class,
responseWriter.getBody());
+
+ assertNotNull(repositoryConf);
+ assertEquals(repositoryService.getConfig().getDefaultRepositoryName(),
repositoryConf.getDefaultRepositoryName());
+ assertEquals(repositoryService.getConfig().getRepositoryConfigurations().size(),
repositoryConf.getRepositories()
+ .size());
+ }
+
+ public void testCreateRepository() throws Exception
+ {
+ String wsName = "ws_over_rest";
+ String rName = "repo_over_rest";
+
+ RepositoryEntry rDefault =
+ repositoryService.getConfig().getRepositoryConfiguration(
+ repositoryService.getConfig().getDefaultRepositoryName());
+ RepositoryEntry rEntry = new RepositoryEntry();
+
+ rEntry.setAccessControl(rDefault.getAccessControl());
+ rEntry.setAuthenticationPolicy(rDefault.getAuthenticationPolicy());
+ rEntry.setDefaultWorkspaceName("ws_over_rest");
+ rEntry.setName(rName);
+ rEntry.setSecurityDomain(rDefault.getSecurityDomain());
+ rEntry.setSessionTimeOut(rDefault.getSessionTimeOut());
+ rEntry.setSystemWorkspaceName(wsName);
+
+ WorkspaceEntry wEntry =
+ makeWorkspaceEntry(rDefault.getWorkspaceEntries().get(0), rName, wsName,
"jdbcjcr_to_rest_repo");
+ rEntry.addWorkspace(wEntry);
+
+ JsonGeneratorImpl generatorImpl = new JsonGeneratorImpl();
+ JsonValue json = generatorImpl.createJsonObject(rEntry);
+
+ MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
+ headers.putSingle("Content-Type", "application/json;
charset=UTF-8");
+
+ ContainerRequestUserRole creq =
+ new ContainerRequestUserRole("POST", new
URI(REST_REPOSITORY_SERVICE_PATH
+ + RestRepositoryService.Constants.OperationType.NEW_REPOSITORY),
new URI(""),
+ new
ByteArrayInputStream(json.toString().getBytes("UTF-8")), new
InputHeadersMap(headers));
+
+ ByteArrayContainerResponseWriter responseWriter = new
ByteArrayContainerResponseWriter();
+ ContainerResponse cres = new ContainerResponse(responseWriter);
+ handler.handleRequest(creq, cres);
+
+ assertEquals(201, cres.getStatus());
+
+ URI location = (URI) getObject(URI.class, responseWriter.getBody());
+
+ assertNotNull(location);
+ // assertEquals(repositoryService.getConfig().getDefaultRepositoryName(),
repositoryConf.getDefaultRepositoryName());
+ //
assertEquals(repositoryService.getConfig().getRepositoryConfigurations().size(),
repositoryConf.getRepositories().size());
+ }
+
+ /**
+ * Will be created the Object from JSON binary data.
+ *
+ * @param cl
+ * Class
+ * @param data
+ * binary data (JSON)
+ * @return Object
+ * @throws Exception
+ * will be generated Exception
+ */
+ private Object getObject(Class cl, byte[] data) throws Exception
+ {
+ JsonHandler jsonHandler = new JsonDefaultHandler();
+ JsonParser jsonParser = new JsonParserImpl();
+ InputStream inputStream = new ByteArrayInputStream(data);
+ jsonParser.parse(inputStream, jsonHandler);
+ JsonValue jsonValue = jsonHandler.getJsonObject();
+
+ return new BeanBuilder().createObject(cl, jsonValue);
+ }
+
+ protected WorkspaceEntry makeWorkspaceEntry(WorkspaceEntry defWEntry, String repoNmae,
String wsName,
+ String sourceName)
+ {
+ WorkspaceEntry ws1back = new WorkspaceEntry();
+ ws1back.setName(wsName);
+ ws1back.setUniqueName(repoNmae + "_" + wsName);
+
+ ws1back.setAccessManager(defWEntry.getAccessManager());
+ ws1back.setAutoInitializedRootNt(defWEntry.getAutoInitializedRootNt());
+ ws1back.setAutoInitPermissions(defWEntry.getAutoInitPermissions());
+ ws1back.setCache(defWEntry.getCache());
+ ws1back.setLockManager(defWEntry.getLockManager());
+
+ // Indexer
+ ArrayList qParams = new ArrayList();
+ qParams.add(new SimpleParameterEntry("indexDir", "target" +
File.separator + wsName));
+ QueryHandlerEntry qEntry = new
QueryHandlerEntry(defWEntry.getQueryHandler().getType(), qParams);
+
+ ws1back.setQueryHandler(qEntry);
+
+ ArrayList params = new ArrayList();
+ for (Iterator i = defWEntry.getContainer().getParameters().iterator();
i.hasNext();)
+ {
+ SimpleParameterEntry p = (SimpleParameterEntry) i.next();
+ SimpleParameterEntry newp = new SimpleParameterEntry(p.getName(),
p.getValue());
+
+ if (newp.getName().equals("source-name"))
+ newp.setValue(sourceName);
+ else if (newp.getName().equals("swap-directory"))
+ newp.setValue("target/temp/swap/" + wsName);
+ else if (newp.getName().equals("multi-db"))
+ newp.setValue("false");
+
+ params.add(newp);
+ }
+
+ ContainerEntry ce = new ContainerEntry(defWEntry.getContainer().getType(),
params);
+ ws1back.setContainer(ce);
+
+ return ws1back;
+ }
+}
Property changes on:
jcr/trunk/component/ext/src/test/java/org/exoplatform/services/jcr/ext/repository/RestRepositoryServiceTest.java
___________________________________________________________________
Name: svn:eol-style
+ native