2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 // $Id: OFSFileRenderer.m 4 2004-08-20 17:04:31Z helge $
23 #include "OFSFileRenderer.h"
25 #include <NGObjWeb/WORequest.h>
26 #include <NGObjWeb/WOResponse.h>
27 #include <NGObjWeb/WOContext.h>
28 #include "NSException+HTTP.h"
31 @interface OFSFile(Render)
33 - (id)davContentLength;
34 - (NSDate *)davLastModified;
38 @implementation OFSFileRenderer
40 static NSTimeZone *gmt = nil;
43 gmt = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
46 + (id)sharedRenderer {
47 static OFSFileRenderer *singleton = nil;
49 singleton = [[OFSFileRenderer alloc] init];
55 - (NSException *)renderHeadOfObject:(id)_object inContext:(WOContext *)_ctx {
63 if ((tmp = [_object contentTypeInContext:_ctx]))
64 [r setHeader:tmp forKey:@"content-type"];
65 if ((tmp = [_object davContentLength]))
66 [r setHeader:tmp forKey:@"content-length"];
68 if ((tmp = [_object davLastModified])) {
71 #if COCOA_Foundation_LIBRARY
72 date = [[NSCalendarDate alloc] initWithTimeIntervalSinceReferenceDate:
73 [tmp timeIntervalSinceReferenceDate]];
75 date = [[NSCalendarDate alloc] initWithTimeIntervalSince1970:
76 [tmp timeIntervalSince1970]];
78 [date setTimeZone:gmt];
80 // "Tue, 10 Jul 2001 14:09:06 GMT"
81 tmp = [date descriptionWithCalendarFormat:@"%a, %d %b %Y %H:%M:%S GMT"];
83 [r setHeader:tmp forKey:@"last-modified"];
89 - (NSException *)renderBodyOfObject:(id)_object inContext:(WOContext *)_ctx {
95 fm = [_object fileManager];
96 storePath = [_object storagePath];
97 content = [fm contentsAtPath:storePath];
99 /* some error handling */
101 if (content == nil) {
102 // TODO: should render exception ?
103 if ([fm respondsToSelector:@selector(lastException)])
104 return (id)[fm lastException];
105 return [NSException exceptionWithHTTPStatus:404 /* not found */];
110 [r setContent:content];
114 - (NSException *)renderObject:(id)_object inContext:(WOContext *)_ctx {
117 if ((e = [self renderHeadOfObject:_object inContext:_ctx]))
120 if (![[[_ctx request] method] isEqualToString:@"HEAD"]) {
121 if ((e = [self renderBodyOfObject:_object inContext:_ctx]))
127 - (BOOL)canRenderObject:(id)_object inContext:(WOContext *)_ctx {
128 return [_object isKindOfClass:[OFSFile class]];
131 @end /* OFSFileRenderer */