]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOEmbeddedObject.m
minor improvement to WOHttpAdaptor, bumped framework revisions
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOEmbeddedObject.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 "WOHTMLDynamicElement.h"
23 #include "WOElement+private.h"
24 #include <NGObjWeb/WOResourceManager.h>
25 #include <NGObjWeb/WOApplication.h>
26 #include "decommon.h"
27
28 @interface WOEmbeddedObject : WOHTMLDynamicElement
29 {
30   // WODynamicElement: extraAttributes
31   // WODynamicElement: otherTagString
32 @protected
33   WOAssociation *filename;  // path relative to WebServerResources
34   WOAssociation *framework;
35   WOAssociation *src;       // absolute URL
36   WOAssociation *value;     // data (eg from a database)
37
38   /* new in WO4 */
39   WOAssociation *data;
40   WOAssociation *mimeType;
41   WOAssociation *key;
42 }
43
44 @end /* WOEmbeddedObject */
45
46 @implementation WOEmbeddedObject
47
48 - (id)initWithName:(NSString *)_name
49   associations:(NSDictionary *)_config
50   template:(WOElement *)_tmpl
51 {
52   if ((self = [super initWithName:_name associations:_config template:_tmpl])) {
53     self->filename  = OWGetProperty(_config, @"filename");
54     self->framework = OWGetProperty(_config, @"framework");
55     self->src       = OWGetProperty(_config, @"src");
56     self->value     = OWGetProperty(_config, @"value");
57
58     self->data      = OWGetProperty(_config, @"data");
59     self->mimeType  = OWGetProperty(_config, @"mimeType");
60     self->key       = OWGetProperty(_config, @"key");
61
62     if (self->key)
63       NSLog(@"WARNING: 'key' association in WOEmbeddedObject not supported !");
64   }
65   return self;
66 }
67
68 #if !LIB_FOUNDATION_BOEHM_GC
69 - (void)dealloc {
70   RELEASE(self->key);
71   RELEASE(self->data);
72   RELEASE(self->mimeType);
73   RELEASE(self->framework);
74   RELEASE(self->filename);
75   RELEASE(self->src);
76   RELEASE(self->value);
77   [super dealloc];
78 }
79 #endif
80
81 // ******************** responder ********************
82
83 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
84   if (self->value) {
85     WOElement *element;
86
87     if ((element = [self->value valueInComponent:[_ctx component]]) == nil) {
88       [[_ctx session] debugWithFormat:
89           @"WARNING: missing element value for WOEmbeddedObject %@", self];
90       return nil;
91     }
92
93     [element appendToResponse:[_ctx response] inContext:_ctx];
94     return [_ctx response];
95   }
96   else if (self->data) {
97     WOComponent *sComponent;
98     NSData     *adata;
99     NSString   *atype;
100     WOResponse *response;
101     
102     sComponent = [_ctx component];
103     adata = [self->data     valueInComponent:sComponent];
104     atype = [self->mimeType stringValueInComponent:sComponent];
105     
106     response = [_ctx response];
107     
108     [response setContent:adata];
109     [response setHeader:atype ? atype : @"application/octet-stream"
110               forKey:@"content-type"];
111     
112     return response;
113   }
114   else {
115     [[_ctx session] debugWithFormat:
116                       @"no value configured for WOEmbeddedObject %@", self];
117     return nil;
118   }
119 }
120
121 #define StrVal(__x__) [self->__x__ stringValueInComponent:sComponent]
122
123 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
124   if (![[_ctx request] isFromClientComponent]) {
125     WOComponent *sComponent = [_ctx component];
126     NSString *uUri = [self->src      stringValueInComponent:sComponent];
127     NSString *uFi  = [self->filename stringValueInComponent:sComponent];
128     NSArray  *languages;
129
130     sComponent = [_ctx component];
131     uUri = [self->src      stringValueInComponent:sComponent];
132     uFi  = [self->filename stringValueInComponent:sComponent];
133     
134     WOResponse_AddCString(_response, "<embed src=\"");
135     
136     if ((self->data != nil) || (self->value != nil)) {
137       /* a component action link */
138       uUri = [_ctx componentActionURL];
139       if (uFi) {
140         uUri = [uUri stringByAppendingString:@"/"];
141         uUri = [uUri stringByAppendingString:uFi];
142       }
143       WOResponse_AddString(_response, uUri);
144     }
145     else if (uFi) {
146       WOResourceManager *rm;
147       NSString  *frameworkName;
148       
149       if ((rm = [[_ctx component] resourceManager]) == nil)
150         rm = [[_ctx application] resourceManager];
151       
152       /* If 'framework' binding is not set, use parent component's framework */
153       if (self->framework){
154         frameworkName = [self->framework stringValueInComponent:[_ctx component]];
155         if (frameworkName != nil && [frameworkName isEqualToString:@"app"])
156           frameworkName = nil;
157       }
158       else
159         frameworkName = [[_ctx component] frameworkName];
160       
161       languages = [_ctx resourceLookupLanguages];
162       uFi       = [rm urlForResourceNamed:uFi
163                       inFramework:frameworkName
164                       languages:languages
165                       request:[_ctx request]];
166       if (uFi == nil) {
167         NSLog(@"%@: did not find resource '%@'", sComponent,
168               [self->filename stringValueInComponent:sComponent]);
169         uFi = uUri;
170       }
171       [_response appendContentHTMLAttributeValue:uFi];
172     }
173     else if (uUri) {
174       [_response appendContentHTMLAttributeValue:uUri];
175     }
176     else {
177       [sComponent logWithFormat:@"missing resource URL for element %@", self];
178     }
179     
180     WOResponse_AddChar(_response, '"');
181   
182     [self appendExtraAttributesToResponse:_response inContext:_ctx];
183     if (self->otherTagString) {
184       WOResponse_AddChar(_response, ' ');
185       WOResponse_AddString(_response,
186                            [self->otherTagString stringValueInComponent:
187                                                    [_ctx component]]);
188     }
189     WOResponse_AddEmptyCloseParens(_response, _ctx);
190   }
191 }
192
193 // description
194
195 - (NSString *)associationDescription {
196   NSMutableString *str = [[NSMutableString alloc] init];
197
198   if (self->filename)  [str appendFormat:@" filename=%@",  self->filename];
199   if (self->framework) [str appendFormat:@" framework=%@", self->framework];
200   if (self->src)       [str appendFormat:@" src=%@",       self->src];
201   if (self->value)     [str appendFormat:@" value=%@",     self->value];
202   
203   return AUTORELEASE(str);
204 }
205
206 @end /* WOEmbeddedObject */