You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ace.apache.org by ma...@apache.org on 2011/07/29 01:11:20 UTC

svn commit: r1152034 - in /incubator/ace/trunk/ace-client-rest/src: main/java/org/apache/ace/client/rest/ test/java/org/ test/java/org/apache/ test/java/org/apache/ace/ test/java/org/apache/ace/client/ test/java/org/apache/ace/client/rest/

Author: marrs
Date: Thu Jul 28 23:11:19 2011
New Revision: 1152034

URL: http://svn.apache.org/viewvc?rev=1152034&view=rev
Log:
ACE-160 applied the patch, plus one other fix to keep things symmetrical, added a test for it as well

Added:
    incubator/ace/trunk/ace-client-rest/src/test/java/org/
    incubator/ace/trunk/ace-client-rest/src/test/java/org/apache/
    incubator/ace/trunk/ace-client-rest/src/test/java/org/apache/ace/
    incubator/ace/trunk/ace-client-rest/src/test/java/org/apache/ace/client/
    incubator/ace/trunk/ace-client-rest/src/test/java/org/apache/ace/client/rest/
    incubator/ace/trunk/ace-client-rest/src/test/java/org/apache/ace/client/rest/RESTClientTest.java   (with props)
Modified:
    incubator/ace/trunk/ace-client-rest/src/main/java/org/apache/ace/client/rest/RESTClientServlet.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=1152034&r1=1152033&r2=1152034&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 Thu Jul 28 23:11:19 2011
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
@@ -37,6 +38,8 @@ import org.apache.ace.client.repository.
 import org.apache.ace.client.repository.SessionFactory;
 import org.apache.felix.dm.Component;
 import org.apache.felix.dm.DependencyManager;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedService;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
@@ -46,21 +49,42 @@ import com.google.gson.JsonPrimitive;
 /**
  * Servlet that offers a REST client API.
  */
-public class RESTClientServlet extends HttpServlet {
+public class RESTClientServlet extends HttpServlet implements ManagedService {
     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. */
     private static final String WORK_FOLDER = "work";
-    
+    /** URL of the repository to talk to. */
+    private static final String KEY_REPOSITORY_URL = "repository.url";
+    /** URL of the OBR to talk to. */
+    private static final String KEY_OBR_URL = "obr.url";
+    /** Name of the customer. */
+    private static final String KEY_CUSTOMER_NAME = "customer.name";
+    /** Name of the store repository. */
+    private static final String KEY_STORE_REPOSITORY_NAME = "store.repository.name";
+    /** Name of the license repository. */
+    private static final String KEY_LICENSE_REPOSITORY_NAME = "license.repository.name";
+    /** Name of the deployment repository. */
+    private static final String KEY_DEPLOYMENT_REPOSITORY_NAME = "deployment.repository.name";
+    /** Name of the user to log in as. */
+    private static final String KEY_USER_NAME = "user.name";
+
     private static long m_sessionID = 1;
-    
+
     private volatile DependencyManager m_dm;
     private volatile SessionFactory m_sessionFactory;
-    
+
     private final Map<String, Workspace> m_workspaces = new HashMap<String, Workspace>();
     private final Map<String, Component> m_workspaceComponents = new HashMap<String, Component>();
     private Gson m_gson;
+    private String m_repositoryURL;
+    private String m_obrURL;
+    private String m_customerName;
+    private String m_storeRepositoryName;
+    private String m_licenseRepositoryName;
+    private String m_deploymentRepositoryName;
+    private String m_serverUser;
     
     public RESTClientServlet() {
         m_gson = (new GsonBuilder())
@@ -138,9 +162,7 @@ public class RESTClientServlet extends H
                     Component component;
                     synchronized (m_workspaces) {
                         sessionID = "rest-" + m_sessionID++;
-                        // TODO OBR with or without trailing slash?
-                        // TODO this needs to come from configuration
-                        workspace = new Workspace(sessionID, "http://localhost:8080/repository", "http://localhost:8080/obr", "apache", "shop", "gateway", "deployment", "d");
+                        workspace = new Workspace(sessionID, m_repositoryURL, m_obrURL, m_customerName, m_storeRepositoryName, m_licenseRepositoryName, m_deploymentRepositoryName, m_serverUser);
                         m_workspaces.put(sessionID, workspace);
                         component = m_dm.createComponent().setImplementation(workspace);
                         m_workspaceComponents.put(sessionID, component);
@@ -266,7 +288,6 @@ public class RESTClientServlet extends H
                         resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Could not find work area.");
                         return;
                     }
-                    
                 }
             }
         }
