001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.providers; 020 021import java.io.File; 022import java.io.FileInputStream; 023import java.io.FileOutputStream; 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.OutputStream; 027import java.util.Properties; 028 029import org.apache.log4j.Logger; 030import org.apache.wiki.WikiPage; 031import org.apache.wiki.api.exceptions.ProviderException; 032 033/** 034 * Provides a simple directory based repository for Wiki pages. 035 * <P> 036 * All files have ".txt" appended to make life easier for those 037 * who insist on using Windows or other software which makes assumptions 038 * on the files contents based on its name. 039 * 040 */ 041public class FileSystemProvider 042 extends AbstractFileProvider 043{ 044 private static final Logger log = Logger.getLogger(FileSystemProvider.class); 045 /** 046 * All metadata is stored in a file with this extension. 047 */ 048 public static final String PROP_EXT = ".properties"; 049 050 /** 051 * {@inheritDoc} 052 */ 053 public void putPageText( WikiPage page, String text ) 054 throws ProviderException 055 { 056 try 057 { 058 super.putPageText( page, text ); 059 putPageProperties( page ); 060 } 061 catch( IOException e ) 062 { 063 log.error( "Saving failed" ); 064 } 065 } 066 067 /** 068 * Stores basic metadata to a file. 069 */ 070 private void putPageProperties( WikiPage page ) 071 throws IOException 072 { 073 Properties props = new Properties(); 074 OutputStream out = null; 075 076 try 077 { 078 String author = page.getAuthor(); 079 String changenote = (String)page.getAttribute( WikiPage.CHANGENOTE ); 080 String viewcount = (String)page.getAttribute( WikiPage.VIEWCOUNT ); 081 082 if( author != null ) 083 { 084 props.setProperty( WikiPage.AUTHOR, author ); 085 } 086 087 if( changenote != null ) 088 { 089 props.setProperty( WikiPage.CHANGENOTE, changenote ); 090 } 091 092 if( viewcount != null ) 093 { 094 props.setProperty( WikiPage.VIEWCOUNT, viewcount ); 095 } 096 097 // Get additional custom properties from page and add to props 098 getCustomProperties(page, props); 099 100 101 File file = new File( getPageDirectory(), 102 mangleName(page.getName())+PROP_EXT ); 103 104 out = new FileOutputStream( file ); 105 106 props.store( out, "JSPWiki page properties for page "+page.getName() ); 107 } 108 finally 109 { 110 if( out != null ) out.close(); 111 } 112 } 113 114 /** 115 * Gets basic metadata from file. 116 */ 117 private void getPageProperties( WikiPage page ) 118 throws IOException 119 { 120 Properties props = new Properties(); 121 InputStream in = null; 122 123 try 124 { 125 File file = new File( getPageDirectory(), 126 mangleName(page.getName())+PROP_EXT ); 127 128 if( file.exists() ) 129 { 130 in = new FileInputStream( file ); 131 132 props.load(in); 133 134 page.setAuthor( props.getProperty( WikiPage.AUTHOR ) ); 135 136 String changenote = props.getProperty( WikiPage.CHANGENOTE ); 137 if( changenote != null ) 138 { 139 page.setAttribute( WikiPage.CHANGENOTE, changenote ); 140 } 141 142 String viewcount = props.getProperty( WikiPage.VIEWCOUNT ); 143 if( viewcount != null ) 144 { 145 page.setAttribute( WikiPage.VIEWCOUNT, viewcount ); 146 } 147 148 // Set the props values to the page attributes 149 setCustomProperties(page, props); 150 } 151 } 152 finally 153 { 154 if( in != null ) in.close(); 155 } 156 } 157 158 /** 159 * {@inheritDoc} 160 */ 161 public WikiPage getPageInfo( String page, int version ) 162 throws ProviderException 163 { 164 WikiPage p = super.getPageInfo( page, version ); 165 166 if( p != null ) 167 { 168 try 169 { 170 getPageProperties( p ); 171 } 172 catch( IOException e ) 173 { 174 log.error("Unable to read page properties", e ); 175 throw new ProviderException("Unable to read page properties, check logs."); 176 } 177 } 178 179 return p; 180 } 181 182 /** 183 * {@inheritDoc} 184 */ 185 public void deletePage(String pageName) throws ProviderException 186 { 187 super.deletePage(pageName); 188 189 File file = new File( getPageDirectory(), 190 mangleName(pageName)+PROP_EXT ); 191 192 if( file.exists() ) file.delete(); 193 } 194 195 /** 196 * {@inheritDoc} 197 */ 198 public void movePage( String from, 199 String to ) 200 throws ProviderException 201 { 202 File fromPage = findPage( from ); 203 File toPage = findPage( to ); 204 205 fromPage.renameTo( toPage ); 206 } 207}