package org.ops4j.pax.web.deployer.internal;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.felix.fileinstall.ArtifactUrlTransformer;

/**
 * An Apache Felix FileInstall transform for WAR files.
 *
 * @author Alin Dreghiciu
 */
public class WarDeployer
    implements ArtifactUrlTransformer
{

    /**
     * Logger.
     */
    private static final Log LOG = LogFactory.getLog( WarDeployer.class );

    public boolean canHandle( final File artifact )
    {
        // TODO maybe better would be to open the file and look for web.xml
        if( !artifact.isFile() || !artifact.getName().endsWith( "war" ) )
        {
            return false;
        }

        try
        {
            new URL( "webbundle", null, artifact.toURL().toExternalForm() );
        }
        catch( MalformedURLException e )
        {
            LOG.warn(
                String.format(
                    "File %s could nto be transformed. Most probably that Pax URL WAR handler is not installed",
                    artifact.getAbsolutePath()
                )
            );
            return false;
        }

        return true;
    }

    public URL transform( final URL artifact )
        throws Exception
    {
        final String path = artifact.getPath();
        if( path != null )
        {
            final int idx = path.lastIndexOf( "/" );
            if( idx > 0 )
            {
                final String[] name = path.substring( idx + 1 ).split( "\\." );
                final StringBuilder url = new StringBuilder();
                url.append( artifact.toExternalForm() );
                if( artifact.toExternalForm().contains( "?" ) )
                {
                    url.append( "&" );
                }
                else
                {
                    url.append( "?" );
                }
                url.append( "Webapp-Context=" ).append( name[0] );
                url.append( "&" );
                url.append( "Bundle-SymbolicName=" ).append( name[0] );

                return new URL( "webbundle", null, url.toString() );
            }
        }
        return new URL( "webbundle", null, artifact.toExternalForm() );
    }

}

@@ -120,16 +120,142 @@
 et.URL;%0A
+import java.util.jar.Attributes;%0Aimport java.util.jar.JarEntry;%0Aimport java.util.jar.JarFile;%0Aimport java.util.jar.Manifest;%0A%0A
 import o
@@ -666,256 +666,692 @@
     
-public boolean canHandle( final File artifact )%0A    %7B%0A        // TODO maybe better would be to open the file and look for web.xml%0A        if( !artifact.isFile() %7C%7C !artifact.getName().endsWith( %22war%22 ) )%0A        %7B%0A            return false;%0A        
+/**%0A     * Standard PATH separator %0A     */%0A%09private static final String PATH_SEPERATOR = %22/%22;%0A%0A%0A    public boolean canHandle( final File artifact )%0A    %7B%0A    %09try %7B%0A%09%09%09JarFile jar = new JarFile(artifact);%0A%09%09%09JarEntry entry = jar.getJarEntry(%22WEB-INF/web.xml%22);%0A%09%09%09// Only handle WAR artifacts%0A%09%09%09if (entry == null) %7B%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%09%09%09// Only handle non OSGi bundles%0A%09%09%09Manifest m = jar.getManifest();%0A%09%09%09if (m!= null && m.getMainAttributes().getValue(%0A%09%09%09%09%09new Attributes.Name(%22Bundle-SymbolicName%22)) != null%0A%09%09%09%09%09&& m.getMainAttributes().getValue(%0A%09%09%09%09%09%09%09new Attributes.Name(%22Bundle-Version%22)) != null) %7B%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09return false;%0A%09%09
 %7D%0A%0A 
@@ -1958,69 +1958,443 @@
     
-if( path != null )%0A        %7B%0A            final int idx = p
+final String protocol = artifact.getProtocol();%0A        if( path != null )%0A        %7B%0A        %09int idx = -1;%0A        %09// match the last slash to retrieve the name of the archive%0A    %09%09if (%22jardir%22.equalsIgnoreCase(protocol)) %7B%0A    %09%09    // just to make sure this works on all kinds of windows%0A    %09%09    File fileInstance = new File(path);%0A    %09%09    // with a jardir this is system specific%0A    %09%09    idx = fileInstance.getAbsoluteP
 ath
+()
 .las
@@ -2406,15 +2406,247 @@
 xOf(
- %22/%22 );
+File.separator);%0A    %09%09%7D else %7B%0A    %09%09    // a standard file is not system specific, this is always a standardized URL path%0A    %09%09%09idx = path.lastIndexOf(PATH_SEPERATOR);%0A    %09%09%7D%0A    %09%09// match the suffix so we get rid of it for displaying
 %0A   

fix for PAXWEB-212