#1520 reuse code from JarResource to extract packed file

Signed-off-by: olivier lamy <olamy@webtide.com>
This commit is contained in:
olivier lamy 2017-05-18 16:04:41 +10:00
parent 8b94f2f275
commit 7d600b0c54
2 changed files with 14 additions and 33 deletions

View File

@ -23,16 +23,16 @@ import org.eclipse.jetty.util.PathWatcher.PathWatchEvent;
import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.JarResource;
import org.eclipse.jetty.util.resource.PathResource; import org.eclipse.jetty.util.resource.PathResource;
import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.security.Credential; import org.eclipse.jetty.util.security.Credential;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -40,8 +40,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/** /**
* PropertyUserStore * PropertyUserStore
@ -148,33 +146,16 @@ public class PropertyUserStore extends UserStore implements PathWatcher.Listener
throws IOException throws IOException
{ {
int fileIndex = configFile.indexOf( "!" ); int fileIndex = configFile.indexOf( "!" );
String filePath = configFile.substring( JAR_FILE.length(), fileIndex );
String entryPath = configFile.substring( fileIndex + 1, configFile.length() ); String entryPath = configFile.substring( fileIndex + 1, configFile.length() );
try (FileInputStream fileInputStream = new FileInputStream( new File( filePath ) )) Path tmpDirectory = Files.createTempDirectory( "users_store" );
{ Path extractedPath = Paths.get(tmpDirectory.toString(), entryPath);
try (ZipInputStream zin = new ZipInputStream( fileInputStream )) // delete if exists as copyTo do not overwrite
{ Files.deleteIfExists( extractedPath );
for ( ZipEntry e; ( e = zin.getNextEntry() ) != null; ) // delete on shutdown
{ extractedPath.toFile().deleteOnExit();
if ( e.getName().equals( entryPath ) ) JarResource.newResource( configFile ).copyTo( tmpDirectory.toFile() );
{ return extractedPath;
Path extractedPath = Files.createTempFile( "users_store", ".tmp" );
byte[] buffer = new byte[1024];
int bytesRead;
try (OutputStream textOutputStream = Files.newOutputStream( extractedPath ))
{
while ( ( bytesRead = zin.read( buffer ) ) != -1 )
{
textOutputStream.write( buffer, 0, bytesRead );
}
}
return extractedPath;
}
}
}
}
throw new RuntimeException( "cannot find file from url " + configFile );
} }
/** /**

View File

@ -115,7 +115,7 @@ public class PropertyUserStoreTest
File users = dir.resolve( "users.txt" ).toFile(); File users = dir.resolve( "users.txt" ).toFile();
writeUser( users ); writeUser( users );
File usersJar = dir.resolve( "users.jar" ).toFile(); File usersJar = dir.resolve( "users.jar" ).toFile();
String entryPath = "/mountain_goat/pale_ale.txt"; String entryPath = "mountain_goat/pale_ale.txt";
try (FileInputStream fileInputStream = new FileInputStream( users )) try (FileInputStream fileInputStream = new FileInputStream( users ))
{ {
try (OutputStream outputStream = new FileOutputStream( usersJar )) try (OutputStream outputStream = new FileOutputStream( usersJar ))
@ -123,7 +123,7 @@ public class PropertyUserStoreTest
try (JarOutputStream jarOutputStream = new JarOutputStream( outputStream )) try (JarOutputStream jarOutputStream = new JarOutputStream( outputStream ))
{ {
// add fake entry // add fake entry
jarOutputStream.putNextEntry( new JarEntry( "/foo/wine" ) ); jarOutputStream.putNextEntry( new JarEntry( "foo/wine" ) );
JarEntry jarEntry = new JarEntry( entryPath ); JarEntry jarEntry = new JarEntry( entryPath );
jarOutputStream.putNextEntry( jarEntry ); jarOutputStream.putNextEntry( jarEntry );
@ -134,12 +134,12 @@ public class PropertyUserStoreTest
jarOutputStream.write( buffer, 0, bytesRead ); jarOutputStream.write( buffer, 0, bytesRead );
} }
// add fake entry // add fake entry
jarOutputStream.putNextEntry( new JarEntry( "/foo/cheese" ) ); jarOutputStream.putNextEntry( new JarEntry( "foo/cheese" ) );
} }
} }
} }
return "jar:file:" + usersJar.getCanonicalPath() + "!" + entryPath; return "jar:file:" + usersJar.getCanonicalPath() + "!/" + entryPath;
} }
private void writeUser(File usersFile) private void writeUser(File usersFile)