Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/
package dev.galasa.artifact.internal;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.jar.Attributes;
import java.util.jar.JarInputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import dev.galasa.artifact.IBundleResources;
import dev.galasa.artifact.ISkeletonProcessor;
import dev.galasa.artifact.SkeletonProcessorException;
import dev.galasa.artifact.TestBundleResourceException;
import dev.galasa.artifact.ISkeletonProcessor.SkeletonType;
import dev.galasa.framework.spi.IFramework;
public class BundleResourcesImpl implements IBundleResources {
private static final String FILE_SEPARATOR = "/";
private final Bundle bundle;
private final ISkeletonProcessor velocitySkeletonProcessor;
private final ISkeletonProcessor ppSkeletonProcessor;
private static final Log logger = LogFactory.getLog(BundleResourcesImpl.class);
public BundleResourcesImpl(Class> owningClass, IFramework framework) {
this.bundle = FrameworkUtil.getBundle(owningClass);
this.velocitySkeletonProcessor = new VelocitySkeletonProcessor(framework);
this.ppSkeletonProcessor = new PlusPlusSkeletonProcessor(framework);
}
public Map retrieveDirectoryContents(String directory) throws TestBundleResourceException {
HashMap directoryContents = new HashMap<>();
List contentPaths = listDirectory(bundle, directory, null);
for (String path : contentPaths) {
directoryContents.put(path, retrieveFile(path));
}
return directoryContents;
}
public InputStream retrieveFile(String filename) throws TestBundleResourceException {
filename = normalisePath(filename);
logger.debug("Searching for artifact: " + filename + " in bundle " + bundle.getSymbolicName());
InputStream is = null;
URL fileURL = bundle.getEntry(filename);
if (fileURL != null) {
String urlString = fileURL.toString();
if (!urlString.contains(filename)) {
throw new TestBundleResourceException(
"The found artifact '" + fileURL.getPath() + "' does not match the case of '" + filename + "'");
}
try {
is = fileURL.openStream();
} catch (IOException e) {
logger.error("IO Error accessing file: " + filename, e);
throw new TestBundleResourceException(e);
}
return is;
}
throw new TestBundleResourceException(
"No such artifact: " + filename + " in bundle " + bundle.getSymbolicName());
}
/**
* This is now horrendously complicated, bear with me
*/
public InputStream retrieveJar(String symbolicName, String version, String directory)
throws TestBundleResourceException {
// Ensure our directory name begins with a file separator so that we always
// search relative to the bundle root
if ((!"".equals(directory)) && (!directory.startsWith(FILE_SEPARATOR))) {
directory = FILE_SEPARATOR + directory;
}
// If we have a version then hopefully the jar is neatly named
// _.jar or -.jar
// and we can do a simple file locate
InputStream jis = null;
if (!"".equals(version)) {
String expectedJarName = directory + symbolicName + "_" + version + ".jar";
String expectedJarNameHyphen = directory + symbolicName + "-" + version + ".jar";
try {
jis = retrieveFile(expectedJarName);
return jis;
} catch (TestBundleResourceException e) {
logger.debug("Failed to find jar under expected name with underscore separator. Will check for hyphen separator");
}
try {
jis = retrieveFile(expectedJarNameHyphen);
return jis;
} catch (TestBundleResourceException e) {
logger.debug("Failed to find jar under expected name with hyphen separator. Will inspect manifests to locate it.", e);
}
}
// If that didn't work we call listJars to find all the jars in the directory
// and bundle given
List foundJars = listDirectory(bundle, directory, "jar");
logger.info("Found Jars:" + foundJars);
// Assuming we have some jars to inspect, lets inspect them
if (!foundJars.isEmpty()) {
String bestJar = null;
String bestVersion = "0.0.0";
for (String jar : foundJars) {
if (!jar.startsWith(FILE_SEPARATOR)) {
jar = FILE_SEPARATOR + jar;
}
// Put the jar entry into a JarInputStream
JarInputStream jaris = null;
try {
jaris = new JarInputStream(bundle.getEntry(jar).openStream());
} catch (IOException e) {
throw new TestBundleResourceException("Unable to open a stream into " + jar, e);
}
// Grab the attributes from the manifest
Attributes attributes = jaris.getManifest().getMainAttributes();
try {
jaris.close();
} catch (IOException e) {
logger.warn("Unable to close jar input stream", e);
}
// Convert JarInputStream to InputStream and store contents as string
InputStream is = retrieveFile(jar);
String jarContents = null;
try {
jarContents = streamAsString(is);
} catch (IOException e) {
throw new TestBundleResourceException("Unable to store InputStream as string", e);
}
Boolean matchingNames = false;
String bundleSymbolicName = attributes.getValue("Bundle-SymbolicName");
//Checks if the symbolic name is listed in the manifest
if (bundleSymbolicName != null) {
if (bundleSymbolicName.equals(symbolicName)) {
matchingNames = true;
}
}
// We don't know exactly what type of jar this is, so we don't know the names of
// the exact contents,
// but if the contents contain the passed symbolic name it's a fair bet it's the
// right name
if (jarContents.contains(symbolicName) || matchingNames == true){
// If we were passed a version let's inspect all the attributes to see if one of
// them looks like a version
boolean hasVersion = false;
for (Entry