ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1150740 - in /incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest: RESTClientServlet.java Workspace.java
Date Mon, 25 Jul 2011 15:06:37 GMT
Author: marrs
Date: Mon Jul 25 15:06:36 2011
New Revision: 1150740

URL: http://svn.apache.org/viewvc?rev=1150740&view=rev
Log:
ACE-151 some fixes and refactoring

Modified:
    incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest/RESTClientServlet.java
    incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest/Workspace.java

Modified: incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest/RESTClientServlet.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest/RESTClientServlet.java?rev=1150740&r1=1150739&r2=1150740&view=diff
==============================================================================
--- incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest/RESTClientServlet.java
(original)
+++ incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest/RESTClientServlet.java
Mon Jul 25 15:06:36 2011
@@ -22,9 +22,11 @@ import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -45,6 +47,7 @@ import com.google.gson.JsonPrimitive;
  * Servlet that offers a REST client API.
  */
 public class RESTClientServlet extends HttpServlet {
+    private static final long serialVersionUID = 5210711248294238039L;
     /** Alias that redirects to the latest version automatically. */
     private static final String LATEST_FOLDER = "latest";
     /** Name of the folder where working copies are kept. */
@@ -61,8 +64,8 @@ public class RESTClientServlet extends H
     
     public RESTClientServlet() {
         m_gson = (new GsonBuilder())
-        .registerTypeHierarchyAdapter(RepositoryObject.class, new RepositoryObjectSerializer())
-        .create();
+            .registerTypeHierarchyAdapter(RepositoryObject.class, new RepositoryObjectSerializer())
+            .create();
     }
     
     @Override
@@ -71,6 +74,7 @@ public class RESTClientServlet extends H
         if (pathElements == null || pathElements.length == 0) {
             // TODO return a list of versions
             resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "Not implemented: list
of versions");
+            return;
         }
         else {
             if (pathElements.length == 1) {
@@ -78,6 +82,7 @@ public class RESTClientServlet extends H
                     // TODO redirect to latest version
                     // resp.sendRedirect("notImplemented" /* to latest version */);
                     resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, "Not implemented:
redirect to latest version");
+                    return;
                 }
             }
             else if (pathElements.length == 3) {
@@ -94,6 +99,7 @@ public class RESTClientServlet extends H
                         return;
                     }
                     resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Could not find workspace:
" + pathElements[1]);
+                    return;
                 }
             }
             else if (pathElements.length == 4) {
@@ -104,16 +110,19 @@ public class RESTClientServlet extends H
                         String entityId = pathElements[3];
                         RepositoryObject repositoryObject = workspace.getRepositoryObject(entityType,
entityId);
                         if (repositoryObject == null) {
-                            // TODO not found
                             resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Repository
object of type " + entityType + " and identity " + entityId + " not found.");
+                            return;
                         }
                         
                         resp.getWriter().println(m_gson.toJson(repositoryObject));
                         return;
                     }
                     resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Could not find workspace:
" + pathElements[1]);
+                    return;
                 }
             }
+            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+            return;
         }
     }
 
@@ -138,7 +147,7 @@ public class RESTClientServlet extends H
                     }
                     m_sessionFactory.createSession(sessionID);
                     m_dm.add(component);
-                    resp.sendRedirect(WORK_FOLDER + "/" + sessionID);
+                    resp.sendRedirect(buildPathFromElements(WORK_FOLDER, sessionID));
                     return;
                 }
             }
@@ -169,7 +178,7 @@ public class RESTClientServlet extends H
                         try {
                             RepositoryValueObject data = m_gson.fromJson(req.getReader(),
RepositoryValueObject.class);
                             RepositoryObject object = workspace.addRepositoryObject(pathElements[2],
data.attributes, data.tags);
-                            resp.sendRedirect(WORK_FOLDER + "/" + pathElements[1] + "/" +
pathElements[2] + "/" + URLEncoder.encode(object.getAssociationFilter(null), "UTF-8"));
+                            resp.sendRedirect(buildPathFromElements(WORK_FOLDER, pathElements[1],
pathElements[2], object.getAssociationFilter(null)));
                             return;
                         }
                         catch (IllegalArgumentException e) {
@@ -177,9 +186,12 @@ public class RESTClientServlet extends H
                         }
                     }
                     resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Could not add entity
of type " + pathElements[2]);
+                    return;
                 }
             }
         }
+        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+        return;
     }
 
     @Override
@@ -188,15 +200,28 @@ public class RESTClientServlet extends H
         if (pathElements != null) {
             if (pathElements.length == 4) {
                 if (pathElements[0].equals(WORK_FOLDER)) {
-                    long id = Long.parseLong(pathElements[1]);
-                    // TODO check if pE[2] is one of the entities we know
-                    long entityId = Long.parseLong(pathElements[3]);
-                    // TODO check if pE[3] is a valid entity id, update it if it is
+                    Workspace workspace = getWorkspace(pathElements[1]);
+                    if (workspace != null) {
+                        try {
+                            RepositoryValueObject data = m_gson.fromJson(req.getReader(),
RepositoryValueObject.class);
+                            RepositoryObject object = workspace.getRepositoryObject(pathElements[2],
pathElements[3]);
+                            updateObjectWithData(object, data);
+                            resp.sendRedirect(buildPathFromElements(WORK_FOLDER, pathElements[1],
pathElements[2], pathElements[3]));
+                            return;
+                        }
+                        catch (IllegalArgumentException e) {
+                            e.printStackTrace();
+                        }
+                    }
+                    resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Could not add entity
of type " + pathElements[2]);
+                    return;
                 }
             }
         }
