View Javadoc

1   package org.apache.continuum.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.io.File;
27  import java.io.PrintWriter;
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collections;
31  import java.util.Date;
32  import java.util.List;
33  
34  public class IndexWriter
35  {
36      private final String logicalResource;
37  
38      private final List<File> localResources;
39  
40      public IndexWriter( DavResource resource, File localResource, String logicalResource )
41      {
42          this.localResources = new ArrayList<File>();
43          this.localResources.add( localResource );
44          this.logicalResource = logicalResource;
45      }
46  
47      public IndexWriter( DavResource resource, List<File> localResources, String logicalResource )
48      {
49          this.localResources = localResources;
50          this.logicalResource = logicalResource;
51      }
52  
53      public void write( OutputContext outputContext )
54      {
55          outputContext.setModificationTime( new Date().getTime() );
56          outputContext.setContentType( "text/html" );
57          outputContext.setETag( "" );
58          if ( outputContext.hasStream() )
59          {
60              PrintWriter writer = new PrintWriter( outputContext.getOutputStream() );
61              writeDocumentStart( writer );
62              writeHyperlinks( writer );
63              writeDocumentEnd( writer );
64              writer.flush();
65              writer.close();
66          }
67      }
68  
69      private void writeDocumentStart( PrintWriter writer )
70      {
71          writer.println( "<html>" );
72          writer.println( "<head>" );
73          writer.println( "<title>Working Copy: /" + logicalResource + "</title>" );
74          writer.println( "</head>" );
75          writer.println( "<body>" );
76          writer.println( "<h3>Working Copy: /" + logicalResource + "</h3>" );
77  
78          // Check if not root
79          if ( logicalResource.length() > 0 )
80          {
81              File file = new File( logicalResource );
82              String parentName = file.getParent() == null ? "/" : file.getParent();
83  
84              //convert to unix path in case archiva is hosted on windows
85              parentName = StringUtils.replace( parentName, "\\", "/" );
86  
87              writer.println( "<ul>" );
88              writer.println( "<li><a href=\"../\">" + parentName + "</a> <i><small>(Parent)</small></i></li>" );
89              writer.println( "</ul>" );
90          }
91  
92          writer.println( "</ul>" );
93      }
94  
95      private void writeDocumentEnd( PrintWriter writer )
96      {
97          writer.println( "</ul>" );
98          writer.println( "</body>" );
99          writer.println( "</html>" );
100     }
101 
102     private void writeHyperlinks( PrintWriter writer )
103     {
104         for ( File localResource : localResources )
105         {
106             List<File> files = new ArrayList<File>( Arrays.asList( localResource.listFiles() ) );
107             Collections.sort( files );
108 
109             for ( File file : files )
110             {
111                 writeHyperlink( writer, file.getName(), file.isDirectory() );
112             }
113         }
114     }
115 
116     private void writeHyperlink( PrintWriter writer, String resourceName, boolean directory )
117     {
118         if ( directory && !"CVS".equals( resourceName ) && !".svn".equals( resourceName ) && !"SCCS".equals(
119             resourceName ) )
120         {
121             writer.println( "<li><a href=\"" + resourceName + "/\">" + resourceName + "</a></li>" );
122         }
123         else if ( !directory && !".cvsignore".equals( resourceName ) && !"vssver.scc".equals( resourceName ) &&
124             !".DS_Store".equals( resourceName ) && !"release.properties".equals( resourceName ) )
125         {
126             writer.println( "<li><a href=\"" + resourceName + "\">" + resourceName + "</a></li>" );
127         }
128     }
129 }