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.commons.lang.StringUtils;
23 import org.apache.jackrabbit.webdav.DavResource;
24 import org.apache.jackrabbit.webdav.io.OutputContext;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.Date;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.io.PrintWriter;
38 public class IndexWriter
40 private final String logicalResource;
42 private final List<File> localResources;
44 private final boolean isVirtual;
46 public IndexWriter(DavResource resource, File localResource, String logicalResource)
48 this.localResources = new ArrayList<File>();
49 this.localResources.add( localResource );
50 this.logicalResource = logicalResource;
51 this.isVirtual = false;
54 public IndexWriter( DavResource resource, List<File> localResources, String logicalResource )
56 this.logicalResource = logicalResource;
57 this.localResources = localResources;
58 this.isVirtual = true;
61 public void write(OutputContext outputContext)
63 outputContext.setModificationTime(new Date().getTime());
64 outputContext.setContentType("text/html");
65 outputContext.setETag("");
66 if (outputContext.hasStream())
68 PrintWriter writer = new PrintWriter(outputContext.getOutputStream());
69 writeDocumentStart(writer);
70 writeHyperlinks(writer);
71 writeDocumentEnd(writer);
77 private void writeDocumentStart(PrintWriter writer)
79 writer.println("<html>");
80 writer.println("<head>");
81 writer.println("<title>Collection: " + logicalResource + "</title>");
82 writer.println("</head>");
83 writer.println("<body>");
84 writer.println("<h3>Collection: " + logicalResource + "</h3>");
87 if (!"/".equals(logicalResource))
89 File file = new File(logicalResource);
90 String parentName = file.getParent().equals("") ? "/" : file.getParent();
92 //convert to unix path in case archiva is hosted on windows
93 parentName = StringUtils.replace(parentName, "\\", "/" );
95 writer.println("<ul>");
96 writer.println("<li><a href=\"../\">" + parentName + "</a> <i><small>(Parent)</small></i></li>");
97 writer.println("</ul>");
100 writer.println("<ul>");
103 private void writeDocumentEnd(PrintWriter writer)
105 writer.println("</ul>");
106 writer.println("</body>");
107 writer.println("</html>");
110 private void writeHyperlinks(PrintWriter writer)
114 for( File localResource : localResources )
116 List<File> files = new ArrayList<File>( Arrays.asList( localResource.listFiles() ) );
117 Collections.sort( files );
119 for ( File file : files )
121 writeHyperlink( writer, file.getName(), file.isDirectory() );
127 // virtual repository - filter unique directories
128 Map<String, List<String>> uniqueChildFiles = new HashMap<String, List<String>>();
129 List<String> sortedList = new ArrayList<String>();
130 for( File resource : localResources )
132 List<File> files = new ArrayList<File>( Arrays.asList( resource.listFiles() ) );
133 for ( File file : files )
135 List<String> mergedChildFiles = new ArrayList<String>();
136 if( uniqueChildFiles.get( file.getName() ) == null )
138 mergedChildFiles.add( file.getAbsolutePath() );
142 mergedChildFiles = uniqueChildFiles.get( file.getName() );
143 if( !mergedChildFiles.contains( file.getAbsolutePath() ) )
145 mergedChildFiles.add( file.getAbsolutePath() );
148 uniqueChildFiles.put( file.getName(), mergedChildFiles );
149 sortedList.add( file.getName() );
153 Collections.sort( sortedList );
154 List<String> written = new ArrayList<String>();
155 for ( String fileName : sortedList )
157 List<String> childFilesFromMap = uniqueChildFiles.get( fileName );
158 for( String childFilePath : childFilesFromMap )
160 File childFile = new File( childFilePath );
161 if( !written.contains( childFile.getName() ) )
163 written.add( childFile.getName() );
164 writeHyperlink( writer, fileName, childFile.isDirectory() );
171 private void writeHyperlink(PrintWriter writer, String resourceName, boolean directory )
175 writer.println("<li><a href=\"" + resourceName + "/\">" + resourceName + "</a></li>");
179 writer.println("<li><a href=\"" + resourceName + "\">" + resourceName + "</a></li>");