1 package org.apache.maven.archiva.webdav.util;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.jackrabbit.webdav.DavResource;
23 import org.apache.jackrabbit.webdav.io.OutputContext;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.io.PrintWriter;
36 * @author <a href="mailto:james@atlassian.com">James William Dumay</a>
38 public class IndexWriter
40 private final DavResource resource;
42 private final String logicalResource;
44 private final List<File> localResources;
46 private final boolean isVirtual;
48 public IndexWriter(DavResource resource, File localResource, String logicalResource)
50 this.resource = resource;
51 this.localResources = new ArrayList<File>();
52 this.localResources.add( localResource );
53 this.logicalResource = logicalResource;
54 this.isVirtual = false;
57 public IndexWriter( DavResource resource, List<File> localResources, String logicalResource )
59 this.resource = resource;
60 this.logicalResource = logicalResource;
61 this.localResources = localResources;
62 this.isVirtual = true;
65 public void write(OutputContext outputContext)
67 outputContext.setModificationTime(new Date().getTime());
68 outputContext.setContentType("text/html");
69 outputContext.setETag("");
70 if (outputContext.hasStream())
72 PrintWriter writer = new PrintWriter(outputContext.getOutputStream());
73 writeDocumentStart(writer);
74 writeHyperlinks(writer);
75 writeDocumentEnd(writer);
81 private void writeDocumentStart(PrintWriter writer)
83 writer.println("<html>");
84 writer.println("<head>");
85 writer.println("<title>Collection: " + logicalResource + "<title>");
86 writer.println("</head>");
87 writer.println("<body>");
88 writer.println("<h3>Collection: " + logicalResource + "</h3>");
91 if (!"/".equals(logicalResource))
93 File file = new File(logicalResource);
94 String parentName = file.getParent().equals("") ? "/" : file.getParent();
96 writer.println("<ul>");
97 writer.println("<li><a href=\"../\">" + parentName + "</a> <i><small>(Parent)</small></i></li>");
98 writer.println("</ul>");
101 writer.println("<ul>");
104 private void writeDocumentEnd(PrintWriter writer)
106 writer.println("</ul>");
107 writer.println("</body>");
108 writer.println("</html>");
111 private void writeHyperlinks(PrintWriter writer)
115 for( File localResource : localResources )
117 List<File> files = new ArrayList<File>( Arrays.asList( localResource.listFiles() ) );
118 Collections.sort( files );
120 for ( File file : files )
122 writeHyperlink( writer, file.getName(), file.isDirectory(), false );
128 // virtual repository - filter unique directories
129 Map<String, File> uniqueChildFiles = new HashMap<String, File>();
130 List<String> sortedList = new ArrayList<String>();
131 for( File resource : localResources )
133 List<File> files = new ArrayList<File>( Arrays.asList( resource.listFiles() ) );
135 for ( File file : files )
137 if( uniqueChildFiles.get( file.getName() ) == null )
139 uniqueChildFiles.put( file.getName(), file );
140 sortedList.add( file.getName() );
145 Collections.sort( sortedList );
146 for ( String fileName : sortedList )
148 writeHyperlink( writer, fileName, ( (File) uniqueChildFiles.get( fileName ) ).isDirectory(), true );
153 private void writeHyperlink(PrintWriter writer, String resourceName, boolean directory, boolean isBrowse )
157 writer.println("<li><a href=\"./" + resourceName + "/\">" + resourceName + "</a></li>");
161 writer.println("<li><a href=\"./" + resourceName + "\">" + resourceName + "</a></li>");