提交 d87fed0e 编写于 作者: J jglick

Can now use .../job/myjob/ws/subdir/*plain* to retrieve a program-friendly dir listing.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@13601 71c3de6d-444a-0410-be80-ed276b4c234a
上级 28535bda
......@@ -1044,6 +1044,10 @@ public abstract class AbstractProject<P extends AbstractProject<P,R>,R extends A
FilePath ws = getWorkspace();
if ((ws == null) || (!ws.exists())) {
// if there's no workspace, report a nice error message
// Would be good if when asked for *plain*, do something else!
// (E.g. return 404, or send empty doc.)
// Not critical; client can just check if content type is not text/plain,
// which also serves to detect old versions of Hudson.
req.getView(this,"noWorkspace.jelly").forward(req,rsp);
} else {
new DirectoryBrowserSupport(this,getDisplayName()+" workspace").serveFile(req, rsp, ws, "folder.gif", true);
......
......@@ -15,6 +15,7 @@ import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
......@@ -91,6 +92,7 @@ public final class DirectoryBrowserSupport {
StringBuilder _rest = new StringBuilder();
int restSize=-1; // number of ".." needed to go back to the 'base' level.
boolean zip=false; // if we are asked to serve a zip file bundle
boolean plain = false; // if asked to serve a plain text directory listing
{
boolean inBase = true;
StringTokenizer pathTokens = new StringTokenizer(path,"/");
......@@ -105,6 +107,10 @@ public final class DirectoryBrowserSupport {
zip=true;
break;
}
if(pathElement.equals("*plain*")) {
plain = true;
break;
}
StringBuilder sb = inBase?_base:_rest;
if(sb.length()>0) sb.append('/');
......@@ -126,6 +132,20 @@ public final class DirectoryBrowserSupport {
baseFile.createZipArchive(rsp.getOutputStream(),rest);
return;
}
if (plain) {
rsp.setContentType("text/plain;charset=UTF-8");
OutputStream os = rsp.getOutputStream();
try {
for (String kid : baseFile.act(new SimpleChildList())) {
os.write(kid.getBytes("UTF-8"));
os.write('\n');
}
os.flush();
} finally {
os.close();
}
return;
}
if(rest.length()==0) {
// if the target page to be displayed is a directory and the path doesn't end with '/', redirect
......@@ -323,6 +343,26 @@ public final class DirectoryBrowserSupport {
}
}
/**
* Simple list of names of children of a folder.
* Subfolders will have a trailing slash appended.
*/
private static final class SimpleChildList implements FileCallable<List<String>> {
private static final long serialVersionUID = 1L;
public List<String> invoke(File f, VirtualChannel channel) throws IOException {
List<String> r = new ArrayList<String>();
String[] kids = f.list(); // no need to sort
for (String kid : kids) {
if (new File(f, kid).isDirectory()) {
r.add(kid + "/");
} else {
r.add(kid);
}
}
return r;
}
}
/**
* Builds a list of list of {@link Path}. The inner
* list of {@link Path} represents one child item to be shown
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册