You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2013/03/27 12:46:22 UTC

svn commit: r1461539 - in /syncope/trunk/core/src: main/java/org/apache/syncope/core/persistence/validation/entity/ main/java/org/apache/syncope/core/util/ test/java/org/apache/syncope/core/persistence/dao/ test/java/org/apache/syncope/core/rest/

Author: ilgrosso
Date: Wed Mar 27 11:46:21 2013
New Revision: 1461539

URL: http://svn.apache.org/r1461539
Log:
[SYNCOPE-137] Better handling of ConnId file: locations

Added:
    syncope/trunk/core/src/main/java/org/apache/syncope/core/util/URIUtil.java   (with props)
Modified:
    syncope/trunk/core/src/main/java/org/apache/syncope/core/persistence/validation/entity/ConnInstanceValidator.java
    syncope/trunk/core/src/main/java/org/apache/syncope/core/util/ConnIdBundleManager.java
    syncope/trunk/core/src/test/java/org/apache/syncope/core/persistence/dao/ConnInstanceTest.java
    syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/ConnInstanceTestITCase.java

Modified: syncope/trunk/core/src/main/java/org/apache/syncope/core/persistence/validation/entity/ConnInstanceValidator.java
URL: http://svn.apache.org/viewvc/syncope/trunk/core/src/main/java/org/apache/syncope/core/persistence/validation/entity/ConnInstanceValidator.java?rev=1461539&r1=1461538&r2=1461539&view=diff
==============================================================================
--- syncope/trunk/core/src/main/java/org/apache/syncope/core/persistence/validation/entity/ConnInstanceValidator.java (original)
+++ syncope/trunk/core/src/main/java/org/apache/syncope/core/persistence/validation/entity/ConnInstanceValidator.java Wed Mar 27 11:46:21 2013
@@ -18,13 +18,11 @@
  */
 package org.apache.syncope.core.persistence.validation.entity;
 
-import java.net.URI;
-import java.net.URISyntaxException;
 import javax.validation.ConstraintValidator;
 import javax.validation.ConstraintValidatorContext;
-import org.apache.commons.lang.ArrayUtils;
 import org.apache.syncope.common.types.EntityViolationType;
 import org.apache.syncope.core.persistence.beans.ConnInstance;
