Author: deepal
Date: Sun Jan 13 21:15:36 2008
New Revision: 12178
Log:
first part of the resource rename.
- In the case of APP rename work as a post to the current path with $rename
- Adding a test case to test secure registry
Added:
trunk/registry/modules/core/src/test/java/org/wso2/registry/secure/JDBCBasedSecureRegistryTest.java
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/AtomRegistry.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
trunk/registry/modules/core/src/main/resources/org/wso2/registry/i18n/resource.properties
trunk/registry/modules/core/src/test/java/org/wso2/registry/app/JettyBasedServerTest.java
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java
(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java
Sun Jan 13 21:15:36 2008
@@ -76,6 +76,18 @@
*/
void delete(String path) throws RegistryException;
+ /**
+ * Move or rename a resources in the registry. Rename can be considered as
+ * - First delete the resource
+ * - Then add the resource to the new location
+ *
+ * @param currentPath : Current path of the resource
+ * @param newPath where to move the reosurce
+ * @return generated path for the resource
+ * @throws RegistryException : If something went wrong
+ */
+ String rename(String currentPath , String newPath) throws
RegistryException;
+
/**
* Returns the paths of the versions of the resource located at the given
path. Version paths
* are returned in the form /projects/myresource?v=12
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
Sun Jan 13 21:15:36 2008
@@ -123,6 +123,7 @@
//APP Constants
public static final String PARAMETER_VERSION = "version";
public static final String PARAMETER_RESTORE = "restore";
+ public static final String PARAMETER_RENAME = "rename";
public static final String PARAMETER_TAGS = "tags";
public static final String PARAMETER_TAG_PATHS = "tagpaths";
public static final String PARAMETER_TAGS_REMOVED = "removeTags";
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/AtomRegistry.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/AtomRegistry.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/AtomRegistry.java
Sun Jan 13 21:15:36 2008
@@ -106,6 +106,9 @@
} else if
(RegistryConstants.PARAMETER_RATINGS.equals(operation)) {
getSecureRegistry(request).rateResource(resourcePath,
Integer.parseInt(entryContent));
+ } else if
(RegistryConstants.PARAMETER_RENAME.equals(operation)) {
+ getSecureRegistry(request).rename(resourcePath,
+ entryContent);
} else {
//TODO : We need to implement the other operation
as well
return new EmptyResponseContext(500);
@@ -114,7 +117,12 @@
} else {
// Need to add a resource
Entry entry = entry_doc.getRoot();
- Resource resource = new Resource();
+ Resource resource ;
+ if (jdbcregistry.resourceExists(entry_id)){
+ resource = jdbcregistry.get(entry_id);
+ } else {
+ resource = new Resource();
+ }
String attValue = entry.getSimpleExtension(new
QName("directory"));
List attributes = entry.getAttributes();
for (Object attribute : attributes) {
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
Sun Jan 13 21:15:36 2008
@@ -225,6 +225,24 @@
}
}
+
+ public String rename(String currentPath, String newPath) throws
RegistryException {
+ Abdera abdera = new Abdera();
+ AbderaClient abderaClient = new AbderaClient(abdera);
+ Entry entry = abdera.getFactory().newEntry();
+ entry.setContent(newPath);
+ ClientResponse resp = abderaClient.post(baseURI + currentPath +
+ URL_SEPARATOR +
+ PARAMETER_RENAME, entry, getAuthorization());
+ if (resp.getType() == Response.ResponseType.SUCCESS) {
+ log.info(Messages.getMessage("resource.rename", currentPath));
+ } else {
+ log.error(Messages.getMessage("rename.resource.fail",
currentPath));
+ throw new
RegistryException(Messages.getMessage("rename.resource.fail", currentPath));
+ }
+ return newPath;
+ }
+
public String[] getVersions(String path) throws RegistryException {
Abdera abdera = new Abdera();
AbderaClient abderaClient = new AbderaClient(abdera);
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.java
Sun Jan 13 21:15:36 2008
@@ -189,6 +189,10 @@
}
+ public String rename(String currentPath, String newPath) throws
RegistryException {
+ throw new UnsupportedOperationException();
+ }
+
public String[] getVersions(String path) throws RegistryException {
throw new
UnsupportedOperationException(Messages.getMessage("unsupported.exception",
"Versioning"));
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
Sun Jan 13 21:15:36 2008
@@ -310,6 +310,20 @@
return suggestedPath;
}
+
+ public String rename(String currentPath, String newPath) throws
RegistryException {
+ if (resourceExists(currentPath)) {
+ resourceDAO.renameResource(currentPath,
+ newPath,
+ getConnection(),
+ User.getCurrentUser(),
+ defaultRealm);
+ return newPath;
+ } else {
+ throw new ResourceNotFoundException(currentPath);
+ }
+ }
+
/**
* Deletes the resource in the given path. If the path refers to a
directory, all child
* resources of the directory will also be deleted.
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
Sun Jan 13 21:15:36 2008
@@ -20,7 +20,10 @@
import org.apache.commons.logging.LogFactory;
import org.wso2.registry.RegistryConstants;
import org.wso2.registry.Resource;
+import org.wso2.registry.RegistryException;
+import org.wso2.registry.utils.AuthorizationUtil;
import org.wso2.registry.jdbc.DatabaseConstants;
+import org.wso2.usermanager.Realm;
import java.sql.*;
import java.util.ArrayList;
@@ -454,8 +457,8 @@
// first add the resource data into the artifacts table
String sql =
- "INSERT INTO ARTIFACTS (PATH, MEDIA_TYPE, DIRECTORY, STATE,
CREATED_TIME, AUTHOR, DESCRIPTION)" +
- "VALUES (?,?,?,?,?,?,?)";
+ "INSERT INTO ARTIFACTS (PATH, MEDIA_TYPE, DIRECTORY, STATE,
CREATED_TIME, AUTHOR, " +
+ "DESCRIPTION) VALUES (?,?,?,?,?,?,?)";
long now = System.currentTimeMillis();
@@ -513,6 +516,17 @@
s.executeUpdate();
}
+ public void markActivated(long id, Connection conn) throws SQLException {
+
+ String sql = "UPDATE ARTIFACTS SET STATE=? WHERE AID=?";
+
+ PreparedStatement s = conn.prepareStatement(sql);
+ s.setInt(1, RegistryConstants.ACTIVE_STATE);
+ s.setLong(2, id);
+ s.executeUpdate();
+ }
+
+
public void addProperties(long resourceID, Properties props, Connection
conn)
throws SQLException {
@@ -589,6 +603,96 @@
return null;
}
+ /**
+ * This method will move a resource from oldpath to newpath , the internal
implementation is such
+ * that frit the release will be deleted from oldPath parent and will be
added to the newptah
+ * so if the oldpath is "foo/bar/r1" then the r1 will be removed from
foo/bar. However later
+ * we can restore and get everything working fine
+ * @param oldPath : Current path of the resource
+ * @param newPath : Where to move the resource
+ * @param conn : Connection to DB
+ * @throws org.wso2.registry.RegistryException : If something went wrong
+ * @param userId : current user
+ * @param realm : Realm
+ */
+ public void renameResource(String oldPath,
+ String newPath,
+ Connection conn ,
+ String userId,
+ Realm realm) throws RegistryException {
+ try {
+ Resource resource = getLatestVersion(oldPath, conn);
+ String resourcePath = resource.getPath();
+ long resourceID = resource.getId();
+ //to see whether the patent node is there in the table , if not
need to add that
+ createParentCollections(newPath, conn, userId, realm);
+ // if the resource is a directory then need to populate the
dependecy table with
+ // the new resource id , while keeping the old one as it is
+ if (resource.isDirectory()) {
+ long resourceId = resource.getId();
+ long resourceVersionNumber =
getLatestVerisonNumber(resourceId, conn);
+ String selectSql = "SELECT DAID, DVN FROM DEPENDENCY WHERE
AID=? AND VN=?";
+ PreparedStatement s = conn.prepareStatement(selectSql);
+ s.setLong(1, resourceId);
+ s.setLong(2, resourceVersionNumber);
+ ResultSet result = s.executeQuery();
+ // adding the resource to artifcat table and update the
version numbers
+ resource.setPath(newPath);
+ if (resource.getAuthorUserName() == null) {
+ resource.setAuthorUserName(userId);
+ }
+ resource.setLastUpdaterUserName(userId);
+ add(resource, conn);
+ AuthorizationUtil.setDefaultAuthorizations(realm, newPath,
userId);
+ long newResourceID = getResourceID(newPath, conn);
+ resource.setId(newResourceID);
+ addResourceVersion(resource, conn);
+
+ // updating the version table
+ long newresourceVersion =
getLatestVerisonNumber(newResourceID, conn);
+ ArrayList dependeyList = new ArrayList();
+ while (result.next()) {
+ long daid = result.getLong("DAID");
+ long dvn = result.getLong("DVN");
+ PreparedStatement ps = getStatementForDependencyTable(conn,
+ newResourceID,
+ newresourceVersion,
+ daid, dvn);
+ dependeyList.add(ps);
+ }
+ for (int i = 0; i < dependeyList.size(); i++) {
+ PreparedStatement preparedStatement =
(PreparedStatement)dependeyList.get(i);
+ preparedStatement.executeUpdate();
+ }
+ } else {
+ // Then its just need to add the resource and no need to
update any of the
+ // dependecy tables
+ resource.setPath(newPath);
+ if (resource.getAuthorUserName() == null) {
+ resource.setAuthorUserName(userId);
+ }
+ resource.setLastUpdaterUserName(userId);
+ add(resource, conn);
+ AuthorizationUtil.setDefaultAuthorizations(realm, newPath,
userId);
+ long newResourceID = getResourceID(newPath, conn);
+ resource.setId(newResourceID);
+ addResourceVersion(resource, conn);
+ }
+ resource.setPath(resourcePath);
+ resource.setId(resourceID);
+ if (resource.isDirectory()) {
+ deleteDirectory(resource, conn);
+ } else {
+ markDeleted(resourcePath, conn);
+ deleteResource(resource, conn);
+ //prepareToDelete(resource.getId(), conn);
+ //resourceDAO.delete(resource.getPath(), conn);
+ }
+ } catch (SQLException e) {
+ throw new RegistryException(e.getMessage());
+ }
+ }
+
public void addResourceVersion(Resource resource, Connection connection)
throws SQLException {
if (resource.getLastModified() == null) {
@@ -811,7 +915,6 @@
String[] parts = oldArtifact.getPath().split("\\?v=");
plainPath = parts[0];
}
-
update(plainPath, oldArtifact, connection);
addResourceVersion(oldArtifact, connection);
@@ -827,6 +930,7 @@
while (childResult.next()) {
long daid = childResult.getLong("DAID");
+ markActivated(daid,connection);
long dvn = childResult.getLong("DVN");
long versionNumber = getLatestVerisonNumber(resourceId,
connection);
PreparedStatement ps =
getStatementForDependencyTable(connection,
@@ -843,60 +947,6 @@
}
/**
- * To restoreVersion or rollback artifact to a given version number
- *
- * @param resourceId : Resource ID
- * @param connection : Connection to DB
- * @param toVersion : to which version we need to restoreVersion
- * @throws SQLException : If something went wrong
- */
- public void restore2(long resourceId, long toVersion, Connection
connection)
- throws SQLException {
-
- String SQL = "SELECT CONTENT, AUTHOR, UPDATED_TIME FROM VERSIONS WHERE
AID=? AND VN=?";
- PreparedStatement statement = connection.prepareStatement(SQL);
- statement.setLong(1, resourceId);
- statement.setLong(2, toVersion);
-
- ResultSet result = statement.executeQuery();
- long versionNumber = 0;
- if (result.next()) {
- Resource artifactToRestore = new Resource();
- artifactToRestore.setContent(result.getObject(1));
- artifactToRestore.setAuthorUserName(result.getString(2));
- artifactToRestore.setLastModified(result.getDate(3));
- versionNumber = insertToVersionTable(artifactToRestore,
connection);
- }
-
- Resource resource = getResourceByID(resourceId, toVersion, connection);
-
- // updating max version table
- updateDependency(resource, versionNumber, false, connection);
- if (resource.isDirectory()) {
- String selectChildSql = "SELECT DAID, DVN FROM DEPENDENCY WHERE
AID=? AND VN=?";
- PreparedStatement selectps =
connection.prepareStatement(selectChildSql);
- selectps.setLong(1, resourceId);
- selectps.setLong(2, toVersion);
- ResultSet childResult = selectps.executeQuery();
- ArrayList dependencyList = new ArrayList();
- while (childResult.next()) {
- long daid = childResult.getLong("DAID");
- long dvn = childResult.getLong("DVN");
- PreparedStatement ps =
getStatementForDependencyTable(connection,
-
resourceId, versionNumber,
- daid,
dvn);
- dependencyList.add(ps);
- }
-
- for (int i = 0; i < dependencyList.size(); i++) {
- PreparedStatement preparedStatement =
(PreparedStatement)dependencyList.get(i);
- preparedStatement.executeUpdate();
- }
- }
-
- }
-
- /**
* To delete a resource , what actually happen here is that update the
version table with the new
* data and resource will not actually removed from the table
*
@@ -944,10 +994,10 @@
ps.setLong(2, nextVersionNumber);
ps.setLong(3, daid);
ps.setLong(4, dvn);
-// System.out.println("Adding dependency: " + parentId + " " +
nextVersionNumber + " " + daid + " " + dvn);
return ps;
}
+
/**
* This method is use to see whether a give resource is active or not ,
one can delete a resource
* and try to access that giving the path of the resource in that case we
should not return the
@@ -1016,4 +1066,55 @@
return parentPath;
}
+
+ /**
+ * This method will first split the given path and then try to create
directory for each part
+ * for example if the path is "/abc/xyx/r1" , then first it will see
whether the resource "/abc"
+ * is there if it is not there then it will create that else go to
"/abc/xyz" , and see whether
+ * the resource is there , if so continue the same procedure
+ * @param path : Location where we need to add the resource
+ * @param conn : DB connection
+ * @param userID : user
+ * @throws SQLException : Something went wrong
+ * @throws RegistryException : Something went wrong
+ * @param realm : Realm
+ */
+ private void createParentCollections(String path,
+ Connection conn,
+ String userID ,
+ Realm realm) throws SQLException,
RegistryException {
+ String[] parts = path.split(RegistryConstants.PATH_SEPARATOR);
+
+ String currentPath = RegistryConstants.ROOT_PATH + parts[1];
+ for (int i = 2; i < parts.length; i++) {
+ Resource currentCollection = getLatestVersion(currentPath, conn);
+ if (currentCollection == null) {
+ currentCollection = new Resource();
+ currentCollection.setPath(currentPath);
+ currentCollection.setDirectory(true);
+ currentCollection.setAuthorUserName(userID);
+ currentCollection.setLastUpdaterUserName(userID);
+ add(currentCollection, conn);
+ AuthorizationUtil.setDefaultAuthorizations(realm, currentPath,
userID);
+ }
+ currentPath = currentPath + RegistryConstants.PATH_SEPARATOR +
parts[i];
+ }
+ }
+
+ private void deleteDirectory(Resource collection, Connection conn) throws
SQLException {
+ List childList = getChildren(collection.getId(), -1, conn);
+ Iterator i = childList.iterator();
+ while (i.hasNext()) {
+ Resource child = (Resource)i.next();
+ if (child.isDirectory()) {
+ deleteDirectory(child, conn);
+ } else {
+ markDeleted(child.getPath(), conn);
+ deleteResource(child, conn);
+ }
+ }
+ markDeleted(collection.getPath(), conn);
+ deleteResource(collection, conn);
+ }
+
}
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java
Sun Jan 13 21:15:36 2008
@@ -55,7 +55,7 @@
versionNumber = Long.parseLong(parts[1]);
}
- Connection conn = null;
+ Connection conn ;
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
@@ -100,7 +100,7 @@
String userID = User.getCurrentUser();
- Connection conn = null;
+ Connection conn ;
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
@@ -134,45 +134,7 @@
// given resource does not exist. add it as a new resource.
// first create all ascendant collections
- String[] parts = path.split(RegistryConstants.PATH_SEPARATOR);
-
- String currentPath = RegistryConstants.ROOT_PATH + parts[1];
- for (int i = 2; i < parts.length; i++) {
-
- Resource currentCollection =
resourceDAO.getLatestVersion(currentPath, conn);
- if (currentCollection == null) {
-
- currentCollection = new Resource();
- currentCollection.setPath(currentPath);
- currentCollection.setDirectory(true);
- currentCollection.setAuthorUserName(userID);
- currentCollection.setLastUpdaterUserName(userID);
-
- resourceDAO.add(currentCollection, conn);
- AuthorizationUtil.setDefaultAuthorizations(realm,
currentPath, userID);
-// currentCollection =
resourceDAO.getLatestVersion(currentPath, conn);
-
- } else {
-
- if (currentCollection.getState() ==
RegistryConstants.DELETED_STATE) {
-
- currentCollection = new Resource();
- currentCollection.setPath(currentPath);
- currentCollection.setDirectory(true);
- currentCollection.setAuthorUserName(userID);
- currentCollection.setLastUpdaterUserName(userID);
- put(currentPath, currentCollection);
-
- } else if (!currentCollection.isDirectory()) {
-
- String message =
-
Messages.getMessage("resource.adding.error", currentPath, path);
- throw new RegistryException(message);
- }
- }
-
- currentPath = currentPath +
RegistryConstants.PATH_SEPARATOR + parts[i];
- }
+ createParentCollections(path, conn, userID);
// now add the required resource
resource.setPath(path);
@@ -186,7 +148,6 @@
resource.setId(resourceDAO.getResourceID(path, conn));
resourceDAO.addResourceVersion(resource, conn);
-
// add new resource as a child of the current parent
//resourceDAO.addChild(currentParent.getId(),
resource.getId(), conn);
}
@@ -214,13 +175,68 @@
return true;
}
+ /**
+ * This method will first split the given path and then try to create
directory for each part
+ * for example if the path is "/abc/xyx/r1" , then first it will see
whether the resource "/abc"
+ * is there if it is not there then it will create that else go to
"/abc/xyz" , and see whether
+ * the resource is there , if so continue the same procedure
+ * @param path : Location where we need to add the resource
+ * @param conn : DB connection
+ * @param userID : user
+ * @throws SQLException : Something went wrong
+ * @throws RegistryException : Something went wrong
+ */
+ private void createParentCollections(String path,
+ Connection conn,
+ String userID) throws SQLException,
RegistryException {
+ String[] parts = path.split(RegistryConstants.PATH_SEPARATOR);
+
+ String currentPath = RegistryConstants.ROOT_PATH + parts[1];
+ for (int i = 2; i < parts.length; i++) {
+
+ Resource currentCollection =
resourceDAO.getLatestVersion(currentPath, conn);
+ if (currentCollection == null) {
+
+ currentCollection = new Resource();
+ currentCollection.setPath(currentPath);
+ currentCollection.setDirectory(true);
+ currentCollection.setAuthorUserName(userID);
+ currentCollection.setLastUpdaterUserName(userID);
+
+ resourceDAO.add(currentCollection, conn);
+ AuthorizationUtil.setDefaultAuthorizations(realm, currentPath,
userID);
+// currentCollection =
resourceDAO.getLatestVersion(currentPath, conn);
+
+ } else {
+
+ if (currentCollection.getState() ==
RegistryConstants.DELETED_STATE) {
+
+ currentCollection = new Resource();
+ currentCollection.setPath(currentPath);
+ currentCollection.setDirectory(true);
+ currentCollection.setAuthorUserName(userID);
+ currentCollection.setLastUpdaterUserName(userID);
+ put(currentPath, currentCollection);
+
+ } else if (!currentCollection.isDirectory()) {
+
+ String message =
+ Messages.getMessage("resource.adding.error",
currentPath, path);
+ throw new RegistryException(message);
+ }
+ }
+
+ currentPath = currentPath + RegistryConstants.PATH_SEPARATOR +
parts[i];
+ }
+ }
+
public boolean importResource(String path, String sourceURL, String
mediaType) throws RegistryException {
return false;
}
public boolean delete(String path) throws RegistryException {
- Connection conn = null;
+ Connection conn ;
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
Sun Jan 13 21:15:36 2008
@@ -22,6 +22,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.registry.*;
+import org.wso2.registry.exceptions.ResourceNotFoundException;
import org.wso2.usermanager.Authenticator;
import org.wso2.usermanager.Authorizer;
import org.wso2.usermanager.Realm;
@@ -187,43 +188,7 @@
public String put(String suggestedPath, Resource resource) throws
RegistryException {
// find the existing ascendent path of the given path
- String pathToSplit = suggestedPath;
- if (pathToSplit.startsWith(RegistryConstants.PATH_SEPARATOR)) {
- pathToSplit =
pathToSplit.substring(RegistryConstants.PATH_SEPARATOR.length());
- }
-
- if (pathToSplit.endsWith(RegistryConstants.PATH_SEPARATOR)) {
- pathToSplit = pathToSplit.substring(
- 0, pathToSplit.length() -
RegistryConstants.PATH_SEPARATOR.length());
- }
-
- String[] parts = pathToSplit.split(RegistryConstants.PATH_SEPARATOR);
- int partCount = parts.length;
- String existingAscendant;
- do {
- existingAscendant = "";
- if (partCount == 0) {
- existingAscendant = RegistryConstants.ROOT_PATH;
- }
-
- for (int i = 0; i < partCount; i++) {
- existingAscendant = existingAscendant +
RegistryConstants.PATH_SEPARATOR + parts[i];
- }
- partCount--;
- } while (!registry.resourceExists(existingAscendant));
-
- // check if the user has put permission for that path
- try {
- if (!authorizer.isUserAuthorized(userID, existingAscendant,
ActionConstants.PUT)) {
- String msg = "Attempted to perform unauthorized operation.";
- log.info(msg);
- throw new AuthorizationFailedException(msg);
- }
- } catch (UserManagerException e) {
- String msg = "Could not check authorization. \nCaused by " +
e.getMessage();
- log.error(msg, e);
- throw new RegistryException(msg);
- }
+ isUuserAllowToAddResource(suggestedPath);
User.setCurrentUser(userID);
@@ -246,9 +211,7 @@
return suggestedPath;
}
- public String importResource(String suggestedPath, String sourceURL,
String mediaType) throws RegistryException {
-
- // find the existing ascendent path of the given path
+ private void isUuserAllowToAddResource(String suggestedPath) throws
RegistryException {
String pathToSplit = suggestedPath;
if (pathToSplit.startsWith(RegistryConstants.PATH_SEPARATOR)) {
pathToSplit =
pathToSplit.substring(RegistryConstants.PATH_SEPARATOR.length());
@@ -286,6 +249,12 @@
log.error(msg, e);
throw new RegistryException(msg);
}
+ }
+
+ public String importResource(String suggestedPath, String sourceURL,
String mediaType) throws RegistryException {
+
+ // find the existing ascendent path of the given path
+ isUuserAllowToAddResource(suggestedPath);
User.setCurrentUser(userID);
@@ -313,6 +282,27 @@
}
+ public String rename(String currentPath, String newPath) throws
RegistryException {
+ if (resourceExists(currentPath)) {
+ try {
+ if (!authorizer.isUserAuthorized(userID, currentPath,
ActionConstants.DELETE)) {
+ String msg = "Attempted to perform unauthorized
operation.";
+ log.info(msg);
+ throw new AuthorizationFailedException(msg);
+ }
+ } catch (UserManagerException e) {
+ String msg = "Could not check authorization. \nCaused by " +
e.getMessage();
+ log.error(msg, e);
+ throw new RegistryException(msg);
+ }
+ isUuserAllowToAddResource(newPath);
+ User.setCurrentUser(userID);
+ return registry.rename(currentPath, newPath);
+ } else {
+ throw new ResourceNotFoundException(currentPath);
+ }
+ }
+
public String[] getVersions(String resourcePath) throws RegistryException {
// if the user has permission to the current version, he will get
permission to all
Modified:
trunk/registry/modules/core/src/main/resources/org/wso2/registry/i18n/resource.properties
==============================================================================
---
trunk/registry/modules/core/src/main/resources/org/wso2/registry/i18n/resource.properties
(original)
+++
trunk/registry/modules/core/src/main/resources/org/wso2/registry/i18n/resource.properties
Sun Jan 13 21:15:36 2008
@@ -79,6 +79,7 @@
resource.not.found=Resource Not found.
resource.not.found.warn=Resource Not found ,please check the resource path {0}.
resource.add=Resource added {0}.
+resource.rename=Resource added {0}.
resource.deleted=Resource deleted {0}.
resource.restoreded=Resource restored {0}.
resource.tagged=Resource tagged {0}.
@@ -86,6 +87,7 @@
resource.commented=Resource commented {0}
resource.rated=Resource rated {0}.
add.resource.fail=Failed to add the resource to the path : {0}.
+rename.resource.fail=Failed to add the resource to the path : {0}.
resource.tag.fails=Failed to tag the resource to the path : {0}.
tag.removed.fails=Failed to remove the tag : {0}
resource.comment.fails=Failed to comment the resource to the path : {0}
Modified:
trunk/registry/modules/core/src/test/java/org/wso2/registry/app/JettyBasedServerTest.java
==============================================================================
---
trunk/registry/modules/core/src/test/java/org/wso2/registry/app/JettyBasedServerTest.java
(original)
+++
trunk/registry/modules/core/src/test/java/org/wso2/registry/app/JettyBasedServerTest.java
Sun Jan 13 21:15:36 2008
@@ -36,4 +36,9 @@
fail("Failed to initialize the registry.");
}
}
+
+
+ public void testResourceRename() throws Exception {
+ super.testResourceRename();
+ }
}
Modified:
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
==============================================================================
---
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
(original)
+++
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
Sun Jan 13 21:15:36 2008
@@ -69,7 +69,7 @@
}
assertEquals("Content is not equal.",
- new String((byte[])r1.getContent()), new
String((byte[])r1f.getContent()));
+ new String((byte[]) r1.getContent()), new String((byte[])
r1f.getContent()));
assertEquals("Author is not equal.", r1.getAuthorUserName(),
r1f.getAuthorUserName());
@@ -119,7 +119,7 @@
assertTrue("Content of /d1 should be a String[]", d1.getContent()
instanceof String[]);
- String[] children = (String[])d1.getContent();
+ String[] children = (String[]) d1.getContent();
boolean found = false;
for (String aChildren : children) {
if (aChildren.startsWith("/d1/r1")) {
@@ -137,7 +137,7 @@
}
assertEquals("Resource content is not stored correctly.",
- new String((byte[])r1.getContent()), new
String((byte[])r1f.getContent()));
+ new String((byte[]) r1.getContent()), new String((byte[])
r1f.getContent()));
try {
registry.delete("/d1");
@@ -227,9 +227,9 @@
fail("Failed to get versioned resource.");
}
- String content1 = new String((byte[])v1.getContent());
- String content2 = new String((byte[])v2.getContent());
- String content3 = new String((byte[])v3.getContent());
+ String content1 = new String((byte[]) v1.getContent());
+ String content2 = new String((byte[]) v2.getContent());
+ String content3 = new String((byte[]) v3.getContent());
assertEquals("Content is not versioned properly.", content1, "R1
content");
assertEquals("Content is not versioned properly.", content2, "New
content");
@@ -248,7 +248,7 @@
fail("Valid get failed.");
}
- String restoredContent = new String((byte[])r5restored.getContent());
+ String restoredContent = new String((byte[]) r5restored.getContent());
assertEquals("Content is not restored properly.", "R1 content",
restoredContent);
}
@@ -344,15 +344,15 @@
fail("Valid get failed.");
}
- String[] chidrenOfv1 = (String[])c10v1.getContent();
+ String[] chidrenOfv1 = (String[]) c10v1.getContent();
assertTrue("collection content is not versioned properly.",
- containsString(chidrenOfv1, "/c10/r1"));
+ containsString(chidrenOfv1, "/c10/r1"));
- String[] chidrenOfv2 = (String[])c10v2.getContent();
+ String[] chidrenOfv2 = (String[]) c10v2.getContent();
assertTrue("collection content is not versioned properly.",
- containsString(chidrenOfv2, "/c10/r1"));
+ containsString(chidrenOfv2, "/c10/r1"));
assertTrue("collection content is not versioned properly.",
- containsString(chidrenOfv2, "/c10/r2"));
+ containsString(chidrenOfv2, "/c10/r2"));
try {
registry.restoreVersion(versionPaths[0]);
@@ -367,14 +367,72 @@
fail("Valid get failed.");
}
- String[] restoredC10Children = (String[])restoredC10.getContent();
+ String[] restoredC10Children = (String[]) restoredC10.getContent();
assertTrue("Collection children are not restored properly.",
- containsString(restoredC10Children, "/c10/r1"));
+ containsString(restoredC10Children, "/c10/r1"));
assertTrue("Collection children are not restored properly.",
- !containsString(restoredC10Children, "/c10/r2"));
+ !containsString(restoredC10Children, "/c10/r2"));
}
+ public void testValueChange() throws Exception {
+ Resource r1 = new Resource();
+ String conent1 = "Content1";
+ r1.setContent(conent1.getBytes());
+ registry.put("/abc/foo", r1);
+ String conent2 = "Content2";
+ r1.setContent(conent2.getBytes());
+ registry.put("/abc/foo", r1);
+ r1 = registry.get("/abc/foo");
+ Object resourceContent = r1.getContent();
+
+ boolean value = Arrays.equals(conent2.getBytes(), (byte[])
resourceContent);
+ assertTrue(value);
+
+ }
+
+ public void testCollectionRename() throws Exception{
+// Resource r1 = new Resource();
+// registry.put("/abc/dec/xyc1", r1);
+// registry.put("/abc/dec/xyc2", r1);
+// registry.rename("/abc/dec" ,"/dec/abc");
+// Resource r2 = registry.get("/dec/abc");
+// System.out.println(r2.getContent());
+ }
+
+ public void testResourceRename() throws Exception {
+ Resource r1 = new Resource();
+ String conent1 = "Content1";
+ r1.setContent(conent1.getBytes());
+ registry.put("/product/wso2/wsas", r1);
+ String[] version = registry.getVersions("/product/wso2");
+ String versionString = version[version.length - 1];
+ registry.rename("/product/wso2/wsas", "/product/private/esb");
+ r1 = registry.get("/product/private/esb");
+ boolean value = Arrays.equals(conent1.getBytes(), (byte[])
r1.getContent());
+ assertTrue(value);
+ String conent2 = "Content2";
+ r1.setContent(conent2.getBytes());
+ registry.put("/product/private/esb", r1);
+ r1 = registry.get("/product/private/esb");
+ value = Arrays.equals(conent2.getBytes(), (byte[]) r1.getContent());
+ assertTrue(value);
+ try {
+ registry.get("/product/wso2/wsas");
+ fail("This has to fail with the exception , saying resource not
found");
+ } catch (RegistryException e) {
+ // everything has gone fine
+ }
+ registry.restoreVersion(versionString);
+ r1 = registry.get("/product/wso2/wsas");
+ value = Arrays.equals(conent1.getBytes(), (byte[]) r1.getContent());
+ assertTrue(value);
+ r1 = registry.get("/product/private/esb");
+ value = Arrays.equals(conent2.getBytes(), (byte[]) r1.getContent());
+ assertTrue(value);
+
+ }
+
public void testComments() {
// add a resource
@@ -413,13 +471,13 @@
}
}
assertTrue("comment '" + comment1 +
- "' is not associated with the artifact /d12/r1",
commentFound);
+ "' is not associated with the artifact /d12/r1", commentFound);
try {
Resource commentsResource = registry.get("/d12/r1;comments");
assertTrue("Comment collection resource should be a directory.",
- commentsResource.isDirectory());
- String[] commentPaths = (String[])commentsResource.getContent();
+ commentsResource.isDirectory());
+ String[] commentPaths = (String[]) commentsResource.getContent();
List commentTexts = new ArrayList();
for (String commentPath : commentPaths) {
@@ -428,9 +486,9 @@
}
assertTrue(comment1 + " is not associated for resource /d12/r1.",
- commentTexts.contains(comment1));
+ commentTexts.contains(comment1));
assertTrue(comment2 + " is not associated for resource /d12/r1.",
- commentTexts.contains(comment2));
+ commentTexts.contains(comment2));
} catch (RegistryException e) {
e.printStackTrace();
@@ -471,8 +529,8 @@
fail("Couldn't get the rating of the resource /d13/r1");
}
- assertEquals("Rating of the resource /d13/r1 should be 4.", rating,
(float)4.0,
- (float)0.01);
+ assertEquals("Rating of the resource /d13/r1 should be 4.", rating,
(float) 4.0,
+ (float) 0.01);
try {
registry.delete("/d13");
@@ -510,7 +568,7 @@
q1.setContent(sql1);
q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE);
q1.setProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME,
- RegistryConstants.RESOURCES_RESULT_TYPE);
+ RegistryConstants.RESOURCES_RESULT_TYPE);
registry.put("/qs/q1", q1);
} catch (RegistryException e) {
@@ -521,10 +579,10 @@
Resource result1 = registry.executeQuery("/qs/q1", new
String[]{"%production%"});
assertTrue("Search with result type Resource should return a
directory.",
- result1.isDirectory());
+ result1.isDirectory());
List matchingPaths = new ArrayList();
- String[] paths = (String[])result1.getContent();
+ String[] paths = (String[]) result1.getContent();
matchingPaths.addAll(Arrays.asList(paths));
assertTrue("Path /c1/r1 should be in the results.",
matchingPaths.contains("/c1/r1"));
@@ -569,7 +627,7 @@
q1.setContent(sql1);
q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE);
q1.setProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME,
- RegistryConstants.RATINGS_RESULT_TYPE);
+ RegistryConstants.RATINGS_RESULT_TYPE);
registry.put("/qs/q2", q1);
} catch (RegistryException e) {
@@ -580,18 +638,18 @@
Resource result1 = registry.executeQuery("/qs/q2", new
String[]{"%production%"});
assertTrue("Search with result type Resource should return a
directory.",
- result1.isDirectory());
+ result1.isDirectory());
- String[] ratingPaths = (String[])result1.getContent();
+ String[] ratingPaths = (String[]) result1.getContent();
assertEquals("There should be two match ratings.",
ratingPaths.length, 2);
Resource rating1 = registry.get(ratingPaths[0]);
assertEquals("First matching rating should be 3",
- rating1.getContent().toString(), "3");
+ rating1.getContent().toString(), "3");
Resource rating2 = registry.get(ratingPaths[1]);
assertEquals("First matching rating should be 3",
- rating2.getContent().toString(), "4");
+ rating2.getContent().toString(), "4");
} catch (RegistryException e) {
fail("Failed to execute query.");
@@ -632,7 +690,7 @@
q1.setContent(sql1);
q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE);
q1.setProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME,
- RegistryConstants.TAGS_RESULT_TYPE);
+ RegistryConstants.TAGS_RESULT_TYPE);
registry.put("/qs/q3", q1);
} catch (RegistryException e) {
@@ -643,18 +701,18 @@
Resource result1 = registry.executeQuery("/qs/q3", new
String[]{"%production%"});
assertTrue("Search with result type tags should return a
directory.",
- result1.isDirectory());
+ result1.isDirectory());
- String[] tagPaths = (String[])result1.getContent();
+ String[] tagPaths = (String[]) result1.getContent();
assertEquals("There should be two matching tags.",
tagPaths.length, 2);
Resource tag1 = registry.get(tagPaths[0]);
assertEquals("First matching tag should be 'java'",
- (String)tag1.getContent(), "java");
+ (String) tag1.getContent(), "java");
Resource tag2 = registry.get(tagPaths[1]);
assertEquals("First matching tag should be 'jsp'",
- (String)tag2.getContent(), "jsp");
+ (String) tag2.getContent(), "jsp");
} catch (RegistryException e) {
fail("Failed to execute query.");
@@ -695,7 +753,7 @@
q1.setContent(sql1);
q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE);
q1.setProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME,
- RegistryConstants.COMMENTS_RESULT_TYPE);
+ RegistryConstants.COMMENTS_RESULT_TYPE);
registry.put("/qs/q4", q1);
} catch (RegistryException e) {
@@ -706,18 +764,18 @@
Resource result1 = registry.executeQuery("/qs/q4", new
String[]{"%production%"});
assertTrue("Search with result type Comment should return a
collection.",
- result1.isDirectory());
+ result1.isDirectory());
- String[] commentPaths = (String[])result1.getContent();
+ String[] commentPaths = (String[]) result1.getContent();
assertEquals("There should be two match comments.",
commentPaths.length, 2);
Resource c1 = registry.get(commentPaths[0]);
assertEquals("First matching comment is incorrect",
- (String)c1.getContent(), "we have to change this
file.");
+ (String) c1.getContent(), "we have to change this file.");
Resource c2 = registry.get(commentPaths[1]);
assertEquals("Second matching comment is incorrect",
- (String)c2.getContent(), "replace this with a better
one");
+ (String) c2.getContent(), "replace this with a better
one");
} catch (RegistryException e) {
fail("Failed to execute query.");
@@ -805,13 +863,13 @@
fail("Failed to get comments using URL.");
}
- String[] commentPaths = (String[])comments.getContent();
+ String[] commentPaths = (String[]) comments.getContent();
List commentStrings = new ArrayList();
for (String commentPath : commentPaths) {
try {
Resource c1 = registry.get(commentPath);
- String commentString = (String)c1.getContent();
+ String commentString = (String) c1.getContent();
commentStrings.add(commentString);
} catch (RegistryException e) {
fail("Failed to get comment using a path.");
@@ -819,13 +877,13 @@
}
assertTrue("Comments are not retrieved properly as resources.",
- commentStrings.contains("this is used to test comments."));
+ commentStrings.contains("this is used to test comments."));
assertTrue("Comments are not retrieved properly as resources.",
- commentStrings.contains("dummy resource."));
+ commentStrings.contains("dummy resource."));
assertTrue("Comments are not retrieved properly as resources.",
- commentStrings.contains("simple test resource."));
+ commentStrings.contains("simple test resource."));
}
public void testRatingsAsResources() {
@@ -854,7 +912,7 @@
fail("Failed to get ratings using URL.");
}
- String[] ratingPaths = (String[])ratings.getContent();
+ String[] ratingPaths = (String[]) ratings.getContent();
int rating = 0;
try {
@@ -862,7 +920,7 @@
Object o = c1.getContent();
if (o instanceof Integer) {
- rating = (Integer)o;
+ rating = (Integer) o;
} else {
rating = Integer.parseInt(o.toString());
}
@@ -914,9 +972,9 @@
Resource r2 = registry.get("/wso2/wsas/v1");
//chcek whether the content is corrcet
- assertEquals("/wso2/wsas/v1/r1", ((String[])r2.getContent())[0]);
+ assertEquals("/wso2/wsas/v1/r1", ((String[]) r2.getContent())[0]);
Resource wsas = registry.get("/wso2/wsas");
- String wasaContent[] = (String[])wsas.getContent();
+ String wasaContent[] = (String[]) wsas.getContent();
assertNotNull(wasaContent);
assertEquals(2, wasaContent.length);
@@ -928,18 +986,18 @@
registry.put("/wso2/wsas/v1/r2", resourceContent2);
wsas = registry.get("/wso2/wsas");
- wasaContent = (String[])wsas.getContent();
+ wasaContent = (String[]) wsas.getContent();
assertNotNull(wasaContent);
assertEquals(2, wasaContent.length);
r2 = registry.get("/wso2/wsas/v1");
//chcek whether the content is corrcet
- assertEquals("/wso2/wsas/v1/r2", ((String[])r2.getContent())[0]);
+ assertEquals("/wso2/wsas/v1/r2", ((String[]) r2.getContent())[0]);
registry.restoreVersion("/wso2/wsas?v=2");
r2 = registry.get("/wso2/wsas/v1");
//chcek whether the content is corrcet
- assertEquals("/wso2/wsas/v1/r1", ((String[])r2.getContent())[0]);
+ assertEquals("/wso2/wsas/v1/r1", ((String[]) r2.getContent())[0]);
}
@@ -1025,7 +1083,7 @@
registry.addComment("/c1", new Comment("I don't agree with having
this collection."));
registry.addComment("/c1/r2", new Comment("Is this a collection
;)"));
registry.addComment("/c2",
- new Comment("I can understand the purpose of
this. Thanks."));
+ new Comment("I can understand the purpose of this.
Thanks."));
} catch (RegistryException e) {
fail("Valid commenting scenario failed.");
}
Added:
trunk/registry/modules/core/src/test/java/org/wso2/registry/secure/JDBCBasedSecureRegistryTest.java
==============================================================================
--- (empty file)
+++
trunk/registry/modules/core/src/test/java/org/wso2/registry/secure/JDBCBasedSecureRegistryTest.java
Sun Jan 13 21:15:36 2008
@@ -0,0 +1,42 @@
+/*
+ * 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.wso2.registry.secure;
+
+import org.wso2.registry.jdbc.JDBCRegistryTest;
+import org.wso2.registry.jdbc.InMemoryJDBCRegistry;
+import org.wso2.registry.jdbc.realm.InMemoryRegistryRealm;
+import org.wso2.registry.jdbc.realm.RegistryRealm;
+import org.wso2.registry.RegistryException;
+import org.wso2.registry.RegistryConstants;
+
+public class JDBCBasedSecureRegistryTest extends JDBCRegistryTest {
+ public void setUp() {
+ try {
+ if (registry == null) {
+ RegistryRealm realm = new InMemoryRegistryRealm();
+ InMemoryJDBCRegistry inmemoryRegsitry = new
InMemoryJDBCRegistry(
+ new InMemoryRegistryRealm());
+ registry = new SecureRegistry(RegistryConstants.ADMIN_USER,
+ "admin", inmemoryRegsitry, realm);
+ }
+ } catch (RegistryException e) {
+ fail("Failed to initialize the registry.");
+ }
+ }
+}
_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev