/*
 * Copyright 2006 Wyona
 *
 *  Licensed 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.wyona.org/licenses/APACHE-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.wyona.yanel.impl.map;

import org.wyona.yanel.core.Path;
import org.wyona.yanel.core.map.Map;
import org.wyona.yanel.core.map.Realm;
import org.wyona.yanel.core.map.RealmConfiguration;

import org.wyona.yarep.core.NoSuchNodeException;
import org.wyona.yarep.core.Repository;
import org.wyona.yarep.core.RepositoryFactory;
import org.wyona.yarep.util.RepoPath;
import org.wyona.yarep.util.YarepUtil;

import org.apache.log4j.Category;

/**
 *
 */
public class MapImpl implements Map {

    private static Category log = Category.getInstance(MapImpl.class);

    RepositoryFactory repoFactory;

    /**
     *
     */
    public MapImpl() {
        try {
            // NOTE: Separate ResourceTypeIdentifier mapping from whatever else is using yarep ...
            repoFactory = new RepositoryFactory("yanel-rti-yarep.properties");
            //repoFactory = new RepositoryFactory();

            RealmConfiguration realmConfig = new RealmConfiguration();  
        } catch(Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     *
     */
    public String getUUID() {
        return "sugus";
    }

    /**
     * See James Clark's explanation on namespaces: http://www.jclark.com/xml/xmlns.htm
     */
    public String getResourceTypeIdentifier(Path path) {
        log.debug("Original path: " + path);
        try {
            RepoPath rp = new YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()),repoFactory);
            Repository repo = rp.getRepo();
            log.debug("Repo Name: " + repo.getName());
            log.debug("New path: " + rp.getPath());

            java.io.BufferedReader br = new java.io.BufferedReader(repo.getReader(new org.wyona.yarep.core.Path(new Path(rp.getPath().toString()).getRTIPath().toString())));
            return br.readLine();
        } catch(NoSuchNodeException e) {
            log.warn(e.getMessage());
            log.warn("TODO: Implement chain of responsibility ...");
            return "<{http://www.wyona.org/yanel/resource/1.0}file/>";
        } catch(Exception e) {
            log.error(e.getMessage(), e);
            log.warn("No resource type identifier for path: " + path);
            return null;
        }
    }

    /**
     *
     */
    public Realm[] getRealms() {
        String[] repoIds = repoFactory.getRepositoryIDs();
        Repository root = repoFactory.firstRepository();
        Realm[] realms = new Realm[repoIds.length];
        for (int i = 0;i < realms.length; i++) {
            Repository repo = repoFactory.newRepository(repoIds[i]);
            if (repoIds[i].equals(root.getID())) {
                // ROOT realm
                realms[i] = new Realm(repo.getName(), repo.getID(), new Path("/"));
            } else {
                realms[i] = new Realm(repo.getName(), repo.getID(), new Path("/" + repoIds[i] + "/"));
            }
        }
        return realms;
    }

    /**
     *
     */
    public Realm getRealm(Path path) {
        RepoPath rp = new YarepUtil().getRepositoryPath(new org.wyona.yarep.core.Path(path.toString()), repoFactory);
        Repository root = repoFactory.firstRepository();
        if (rp.getRepo().getID().equals(root.getID())) {
            return new Realm(rp.getRepo().getName(), rp.getRepo().getID(), new Path("/"));
        } else {
            return new Realm(rp.getRepo().getName(), rp.getRepo().getID(), new Path("/" + rp.getRepo().getID() + "/"));
        }
    }
}

@@ -1201,16 +1201,52 @@
 Factory;
+%0A    RealmConfiguration realmConfig;
 %0A%0A    /*
@@ -1543,35 +1543,16 @@
         
-RealmConfiguration 
 realmCon
@@ -1582,18 +1582,16 @@
 ation();
-  
 %0A       
@@ -3720,32 +3720,60 @@
 , repoFactory);%0A
+        Realm realm = null;%0A
         Reposito
@@ -3876,36 +3876,37 @@
 %7B%0A            re
-turn
+alm =
  new Realm(rp.ge
@@ -3993,20 +3993,21 @@
       re
-turn
+alm =
  new Rea
@@ -4105,21 +4105,143 @@
 );%0A        %7D
+%0A%0A        realm.setProxy(realmConfig.proxyHostName, realmConfig.proxyPort, realmConfig.proxyPrefix);%0A        return realm;
 %0A    %7D%0A%7D%0A

set reverse proxy