Author: chathura
Date: Thu Jan 10 03:24:47 2008
New Revision: 12111
Log:
Implementing the importResource(...) method.
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.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/mediatypes/MediaTypeHandler.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/MediaTypeManager.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/Axis2RepositoryMediaTypeHandler.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/jdbc/mediatypes/builtin/SQLQueryMediaTypeHandler.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/SynapseRepositoryMediaTypeHandler.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/WSDLMediaTypeHandler.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/XSDMediaTypeHandler.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/ResourceHandler.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
Thu Jan 10 03:24:47 2008
@@ -40,7 +40,7 @@
* @return true if a resource exist in the given path, false otherwise.
* @throws RegistryException if an error occurs
*/
- public boolean resourceExists(String path) throws RegistryException;
+ boolean resourceExists(String path) throws RegistryException;
/**
* Adds or updates resources in the registry. If there is no resource in
the given path,
@@ -53,6 +53,20 @@
String put(String suggestedPath, Resource resource) throws
RegistryException;
/**
+ * Creates a resource in the suggested path (which may be altered) by
fetching the resource
+ * content from the given URL.
+ *
+ * @param suggestedPath Path to add the new resource. Although this path
is specified by the
+ * caller of the method, resource may not be actually added to this path.
+ *
+ * @param sourceURL URL to fetch the resource content
+ * @param mediaType
+ * @return Actual path to which the resource is added
+ * @throws RegistryException
+ */
+ String importResource(String suggestedPath, String sourceURL, String
mediaType) throws RegistryException;
+
+ /**
* 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/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
Thu Jan 10 03:24:47 2008
@@ -206,6 +206,13 @@
return suggestedPath;
}
+ public String importResource(String suggestedPath, String sourceURL,
String mediaType) throws RegistryException {
+
+ // TODO: Implement resource import logic for remote case
+
+ return null;
+ }
+
public void delete(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
Thu Jan 10 03:24:47 2008
@@ -133,6 +133,11 @@
return suggestedPath;
}
+ public String importResource(String suggestedPath, String sourceURL,
String mediaType) throws RegistryException {
+ throw new UnsupportedOperationException(
+ "Resource imports are not supported in the InMemoryRegistry.");
+ }
+
private void updateArtifact(Resource currentDirectory, String currentPath)
{
if (currentDirectory != null && currentDirectory.isDirectory()) {
String oldPaths[] = (String[])currentDirectory.getContent();
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
Thu Jan 10 03:24:47 2008
@@ -273,6 +273,43 @@
return suggestedPath;
}
+ public String importResource(String suggestedPath, String sourceURL,
String mediaType) throws RegistryException {
+
+ suggestedPath = preparePath(suggestedPath);
+ mediaTypeManager.importResource(suggestedPath, sourceURL, mediaType);
+
+ Connection conn = getConnection();
+
+ try {
+ conn.setAutoCommit(false);
+
+ long resourceID = resourceDAO.getResourceID(suggestedPath, conn);
+ logsDAO.addLog(resourceID, User.getCurrentUser(), LogEntry.UPDATE,
null, conn);
+
+ conn.commit();
+
+ } catch (SQLException e) {
+ try {
+ conn.rollback();
+ } catch (SQLException e1) {
+ e1.printStackTrace();
+ }
+
+ String msg = e.getMessage();
+ log.error(msg, e);
+ throw new RegistryException(msg);
+
+ } finally {
+ try {
+ conn.close();
+ } catch (SQLException ignore) {
+ log.info("Exception while closing the DB connection in put
method of JDBCRegistry");
+ }
+ }
+
+ return suggestedPath;
+ }
+
/**
* 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/mediatypes/MediaTypeHandler.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/MediaTypeHandler.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/MediaTypeHandler.java
Thu Jan 10 03:24:47 2008
@@ -103,6 +103,21 @@
public abstract boolean put(String path, Resource resource) throws
RegistryException;
/**
+ * Creates a resource in the given path by fetching the resource content
from the given URL.
+ *
+ * @param path Path to add the new resource.
+ * @param sourceURL URL to fetch the resource content
+ *
+ * @param mediaType
+ * @return true if the media type handler processes the import action. If
true is returned it
+ * should perform all tasks to fulfill the import request. Returns
false if the media type
+ * handler does not process the import action. If false is
returned, importResource(...)
+ * method of the DefaultMediaTypeHandler will be invoked.
+ * @throws RegistryException
+ */
+ public abstract boolean importResource(String path, String sourceURL,
String mediaType) throws RegistryException;
+
+ /**
* Processes the DELETE action of the media type.
*
* @param path path of the resource to be deleted.
@@ -126,4 +141,15 @@
*/
public abstract boolean putChild(String childPath, Resource resource)
throws RegistryException;
+ /**
+ * Invokes when a child resource is imported. Only the media type handlers
of collection resources
+ * may have a meaningfull implementation of this method.
+ *
+ * @param childPath Path of the child resource
+ * @param sourceURL URL to import child resource content
+ * @return True if the child resource is allowed to be imported. False if
the child resource is
+ * rejected.
+ * @throws RegistryException
+ */
+ public abstract boolean importChild(String childPath, String sourceURL)
throws RegistryException;
}
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/MediaTypeManager.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/MediaTypeManager.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/MediaTypeManager.java
Thu Jan 10 03:24:47 2008
@@ -141,6 +141,44 @@
}
}
+ public void importResource(String path, String sourceURL, String mediaType)
+ throws RegistryException {
+
+ String parentPath = getParentPath(path);
+ if (parentPath != null) {
+ Resource parentCollection =
defaultMediaTypeHandler.get(parentPath, null);
+
+ if (parentCollection != null && parentCollection.getMediaType() !=
null) {
+ MediaTypeHandler parentMediaTypeHandler =
+
(MediaTypeHandler)mediaTypeHandlers.get(parentCollection.getMediaType());
+ if (parentMediaTypeHandler != null) {
+ boolean allowed = parentMediaTypeHandler.importChild(path,
sourceURL);
+ if (!allowed) {
+ String msg = "Attempted to add unrecognized resource
in to the typed " +
+ "collection " +
parentCollection.getMediaType();
+ log.info(msg);
+ throw new RegistryException(msg);
+ }
+ }
+ }
+ }
+
+ if (mediaType != null) {
+ MediaTypeHandler mediaTypeHandler =
+ (MediaTypeHandler)mediaTypeHandlers.get(mediaType);
+
+ if (mediaTypeHandler != null) {
+ if (!mediaTypeHandler.importResource(path, sourceURL,
mediaType)) {
+ defaultMediaTypeHandler.importResource(path, sourceURL,
mediaType);
+ }
+ } else {
+ defaultMediaTypeHandler.importResource(path, sourceURL,
mediaType);
+ }
+ } else {
+ defaultMediaTypeHandler.importResource(path, sourceURL, mediaType);
+ }
+ }
+
public void delete(String path) throws RegistryException {
Resource resource = getRawArtifact(path);
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/Axis2RepositoryMediaTypeHandler.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/Axis2RepositoryMediaTypeHandler.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/Axis2RepositoryMediaTypeHandler.java
Thu Jan 10 03:24:47 2008
@@ -73,6 +73,10 @@
return true;
}
+ public boolean importResource(String path, String sourceURL, String
mediaType) throws RegistryException {
+ return false;
+ }
+
public boolean delete(String path) throws RegistryException {
return false; //To change body of implemented methods use File |
Settings | File Templates.
}
@@ -95,4 +99,8 @@
return false;
}
+
+ public boolean importChild(String childPath, String sourceURL) throws
RegistryException {
+ return true;
+ }
}
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
Thu Jan 10 03:24:47 2008
@@ -214,6 +214,10 @@
return true;
}
+ public boolean importResource(String path, String sourceURL, String
mediaType) throws RegistryException {
+ return false;
+ }
+
public boolean delete(String path) throws RegistryException {
Connection conn = null;
@@ -280,4 +284,8 @@
// General resource should not have any constraint on adding children.
return true;
}
+
+ public boolean importChild(String childPath, String sourceURL) throws
RegistryException {
+ return true;
+ }
}
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/SQLQueryMediaTypeHandler.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/SQLQueryMediaTypeHandler.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/SQLQueryMediaTypeHandler.java
Thu Jan 10 03:24:47 2008
@@ -57,6 +57,10 @@
return true;
}
+ public boolean importResource(String path, String sourceURL, String
mediaType) throws RegistryException {
+ return false;
+ }
+
public boolean delete(String path) throws RegistryException {
// we are not doing anything special for deleting sql queries
return false;
@@ -66,4 +70,8 @@
// SQL query is a non-collection resource. So don't allow any children.
return false;
}
+
+ public boolean importChild(String childPath, String sourceURL) throws
RegistryException {
+ return false;
+ }
}
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/SynapseRepositoryMediaTypeHandler.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/SynapseRepositoryMediaTypeHandler.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/SynapseRepositoryMediaTypeHandler.java
Thu Jan 10 03:24:47 2008
@@ -95,6 +95,10 @@
return true;
}
+ public boolean importResource(String path, String sourceURL, String
mediaType) throws RegistryException {
+ return false;
+ }
+
public boolean delete(String path) throws RegistryException {
return false;
}
@@ -129,4 +133,8 @@
return false;
}
+
+ public boolean importChild(String childPath, String sourceURL) throws
RegistryException {
+ return true;
+ }
}
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/WSDLMediaTypeHandler.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/WSDLMediaTypeHandler.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/WSDLMediaTypeHandler.java
Thu Jan 10 03:24:47 2008
@@ -21,7 +21,7 @@
import org.wso2.registry.Resource;
import org.wso2.registry.jdbc.mediatypes.MediaTypeHandler;
import org.wso2.registry.jdbc.mediatypes.MediaTypeManager;
-import org.wso2.registry.jdbc.mediatypes.builtin.utils.WSDLFileProcessor;
+import org.wso2.registry.jdbc.mediatypes.utils.WSDLFileProcessor;
import org.wso2.usermanager.Realm;
import javax.sql.DataSource;
@@ -52,6 +52,12 @@
return true;
}
+ public boolean importResource(String path, String sourceURL, String
mediaType) throws RegistryException {
+
+ wsdlFileProcessor.saveWSDLFileToRegistry(sourceURL,
getParentPath(path));
+ return true;
+ }
+
public boolean delete(String path) throws RegistryException {
return false;
}
@@ -60,6 +66,10 @@
return false;
}
+ public boolean importChild(String childPath, String sourceURL) throws
RegistryException {
+ return false;
+ }
+
private String getParentPath(String childPath) {
if (childPath.equals(RegistryConstants.ROOT_PATH)) {
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/XSDMediaTypeHandler.java
==============================================================================
---
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/XSDMediaTypeHandler.java
(original)
+++
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/XSDMediaTypeHandler.java
Thu Jan 10 03:24:47 2008
@@ -18,7 +18,7 @@
import org.wso2.registry.jdbc.mediatypes.MediaTypeHandler;
import org.wso2.registry.jdbc.mediatypes.MediaTypeManager;
-import org.wso2.registry.jdbc.mediatypes.builtin.utils.SchemaFileProcessor;
+import org.wso2.registry.jdbc.mediatypes.utils.SchemaFileProcessor;
import org.wso2.registry.Resource;
import org.wso2.registry.RegistryException;
import org.wso2.registry.RegistryConstants;
@@ -51,6 +51,12 @@
return true;
}
+ public boolean importResource(String path, String sourceURL, String
mediaType) throws RegistryException {
+
+ schemaFileProcessor.saveSchemaFileToRegistry(sourceURL,
getParentPath(path));
+ return true;
+ }
+
public boolean delete(String path) throws RegistryException {
return false;
}
@@ -59,6 +65,10 @@
return false;
}
+ public boolean importChild(String childPath, String sourceURL) throws
RegistryException {
+ return false;
+ }
+
private String getParentPath(String childPath) {
if (childPath.equals(RegistryConstants.ROOT_PATH)) {
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
Thu Jan 10 03:24:47 2008
@@ -268,6 +268,54 @@
return suggestedPath;
}
+ public String importResource(String suggestedPath, String sourceURL,
String mediaType) 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);
+ }
+
+ User.setCurrentUser(userID);
+
+ registry.importResource(suggestedPath, sourceURL, mediaType);
+
+ return suggestedPath;
+ }
+
public void delete(String path) throws RegistryException {
try {
Modified:
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/ResourceHandler.java
==============================================================================
---
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/ResourceHandler.java
(original)
+++
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/ResourceHandler.java
Thu Jan 10 03:24:47 2008
@@ -38,12 +38,8 @@
String fetchURL = request.getParameter("fetchURL");
String mediaType = request.getParameter("mediaType");
- Resource resource = new Resource();
- resource.setProperty("fetchURL", fetchURL);
- resource.setMediaType(mediaType);
-
SecureRegistry userRegistry = getUserRegistry(request);
- userRegistry.put(resourcePath, resource);
+ userRegistry.importResource(resourcePath, fetchURL, mediaType);
}
private static SecureRegistry getUserRegistry(HttpServletRequest request)
_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev