]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOSetHeader.m
use %p for pointer formats, fixed gcc 4.1 warnings, minor code improvs
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOSetHeader.m
1 /*
2   Copyright (C) 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 /*
25   WOSetHeader / <var:set-header/>
26
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).
30   
31   Usage:
32     ChangeContentType: WOSetHeader {
33       header = "content-type";
34       value  = "text/plain";
35     }
36   
37   Bindings:
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])
42 */
43
44 @interface WOSetHeader : WODynamicElement // TODO: should be WOElement?
45 {
46   WOAssociation *object;
47   WOAssociation *header;
48   WOAssociation *value;
49   WOAssociation *addToExisting;
50 }
51
52 @end
53
54 #include <NGObjWeb/WOAssociation.h>
55 #include <NGObjWeb/WOMessage.h>
56 #include <NGObjWeb/WOContext.h>
57 #include "common.h"
58
59 @implementation WOSetHeader
60
61 - (id)initWithName:(NSString *)_name
62   associations:(NSDictionary *)_config
63   template:(WOElement *)_c
64 {
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");
70     
71     if (self->header == nil) self->header = OWGetProperty(_config, @"key");
72     if (self->header == nil) self->header = OWGetProperty(_config, @"name");
73   }
74   return self;
75 }
76
77 - (void)dealloc {
78   [self->object        release];
79   [self->header        release];
80   [self->value         release];
81   [self->addToExisting release];
82   [super dealloc];
83 }
84
85 /* generating response */
86
87 - (id)objectForKey:(NSString *)_key inContext:(WOContext *)_ctx {
88   if (![_key isNotEmpty])
89     return nil;
90
91   if ([_key isEqualToString:@"response"])
92     return [_ctx response];
93   if ([_key isEqualToString:@"request"])
94     return [_ctx request];
95   
96   [self errorWithFormat:@"Unknown object key: '%@'", _key];
97   return nil;
98 }
99
100 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
101   WOMessage *lObject;
102   NSString *k, *v;
103   BOOL     doAdd;
104
105   doAdd = (self->addToExisting != nil)
106     ? [self->addToExisting boolValueInContext:_ctx]
107     : NO;
108
109   k = [self->header stringValueInContext:_ctx];
110   v = [self->value  stringValueInContext:_ctx];
111   
112   /* determine object to manipulate */
113   
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];
119   
120   /* apply */
121   
122   if (doAdd) {
123     if ([v isNotNull])
124       [lObject appendHeader:v forKey:k];
125   }
126   else
127     [lObject setHeader:([v isNotNull] ? v : (NSString *)nil) forKey:k];
128 }
129
130 @end /* WOSetHeader */