/*
 * 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.jackrabbit.jcr2dav;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.RepositoryFactory;

import org.apache.jackrabbit.commons.GenericRepositoryFactory;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.jcr2spi.RepositoryImpl;
import org.apache.jackrabbit.jcr2spi.Jcr2spiRepositoryFactory;
import org.apache.jackrabbit.spi.RepositoryServiceFactory;
import org.apache.jackrabbit.spi2dav.Spi2davRepositoryServiceFactory;
import org.apache.jackrabbit.spi2davex.Spi2davexRepositoryServiceFactory;

/**
 * Repository factory for JCR to WebDAV connections. This factory supports
 * three main configuration parameters:
 * <dl>
 *   <dt>{@link GenericRepositoryFactory#URI org.apache.jackrabbit.repository.uri}</dt>
 *   <dd>
 *     If this parameter contains a valid http or https URI, then an spi2davex
 *     connection to that URI is returned.
 *   </dd>
 *   <dt>{@link Spi2davRepositoryServiceFactory#PARAM_REPOSITORY_URI org.apache.jackrabbit.spi2dav.uri}</dt>
 *   <dd>
 *     If this parameter is specified, then an spi2dav connection
 *     to that URI is returned.
 *   </dd>
 *   <dt>{@link Spi2davexRepositoryServiceFactory#PARAM_REPOSITORY_URI org.apache.jackrabbit.spi2davex.uri}</dt>
 *   <dd>
 *     If this parameter is specified, then an spi2davex connection
 *     to that URI is returned.
 *   </dd>
 * </dl>
 *
 * @since Apache Jackrabbit 2.0
 */
@SuppressWarnings("unchecked")
public class Jcr2davRepositoryFactory implements RepositoryFactory {

    private static final String DAV_URI =
        Spi2davRepositoryServiceFactory.PARAM_REPOSITORY_URI;

    private static final String DAVEX_URI =
        Spi2davexRepositoryServiceFactory.PARAM_REPOSITORY_URI;

    public Repository getRepository(Map parameters) throws RepositoryException {
        if (parameters == null) {
            return null;
        } else if (parameters.containsKey(DAV_URI)) {
            return getRepository(
                    new Spi2davRepositoryServiceFactory(), parameters);
        } else if (parameters.containsKey(DAVEX_URI)) {
            return getRepository(
                    new Spi2davexRepositoryServiceFactory(), parameters);
        } else if (parameters.containsKey(JcrUtils.REPOSITORY_URI)) {
            Map copy = new HashMap(parameters);
            Object parameter = copy.remove(JcrUtils.REPOSITORY_URI);
            try {
                URI uri = new URI(parameter.toString().trim());
                String scheme = uri.getScheme();
                // TODO: Check whether this is a valid dav or davex URI
                // TODO: Support tags like <link rel="jackrabbit-spi2dav" ...>
                if ("http".equalsIgnoreCase(scheme)
                        || "https".equalsIgnoreCase(scheme)) {
                    copy.put(DAVEX_URI, parameter);
                    return getRepository(
                            new Spi2davexRepositoryServiceFactory(), copy);
                } else {
                    return null;
                }
            } catch (URISyntaxException e) {
                return null;
            }
        } else {
            return null;
        }
    }

    private Repository getRepository(
            RepositoryServiceFactory factory, Map parameters)
            throws RepositoryException {
        return RepositoryImpl.create(
                new Jcr2spiRepositoryFactory.RepositoryConfigImpl(
                        factory, parameters));
    }

}

@@ -1050,71 +1050,8 @@
 y;%0A%0A
-import org.apache.jackrabbit.commons.GenericRepositoryFactory;%0A
 impo
@@ -1126,24 +1126,31 @@
 bit.jcr2spi.
+Jcr2spi
 RepositoryIm
@@ -1147,20 +1147,23 @@
 pository
-Impl
+Factory
 ;%0Aimport
@@ -1185,39 +1185,32 @@
 krabbit.jcr2spi.
-Jcr2spi
 RepositoryFactor
@@ -1195,39 +1195,36 @@
 r2spi.Repository
-Factory
+Impl
 ;%0Aimport org.apa
@@ -4170,32 +4170,50 @@
 toryException %7B%0A
+        try %7B%0A    
         return R
@@ -4234,16 +4234,20 @@
 create(%0A
+    
         
@@ -4329,16 +4329,20 @@
         
+    
 factory,
@@ -4352,21 +4352,292 @@
 rameters));%0A
+        %7D catch (RepositoryException e) %7B%0A            // Unable to connect to the specified repository.%0A            // Most likely the server is either not running or%0A            // the given URI does not point to a valid davex server.%0A            return null;%0A        %7D%0A
     %7D%0A%0A%7D%0A

Implement RepositoryFactory in jcr2dav