+        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+        return;
     }
-    
+
     @Override
     protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
         String[] pathElements = getPathElements(req);
@@ -214,21 +239,39 @@ public class RESTClientServlet extends H
                         // TODO delete the work area
                         m_dm.remove(component);
                         m_sessionFactory.destroySession(id);
+                        return;
                     }
                     else {
-                        // return error
+                        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Could not delete
work area.");
+                        return;
                     }
                 }
             }
             else if (pathElements.length == 4) {
                 if (WORK_FOLDER.equals(pathElements[0])) {
-                    long id = Long.parseLong(pathElements[1]);
-                    // TODO check if pE[2] is one of the entities we know
-                    long entityId = Long.parseLong(pathElements[3]);
-                    // TODO check if pE[3] is a valid entity id and delete it if it is
+                    String id = pathElements[1];
+                    String entityType = pathElements[2];
+                    String entityId = pathElements[3];
+                    
+                    Workspace workspace;
+                    Component component;
+                    synchronized (m_workspaces) {
+                        workspace = m_workspaces.get(id);
+                    }
+                    if (workspace != null) {
+                        workspace.deleteRepositoryObject(entityType, entityId);
+                        return;
+                    }
+                    else {
+                        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Could not find
work area.");
+                        return;
+                    }
+                    
                 }
             }
         }
+        resp.sendError(HttpServletResponse.SC_NOT_FOUND);
+        return;
     }
 
     private Workspace getWorkspace(String id) {
@@ -238,9 +281,38 @@ public class RESTClientServlet extends H
         }
         return workspace;
     }
+    
+    /**
+     * Builds a URL path from the supplied elements. Each individual element is URL encoded.
+     * 
+     * @param elements the elements
+     * @return the URL path
+     */
+    private String buildPathFromElements(String... elements) {
+        StringBuilder result = new StringBuilder();
+        for (String element : elements) {
+            if (result.length() > 0) {
+                result.append('/');
+            }
+            try {
+                result.append(URLEncoder.encode(element, "UTF-8"));
+            }
+            catch (UnsupportedEncodingException e) {} // ignored on purpose, any JVM must
support UTF-8
+        }
+        return result.toString();
+    }
 
+    /**
+     * Returns the separate path parts from the request, and URL decodes them.
+     * 
+     * @param req the request
+     * @return the separate path parts
+     */
     private String[] getPathElements(HttpServletRequest req) {
         String path = req.getPathInfo();
+        if (path == null) {
+            return new String[0];
+        }
         if (path.startsWith("/") && path.length() > 1) {
             path = path.substring(1);
         }
@@ -256,4 +328,41 @@ public class RESTClientServlet extends H
         catch (UnsupportedEncodingException e) {}
         return pathElements;
     }
+
+    private void updateObjectWithData(RepositoryObject repositoryObject, RepositoryValueObject
valueObject) {
+        // first handle the attributes
+        for (Entry<String, String> attribute : valueObject.attributes.entrySet()) {
+            String key = attribute.getKey();
+            String value = attribute.getValue();
+            // only add/update the attribute if it actually changed
+            if (!value.equals(repositoryObject.getAttribute(key))) {
+                repositoryObject.addAttribute(key, value);
+            }
+        }
+        Enumeration<String> keys = repositoryObject.getAttributeKeys();
+        while (keys.hasMoreElements()) {
+            String key = keys.nextElement();
+            if (!valueObject.attributes.containsKey(key)) {
+                // TODO since we cannot remove keys right now, we null them
+                repositoryObject.addAttribute(key, null);
+            }
+        }
+        // now handle the tags in a similar way
+        for (Entry<String, String> attribute : valueObject.tags.entrySet()) {
+            String key = attribute.getKey();
+            String value = attribute.getValue();
+            // only add/update the tag if it actually changed
+            if (!value.equals(repositoryObject.getTag(key))) {
+                repositoryObject.addTag(key, value);
+            }
+        }
+        keys = repositoryObject.getTagKeys();
+        while (keys.hasMoreElements()) {
+            String key = keys.nextElement();
+            if (!valueObject.tags.containsKey(key)) {
+                // TODO since we cannot remove keys right now, we null them
+                repositoryObject.addTag(key, null);
+            }
+        }
+    }
 }

Modified: incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest/Workspace.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest/Workspace.java?rev=1150740&r1=1150739&r2=1150740&view=diff
==============================================================================
--- incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest/Workspace.java
(original)
+++ incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest/Workspace.java
Mon Jul 25 15:06:36 2011
@@ -24,9 +24,11 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.ace.client.repository.ObjectRepository;
 import org.apache.ace.client.repository.RepositoryAdmin;
 import org.apache.ace.client.repository.RepositoryObject;
 import org.apache.ace.client.repository.SessionFactory;