@@ -288,14 +309,14 @@ public class RESTClientServlet extends H
      * @param elements the elements
      * @return the URL path
      */
-    private String buildPathFromElements(String... elements) {
+    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"));
+                result.append(URLEncoder.encode(element, "UTF-8").replaceAll("\\+", "%20"));
             }
             catch (UnsupportedEncodingException e) {} // ignored on purpose, any JVM must support UTF-8
         }
@@ -308,7 +329,7 @@ public class RESTClientServlet extends H
      * @param req the request
      * @return the separate path parts
      */
-    private String[] getPathElements(HttpServletRequest req) {
+    String[] getPathElements(HttpServletRequest req) {
         String path = req.getPathInfo();
         if (path == null) {
             return new String[0];
@@ -322,7 +343,7 @@ public class RESTClientServlet extends H
         String[] pathElements = path.split("/");
         try {
             for (int i = 0; i < pathElements.length; i++) {
-                pathElements[i] = URLDecoder.decode(pathElements[i], "UTF-8");
+                pathElements[i] = URLDecoder.decode(pathElements[i].replaceAll("%20", "\\+"), "UTF-8");
             }
         }
         catch (UnsupportedEncodingException e) {}
@@ -365,4 +386,27 @@ public class RESTClientServlet extends H
             }
         }
     }
+
+    public void updated(Dictionary properties) throws ConfigurationException {
+        // Note that configuration changes are only applied to new work areas, started after the
+        // configuration was changed. No attempt is done to "fix" existing work areas, although we
+        // might consider flushing/invalidating them.
+        m_repositoryURL = getProperty(properties, KEY_REPOSITORY_URL, "http://localhost:8080/repository");
+        m_obrURL = getProperty(properties, KEY_OBR_URL, "http://localhost:8080/obr");
+        m_customerName = getProperty(properties, KEY_CUSTOMER_NAME, "apache");
+        m_storeRepositoryName = getProperty(properties, KEY_STORE_REPOSITORY_NAME, "shop");
+        m_licenseRepositoryName = getProperty(properties, KEY_LICENSE_REPOSITORY_NAME, "gateway");
+        m_deploymentRepositoryName = getProperty(properties, KEY_DEPLOYMENT_REPOSITORY_NAME, "deployment");
+        m_serverUser = getProperty(properties, KEY_USER_NAME, "d");
+    }
+    
+    String getProperty(Dictionary properties, String key, String defaultValue) {
+        if (properties != null) {
+            Object value = properties.get(key);
+            if (value != null && value instanceof String) {
+                return (String) value;
+            }
+        }
+        return defaultValue;
+    }
 }

Added: incubator/ace/trunk/ace-client-rest/src/test/java/org/apache/ace/client/rest/RESTClientTest.java
URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-client-rest/src/test/java/org/apache/ace/client/rest/RESTClientTest.java?rev=1152034&view=auto
==============================================================================
--- incubator/ace/trunk/ace-client-rest/src/test/java/org/apache/ace/client/rest/RESTClientTest.java (added)
+++ incubator/ace/trunk/ace-client-rest/src/test/java/org/apache/ace/client/rest/RESTClientTest.java Thu Jul 28 23:11:19 2011
@@ -0,0 +1,50 @@
+/*
+ * 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.apache.ace.client.rest;
+
+import java.util.Properties;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.mockito.Mockito;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+@Test
+public class RESTClientTest {
+    public void testPathTransforms() {
+        String path = "one/two/last%20path";
+        RESTClientServlet s = new RESTClientServlet();
+        HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+        Mockito.when(request.getPathInfo()).thenReturn(path);
+        String[] elements = s.getPathElements(request);
+        Assert.assertEquals(elements[0], "one");
+        Assert.assertEquals(elements[1], "two");
+        Assert.assertEquals(elements[2], "last path");
+        String result = s.buildPathFromElements(elements);
+        Assert.assertEquals(result, path);
+    }
+    
+    public void testPropertyGetter() {
+        RESTClientServlet s = new RESTClientServlet();
+        Assert.assertEquals(s.getProperty(new Properties() {{ put("key", "value"); }},  "key", "notused"), "value");
+        Assert.assertEquals(s.getProperty(new Properties() {{ put("unusedkey", "value"); }},  "key", "default"), "default");
+        Assert.assertEquals(s.getProperty(null,  "key", "default"), "default");
+    }
+}

Propchange: incubator/ace/trunk/ace-client-rest/src/test/java/org/apache/ace/client/rest/RESTClientTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain