Author: glen
Date: Thu Jan 24 06:11:58 2008
New Revision: 12838

Log:

* First cut at remote imports

* Use the media type of the imported resource if an override wasn't specified

* Return more stack traces on remote error responses to aid debugging

* Code cleanup / spelling

Modified:
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/APPConstants.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/jdbc/JDBCRegistry.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/DefaultMediaTypeHandler.java

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/APPConstants.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/APPConstants.java
   (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/APPConstants.java
   Thu Jan 24 06:11:58 2008
@@ -20,4 +20,5 @@
 public interface APPConstants {
     static final QName COMMENTS_QNAME = new QName("isComments");
     static final QName COMMENTID_QNAME = new QName("commentID");
+    static final String IMPORT_MEDIATYPE = "application/resource-import";
 }

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
   Thu Jan 24 06:11:58 2008
@@ -104,6 +104,23 @@
         // Need to add a resource
         Abdera abdera = request.getAbdera();
         ResponseContext rc;
+
+        String contentType = request.getContentType().toString();
+        if (IMPORT_MEDIATYPE.equals(contentType)) {
+            // This is an import.
+            String importURL = request.getParameter("importURL");
+            String suggestedPath = request.getSlug();
+            Resource metadata = new Resource();
+            String location;
+            try {
+                location = jdbcregistry.importResource(suggestedPath, 
importURL, metadata);
+            } catch (RegistryException e) {
+                return new StringResponseContext(abdera, e, 500);
+            }
+            rc = new EmptyResponseContext(200);
+            rc.setLocation(location);
+        }
+
         try {
             Parser parser = abdera.getParser();
             Document<Entry> entry_doc = 
(Document<Entry>)request.getDocument(parser).clone();
@@ -227,7 +244,7 @@
 //                }
                 return new EmptyResponseContext(204);
             } catch (RegistryException e) {
-                return new EmptyResponseContext(500);
+                return new StringResponseContext(request.getAbdera(), e, 500);
             }
         } else {
             return new EmptyResponseContext(500);
@@ -279,7 +296,7 @@
             }
             return new EmptyResponseContext(200);
         } catch (Exception pe) {
-            return new EmptyResponseContext(500);
+            return new StringResponseContext(request.getAbdera(), pe, 500);
         }
     }
 
@@ -306,10 +323,10 @@
         } catch (ResourceNotFoundException e) {
             return new EmptyResponseContext(404);
         } catch (RegistryException e) {
-            return new EmptyResponseContext(500);
+            return new StringResponseContext(request.getAbdera(), e, 500);
         }
 
-        Feed feed = null;
+        Feed feed;
         if (idx > -1) {
             String operation = entry_id.substring(idx + 1);
 
@@ -337,7 +354,7 @@
                                                  abdera, "versionLink",
                                                  feed);
                 } catch (RegistryException e) {
-                    return new EmptyResponseContext(500);
+                    return new StringResponseContext(request.getAbdera(), e, 
500);
                 }
             } else if (RegistryConstants.PARAMETER_TAGS.equals(operation)) {
                 // if the user [parameter]  name is null then need to return 
all the
@@ -387,7 +404,7 @@
                 try {
                     feed = getFeedForLogs(abdera, resourcePath, from, to, 
author, action, request);
                 } catch (RegistryException e) {
-                    return new EmptyResponseContext(500);
+                    return new StringResponseContext(request.getAbdera(), e, 
500);
                 }
             } else if (RegistryConstants.PARAMETER_QUERY.equals(operation)) {
                 try {
@@ -397,7 +414,7 @@
                     resource = 
getSecureRegistry(request).executeQuery(resourcePath, parameters);
                     feed = populateFeed(abdera, baseURI, resourcePath, 
resource, true);
                 } catch (RegistryException e) {
-                    return new EmptyResponseContext(500);
+                    return new StringResponseContext(request.getAbdera(), e, 
500);
                 }
             } else if 
