diff --git a/.gitignore b/.gitignore index e045012..af40f4e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/.settings +.settings /kbs /kbs_prepare /update diff --git a/juseppe-cli/.settings/org.eclipse.core.resources.prefs b/juseppe-cli/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..474bee1 --- /dev/null +++ b/juseppe-cli/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=utf-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=utf-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/juseppe-cli/.settings/org.eclipse.jdt.core.prefs b/juseppe-cli/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..f494d1d --- /dev/null +++ b/juseppe-cli/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/juseppe-cli/.settings/org.eclipse.m2e.core.prefs b/juseppe-cli/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..14b697b --- /dev/null +++ b/juseppe-cli/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/juseppe-core/.settings/org.eclipse.core.resources.prefs b/juseppe-core/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..9defc52 --- /dev/null +++ b/juseppe-core/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +encoding//src/main/java=utf-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=utf-8 +encoding//src/test/resources=UTF-8 +encoding//target/generated-sources/java-gen=utf-8 +encoding/=UTF-8 diff --git a/juseppe-core/.settings/org.eclipse.jdt.core.prefs b/juseppe-core/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..f494d1d --- /dev/null +++ b/juseppe-core/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/juseppe-core/.settings/org.eclipse.m2e.core.prefs b/juseppe-core/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..14b697b --- /dev/null +++ b/juseppe-core/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/juseppe-core/src/main/java/ru/lanwen/jenkins/juseppe/gen/HPI.java b/juseppe-core/src/main/java/ru/lanwen/jenkins/juseppe/gen/HPI.java index fdbbf64..b1122c9 100644 --- a/juseppe-core/src/main/java/ru/lanwen/jenkins/juseppe/gen/HPI.java +++ b/juseppe-core/src/main/java/ru/lanwen/jenkins/juseppe/gen/HPI.java @@ -1,16 +1,15 @@ package ru.lanwen.jenkins.juseppe.gen; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.FilenameUtils; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import ru.lanwen.jenkins.juseppe.beans.Dependency; -import ru.lanwen.jenkins.juseppe.beans.Developer; -import ru.lanwen.jenkins.juseppe.beans.Plugin; +import static org.apache.commons.lang3.StringUtils.defaultIfBlank; +import static org.apache.commons.lang3.StringUtils.isBlank; +import static org.apache.commons.lang3.StringUtils.substringBefore; +import static org.apache.commons.lang3.StringUtils.trimToEmpty; +import static org.apache.commons.lang3.Validate.notBlank; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.security.MessageDigest; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; @@ -22,11 +21,17 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import static org.apache.commons.lang3.StringUtils.defaultIfBlank; -import static org.apache.commons.lang3.StringUtils.isBlank; -import static org.apache.commons.lang3.StringUtils.substringBefore; -import static org.apache.commons.lang3.StringUtils.trimToEmpty; -import static org.apache.commons.lang3.Validate.notBlank; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import ru.lanwen.jenkins.juseppe.beans.Dependency; +import ru.lanwen.jenkins.juseppe.beans.Developer; +import ru.lanwen.jenkins.juseppe.beans.Plugin; /** * HPI Class for extracting HPI information from hpi file. @@ -52,34 +57,34 @@ public class HPI { public static final String MANIFEST_PATH = "META-INF/MANIFEST.MF"; + public static Plugin loadHPI(final File file) throws IOException { + final JarFile jarFile = new JarFile(file); + final long timestamp = jarFile.getEntry(MANIFEST_PATH).getTime(); - public static Plugin loadHPI(File file) throws IOException { - JarFile jarFile = new JarFile(file); - long timestamp = jarFile.getEntry(MANIFEST_PATH).getTime(); - - Plugin hpi = HPI.from(jarFile.getManifest().getMainAttributes(), timestamp); + final Plugin hpi = HPI.from(file, jarFile.getManifest().getMainAttributes(), timestamp); - String wiki = getWiki(file); + final String wiki = getWiki(file); if (StringUtils.isNotBlank(wiki)) { hpi.setWiki(wiki); } + jarFile.close(); + return hpi.withExcerpt(getExcerpt(file)); } - private static Plugin from(Attributes attributes, long timestamp) throws IOException { + private static Plugin from(final File file, final Attributes attributes, final long timestamp) throws IOException { + final Digests digests = digests(file); + return new Plugin() .withReleaseTimestamp(releaseTimestampDateFormat().format(timestamp)) .withBuildDate(buildDateTimeFormat().format(timestamp)) .withName( - notBlank(attributes.getValue(PLUGIN_NAME), "Plugin short name can't be empty") - ) + notBlank(attributes.getValue(PLUGIN_NAME), "Plugin short name can't be empty")) .withVersion( defaultIfBlank( attributes.getValue(PLUGIN_VERSION), - attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION) - ) - ) + attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION))) .withGroup(attributes.getValue(GROUP_ID)) .withWiki(attributes.getValue(PLUGIN_WIKI_URL)) .withTitle(attributes.getValue(PLUGIN_TITLE)) @@ -87,11 +92,33 @@ private static Plugin from(Attributes attributes, long timestamp) throws IOExcep .withRequiredCore(attributes.getValue(PLUGIN_REQUIRED_JENKINS_VERSION)) .withBuiltBy(attributes.getValue(PLUGIN_BUILT_BY)) .withDependencies(getDependencies(attributes)) - .withDevelopers(getDevelopers(attributes)); + .withDevelopers(getDevelopers(attributes)) + .withSha1(digests.sha1) + .withSha512(digests.sha512); + } + + private static Digests digests(final File file) { + final Digests digests = new Digests(); + try (FileInputStream fin = new FileInputStream(file)) { + final MessageDigest sha1 = DigestUtils.getSha1Digest(); + final MessageDigest sha512 = DigestUtils.getSha512Digest(); + final byte[] buf = new byte[2048]; + int len; + while ((len = fin.read(buf, 0, buf.length)) >= 0) { + sha1.update(buf, 0, len); + sha512.update(buf, 0, len); + } + + digests.sha1 = new String(Base64.encodeBase64(sha1.digest()), "UTF-8"); + digests.sha512 = new String(Base64.encodeBase64(sha512.digest()), "UTF-8"); + } catch (final Exception ex) { + LOG.error(ex.getMessage(), ex); + } + return digests; } private static SimpleDateFormat releaseTimestampDateFormat() { - SimpleDateFormat releaseFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.00Z'", Locale.US); + final SimpleDateFormat releaseFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.00Z'", Locale.US); releaseFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return releaseFormat; } @@ -100,26 +127,26 @@ private static SimpleDateFormat buildDateTimeFormat() { return new SimpleDateFormat("MMM dd, yyyy", Locale.US); } - protected static List getDependencies(Attributes attributes) throws IOException { - String deps = attributes.getValue(PLUGIN_DEPENDENCIES); + protected static List getDependencies(final Attributes attributes) throws IOException { + final String deps = attributes.getValue(PLUGIN_DEPENDENCIES); if (deps == null) { return Collections.emptyList(); } - List result = new ArrayList<>(); - for (String token : deps.split(",")) { + final List result = new ArrayList<>(); + for (final String token : deps.split(",")) { result.add(parseDependencyFrom(token)); } return result; } protected static Dependency parseDependencyFrom(String token) { - boolean optional = token.endsWith(OPTIONAL_DEPENDENCY); + final boolean optional = token.endsWith(OPTIONAL_DEPENDENCY); if (optional) { token = substringBefore(token, OPTIONAL_DEPENDENCY); } - String[] pieces = token.split(":"); + final String[] pieces = token.split(":"); return new Dependency() .withName(pieces[0]) @@ -127,21 +154,20 @@ protected static Dependency parseDependencyFrom(String token) { .withOptional(optional); } - protected static List getDevelopers(Attributes attributes) throws IOException { - String devs = attributes.getValue(PLUGIN_DEVELOPERS); + protected static List getDevelopers(final Attributes attributes) throws IOException { + final String devs = attributes.getValue(PLUGIN_DEVELOPERS); if (isBlank(devs)) { return Collections.emptyList(); } - List result = new ArrayList<>(); - Matcher matcher = DEVELOPERS_PATTERN.matcher(devs); + final List result = new ArrayList<>(); + final Matcher matcher = DEVELOPERS_PATTERN.matcher(devs); int totalMatched = 0; while (matcher.find()) { result.add(new Developer() .withName(trimToEmpty(matcher.group(1))) .withDeveloperId(trimToEmpty(matcher.group(2))) - .withEmail(trimToEmpty(matcher.group(3))) - ); + .withEmail(trimToEmpty(matcher.group(3)))); totalMatched += matcher.end() - matcher.start(); } if (totalMatched < devs.length()) { @@ -151,32 +177,37 @@ protected static List getDevelopers(Attributes attributes) throws IOE return result; } - - protected static String getWiki(File hpiFile) { + protected static String getWiki(final File hpiFile) { try { - String baseName = FilenameUtils.getBaseName(hpiFile.getName()); - File exceptFile = new File(hpiFile.getParent(), baseName + ".wiki"); + final String baseName = FilenameUtils.getBaseName(hpiFile.getName()); + final File exceptFile = new File(hpiFile.getParent(), baseName + ".wiki"); if (exceptFile.exists()) { return FileUtils.readFileToString(exceptFile, "UTF-8"); } else { return ""; } - } catch (Exception e) { + } catch (final Exception e) { return ""; } } - protected static String getExcerpt(File hpiFile) { + protected static String getExcerpt(final File hpiFile) { try { - String baseName = FilenameUtils.getBaseName(hpiFile.getName()); - File exceptFile = new File(hpiFile.getParent(), baseName + ".info"); + final String baseName = FilenameUtils.getBaseName(hpiFile.getName()); + final File exceptFile = new File(hpiFile.getParent(), baseName + ".info"); if (exceptFile.exists()) { return FileUtils.readFileToString(exceptFile, "UTF-8").replace("\r\n", "
\n"); } else { return ""; } - } catch (Exception e) { + } catch (final Exception e) { return ""; } } -} + + private static class Digests { + private String sha1; + private String sha512; + } + +} \ No newline at end of file diff --git a/juseppe-core/src/main/resources/jsonschema/plugin.json b/juseppe-core/src/main/resources/jsonschema/plugin.json index e643adc..a6e641d 100644 --- a/juseppe-core/src/main/resources/jsonschema/plugin.json +++ b/juseppe-core/src/main/resources/jsonschema/plugin.json @@ -48,6 +48,14 @@ "type": "string" }, + "sha1": { + "type": "string" + }, + + "sha512": { + "type": "string" + }, + "dependencies": { "type": "array", "items": {