/*
 * #%L
 * Wisdom-Framework
 * %%
 * Copyright (C) 2013 - 2014 Wisdom Framework
 * %%
 * 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.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.
 * #L%
 */
package org.wisdom.resources;

import org.osgi.framework.Bundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wisdom.api.configuration.ApplicationConfiguration;
import org.wisdom.api.crypto.Crypto;
import org.wisdom.api.http.Context;
import org.wisdom.api.http.Result;
import org.wisdom.api.http.Results;

import java.io.File;
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;

/**
 * Represents and serves a web jar library embedded in a bundle.
 */
class BundleWebJarLib extends WebJarLib {

    public final Bundle bundle;

    public static final Logger LOGGER = LoggerFactory.getLogger(BundleWebJarLib.class);

    private Map<String, URL> index = new TreeMap<>();

    BundleWebJarLib(String name, String version, Bundle bundle) {
        super(name, version);
        this.bundle = bundle;
        index();
    }

    @Override
    public Collection<String> names() {
        return index.keySet();
    }

    public Result get(String path, Context context, ApplicationConfiguration configuration, Crypto crypto) {
        URL url = index.get(path);
        return CacheUtils.fromBundle(bundle, url, context, configuration, crypto);
    }

    public Collection<String> resources() {
        return index.keySet();
    }

    private void index() {
        LOGGER.debug("Indexing files for WebJar library {}-{} contained in bundle {} [{}]", name, version,
                bundle.getSymbolicName(), bundle.getBundleId());

        Enumeration<URL> urls = bundle.findEntries(WebJarController.WEBJAR_LOCATION, "*", true);

        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            String path = url.getPath().substring(WebJarController.WEBJAR_LOCATION.length() + 1);
            index.put(path, url);
        }
    }


}

@@ -946,65 +946,8 @@
 lt;%0A
-import org.wisdom.api.http.Results;%0A%0Aimport java.io.File;
 %0Aimp
@@ -2217,24 +2217,109 @@
 *%22, true);%0A%0A
+        String root = %22/%22 + WebJarController.WEBJAR_LOCATION + name + %22/%22 + version;%0A
         whil
@@ -2385,24 +2385,120 @@
 tElement();%0A
+            if (url.getPath().startsWith(root) && url.getPath().length() %3E root.length()) %7B%0A    
             
@@ -2539,40 +2539,12 @@
 ing(
-WebJarController.WEBJAR_LOCATION
+root
 .len
@@ -2555,16 +2555,20 @@
 ) + 1);%0A
+    
         
@@ -2593,16 +2593,30 @@
 , url);%0A
+            %7D%0A
         
@@ -2624,11 +2624,129 @@
 %0A    %7D%0A%0A
+    @Override%0A    public String toString() %7B%0A        return super.toString() + %22%5B%22 + bundle.getBundleId() + %22%5D%22;%0A    %7D
 %0A%7D%0A

Fix resource path computation (#136)