2
Copyright (C) 2000-2005 SKYRIX Software AG
4
This file is part of SOPE.
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
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.
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
22
#include <NGObjWeb/WODynamicElement.h>
24
@interface WOResourceURL : WODynamicElement
26
// WODynamicElement: extraAttributes
27
// WODynamicElement: otherTagString
29
WOAssociation *filename; // path relative to WebServerResources
30
WOAssociation *framework;
31
WOAssociation *data; // data (eg from a database)
32
WOAssociation *mimeType; // the type of data
33
WOAssociation *key; // the cache key
34
WOElement *template; // subelements
39
#include "WOElement+private.h"
40
#include <NGObjWeb/WOApplication.h>
41
#include <NGObjWeb/WOResourceManager.h>
44
@implementation WOResourceURL
46
- (id)initWithName:(NSString *)_name
47
associations:(NSDictionary *)_config
48
template:(WOElement *)_tmpl
50
if ((self = [super initWithName:_name associations:_config template:_tmpl])) {
51
self->template = [_tmpl retain];
52
self->filename = OWGetProperty(_config, @"filename");
53
self->framework = OWGetProperty(_config, @"framework");
54
self->data = OWGetProperty(_config, @"data");
55
self->mimeType = OWGetProperty(_config, @"mimeType");
56
self->key = OWGetProperty(_config, @"key");
62
RELEASE(self->template);
63
RELEASE(self->framework);
64
RELEASE(self->filename);
67
RELEASE(self->mimeType);
73
#define StrVal(__x__) [self->__x__ stringValueInComponent:sComponent]
75
/* request handling */
77
- (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
78
[self->template takeValuesFromRequest:_req inContext:_ctx];
81
- (id)invokeActionForRequest:(WORequest *)_request
82
inContext:(WOContext *)_ctx
84
WOComponent *sComponent = [_ctx component];
89
if (self->data == nil)
90
return [self->template invokeActionForRequest:_request inContext:_ctx];
92
adata = [self->data valueInComponent:sComponent];
93
atype = [self->mimeType stringValueInComponent:sComponent];
95
response = [_ctx response];
97
[response setContent:adata];
98
[response setHeader:atype ? atype : @"application/octet-stream"
99
forKey:@"content-type"];
104
/* response generation */
106
- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
107
WOComponent *sComponent;
110
if ([[_ctx request] isFromClientComponent])
113
sComponent = [_ctx component];
115
uFi = [self->filename stringValueInComponent:sComponent];
118
WOResourceManager *rm;
119
NSString *frameworkName;
122
if ((rm = [[_ctx component] resourceManager]) == nil)
123
rm = [[_ctx application] resourceManager];
125
/* If 'framework' binding is not set, use parent component's framework */
126
if (self->framework){
127
frameworkName = [self->framework stringValueInComponent:sComponent];
128
if (frameworkName != nil && [frameworkName isEqualToString:@"app"])
132
frameworkName = [sComponent frameworkName];
134
languages = [_ctx hasSession]
135
? [[_ctx session] languages]
136
: [[_ctx request] browserLanguages];
137
uFi = [rm urlForResourceNamed:uFi
138
inFramework:frameworkName
140
request:[_ctx request]];
142
NSLog(@"%@: did not find resource '%@'", sComponent,
143
[self->filename stringValueInComponent:sComponent]);
146
[_response appendContentHTMLAttributeValue:uFi];
148
else if (self->data != nil) {
151
if ((kk = [self->key stringValueInComponent:sComponent])) {
153
WOResourceManager *rm;
155
if ((rm = [[_ctx component] resourceManager]) == nil)
156
rm = [[_ctx application] resourceManager];
158
[rm setData:[self->data valueInComponent:sComponent]
160
mimeType:[self->mimeType stringValueInComponent:sComponent]
161
session:[_ctx hasSession] ? [_ctx session] : nil];
163
url = [_ctx urlWithRequestHandlerKey:
164
[WOApplication resourceRequestHandlerKey]
165
path:[@"/" stringByAppendingString:kk]
168
WOResponse_AddString(_response, url);
171
/* a component action link */
172
uFi = [_ctx componentActionURL];
173
WOResponse_AddString(_response, uFi);
177
[sComponent logWithFormat:@"missing resource URL for element %@", self];
181
[self->template appendToResponse:_response inContext:_ctx];
186
- (NSString *)associationDescription {
187
NSMutableString *str = [[NSMutableString alloc] init];
189
if (self->filename) [str appendFormat:@" filename=%@", self->filename];
190
if (self->framework) [str appendFormat:@" framework=%@", self->framework];
191
if (self->data) [str appendFormat:@" data=%@", self->data];
192
if (self->mimeType) [str appendFormat:@" mimeType=%@", self->mimeType];
193
if (self->key) [str appendFormat:@" key=%@", self->key];
195
return AUTORELEASE(str);
198
@end /* WOResourceURL */