+import org.apache.syncope.core.util.URIUtil;
 
 public class ConnInstanceValidator extends AbstractValidator implements
         ConstraintValidator<ConnInstanceCheck, ConnInstance> {
@@ -39,12 +37,13 @@ public class ConnInstanceValidator exten
     public boolean isValid(final ConnInstance connInstance, final ConstraintValidatorContext context) {
         boolean isValid = true;
         try {
-            URI location = new URI(connInstance.getLocation());
-            isValid = ArrayUtils.contains(ALLOWED_SCHEMES, location.getScheme());
-        } catch (URISyntaxException e) {
+            URIUtil.buildForConnId(connInstance.getLocation());
+        } catch (Exception e) {
+            LOG.error("While validating {}", connInstance.getLocation(), e);
+
             context.disableDefaultConstraintViolation();
             context.buildConstraintViolationWithTemplate(EntityViolationType.InvalidConnInstanceLocation.toString())
-                    .addNode(" is not a valid URI for file:// or connid(s):// schemes").addConstraintViolation();
+                    .addNode("location").addConstraintViolation();
             isValid = false;
         }
 

Modified: syncope/trunk/core/src/main/java/org/apache/syncope/core/util/ConnIdBundleManager.java
URL: http://svn.apache.org/viewvc/syncope/trunk/core/src/main/java/org/apache/syncope/core/util/ConnIdBundleManager.java?rev=1461539&r1=1461538&r2=1461539&view=diff
==============================================================================
--- syncope/trunk/core/src/main/java/org/apache/syncope/core/util/ConnIdBundleManager.java (original)
+++ syncope/trunk/core/src/main/java/org/apache/syncope/core/util/ConnIdBundleManager.java Wed Mar 27 11:46:21 2013
@@ -22,7 +22,6 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
-import java.net.URISyntaxException;
 import java.net.URL;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
@@ -93,9 +92,9 @@ public final class ConnIdBundleManager {
         List<URI> locations = new ArrayList<URI>();
         for (String location : stringLocations) {
             try {
-                locations.add(new URI(location.trim()));
+                locations.add(URIUtil.buildForConnId(location));
                 LOG.info("Valid ConnId location: {}", location.trim());
-            } catch (URISyntaxException e) {
+            } catch (Exception e) {
                 LOG.error("Invalid ConnId location: {}", location.trim(), e);
             }
         }
@@ -236,8 +235,8 @@ public final class ConnIdBundleManager {
         // check ConnIdLocation
         URI uriLocation = null;
         try {
-            uriLocation = new URI(location);
-        } catch (URISyntaxException e) {
+            uriLocation = URIUtil.buildForConnId(location);
+        } catch (Exception e) {
             throw new IllegalArgumentException("Invalid ConnId location " + location, e);
         }
 

Added: syncope/trunk/core/src/main/java/org/apache/syncope/core/util/URIUtil.java
URL: http://svn.apache.org/viewvc/syncope/trunk/core/src/main/java/org/apache/syncope/core/util/URIUtil.java?rev=1461539&view=auto
==============================================================================
--- syncope/trunk/core/src/main/java/org/apache/syncope/core/util/URIUtil.java (added)
+++ syncope/trunk/core/src/main/java/org/apache/syncope/core/util/URIUtil.java Wed Mar 27 11:46:21 2013
@@ -0,0 +1,61 @@
+/*
+ * 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.syncope.core.util;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+public final class URIUtil {
+
+    private URIUtil() {
+        // empty constructor for static utility class
+    }
+
+    /**
+     * Build a valid URI out of the given location.
+     * Only "file", "connid" and "connids" schemes are allowed.
+     * For "file", invalid characters are handled via intermediate transformation into URL.
+     *
+     * @param location the candidate location for URI
+     * @return valid URI for the given location
+     * @throws MalformedURLException if the intermediate URL is not valid
+     * @throws URISyntaxException if the given location does not correspond to a valid URI
+     */
+    public static URI buildForConnId(final String location) throws MalformedURLException, URISyntaxException {
+        final String candidate = location.trim();
+
+        if (!location.startsWith("file:")
+                && !location.startsWith("connid:") && !location.startsWith("connids:")) {
+
+            throw new IllegalArgumentException(candidate + " is not a valid URI for file or connid(s) schemes");
+        }
+
+        URI uri;
+        if (location.startsWith("file:")) {
+            uri = new File(new URL(candidate).getFile()).getAbsoluteFile().toURI();
+        } else {
+            uri = new URI(candidate);
+        }
+
+        return uri;
+    }
+}

Propchange: syncope/trunk/core/src/main/java/org/apache/syncope/core/util/URIUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: syncope/trunk/core/src/main/java/org/apache/syncope/core/util/URIUtil.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: syncope/trunk/core/src/main/java/org/apache/syncope/core/util/URIUtil.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: syncope/trunk/core/src/test/java/org/apache/syncope/core/persistence/dao/ConnInstanceTest.java
URL: http://svn.apache.org/viewvc/syncope/trunk/core/src/test/java/org/apache/syncope/core/persistence/dao/ConnInstanceTest.java?rev=1461539&r1=1461538&r2=1461539&view=diff
==============================================================================
--- syncope/trunk/core/src/test/java/org/apache/syncope/core/persistence/dao/ConnInstanceTest.java (original)
+++ syncope/trunk/core/src/test/java/org/apache/syncope/core/persistence/dao/ConnInstanceTest.java Wed Mar 27 11:46:21 2013
@@ -63,7 +63,7 @@ public class ConnInstanceTest extends Ab
     public void save() throws ClassNotFoundException {
         ConnInstance connInstance = new ConnInstance();
 
-        connInstance.setLocation(new File("java.io.tmpdir").toURI().toString());
+        connInstance.setLocation(new File(System.getProperty("java.io.tmpdir")).toURI().toString());
 
         // set connector version
         connInstance.setVersion("1.0");

Modified: syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/ConnInstanceTestITCase.java
URL: http://svn.apache.org/viewvc/syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/ConnInstanceTestITCase.java?rev=1461539&r1=1461538&r2=1461539&view=diff
==============================================================================
--- syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/ConnInstanceTestITCase.java (original)
+++ syncope/trunk/core/src/test/java/org/apache/syncope/core/rest/ConnInstanceTestITCase.java Wed Mar 27 11:46:21 2013
@@ -98,7 +98,7 @@ public class ConnInstanceTestITCase exte
     public void create() {
         ConnInstanceTO connectorTO = new ConnInstanceTO();
 
-        connectorTO.setLocation(ConnIdBundleManager.getConnManagers().keySet().iterator().next().toString());
+        connectorTO.setLocation(connectorService.read(100L).getLocation());
 
         // set connector version
         connectorTO.setVersion(connidSoapVersion);
@@ -360,7 +360,6 @@ public class ConnInstanceTestITCase exte
     @Test
     public void read() {
         ConnInstanceTO connectorInstanceTO = connectorService.read(100L);
-
         assertNotNull(connectorInstanceTO);
     }
 
@@ -424,7 +423,7 @@ public class ConnInstanceTestITCase exte
     @Test
     public void validate() {
         ConnInstanceTO connectorTO = new ConnInstanceTO();
-        
+
         for (URI location : ConnIdBundleManager.getConnManagers().keySet()) {
             if (!location.getScheme().equals("file")) {
                 connectorTO.setLocation(location.toString());
@@ -557,7 +556,7 @@ public class ConnInstanceTestITCase exte
         // ----------------------------------------
         ConnInstanceTO connectorTO = new ConnInstanceTO();
 
-        connectorTO.setLocation(ConnIdBundleManager.getConnManagers().keySet().iterator().next().toString());
+        connectorTO.setLocation(connectorService.read(100L).getLocation());
 
         // set connector version
         connectorTO.setVersion(connidSoapVersion);
@@ -600,7 +599,6 @@ public class ConnInstanceTestITCase exte
         connectorTO.setConfiguration(conf);
 
         try {
-
             assertFalse(connectorService.check(connectorTO));
 
             Response response = connectorService.create(connectorTO);