~ubuntu-branches/ubuntu/edgy/sope/edgy

« back to all changes in this revision

Viewing changes to sope-appserver/SoOFS/OFSFileRenderer.m

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Ley
  • Date: 2005-08-19 16:53:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050819165331-hs683wz1osm708pw
Tags: upstream-4.4rc.2
ImportĀ upstreamĀ versionĀ 4.4rc.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2002-2005 SKYRIX Software AG
 
3
 
 
4
  This file is part of SOPE.
 
5
 
 
6
  SOPE 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
 
9
  later version.
 
10
 
 
11
  SOPE 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.
 
15
 
 
16
  You should have received a copy of the GNU Lesser General Public
 
17
  License along with SOPE; see the file COPYING.  If not, write to the
 
18
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 
19
  02111-1307, USA.
 
20
*/
 
21
 
 
22
#include "OFSFileRenderer.h"
 
23
#include "OFSFile.h"
 
24
#include <NGObjWeb/WORequest.h>
 
25
#include <NGObjWeb/WOResponse.h>
 
26
#include <NGObjWeb/WOContext.h>
 
27
#include "NSException+HTTP.h"
 
28
#include "common.h"
 
29
 
 
30
@interface OFSFile(Render)
 
31
 
 
32
- (id)davContentLength;
 
33
- (NSDate *)davLastModified;
 
34
 
 
35
@end
 
36
 
 
37
@implementation OFSFileRenderer
 
38
 
 
39
static NSTimeZone *gmt = nil;
 
40
 
 
41
+ (void)initialize {
 
42
  gmt = [[NSTimeZone timeZoneWithAbbreviation:@"GMT"] retain];
 
43
}
 
44
 
 
45
+ (id)sharedRenderer {
 
46
  static OFSFileRenderer *singleton = nil;
 
47
  if (singleton == nil)
 
48
    singleton = [[OFSFileRenderer alloc] init];
 
49
  return singleton;
 
50
}
 
51
 
 
52
/* rendering */
 
53
 
 
54
- (NSException *)renderHeadOfObject:(id)_object inContext:(WOContext *)_ctx {
 
55
  WOResponse *r;
 
56
  id tmp;
 
57
  
 
58
  r = [_ctx response];
 
59
  
 
60
  /* render headers */
 
61
  
 
62
  if ((tmp = [_object contentTypeInContext:_ctx]))
 
63
    [r setHeader:tmp forKey:@"content-type"];
 
64
  if ((tmp = [_object davContentLength]))
 
65
    [r setHeader:tmp forKey:@"content-length"];
 
66
  
 
67
  if ((tmp = [_object davLastModified])) {
 
68
    NSCalendarDate *date;
 
69
 
 
70
#if COCOA_Foundation_LIBRARY
 
71
    date = [[NSCalendarDate alloc] initWithTimeIntervalSinceReferenceDate:
 
72
                                     [tmp timeIntervalSinceReferenceDate]];
 
73
#else
 
74
    date = [[NSCalendarDate alloc] initWithTimeIntervalSince1970:
 
75
                                     [tmp timeIntervalSince1970]];
 
76
#endif
 
77
    [date setTimeZone:gmt];
 
78
    
 
79
    // "Tue, 10 Jul 2001 14:09:06 GMT"
 
80
    tmp = [date descriptionWithCalendarFormat:@"%a, %d %b %Y %H:%M:%S GMT"];
 
81
    [date release];
 
82
    [r setHeader:tmp forKey:@"last-modified"];
 
83
  }
 
84
  
 
85
  return nil;
 
86
}
 
87
 
 
88
- (NSException *)renderBodyOfObject:(id)_object inContext:(WOContext *)_ctx {
 
89
  WOResponse *r;
 
90
  NSData     *content;
 
91
  NSString   *storePath;
 
92
  id fm;
 
93
  
 
94
  fm        = [_object fileManager];
 
95
  storePath = [_object storagePath];
 
96
  content   = [fm contentsAtPath:storePath];
 
97
  
 
98
  /* some error handling */
 
99
  
 
100
  if (content == nil) {
 
101
    // TODO: should render exception ?
 
102
    if ([fm respondsToSelector:@selector(lastException)])
 
103
      return (id)[fm lastException];
 
104
    return [NSException exceptionWithHTTPStatus:404 /* not found */];
 
105
  }
 
106
  
 
107
  /* render body */
 
108
  r = [_ctx response];
 
109
  [r setContent:content];
 
110
  return nil;
 
111
}
 
112
 
 
113
- (NSException *)renderObject:(id)_object inContext:(WOContext *)_ctx {
 
114
  NSException *e;
 
115
  
 
116
  if ((e = [self renderHeadOfObject:_object inContext:_ctx]))
 
117
    return e;
 
118
  
 
119
  if (![[[_ctx request] method] isEqualToString:@"HEAD"]) {
 
120
    if ((e = [self renderBodyOfObject:_object inContext:_ctx]))
 
121
      return e;
 
122
  }
 
123
  return nil;
 
124
}
 
125
 
 
126
- (BOOL)canRenderObject:(id)_object inContext:(WOContext *)_ctx {
 
127
  return [_object isKindOfClass:[OFSFile class]];
 
128
}
 
129
 
 
130
@end /* OFSFileRenderer */