Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new TEMPLATE content token that can be used to pull in email content from a file.
- Loading branch information
Showing
9 changed files
with
256 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/main/java/hudson/plugins/emailext/plugins/content/AbstractEvalContent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2015 acearl. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package hudson.plugins.emailext.plugins.content; | ||
|
||
import hudson.Plugin; | ||
import hudson.model.AbstractBuild; | ||
import hudson.model.TaskListener; | ||
import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor; | ||
import hudson.tasks.Mailer; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.lib.configprovider.ConfigProvider; | ||
import org.jenkinsci.lib.configprovider.model.Config; | ||
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro; | ||
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException; | ||
|
||
/** | ||
* | ||
* @author acearl | ||
*/ | ||
public abstract class AbstractEvalContent extends DataBoundTokenMacro { | ||
|
||
protected static Object configProvider; | ||
protected static final String EMAIL_TEMPLATES_DIRECTORY = "email-templates"; | ||
protected final String macroName; | ||
|
||
public AbstractEvalContent(String macroName) { | ||
this.macroName = macroName; | ||
} | ||
|
||
@Override | ||
public abstract String evaluate(AbstractBuild<?, ?> ab, TaskListener tl, String string) throws MacroEvaluationException, IOException, InterruptedException; | ||
|
||
@Override | ||
public boolean acceptsMacroName(String macroName) { | ||
return macroName.equals(this.macroName); | ||
} | ||
|
||
public static File scriptsFolder() { | ||
return new File(Jenkins.getInstance().getRootDir(), EMAIL_TEMPLATES_DIRECTORY); | ||
} | ||
|
||
protected abstract ConfigProvider getConfigProvider(); | ||
|
||
@Override | ||
public boolean hasNestedContent() { | ||
return false; | ||
} | ||
|
||
protected InputStream getFileInputStream(String fileName, String extension) | ||
throws FileNotFoundException { | ||
|
||
InputStream inputStream; | ||
if(fileName.startsWith("managed:")) { | ||
String managedFileName = fileName.substring(8); | ||
try { | ||
inputStream = getManagedFile(managedFileName); | ||
} catch(NoClassDefFoundError e) { | ||
inputStream = null; | ||
} | ||
|
||
if(inputStream == null) { | ||
throw new FileNotFoundException(String.format("Managed file '%s' not found", managedFileName)); | ||
} | ||
return inputStream; | ||
} | ||
|
||
// add .jelly if needed | ||
if (!fileName.endsWith(extension)) { | ||
fileName += extension; | ||
} | ||
|
||
inputStream = getClass().getClassLoader().getResourceAsStream( | ||
"hudson/plugins/emailext/templates/" + fileName); | ||
|
||
if (inputStream == null) { | ||
final File templateFile = new File(scriptsFolder(), fileName); | ||
inputStream = new FileInputStream(templateFile); | ||
} | ||
|
||
return inputStream; | ||
} | ||
|
||
private InputStream getManagedFile(String fileName) { | ||
Plugin plugin = Jenkins.getInstance().getPlugin("config-file-provider"); | ||
InputStream stream = null; | ||
if(plugin != null) { | ||
Config config = null; | ||
ConfigProvider provider = getConfigProvider(); | ||
for(Config c : provider.getAllConfigs()) { | ||
if(c.name.equalsIgnoreCase(fileName) && provider.isResponsibleFor(c.id)) { | ||
config = c; | ||
break; | ||
} | ||
} | ||
|
||
if(config != null) { | ||
stream = new ByteArrayInputStream(config.content.getBytes()); | ||
} | ||
} | ||
return stream; | ||
} | ||
|
||
protected String generateMissingFile(String type, String fileName) { | ||
return type + " file [" + fileName + "] was not found in $JENKINS_HOME/" + EMAIL_TEMPLATES_DIRECTORY + "."; | ||
} | ||
|
||
protected String getCharset(AbstractBuild<?, ?> build) { | ||
String charset = Mailer.descriptor().getCharset(); | ||
ExtendedEmailPublisherDescriptor descriptor = Jenkins.getInstance().getDescriptorByType(ExtendedEmailPublisherDescriptor.class); | ||
String overrideCharset = descriptor.getCharset(); | ||
if (overrideCharset != null) { | ||
charset = overrideCharset; | ||
} | ||
return charset; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.