(RegistryConstants.PARAMETER_RESOURCE_EXIST.equals(operation)) {
                 feed = getFeedResourceExist(abdera, resourcePath, request);
@@ -737,33 +754,6 @@
     }
 
     /**
-     * This method will create a feed element to representing rating value for 
a given path by given
-     * user. The rating value will be there in the text of the feed
-     *
-     * @param abdera   : An instance of Abdera
-     * @param path     : Resource path
-     * @param userName : user name
-     * @param request  : RequestContext
-     * @return : Created feed
-     */
-    private Feed getFeedForRating(Abdera abdera,
-                                  String path,
-                                  String userName,
-                                  RequestContext request) {
-        try {
-            Factory factory = abdera.getFactory();
-            Feed feed = factory.newFeed();
-            feed.setUpdated(new Date());
-            feed.setId("http://wso2.org/jdbcregistry:Rating"; + path);
-            feed.setTitle(userName + " ratings for " + path);
-            feed.setSubtitle("" + getSecureRegistry(request).getRating(path, 
userName));
-            return feed;
-        } catch (RegistryException e) {
-            return null;
-        }
-    }
-
-    /**
      * To get the collection of comments for a given path a feed element will 
be generated with a
      * set of entry . And each entry will represent - commented Author - 
Commented Time - And the
      * content

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 24 06:11:58 2008
@@ -48,7 +48,7 @@
     private Log log = LogFactory.getLog(RemoteRegistry.class);
     //This will keep the value of username and the password for authorization
     private String authorizationString = null;
-    private String usreName = null;
+    private String username = null;
 
     /**
      * To create a remote registry need to provide a URL of a remote registry 
and the URL should be
@@ -79,7 +79,7 @@
         if (baseURI.endsWith("/")) {
             baseURI = baseURI.substring(0, baseURI.length() - 1);
         }
-        this.usreName = userName;
+        this.username = userName;
         if (userName != null && password != null) {
             authorizationString = userName + ":" + password;
             authorizationString = "Basic " + 
Base64.encode(authorizationString.getBytes());
@@ -91,7 +91,7 @@
         AbderaClient abderaClient = new AbderaClient(abdera);
         ClientResponse clientResponse = abderaClient.get(baseURI + path, 
getAuthorization());
         if (clientResponse.getType() == Response.ResponseType.CLIENT_ERROR ||
-                clientResponse.getType() == Response.ResponseType.SERVER_ERROR 
) {
+            clientResponse.getType() == Response.ResponseType.SERVER_ERROR) {
             throw new ResourceNotFoundException(path);
         }
         Document introspection = clientResponse.getDocument();
@@ -219,17 +219,17 @@
         AbderaClient abderaClient = new AbderaClient(abdera);
         Entry entry = abdera.getFactory().newEntry();
         entry.setSummary(resource.getDescription());
-        if (resource.getAuthorUserName() !=null) {
+        if (resource.getAuthorUserName() != null) {
             entry.addAuthor(resource.getAuthorUserName());
         } else {
-            entry.addAuthor(usreName);
+            entry.addAuthor(username);
         }
         java.util.Properties properties = resource.getProperties();
         addPropertyExtensionElement(properties, abdera, entry, 
PropertyExtensionFactory.PROPERTIES,
                                     PropertyExtensionFactory.PROPERTY);
         entry.addSimpleExtension(new QName("mediaType"), 
resource.getMediaType());
         entry.addSimpleExtension(new QName("parentPath"), 
resource.getParentPath());
-        if(resource.isContentModified()) {
+        if (resource.isContentModified()) {
             entry.addSimpleExtension(new QName("contentModified"), "true");
         }
         if (resource.isDirectory()) {
@@ -313,8 +313,15 @@
 
     public String importResource(String suggestedPath, String sourceURL, 
Resource metadata)
             throws RegistryException {
-        // TODO: Implement resource import logic for remote case
+        Abdera abdera = new Abdera();
+        AbderaClient abderaClient = new AbderaClient(abdera);
 
+        String postData = "?importURL=" + sourceURL;
+        ByteArrayInputStream bis = new 
ByteArrayInputStream(postData.getBytes());
+        RequestOptions opts = getAuthorization();
+        opts.setSlug(suggestedPath);
+        opts.setContentType(IMPORT_MEDIATYPE);
+        ClientResponse response = abderaClient.post(baseURI + postData, bis, 
opts);
         return null;
     }
 

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 24 06:11:58 2008
@@ -87,9 +87,9 @@
      * @param dataSource Data source to be used to store resources and 
metadata. Note that the same
      *                   data source may or may not be used as the data source 
of user manager.
      * @param realm      User manager realm to handle authorizations. It is 
strongly recommended to
-     *                   use a realm instance constructed using the 
RegistryRealmFactory. All initial
-     *                   users, roles and authorizations will be set on realms 
obtained using the
-     *                   RegistryRealmFactory.
+     *                   use a realm instance constructed using the 
RegistryRealmFactory. All
+     *                   initial users, roles and authorizations will be set 
on realms obtained
+     *                   using the RegistryRealmFactory.
      * @throws RegistryException : If something went wrong
      */
     public JDBCRegistry(DataSource dataSource, Realm realm) throws 
