-------------------------------------------------------------------------------- -- Title: LWDAVResponse.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: February 1, 2005 -- Legal: Copyright (C) 2005 Raphaël Szwarc -------------------------------------------------------------------------------- -- import dependencies local LWComponent = require( "LWComponent" ) local LWLink = require( "LWLink" ) local LWResponse = require( "LWResponse" ) local LWString = require( "LWString" ) local MIMEType = require( "MIMEType" ) local LFSFile = require( "LFSFile" ) local LUList = require( "LUList" ) local LUString = require( "LUString" ) -- define the class local super = LWComponent local self = super() -- method to access this component depth function self:depth() return self:bindings():get( "depth" ) or 0 end -- method to access this component object function self:object() return self:bindings():get( "object" ) end -- method to access this component resources function self:resources() local someResources = LUList() local anObject = self:object() someResources:add( anObject ) if self:depth() > 0 and anObject:isDirectory() == true then local aPath = anObject:path() for aName in anObject:iterator( self:filter() ) do local aResource = LFSFile( aPath, aName ) someResources:add( aResource ) end end return someResources end -- method to access a given resource href function self:href( aResource ) local aService = self:context():service() local aValue = aService:pathWithObject( aResource, self:context() ) return LWLink:encode( aValue ) end -- method to access a given resource display name function self:displayName( aResource ) local aValue = aResource:name() return LWString:encode( aValue ) end -- method to access a given resource content length function self:contentLength( aResource ) return tostring( aResource:size() ) end -- method to access a given resource display name function self:contentType( aResource ) if aResource:isFile() == true then return MIMEType:typeWithExtension( aResource:extension() ) end return "text/html" end -- method to access a given resource modification date function self:lastModified( aResource ) local aValue = aResource:lastModified() return LWResponse:stringWithTime( aValue ) end -- method to access a given resource etag function self:etag( aResource ) if aResource == nil then error( "nil", 2 ) end return aResource:id() end -- method to access a given resource type function self:resourceType( aResource ) if aResource:isDirectory() == true then return "" end return nil end -- method for string representation function self:toString() local aTemplate = self:template() local someResources = self:resources() aTemplate:put( "encoding", LWResponse:encoding() ) for anIndex, aResource in someResources:iterator() do local aResourceTemplate = aTemplate:get( "resources" ) aResourceTemplate:put( "href", self:href( aResource ) ) aResourceTemplate:put( "displayName", self:displayName( aResource ) ) aResourceTemplate:put( "contentLength", self:contentLength( aResource ) ) aResourceTemplate:put( "contentType", self:contentType( aResource ) ) aResourceTemplate:put( "etag", self:etag( aResource ) ) aResourceTemplate:put( "lastModified", self:lastModified( aResource ) ) aResourceTemplate:put( "resourceType", self:resourceType( aResource ) ) aTemplate:put( "resources", aResourceTemplate ) end self:context():response():setStatus( 207 ) self:context():response():headers():put( "content-type", "application/xml; charset=" .. LWResponse:encoding() ) self:context():response():headers():put( "etag", self:etag( self:object() ) ) self:context():response():headers():put( "last-modified", self:lastModified( self:object() ) ) return aTemplate:toString() end -- method to access the component file filter for a given context function self:filter() --[[ return function( aDirectory, aFileName ) if aFileName ~= nil and aFileName:len() > 1 and LUString:startsWith( aFileName, "." ) == false and LUString:endsWith( aFileName, ".temp" ) == false and aFileName:find( "%.%." ) == nil then local _, count = aFileName:gsub( "%c", "" ) if count == 0 then return true end end return false end ]]-- end return self