Skip to content

Commit 3cd3102

Browse files
committed
Fixing ResourceStep
1 parent fec1f4e commit 3cd3102

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/main/java/org/jenkinsci/plugins/workflow/libs/LibraryAdder.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
package org.jenkinsci.plugins.workflow.libs;
2626

27-
import hudson.AbortException;
2827
import hudson.Extension;
2928
import hudson.ExtensionList;
3029
import hudson.FilePath;
@@ -50,6 +49,7 @@
5049
import java.util.logging.Logger;
5150
import edu.umd.cs.findbugs.annotations.CheckForNull;
5251
import edu.umd.cs.findbugs.annotations.NonNull;
52+
import java.util.jar.JarEntry;
5353
import java.util.jar.JarFile;
5454
import java.util.regex.Matcher;
5555
import java.util.regex.Pattern;
@@ -299,32 +299,27 @@ static URL retrieve(@NonNull LibraryRecord record, @NonNull LibraryRetriever ret
299299
Run<?,?> run = (Run) executable;
300300
LibrariesAction action = run.getAction(LibrariesAction.class);
301301
if (action != null) {
302-
// TODO handle *.jar
303302
FilePath libs = new FilePath(run.getRootDir()).child("libs");
304303
for (LibraryRecord library : action.getLibraries()) {
305-
FilePath libResources = libs.child(library.getDirectoryName() + "/resources/");
306-
FilePath f = libResources.child(name);
307-
if (!new File(f.getRemote()).getCanonicalFile().toPath().startsWith(new File(libResources.getRemote()).getCanonicalPath())) {
308-
throw new AbortException(name + " references a file that is not contained within the library: " + library.name);
309-
} else if (f.exists()) {
310-
resources.put(library.name, readResource(f, encoding));
304+
FilePath libJar = libs.child(library.getDirectoryName() + ".jar");
305+
try (JarFile jf = new JarFile(libJar.getRemote())) {
306+
JarEntry je = jf.getJarEntry("resources/" + name);
307+
if (je != null) {
308+
try (InputStream in = jf.getInputStream(je)) {
309+
if ("Base64".equals(encoding)) {
310+
resources.put(library.name, Base64.getEncoder().encodeToString(IOUtils.toByteArray(in)));
311+
} else {
312+
resources.put(library.name, IOUtils.toString(in, encoding)); // The platform default is used if encoding is null.
313+
}
314+
}
315+
}
311316
}
312317
}
313318
}
314319
}
315320
return resources;
316321
}
317322

318-
private static String readResource(FilePath file, @CheckForNull String encoding) throws IOException, InterruptedException {
319-
try (InputStream in = file.read()) {
320-
if ("Base64".equals(encoding)) {
321-
return Base64.getEncoder().encodeToString(IOUtils.toByteArray(in));
322-
} else {
323-
return IOUtils.toString(in, encoding); // The platform default is used if encoding is null.
324-
}
325-
}
326-
}
327-
328323
@Extension public static class GlobalVars extends GlobalVariableSet {
329324

330325
@Override public Collection<GlobalVariable> forRun(Run<?,?> run) {

0 commit comments

Comments
 (0)