RegistryException {
@@ -234,11 +234,13 @@
      * issues.
      *
      * @param suggestedPath the path which we'd like to use for the new 
resource.
-     * @param resource Resource instance for the new resource
+     * @param resource      Resource instance for the new resource
      * @return the actual path that the server chose to use for our Resource
      * @throws org.wso2.registry.RegistryException
+     *
      */
-    public synchronized String put(String suggestedPath, Resource resource) 
throws RegistryException {
+    public synchronized String put(String suggestedPath, Resource resource)
+            throws RegistryException {
         suggestedPath = preparePath(suggestedPath);
 
         String actualPath = null;
@@ -285,7 +287,8 @@
         }
     }
 
-    public String importResource(String suggestedPath, String sourceURL, 
Resource metadata) throws RegistryException {
+    public String importResource(String suggestedPath, String sourceURL, 
Resource metadata)
+            throws RegistryException {
 
         suggestedPath = preparePath(suggestedPath);
         mediaTypeManager.importResource(suggestedPath, sourceURL, metadata);
@@ -326,13 +329,13 @@
     public String rename(String currentPath, String newPath) throws 
RegistryException {
         if (resourceExists(currentPath)) {
             resourceDAO.renameResource(currentPath,
-                    newPath,
-                    getConnection(),
-                    User.getCurrentUser(),
-                    defaultRealm);
+                                       newPath,
+                                       getConnection(),
+                                       User.getCurrentUser(),
+                                       defaultRealm);
             return newPath;
         } else {
-             throw new ResourceNotFoundException(currentPath);
+            throw new ResourceNotFoundException(currentPath);
         }
     }
 
@@ -665,7 +668,8 @@
 
         } catch (SQLException e) {
 
-            String msg = "Could not remove the tag: " + tag + " from the 
resource at path: " + path + ".";
+            String msg = "Could not remove the tag: " + tag + " from the 
resource at path: " +
+                         path + ".";
             log.error(msg, e);
 
             try {
@@ -746,17 +750,17 @@
                 e.printStackTrace();
             }
         }
-       return commentPath;
+        return commentPath;
     }
 
 
     public void editComment(String commentPath, String text) throws 
