]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOResourceURL.m
minor improvement to WOHttpAdaptor, bumped framework revisions
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOResourceURL.m
1 /*
2   Copyright (C) 2000-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 <NGObjWeb/WODynamicElement.h>
23
24 @interface WOResourceURL : WODynamicElement
25 {
26   // WODynamicElement: extraAttributes
27   // WODynamicElement: otherTagString
28 @protected
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
35 }
36
37 @end
38
39 #include "WOElement+private.h"
40 #include <NGObjWeb/WOApplication.h>
41 #include <NGObjWeb/WOResourceManager.h>
42 #include "decommon.h"
43
44 @implementation WOResourceURL
45
46 - (id)initWithName:(NSString *)_name
47   associations:(NSDictionary *)_config
48   template:(WOElement *)_tmpl
49 {
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");
57   }
58   return self;
59 }
60
61 - (void)dealloc {
62   RELEASE(self->template);
63   RELEASE(self->framework);
64   RELEASE(self->filename);
65   RELEASE(self->data);
66   RELEASE(self->key);
67   RELEASE(self->mimeType);
68   [super dealloc];
69 }
70
71 /* responder */
72
73 #define StrVal(__x__) [self->__x__ stringValueInComponent:sComponent]
74
75 /* request handling */
76
77 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
78   [self->template takeValuesFromRequest:_req inContext:_ctx];
79 }
80
81 - (id)invokeActionForRequest:(WORequest *)_request
82   inContext:(WOContext *)_ctx
83 {
84   WOComponent *sComponent = [_ctx component];
85   NSData     *adata;
86   NSString   *atype;
87   WOResponse *response;
88
89   if (self->data == nil)
90     return [self->template invokeActionForRequest:_request inContext:_ctx];
91   
92   adata = [self->data     valueInComponent:sComponent];
93   atype = [self->mimeType stringValueInComponent:sComponent];
94
95   response = [_ctx response];
96     
97   [response setContent:adata];
98   [response setHeader:atype ? atype : @"application/octet-stream"
99             forKey:@"content-type"];
100     
101   return response;
102 }
103
104 /* response generation */
105
106 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
107   WOComponent *sComponent;
108   NSString    *uFi;
109   
110   if ([[_ctx request] isFromClientComponent])
111     return;
112   
113   sComponent = [_ctx component];
114     
115   uFi = [self->filename stringValueInComponent:sComponent];
116   
117   if (uFi) {
118     WOResourceManager *rm;
119     NSString          *frameworkName;
120     NSArray           *languages;
121     
122     if ((rm = [[_ctx component] resourceManager]) == nil)
123       rm = [[_ctx application] resourceManager];
124     
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"])
129         frameworkName = nil;
130     }
131     else
132       frameworkName = [sComponent frameworkName];
133     
134     languages = [_ctx resourceLookupLanguages];
135     uFi       = [rm urlForResourceNamed:uFi
136                     inFramework:frameworkName
137                     languages:languages
138                      request:[_ctx request]];
139     if (uFi == nil) {
140       NSLog(@"%@: did not find resource '%@'", sComponent,
141             [self->filename stringValueInComponent:sComponent]);
142     }
143     else
144       [_response appendContentHTMLAttributeValue:uFi];
145   }
146   else if (self->data != nil) {
147     NSString *kk;
148     
149     if ((kk = [self->key stringValueInComponent:sComponent])) {
150       NSString          *url;
151       WOResourceManager *rm;
152       
153       if ((rm = [[_ctx component] resourceManager]) == nil)
154         rm = [[_ctx application] resourceManager];
155     
156       [rm setData:[self->data valueInComponent:sComponent]
157           forKey:kk
158           mimeType:[self->mimeType stringValueInComponent:sComponent]
159           session:[_ctx hasSession] ? [_ctx session] : nil];
160       
161       url = [_ctx urlWithRequestHandlerKey:
162                     [WOApplication resourceRequestHandlerKey]
163                   path:[@"/" stringByAppendingString:kk]
164                   queryString:nil];
165       
166       WOResponse_AddString(_response, url);
167     }
168     else {
169       /* a component action link */
170       uFi = [_ctx componentActionURL];
171       WOResponse_AddString(_response, uFi);
172     }
173   }
174   else {
175     [sComponent logWithFormat:@"missing resource URL for element %@", self];
176   }
177   
178   /* content */
179   [self->template appendToResponse:_response inContext:_ctx];
180 }
181
182 /* description */
183
184 - (NSString *)associationDescription {
185   NSMutableString *str = [[NSMutableString alloc] init];
186
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];
192   
193   return AUTORELEASE(str);
194 }
195
196 @end /* WOResourceURL */