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 resourceLookupLanguages];
135 uFi = [rm urlForResourceNamed:uFi
136 inFramework:frameworkName
138 request:[_ctx request]];
140 NSLog(@"%@: did not find resource '%@'", sComponent,
141 [self->filename stringValueInComponent:sComponent]);
144 [_response appendContentHTMLAttributeValue:uFi];
146 else if (self->data != nil) {
149 if ((kk = [self->key stringValueInComponent:sComponent])) {
151 WOResourceManager *rm;
153 if ((rm = [[_ctx component] resourceManager]) == nil)
154 rm = [[_ctx application] resourceManager];
156 [rm setData:[self->data valueInComponent:sComponent]
158 mimeType:[self->mimeType stringValueInComponent:sComponent]
159 session:[_ctx hasSession] ? [_ctx session] : nil];
161 url = [_ctx urlWithRequestHandlerKey:
162 [WOApplication resourceRequestHandlerKey]
163 path:[@"/" stringByAppendingString:kk]
166 WOResponse_AddString(_response, url);
169 /* a component action link */
170 uFi = [_ctx componentActionURL];
171 WOResponse_AddString(_response, uFi);
175 [sComponent logWithFormat:@"missing resource URL for element %@", self];
179 [self->template appendToResponse:_response inContext:_ctx];
184 - (NSString *)associationDescription {
185 NSMutableString *str = [[NSMutableString alloc] init];
187 if (self->filename) [str appendFormat:@" filename=%@", self->filename];
188 if (self->framework) [str appendFormat:@" framework=%@", self->framework];
189 if (self->data) [str appendFormat:@" data=%@", self->data];
190 if (self->mimeType) [str appendFormat:@" mimeType=%@", self->mimeType];
191 if (self->key) [str appendFormat:@" key=%@", self->key];
193 return AUTORELEASE(str);
196 @end /* WOResourceURL */