RegistryException {
-        String [] parts = commentPath.split(";");
+        String[] parts = commentPath.split(";");
         String commentPart = parts[1];
         if (parts.length == 2 && commentPart.startsWith("comments:")) {
             String commentId = parts[1].substring(9);
             try {
-              commentsDAO.updateComment(Long.parseLong(commentId), text, 
getConnection());
+                commentsDAO.updateComment(Long.parseLong(commentId), text, 
getConnection());
             } catch (SQLException e) {
                 //TODO : i18n
             }
@@ -1020,7 +1024,7 @@
         String currentPath = resourcePath;
         if (resourcePath.indexOf("?") > 0) {
             currentPath = resourcePath.split("\\?")[0];
-        } else if (resourcePath.indexOf(";")>0) {
+        } else if (resourcePath.indexOf(";") > 0) {
             currentPath = resourcePath.split("\\;")[0];
         }
 

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 24 06:11:58 2008
@@ -21,22 +21,23 @@
 import org.wso2.registry.RegistryConstants;
 import org.wso2.registry.RegistryException;
 import org.wso2.registry.Resource;
-import org.wso2.registry.config.RegistryContext;
 import org.wso2.registry.config.MediaTypeHandlerConfiguration;
+import org.wso2.registry.config.RegistryContext;
 import org.wso2.registry.exceptions.ResourceNotFoundException;
 import org.wso2.registry.i18n.Messages;
 import org.wso2.registry.jdbc.dao.VersionedResourceDAO;
-import org.wso2.registry.jdbc.mediatypes.builtin.*;
+import org.wso2.registry.jdbc.mediatypes.builtin.DefaultMediaTypeHandler;
+import org.wso2.registry.jdbc.mediatypes.builtin.SQLQueryMediaTypeHandler;
 import org.wso2.usermanager.Realm;
 
 import javax.sql.DataSource;
+import java.lang.reflect.Constructor;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.HashMap;
-import java.util.Map;
-import java.util.List;
 import java.util.Iterator;
-import java.lang.reflect.Constructor;
+import java.util.List;
+import java.util.Map;
 
 /**
  * Handles the actions by delegating them to registered media type handlers. 
If a mediatype handler
@@ -67,7 +68,7 @@
         mediaTypeHandlers.put(RegistryConstants.SQL_QUERY_MEDIA_TYPE, 
sqlMediaTypeHandler);
 
         RegistryContext registryContext =
-                (RegistryContext) 
System.getProperties().get(RegistryConstants.REGISTRY_CONTEXT);
+                
(RegistryContext)System.getProperties().get(RegistryConstants.REGISTRY_CONTEXT);
 
         if (registryContext != null) {
             List mediaTypeConfigs = registryContext.getMediaTypeHandlers();
@@ -76,21 +77,23 @@
             while (mediaTypeIterator.hasNext()) {
 
                 MediaTypeHandlerConfiguration configuration =
-                        (MediaTypeHandlerConfiguration) 
mediaTypeIterator.next();
+                        
(MediaTypeHandlerConfiguration)mediaTypeIterator.next();
 
                 try {
                     Class handlerClass = 
Class.forName(configuration.getMediaTypeHandler());
 
                     Constructor handlerConstructor = 
handlerClass.getConstructor(
-                            new Class[] { DataSource.class, Realm.class, 
MediaTypeManager.class});
+                            DataSource.class, Realm.class, 
MediaTypeManager.class);
 
                     MediaTypeHandler mediaTypeHandler = (MediaTypeHandler)
-                            handlerConstructor.newInstance(new Object[] { 
dataSource, realm, this});
+                            handlerConstructor.newInstance(dataSource, realm, 
this);
 
                     mediaTypeHandlers.put(configuration.getMediaType(), 
mediaTypeHandler);
 
                 } catch (ClassNotFoundException cnfe) {
-                    String msg = "Could not find the implementation class: " + 
configuration.getMediaTypeHandler() + " for the media type: " + 
configuration.getMediaType();
+                    String msg = "Could not find the implementation class: " +
+                                 configuration.getMediaTypeHandler() + " for 
the media type: " +
+                                 configuration.getMediaType();
                     log.error(msg, cnfe);
                     throw new RegistryException(msg, cnfe);
 
@@ -158,7 +161,7 @@
                     boolean allowed = parentMediaTypeHandler.putChild(path, 
resource);
                     if (!allowed) {
                         String msg = "Attempted to add unrecognized resource 
in to the typed " +
-                                "collection " + 
parentCollection.getMediaType();
+                                     "collection " + 
parentCollection.getMediaType();
                         log.info(msg);
                         throw new RegistryException(msg);
                     }
@@ -196,7 +199,7 @@
                     boolean allowed = parentMediaTypeHandler.importChild(path, 
sourceURL);
                     if (!allowed) {
                         String msg = "Attempted to add unrecognized resource 
in to the typed " +
-                                "collection " + 
parentCollection.getMediaType();
+                                     "collection " + 
parentCollection.getMediaType();
                         log.info(msg);
                         throw new RegistryException(msg);
                     }
@@ -256,7 +259,7 @@
 
         Resource resource;
 
-        Connection conn = null;
+        Connection conn;
         try {
             conn = dataSource.getConnection();
         } catch (SQLException e) {

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 24 06:11:58 2008
@@ -29,16 +29,16 @@
 import org.wso2.usermanager.Realm;
 
 import javax.sql.DataSource;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
 import java.sql.Connection;
 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 {
 
@@ -63,7 +63,7 @@
 
         plainPath = getPreparedPath(plainPath);
 
-        Connection conn ;
+        Connection conn;
         try {
             conn = dataSource.getConnection();
         } catch (SQLException e) {
@@ -108,7 +108,7 @@
 
         String userID = User.getCurrentUser();
 
-        Connection conn ;
+        Connection conn;
         try {
             conn = dataSource.getConnection();
         } catch (SQLException e) {
@@ -188,10 +188,11 @@
      * 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 path   : Location where we need to add the resource
+     * @param conn   : DB connection
      * @param userID : user
-     * @throws SQLException : Something went wrong
+     * @throws SQLException      : Something went wrong
      * @throws RegistryException : Something went wrong
      */
     private void createParentCollections(String path,
@@ -238,7 +239,8 @@
         }
     }
 
-    public boolean importResource(String path, String sourceURL, Resource 
metadata) throws RegistryException {
+    public boolean importResource(String path, String sourceURL, Resource 
metadata)
+            throws RegistryException {
 
         URL url;
         try {
@@ -261,7 +263,10 @@
 
             Resource resource = new Resource();
             resource.setPath(path);
-            resource.setMediaType(metadata.getMediaType());
+            String mediaType = metadata.getMediaType();
+            if (mediaType == null)
+                mediaType = uc.getContentType();
+            resource.setMediaType(mediaType);
             resource.setDescription(metadata.getDescription());
             resource.setContent(out.toByteArray());
 
@@ -276,7 +281,7 @@
 
     public boolean delete(String path) throws RegistryException {
 
-        Connection conn ;
+        Connection conn;
         try {
             conn = dataSource.getConnection();
         } catch (SQLException e) {
@@ -320,7 +325,8 @@
         return true;
     }
 
-    private void deleteDirectory(Resource collection, Connection conn) throws 
SQLException  , RegistryException{
+    private void deleteDirectory(Resource collection, Connection conn)
+            throws SQLException, RegistryException {
         List childList = resourceDAO.getChildren(collection.getId(), -1, conn);
         Iterator i = childList.iterator();
         while (i.hasNext()) {

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

Reply via email to