1 package org.apache.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;
27 import java.io.PrintWriter;
28 import java.text.DateFormat;
29 import java.text.DecimalFormat;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.Date;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Locale;
41 public class IndexWriter
43 private final String logicalResource;
45 private final List<File> localResources;
47 private final boolean isVirtual;
49 public IndexWriter( DavResource resource, File localResource, String logicalResource )
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.logicalResource = logicalResource;
60 this.localResources = localResources;
61 this.isVirtual = true;
64 public void write( OutputContext outputContext )
66 outputContext.setModificationTime( new Date().getTime() );
67 outputContext.setContentType( "text/html" );
68 outputContext.setETag( "" ); // skygo ETag MRM-1127 seems to be fixed
69 if ( outputContext.hasStream() )
71 PrintWriter writer = new PrintWriter( outputContext.getOutputStream() );
72 writeDocumentStart( writer );
73 writeHyperlinks( writer );
74 writeDocumentEnd( writer );
80 private void writeDocumentStart( PrintWriter writer )
82 writer.println("<!DOCTYPE html>");
83 writer.println( "<html>" );
84 writer.println( "<head>" );
85 writer.println( "<title>Collection: /" + logicalResource + "</title>" );
86 writer.println( "<style type=\"text/css\">" );
87 writer.println( "ul{list-style:none;}" ); //list-style-image: url("../images/folder.png");
88 StringBuilder relative = new StringBuilder("../../");
89 if ( logicalResource.length() > 0 )
91 String tmpRelative = StringUtils.replace( logicalResource, "\\", "/" );
92 for (int i=0;i<tmpRelative.split("/").length;i++)
94 relative.append("../");
97 writer.println( ".file{background:url(" + relative.toString() + "images/package-x-generic.png) no-repeat scroll 0 0 transparent;}" );
98 writer.println( ".folder{background:url(" + relative.toString() + "images/folder.png) no-repeat scroll 0 0 transparent;}" );
99 writer.println( "a{color:#0088CC;text-decoration: none;padding-left:20px;}" );
100 writer.println( ".collection tr:nth-child(odd){background-color:#fafafa;}" );
101 writer.println( "tr td:nth-child(2){width:150px;color:#cc8800;text-align:right;}" );
102 writer.println( "tr td:nth-child(3){width:150px;color:#0000cc;text-align:center;}" );
103 writer.println( "th td:nth-child(2){width:150px;}" );
104 writer.println( "th td:nth-child(3){width:150px;}" );
105 writer.println( "</style>" );
106 writer.println( "</head>" );
107 writer.println( "<body>" );
108 writer.println( "<h3>Collection: /" + logicalResource + "</h3>" );
111 if ( logicalResource.length() > 0 )
113 File file = new File( logicalResource );
114 String parentName = file.getParent() == null ? "/" : file.getParent();
116 //convert to unix path in case archiva is hosted on windows
117 parentName = StringUtils.replace( parentName, "\\", "/" );
119 writer.println( "<ul>" );
120 writer.println( "<li><a class=\"folder\" href=\"../\">" + parentName + "</a> <i><small>(Parent)</small></i></li>" );
121 writer.println( "</ul>" );
124 writer.println( "<table class=\"collection\">" );
125 writer.println( "<tr><th>Name</th><th>Size (Bytes)</th><th>Last Modified</th></tr>" );
128 private void writeDocumentEnd( PrintWriter writer )
130 writer.println( "</table>" );
131 writer.println( "</body>" );
132 writer.println( "</html>" );
135 private void writeHyperlinks( PrintWriter writer )
139 for ( File localResource : localResources )
141 List<File> files = new ArrayList<File>( Arrays.asList( localResource.listFiles() ) );
142 Collections.sort( files );
144 for ( File file : files )
146 writeHyperlink( writer, file.getName(), file.lastModified(), file.length(), file.isDirectory() );
152 // virtual repository - filter unique directories
153 Map<String, List<String>> uniqueChildFiles = new HashMap<String, List<String>>();
154 List<String> sortedList = new ArrayList<String>();
155 for ( File resource : localResources )
157 List<File> files = new ArrayList<File>( Arrays.asList( resource.listFiles() ) );
158 for ( File file : files )
160 List<String> mergedChildFiles = new ArrayList<String>();
161 if ( uniqueChildFiles.get( file.getName() ) == null )
163 mergedChildFiles.add( file.getAbsolutePath() );
167 mergedChildFiles = uniqueChildFiles.get( file.getName() );
168 if ( !mergedChildFiles.contains( file.getAbsolutePath() ) )
170 mergedChildFiles.add( file.getAbsolutePath() );
173 uniqueChildFiles.put( file.getName(), mergedChildFiles );
174 sortedList.add( file.getName() );
178 Collections.sort( sortedList );
179 List<String> written = new ArrayList<String>();
180 for ( String fileName : sortedList )
182 List<String> childFilesFromMap = uniqueChildFiles.get( fileName );
183 for ( String childFilePath : childFilesFromMap )
185 File childFile = new File( childFilePath );
186 if ( !written.contains( childFile.getName() ) )
188 written.add( childFile.getName() );
189 writeHyperlink( writer, fileName, childFile.lastModified(), childFile.length(), childFile.isDirectory() );
196 private static String fileDateFormat( long date )
198 DateFormat dateFormatter = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault() );
199 Date aDate = new Date( date );
200 return dateFormatter.format( aDate );
203 private void writeHyperlink( PrintWriter writer, String resourceName, long lastModified, long fileSize, boolean directory )
207 writer.println( "<tr><td><a class=\"folder\" href=\"" + resourceName + "/\">" + resourceName + "</a></td><td> </td><td> </td></tr>" );
211 writer.println( "<tr><td><a class=\"file\" href=\"" + resourceName + "\">" + resourceName + "</a></td><td class=\"size\">" + fileSize + " </td><td class=\"date\">" + fileDateFormat( lastModified ) + "</td></tr>" );