+import org.apache.ace.client.repository.object.ArtifactObject;
 import org.apache.ace.client.repository.repository.Artifact2GroupAssociationRepository;
 import org.apache.ace.client.repository.repository.ArtifactRepository;
 import org.apache.ace.client.repository.repository.GatewayRepository;
@@ -144,27 +146,7 @@ public class Workspace {
         try {
             List list = null;
             Filter filter = FrameworkUtil.createFilter(entityId);
-            if (ARTIFACT.equals(entityType)) {
-                list = m_artifactRepository.get(filter);
-            }
-            if (ARTIFACT2FEATURE.equals(entityType)) {
-                list = m_artifact2FeatureAssociationRepository.get(filter);
-            }
-            if (FEATURE.equals(entityType)) {
-                list = m_featureRepository.get(filter);
-            }
-            if (FEATURE2DISTRIBUTION.equals(entityType)) {
-                list = m_feature2DistributionAssociationRepository.get(filter);
-            }
-            if (DISTRIBUTION.equals(entityType)) {
-                list = m_distributionRepository.get(filter);
-            }
-            if (DISTRIBUTION2TARGET.equals(entityType)) {
-                list = m_distribution2TargetAssociationRepository.get(filter);
-            }
-            if (TARGET.equals(entityType)) {
-                list = m_statefulTargetRepository.get(filter);
-            }
+            list = getObjectRepository(entityType).get(filter);
             if (list != null && list.size() == 1) {
                 return (RepositoryObject) list.get(0);
             }
@@ -176,28 +158,7 @@ public class Workspace {
     }
 
     public List<RepositoryObject> getRepositoryObjects(String entityType) {
-        List list = null;
-        if (ARTIFACT.equals(entityType)) {
-            list = m_artifactRepository.get();
-        }
-        if (ARTIFACT2FEATURE.equals(entityType)) {
-            list = m_artifact2FeatureAssociationRepository.get();
-        }
-        if (FEATURE.equals(entityType)) {
-            list = m_featureRepository.get();
-        }
-        if (FEATURE2DISTRIBUTION.equals(entityType)) {
-            list = m_feature2DistributionAssociationRepository.get();
-        }
-        if (DISTRIBUTION.equals(entityType)) {
-            list = m_distributionRepository.get();
-        }
-        if (DISTRIBUTION2TARGET.equals(entityType)) {
-            list = m_distribution2TargetAssociationRepository.get();
-        }
-        if (TARGET.equals(entityType)) {
-            list = m_statefulTargetRepository.get();
-        }
+        List list = getObjectRepository(entityType).get();
         if (list != null) {
             return list;
         }
@@ -207,29 +168,46 @@ public class Workspace {
     }
 
     public RepositoryObject addRepositoryObject(String entityType, Map<String, String>
attributes, Map<String, String> tags) throws IllegalArgumentException{
+        return getObjectRepository(entityType).create(attributes, tags);
+    }
+    
+    public void deleteRepositoryObject(String entityType, String entityId) {
+        RepositoryObject result = null;
+        try {
+            List list = null;
+            Filter filter = FrameworkUtil.createFilter(entityId);
+            ObjectRepository objectRepository = getObjectRepository(entityType);
+            list = objectRepository.get(filter);
+            if (list != null && list.size() == 1) {
+                objectRepository.remove((RepositoryObject) list.get(0));
+            }
+        }
+        catch (InvalidSyntaxException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private ObjectRepository getObjectRepository(String entityType) {
         if (ARTIFACT.equals(entityType)) {
-            return m_artifactRepository.create(attributes, tags);
+            return m_artifactRepository;
         }
         if (ARTIFACT2FEATURE.equals(entityType)) {
-            // TODO this is a fairly low level way to create associations
-            return m_artifact2FeatureAssociationRepository.create(attributes, tags);
+            return m_artifact2FeatureAssociationRepository;
         }
         if (FEATURE.equals(entityType)) {
-            return m_featureRepository.create(attributes, tags);
+            return m_featureRepository;
         }
         if (FEATURE2DISTRIBUTION.equals(entityType)) {
-            // TODO this is a fairly low level way to create associations
-            return m_feature2DistributionAssociationRepository.create(attributes, tags);
+            return m_feature2DistributionAssociationRepository;
         }
         if (DISTRIBUTION.equals(entityType)) {
-            return m_distributionRepository.create(attributes, tags);
+            return m_distributionRepository;
         }
         if (DISTRIBUTION2TARGET.equals(entityType)) {
-            // TODO this is a fairly low level way to create associations
-            return m_distribution2TargetAssociationRepository.create(attributes, tags);
+            return m_distribution2TargetAssociationRepository;
         }
         if (TARGET.equals(entityType)) {
-            return m_targetRepository.create(attributes, tags);
+            return m_targetRepository;
         }
         throw new IllegalArgumentException("Unknown entity type: " + entityType);
     }



Mime
View raw message