Skip to content
Permalink
Browse files

[JENKINS-15497] Adding a new object to represent a TapAttachment. Thi…

…s new design allows us to send the file-size, file-name and other values found in a map diagnostics
  • Loading branch information...
kinow committed Oct 16, 2012
1 parent 15d48ef commit a4a18a7d5e5570216c725a66d264407ba19c0bd9
Showing with 125 additions and 10 deletions.
  1. +10 −10 src/main/java/org/tap4j/plugin/TapResult.java
  2. +115 −0 src/main/java/org/tap4j/plugin/model/TapAttachment.java
@@ -37,7 +37,6 @@
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;
@@ -48,6 +47,7 @@
import org.tap4j.model.TestResult;
import org.tap4j.model.TestSet;
import org.tap4j.plugin.model.ParseErrorTestSetMap;
import org.tap4j.plugin.model.TapAttachment;
import org.tap4j.plugin.model.TestSetMap;
import org.tap4j.plugin.util.DiagnosticUtil;
import org.tap4j.util.DirectiveValues;
@@ -271,15 +271,15 @@ public void doDownloadAttachment(StaplerRequest request, StaplerResponse respons
TapConsumer consumer = TapConsumerFactory.makeTap13YamlConsumer();
TestSet ts = consumer.load(tapStream);

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

sos.write(Base64.decodeBase64(content));
sos.write(attachment.getContent());
sos.print('\n');
} else {
sos.println("Couldn't locate attachment in YAMLish: " + f);
@@ -300,7 +300,7 @@ public void doDownloadAttachment(StaplerRequest request, StaplerResponse respons
* @param key
* @return
*/
private String getContent(TestSet ts, String key) {
private TapAttachment getAttachment(TestSet ts, String key) {
for(TestResult tr : ts.getTestResults()){
Map<String, Object> diagnostics = tr.getDiagnostic();
String parentKey = null;
@@ -312,7 +312,7 @@ private String getContent(TestSet ts, String key) {
}

@SuppressWarnings("unchecked")
private String recursivelySearch(Map<String, Object> diagnostics, String parentKey, String key) {
private TapAttachment recursivelySearch(Map<String, Object> diagnostics, String parentKey, String key) {
for(String diagnosticKey : diagnostics.keySet()) {
Object value = diagnostics.get(diagnosticKey);
if(value != null) {
@@ -324,13 +324,13 @@ private String recursivelySearch(Map<String, Object> diagnostics, String parentK
if(o == null)
o = diagnostics.get("File-content");
if(o != null && o instanceof String)
return (String)o;
return new TapAttachment(Base64.decodeBase64((String)o), diagnostics);
} else if(diagnosticKey.equalsIgnoreCase("file-name") && value.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 new TapAttachment(Base64.decodeBase64((String)o), diagnostics);
}
}
}
@@ -0,0 +1,115 @@
/*
* The MIT License
*
* Copyright (c) <2012> <Bruno P. Kinoshita>
*
* 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 org.tap4j.plugin.model;

import java.util.Map;

/**
*
* @author Bruno P. Kinoshita - http://www.kinoshita.eti.br
* @since 0.1
*/
public class TapAttachment {

private final String fileName;
private final byte[] content;
private final int size;
private final String fileType;

/**
* @param fileName
* @param content
* @param size
* @param fileType
*/
public TapAttachment(String fileName, byte[] content, int size,
String fileType) {
super();
this.fileName = fileName;
this.content = content;
this.size = size;
this.fileType = fileType;
}

/**
* @param fileName
* @param content
* @param size
* @param fileType
*/
public TapAttachment(byte[] content, Map<String, Object> diagnostics) {
super();
this.content = content;
int size = -1;
String fileType = "";
String fileName = "tapAttachment";
for(String key : diagnostics.keySet()) {
if(diagnostics.get(key) instanceof Map<?, ?> == Boolean.FALSE) {
if(key.equalsIgnoreCase("file-size")) {
try {
size = (int) Long.parseLong(diagnostics.get(key).toString());
} catch(NumberFormatException nfe) {
// Do nothing
}
} else if(key.equalsIgnoreCase("file-type")) {
fileType = (String) diagnostics.get(key);
} else if(key.equalsIgnoreCase("file-name")) {
fileName = (String) diagnostics.get(key);
}
}
}
this.size = size;
this.fileType = fileType;
this.fileName = fileName;
}

/**
* @return the fileName
*/
public String getFileName() {
return fileName;
}

/**
* @return the content
*/
public byte[] getContent() {
return content;
}

/**
* @return the size
*/
public int getSize() {
return size;
}

/**
* @return the fileType
*/
public String getFileType() {
return fileType;
}

}

0 comments on commit a4a18a7

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