Skip to content
Permalink
Browse files

[FIXED JENKINS-15497] Display link to download YAMLish attachments.

  • Loading branch information...
kinow committed Oct 12, 2012
1 parent f5cdddb commit 3eff2031f16f744d8b7484c119982a9160fd7377
@@ -34,7 +34,15 @@
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;
import org.tap4j.consumer.TapConsumer;
import org.tap4j.consumer.TapConsumerFactory;
import org.tap4j.model.BailOut;
import org.tap4j.model.Directive;
import org.tap4j.model.TestResult;
@@ -206,8 +214,8 @@ private boolean isFailure(TestResult testResult) {
/**
* Called from TapResult/index..jelly
*/
public String createDiagnosticTable(Map<String, Object> diagnostic) {
return DiagnosticUtil.createDiagnosticTable(diagnostic);
public String createDiagnosticTable(String tapFile, Map<String, Object> diagnostic) {
return DiagnosticUtil.createDiagnosticTable(tapFile, diagnostic);
}

public boolean isTestResult(Object tapResult) {
@@ -251,5 +259,83 @@ public String getContents(String fileName) {
}
return contents;
}

public void doDownloadAttachment(StaplerRequest request, StaplerResponse response) {
String f = request.getParameter("f");
String key = request.getParameter("key");
try {
FilePath tapDir = new FilePath(new FilePath(new File(build.getRootDir(), "tap")), f);
ServletOutputStream sos = response.getOutputStream();
if(tapDir.exists()) {
String tapStream = tapDir.readToString();
TapConsumer consumer = TapConsumerFactory.makeTap13YamlConsumer();
TestSet ts = consumer.load(tapStream);

String content = getContent(ts, key);
if(StringUtils.isNotBlank(content)) {
response.setContentType("application/force-download");
//response.setContentLength((int)tapDir.length());
response.setContentLength(-1);
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Content-Disposition","attachment; filename=\"" + f + "\"");//fileName);

sos.write(Base64.decodeBase64(content));
sos.print('\n');
} else {
sos.println("Couldn't locate attachment in YAMLish: " + f);
}
} else {
sos.println("Couldn't read FilePath.");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
}
}

/**
* @param ts
* @param key
* @return
*/
private String getContent(TestSet ts, String key) {
for(TestResult tr : ts.getTestResults()){
Map<String, Object> diagnostics = tr.getDiagnostic();
String parentKey = null;
if(diagnostics != null && diagnostics.size() > 0) {
return recursivelySearch(diagnostics, parentKey, key);
}
}
return null;
}

@SuppressWarnings("unchecked")
private String recursivelySearch(Map<String, Object> diagnostics, String parentKey, String key) {
for(String diagnosticKey : diagnostics.keySet()) {
Object value = diagnostics.get(diagnosticKey);
if(value != null) {
if(value instanceof Map<?, ?>) {
return recursivelySearch((Map<String, Object>)value, diagnosticKey, key);
} else {
if(parentKey != null && parentKey.equals(key)) {
Object o = diagnostics.get("File-Content");
if(o == null)
o = diagnostics.get("File-content");
if(o != null && o instanceof String)
return (String)o;
} else if(diagnosticKey.equalsIgnoreCase("file-name") && diagnosticKey.equals(key)) {
Object o = diagnostics.get("File-Content");
if(o == null)
o = diagnostics.get("File-content");
if(o != null && o instanceof String)
return (String)o;
}
}
}
}
return null;
}

}
@@ -47,39 +47,68 @@ private DiagnosticUtil() {
super();
}

public static String createDiagnosticTable(Map<String, Object> diagnostic) {
public static String createDiagnosticTable(String tapFile, Map<String, Object> diagnostic) {
StringBuilder sb = new StringBuilder();
createDiagnosticTableRecursively(diagnostic, sb, 1); // 1 is the first
createDiagnosticTableRecursively(tapFile, null, diagnostic, sb, 1); // 1 is the first
// depth
return sb.toString();
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void createDiagnosticTableRecursively(
public static void createDiagnosticTableRecursively(String tapFile, String parentKey,
Map<String, Object> diagnostic, StringBuilder sb, int depth) {

sb.append(INNER_TABLE_HEADER);

RENDER_TYPE renderType = getMapEntriesRenderType(diagnostic);

for (Entry<String, Object> entry : diagnostic.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
sb.append("<tr>");

for (int i = 0; i < depth; ++i) {
sb.append("<td width='5%' class='hidden'> </td>");
if(renderType == RENDER_TYPE.IMAGE) {
for (Entry<String, Object> entry : diagnostic.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
sb.append("<tr>");

for (int i = 0; i < depth; ++i) {
sb.append("<td width='5%' class='hidden'> </td>");
}
sb.append("<td style=\"width: auto;\">" + key + "</td>");
if(key.equals("File-Content")) {
String fileName = "attachment";
Object o = diagnostic.get("File-Name");
if(o!=null && o instanceof String) {
fileName = (String)o;
}
String downloadKey = fileName;
if(parentKey != null){
if(depth > 3 && !parentKey.trim().equalsIgnoreCase("files") && !parentKey.trim().equalsIgnoreCase("extensions")) {
downloadKey = parentKey;
}
}
sb.append("<td><a href='downloadAttachment?f="+tapFile+"&key="+downloadKey+"'>"+fileName+"</a></td>");
} else {
sb.append("<td>" + org.apache.commons.lang.StringEscapeUtils.escapeHtml(value.toString()) + "</td>");
}
sb.append("</tr>");
}
sb.append("<td style=\"width: auto;\">" + key + "</td>");
if (value instanceof java.util.Map) {
sb.append("<td> </td>");
createDiagnosticTableRecursively((java.util.Map) value, sb,
(depth + 1));
} else {
sb.append("<td>" + getRenderedValue(key, value, renderType)
+ "</td>");
} else {
for (Entry<String, Object> entry : diagnostic.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
sb.append("<tr>");

for (int i = 0; i < depth; ++i) {
sb.append("<td width='5%' class='hidden'> </td>");
}
sb.append("<td style=\"width: auto;\">" + key + "</td>");
if (value instanceof java.util.Map) {
sb.append("<td> </td>");
createDiagnosticTableRecursively(tapFile, key, (java.util.Map) value, sb,
(depth + 1));
} else {
sb.append("<td>" + org.apache.commons.lang.StringEscapeUtils.escapeHtml(value.toString()) + "</td>");
}
sb.append("</tr>");
}
sb.append("</tr>");
}

sb.append(INNER_TABLE_FOOTER);
@@ -101,25 +130,4 @@ private static RENDER_TYPE getMapEntriesRenderType(
return renderType;
}

/**
* @param key
* @param value
* @param renderType
* @return
*/
private static String getRenderedValue(String key, Object value,
RENDER_TYPE renderType) {
switch (renderType) {
case IMAGE:
if (key.equals("File-Content")) {
return "Base64 content suppressed!";
} else {
return org.apache.commons.lang.StringEscapeUtils.escapeHtml(value.toString());
}
default:
case TEXT:
return org.apache.commons.lang.StringEscapeUtils.escapeHtml(value.toString());
}
}

}
@@ -42,7 +42,7 @@
<j:forEach var="tapLine" items="${map.testSet.tapLines}">

<!-- TAP Test Result information -->
<tap:line tapLine="${tapLine}" />
<tap:line tapLine="${tapLine}" tapFile="${map.fileName}" />

</j:forEach>

@@ -5,6 +5,7 @@
<j:choose>
<j:when test="${attrs.tapLine != null}">
<j:set var="tapLine" value="${attrs.tapLine}" />
<j:set var="tapFile" value="${attrs.tapFile}" />

<j:choose>
<j:when test="${it.isTestResult( tapLine )}">
@@ -17,7 +18,7 @@
<!-- Comments diagnostics -->
<tap:comments diagnostic="${tapLine.comments}" />
<!-- YAML Diagnostics -->
<tap:yaml diagnostic="${tapLine.diagnostic}" />
<tap:yaml diagnostic="${tapLine.diagnostic}" tapFile="${tapFile}" />
</j:when>
<j:when test="${it.isBailOut( tapLine )}">
<tr>
@@ -6,8 +6,9 @@
test="${attrs.diagnostic != null and attrs.diagnostic.size() > 0 }">

<j:set var="diagnostic" value="${attrs.diagnostic}" />
<j:set var="tapFile" value="${attrs.tapFile}" />

${ it.createDiagnosticTable( diagnostic ) }
${ it.createDiagnosticTable( tapFile, diagnostic ) }

<!--tr>
<table width="100%" class="yaml">

0 comments on commit 3eff203

Please sign in to comment.
You can’t perform that action at this time.