2 Copyright (C) 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>
25 WOSetHeader / <var:set-header/>
27 This element can set/add a header field using -setHeader:forKey:. Usually its
28 used with a WOResponse (context.response is the default 'object'), but can be
29 used with arbitary objects implementing the same API (eg context.request).
32 ChangeContentType: WOSetHeader {
33 header = "content-type";
38 header|key|name - name of header (should be lowercase for WOResponse)
39 value - value to apply
40 addToExisting - use -appendHeader:forKey: or -setHeader:forKey:?
41 object - object to manipulate (defaults to [context response])
44 @interface WOSetHeader : WODynamicElement // TODO: should be WOElement?
46 WOAssociation *object;
47 WOAssociation *header;
49 WOAssociation *addToExisting;
54 #include <NGObjWeb/WOAssociation.h>
55 #include <NGObjWeb/WOMessage.h>
56 #include <NGObjWeb/WOContext.h>
59 @implementation WOSetHeader
61 - (id)initWithName:(NSString *)_name
62 associations:(NSDictionary *)_config
63 template:(WOElement *)_c
65 if ((self = [super initWithName:_name associations:_config template:_c])) {
66 self->header = OWGetProperty(_config, @"header");
67 self->value = OWGetProperty(_config, @"value");
68 self->addToExisting = OWGetProperty(_config, @"addToExisting");
69 self->object = OWGetProperty(_config, @"object");
71 if (self->header == nil) self->header = OWGetProperty(_config, @"key");
72 if (self->header == nil) self->header = OWGetProperty(_config, @"name");
78 [self->object release];
79 [self->header release];
80 [self->value release];
81 [self->addToExisting release];
85 /* generating response */
87 - (id)objectForKey:(NSString *)_key inContext:(WOContext *)_ctx {
88 if (![_key isNotEmpty])
91 if ([_key isEqualToString:@"response"])
92 return [_ctx response];
93 if ([_key isEqualToString:@"request"])
94 return [_ctx request];
96 [self errorWithFormat:@"Unknown object key: '%@'", _key];
100 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
105 doAdd = (self->addToExisting != nil)
106 ? [self->addToExisting boolValueInContext:_ctx]
109 k = [self->header stringValueInContext:_ctx];
110 v = [self->value stringValueInContext:_ctx];
112 /* determine object to manipulate */
114 lObject = (self->object != nil)
115 ? [self->object valueInContext:_ctx]
116 : (id)[_ctx response];
117 if ([lObject isKindOfClass:[NSString class]])
118 lObject = [self objectForKey:(NSString *)lObject inContext:_ctx];
124 [lObject appendHeader:v forKey:k];
127 [lObject setHeader:([v isNotNull] ? v : (NSString *)nil) forKey:k];
130 @end /* WOSetHeader */