Coverage Report - org.apache.maven.archiva.webdav.util.IndexWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
IndexWriter
0%
0/79
0%
0/28
0
 
 1  
 package org.apache.maven.archiva.webdav.util;
 2  
 
 3  
 /*
 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
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 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
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.commons.lang.StringUtils;
 23  
 import org.apache.jackrabbit.webdav.DavResource;
 24  
 import org.apache.jackrabbit.webdav.io.OutputContext;
 25  
 
 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;
 32  
 import java.util.Map;
 33  
 import java.io.PrintWriter;
 34  
 import java.io.File;
 35  
 
 36  
 /**
 37  
  */
 38  
 public class IndexWriter
 39  
 {
 40  
     private final String logicalResource;
 41  
     
 42  
     private final List<File> localResources;
 43  
     
 44  
     private final boolean isVirtual;
 45  
     
 46  
     public IndexWriter(DavResource resource, File localResource, String logicalResource)
 47  0
     {
 48  0
         this.localResources = new ArrayList<File>();
 49  0
         this.localResources.add( localResource );
 50  0
         this.logicalResource = logicalResource;
 51  0
         this.isVirtual = false;
 52  0
     }
 53  
     
 54  
     public IndexWriter( DavResource resource, List<File> localResources, String logicalResource )
 55  0
     {
 56  0
         this.logicalResource = logicalResource;
 57  0
         this.localResources = localResources;
 58  0
         this.isVirtual = true;
 59  0
     }
 60  
 
 61  
     public void write(OutputContext outputContext)
 62  
     {   
 63  0
         outputContext.setModificationTime(new Date().getTime());
 64  0
         outputContext.setContentType("text/html");
 65  0
         outputContext.setETag("");
 66  0
         if (outputContext.hasStream())
 67  
         {
 68  0
             PrintWriter writer = new PrintWriter(outputContext.getOutputStream());
 69  0
             writeDocumentStart(writer);
 70  0
             writeHyperlinks(writer);
 71  0
             writeDocumentEnd(writer);
 72  0
             writer.flush();
 73  0
             writer.close();
 74  
         } 
 75  0
     }
 76  
 
 77  
     private void writeDocumentStart(PrintWriter writer)
 78  
     {
 79  0
         writer.println("<html>");
 80  0
         writer.println("<head>");
 81  0
         writer.println("<title>Collection: /" + logicalResource + "</title>");
 82  0
         writer.println("</head>");
 83  0
         writer.println("<body>");
 84  0
         writer.println("<h3>Collection: /" + logicalResource + "</h3>");
 85  
 
 86  
         //Check if not root
 87  0
         if (logicalResource.length() > 0)
 88  
         {
 89  0
             File file = new File(logicalResource);
 90  0
             String parentName = file.getParent() == null ? "/" : file.getParent();
 91  
             
 92  
             //convert to unix path in case archiva is hosted on windows
 93  0
             parentName = StringUtils.replace(parentName, "\\", "/" );
 94  
 
 95  0
             writer.println("<ul>");
 96  0
             writer.println("<li><a href=\"../\">" + parentName + "</a> <i><small>(Parent)</small></i></li>");
 97  0
             writer.println("</ul>");
 98  
         }
 99  
 
 100  0
         writer.println("<ul>");
 101  0
     }
 102  
 
 103  
     private void writeDocumentEnd(PrintWriter writer)
 104  
     {
 105  0
         writer.println("</ul>");
 106  0
         writer.println("</body>");
 107  0
         writer.println("</html>");
 108  0
     }
 109  
 
 110  
     private void writeHyperlinks(PrintWriter writer)
 111  
     {   
 112  0
         if( !isVirtual )
 113  
         {
 114  0
             for( File localResource : localResources )
 115  
             {
 116  0
                 List<File> files = new ArrayList<File>( Arrays.asList( localResource.listFiles() ) ); 
 117  0
                 Collections.sort( files );
 118  
                 
 119  0
                 for ( File file : files )
 120  
                 {
 121  0
                     writeHyperlink( writer, file.getName(), file.isDirectory() );
 122  
                 }
 123  0
             }
 124  
         }
 125  
         else 
 126  
         {            
 127  
             // virtual repository - filter unique directories
 128  0
             Map<String, List<String>> uniqueChildFiles = new HashMap<String, List<String>>();
 129  0
             List<String> sortedList = new ArrayList<String>();
 130  0
             for( File resource : localResources )
 131  
             {   
 132  0
                 List<File> files = new ArrayList<File>( Arrays.asList( resource.listFiles() ) ); 
 133  0
                 for ( File file : files )
 134  
                 {   
 135  0
                     List<String> mergedChildFiles = new ArrayList<String>();
 136  0
                     if( uniqueChildFiles.get( file.getName() ) == null )
 137  
                     {
 138  0
                         mergedChildFiles.add( file.getAbsolutePath() );                        
 139  
                     }
 140  
                     else
 141  
                     {
 142  0
                         mergedChildFiles = uniqueChildFiles.get( file.getName() );
 143  0
                         if( !mergedChildFiles.contains( file.getAbsolutePath() ) )
 144  
                         {
 145  0
                             mergedChildFiles.add( file.getAbsolutePath() );
 146  
                         }
 147  
                     }
 148  0
                     uniqueChildFiles.put( file.getName(), mergedChildFiles );
 149  0
                     sortedList.add( file.getName() );
 150  0
                 }
 151  0
             }
 152  
              
 153  0
             Collections.sort( sortedList );
 154  0
             List<String> written = new ArrayList<String>();
 155  0
             for ( String fileName : sortedList )
 156  
             {   
 157  0
                 List<String> childFilesFromMap = uniqueChildFiles.get( fileName );
 158  0
                 for( String childFilePath : childFilesFromMap )
 159  
                 {   
 160  0
                     File childFile = new File( childFilePath );
 161  0
                     if( !written.contains( childFile.getName() ) )
 162  
                     {   
 163  0
                         written.add( childFile.getName() );
 164  0
                         writeHyperlink( writer, fileName, childFile.isDirectory() );                        
 165  
                     }
 166  0
                 }
 167  0
             }
 168  
         }
 169  0
     }
 170  
 
 171  
     private void writeHyperlink(PrintWriter writer, String resourceName, boolean directory )
 172  
     {        
 173  0
         if (directory)
 174  
         {
 175  0
             writer.println("<li><a href=\"" + resourceName + "/\">" + resourceName + "</a></li>");
 176  
         }
 177  
         else
 178  
         {
 179  0
             writer.println("<li><a href=\"" + resourceName + "\">" + resourceName + "</a></li>");
 180  
         }
 181  0
     }    
 182  
 }