Author: chathura
Date: Tue Jan 22 23:52:10 2008
New Revision: 12745

Log:


Implemented resource imports form URLs. Now it is possible to give a URL and 
create a resource from it.
Content of the URL is imported by the backend and sets it as the content of the 
resource.
Changed the importResource(...) method of the Registry API to support all 
resource metadata.



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   
Tue Jan 22 23:52:10 2008
@@ -32,11 +32,11 @@
      * @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 media type of the remote resource
+     * @param metadata
      * @return Actual path to which the resource is added
      * @throws RegistryException
      */
-    String importResource(String suggestedPath, String sourceURL, String 
mediaType) throws RegistryException;
+    String importResource(String suggestedPath, String sourceURL, Resource 
metadata) throws RegistryException;
 
     /**
      * Deletes the resource in the given path. If the path refers to a 
directory, all child

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
 Tue Jan 22 23:52:10 2008
@@ -291,7 +291,7 @@
     }
 
 
-    public String importResource(String suggestedPath, String sourceURL, 
String mediaType)
+    public String importResource(String suggestedPath, String sourceURL, 
Resource metadata)
             throws RegistryException {
         // TODO: Implement resource import logic for remote case
 

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
  Tue Jan 22 23:52:10 2008
@@ -137,7 +137,7 @@
         return suggestedPath;
     }
 
-    public String importResource(String suggestedPath, String sourceURL, 
String mediaType) throws RegistryException {
+    public String importResource(String suggestedPath, String sourceURL, 
Resource metadata) throws RegistryException {
         throw new UnsupportedOperationException(
                 "Resource imports are not supported in the InMemoryRegistry.");
     }

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
  Tue Jan 22 23:52:10 2008
@@ -273,10 +273,10 @@
         return suggestedPath;
     }
 
-    public String importResource(String suggestedPath, String sourceURL, 
String mediaType) throws RegistryException {
+    public String importResource(String suggestedPath, String sourceURL, 
Resource metadata) throws RegistryException {
 
         suggestedPath = preparePath(suggestedPath);
-        mediaTypeManager.importResource(suggestedPath, sourceURL, mediaType);
+        mediaTypeManager.importResource(suggestedPath, sourceURL, metadata);
 
         Connection conn = getConnection();
 

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
   Tue Jan 22 23:52:10 2008
@@ -108,14 +108,14 @@
      * @param path Path to add the new resource.
      * @param sourceURL URL to fetch the resource content
      *
-     * @param mediaType
+     * @param metadata
      * @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;
+    public abstract boolean importResource(String path, String sourceURL, 
Resource metadata) throws RegistryException;
 
     /**
      * Processes the DELETE action of the media type.

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
   Tue Jan 22 23:52:10 2008
@@ -25,7 +25,6 @@
 import org.wso2.registry.config.MediaTypeHandlerConfiguration;
 import org.wso2.registry.exceptions.ResourceNotFoundException;
 import org.wso2.registry.i18n.Messages;
-import org.wso2.registry.jdbc.JDBCRegistry;
 import org.wso2.registry.jdbc.dao.VersionedResourceDAO;
 import org.wso2.registry.jdbc.mediatypes.builtin.*;
 import org.wso2.usermanager.Realm;
@@ -183,7 +182,7 @@
         }
     }
 
-    public void importResource(String path, String sourceURL, String mediaType)
+    public void importResource(String path, String sourceURL, Resource 
metadata)
             throws RegistryException {
 
         String parentPath = getParentPath(path);
@@ -205,19 +204,19 @@
             }
         }
 
-        if (mediaType != null) {
+        if (metadata.getMediaType() != null) {
             MediaTypeHandler mediaTypeHandler =
-                    (MediaTypeHandler)mediaTypeHandlers.get(mediaType);
+                    
(MediaTypeHandler)mediaTypeHandlers.get(metadata.getMediaType());
 
             if (mediaTypeHandler != null) {
-                if (!mediaTypeHandler.importResource(path, sourceURL, 
mediaType)) {
-                    defaultMediaTypeHandler.importResource(path, sourceURL, 
mediaType);
+                if (!mediaTypeHandler.importResource(path, sourceURL, 
metadata)) {
+                    defaultMediaTypeHandler.importResource(path, sourceURL, 
metadata);
                 }
             } else {
-                defaultMediaTypeHandler.importResource(path, sourceURL, 
mediaType);
+                defaultMediaTypeHandler.importResource(path, sourceURL, 
metadata);
             }
         } else {
-            defaultMediaTypeHandler.importResource(path, sourceURL, mediaType);
+            defaultMediaTypeHandler.importResource(path, sourceURL, metadata);
         }
     }
 

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
    Tue Jan 22 23:52:10 2008
@@ -73,7 +73,7 @@
         return true;
     }
 
-    public boolean importResource(String path, String sourceURL, String 
mediaType) throws RegistryException {
+    public boolean importResource(String path, String sourceURL, Resource 
metadata) throws RegistryException {
         return false;
     }
 

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
    Tue Jan 22 23:52:10 2008
@@ -33,6 +33,12 @@
 import java.sql.SQLException;
 import java.util.Iterator;
 import java.util.List;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.net.URLConnection;
+import java.io.IOException;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
 
 public class DefaultMediaTypeHandler extends MediaTypeHandler {
 
@@ -230,7 +236,39 @@
         }
     }
 
-    public boolean importResource(String path, String sourceURL, String 
mediaType) throws RegistryException {
+    public boolean importResource(String path, String sourceURL, Resource 
metadata) throws RegistryException {
+
+        URL url;
+        try {
+            url = new URL(sourceURL);
+        } catch (MalformedURLException e) {
+            String msg = "Given source URL is not valid.";
+            throw new RegistryException(msg, e);
+        }
+
+        try {
+            URLConnection uc = url.openConnection();
+            InputStream in = uc.getInputStream();
+
+            int data;
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            while ((data = in.read()) > 0) {
+                out.write(data);
+            }
+            out.flush();
+
+            Resource resource = new Resource();
+            resource.setPath(path);
+            resource.setMediaType(metadata.getMediaType());
+            resource.setDescription(metadata.getDescription());
+            resource.setContent(out.toByteArray());
+
+            put(path, resource);
+
+        } catch (IOException e) {
+            String msg = "Could not read from the given URL: " + sourceURL;
+            throw new RegistryException(msg, e);
+        }
         return false;
     }
 

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
   Tue Jan 22 23:52:10 2008
@@ -57,7 +57,7 @@
         return true;
     }
 
-    public boolean importResource(String path, String sourceURL, String 
mediaType) throws RegistryException {
+    public boolean importResource(String path, String sourceURL, Resource 
metadata) 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
  Tue Jan 22 23:52:10 2008
@@ -95,7 +95,7 @@
         return true;
     }
 
-    public boolean importResource(String path, String sourceURL, String 
mediaType) throws RegistryException {
+    public boolean importResource(String path, String sourceURL, Resource 
metadata) throws RegistryException {
         return false;
     }
 

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
       Tue Jan 22 23:52:10 2008
@@ -50,7 +50,7 @@
         return false;
     }
 
-    public boolean importResource(String path, String sourceURL, String 
mediaType) throws RegistryException {
+    public boolean importResource(String path, String sourceURL, Resource 
metadata) throws RegistryException {
 
         wsdlFileProcessor.saveWSDLFileToRegistry(sourceURL, 
getParentPath(path), true);
         return true;

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
        Tue Jan 22 23:52:10 2008
@@ -50,7 +50,7 @@
         return false;
     }
 
-    public boolean importResource(String path, String sourceURL, String 
mediaType) throws RegistryException {
+    public boolean importResource(String path, String sourceURL, Resource 
metadata) throws RegistryException {
 
         schemaFileProcessor.saveSchemaFileToRegistry(sourceURL, 
getParentPath(path), true);
         return true;

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
      Tue Jan 22 23:52:10 2008
@@ -251,7 +251,7 @@
         }
     }
 
-    public String importResource(String suggestedPath, String sourceURL, 
String mediaType)
+    public String importResource(String suggestedPath, String sourceURL, 
Resource metadata)
             throws RegistryException {
 
         // find the existing ascendent path of the given path
@@ -259,7 +259,7 @@
 
         User.setCurrentUser(userID);
 
-        registry.importResource(suggestedPath, sourceURL, mediaType);
+        registry.importResource(suggestedPath, sourceURL, metadata);
 
         return suggestedPath;
     }

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
     Tue Jan 22 23:52:10 2008
@@ -37,9 +37,13 @@
 
         String fetchURL = request.getParameter("fetchURL");
         String mediaType = request.getParameter("mediaType");
+        String description = request.getParameter("description");
+        Resource metadata = new Resource();
+        metadata.setMediaType(mediaType);
+        metadata.setDescription(description);
 
         SecureRegistry userRegistry = getUserRegistry(request);
-        userRegistry.importResource(resourcePath, fetchURL, mediaType);
+        userRegistry.importResource(resourcePath, fetchURL, metadata);
     }
 
     private static SecureRegistry getUserRegistry(HttpServletRequest request)

_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev

Reply via email to