]> err.no Git - sope/blob - sope-appserver/NGObjWeb/Associations/WOResourceURLAssociation.m
improved SOPE security exceptions
[sope] / sope-appserver / NGObjWeb / Associations / WOResourceURLAssociation.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo 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   OGo 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 OGo; 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 "WOResourceURLAssociation.h"
23 #include <NGObjWeb/WOApplication.h>
24 #include <NGObjWeb/WOComponent.h>
25 #include <NGObjWeb/WOContext.h>
26 #include <NGObjWeb/WORequest.h>
27 #include <NGObjWeb/WOResourceManager.h>
28 #include <NGObjWeb/WOSession.h>
29 #include "common.h"
30
31 @implementation WOResourceURLAssociation
32
33 static BOOL doDebug = NO;
34
35 + (int)version {
36   return [super version] + 0 /* v2 */;
37 }
38 + (void)initialize {
39   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
40   NSAssert2([super version] == 2,
41             @"invalid superclass (%@) version %i !",
42             NSStringFromClass([self superclass]), [super version]);
43   
44   doDebug = [ud boolForKey:@"WOResourceURLAssociationDebugEnabled"];
45 }
46
47 - (id)initWithString:(NSString *)_name {
48   if ([_name length] == 0) {
49     if (doDebug) {
50       NSLog(@"WARNING(%s): got passed no resource name!", 
51             __PRETTY_FUNCTION__);
52     }
53     [self release];
54     return nil;
55   }
56   if ((self = [super init])) {
57     self->resourceName = [_name copy];
58   }
59   return self;
60 }
61 - (id)init {
62   return [self initWithString:nil];
63 }
64
65 - (void)dealloc {
66   [self->resourceName release];
67   [super dealloc];
68 }
69
70 /* accessors */
71
72 - (NSString *)resourceName {
73   return self->resourceName;
74 }
75 - (NSString *)frameworkName {
76   return nil;
77 }
78
79 /* value */
80
81 - (void)setValue:(id)_value inComponent:(WOComponent *)_component {
82   /* resource-url association values cannot be set */
83   [NSException raise:@"AssociationException"
84                format:@"association value is not settable !"];
85 }
86 - (id)valueInComponent:(WOComponent *)_component {
87   WOResourceManager *rm;
88   WOContext *ctx;
89   NSArray   *langs;
90   WORequest *rq;
91   id url;
92   
93   if (doDebug)
94     [self debugWithFormat:@"lookup resource: %@", [self resourceName]];
95
96   if ((ctx = [_component context]) == nil)
97     ctx = [[WOApplication application] context];
98   rq    = [ctx request];
99   langs = [ctx hasSession]
100     ? [[ctx session] languages]
101     : [[ctx request] browserLanguages];
102   
103   if (doDebug) {
104     [self debugWithFormat:@"  languages: %@", 
105             [langs componentsJoinedByString:@","]];
106   }
107   
108   if ((rm = [_component resourceManager]) == nil) {
109     WOApplication *app;
110     
111     if ((app = [ctx application]) == nil)
112       app = [WOApplication application];
113     
114     rm = [app resourceManager];
115   }
116   if (rm == nil) {
117     [self logWithFormat:@"WARNING: found no resource manager!"];
118     return nil;
119   }
120   if (doDebug) [self debugWithFormat:@"  resource-manager: %@", rm];
121   
122   url = [rm urlForResourceNamed:[self resourceName]
123             inFramework:[self frameworkName]
124             languages:langs
125             request:rq];
126   if (doDebug) {
127     if (url != nil)
128       [self debugWithFormat:@"  => URL: %@", url];
129     else
130       [self debugWithFormat:@"  => resource not found!"];
131   }
132   return url;
133 }
134
135 - (BOOL)isValueConstant {
136   return NO;
137 }
138 - (BOOL)isValueSettable {
139   return NO;
140 }
141
142 /* NSCopying */
143
144 - (id)copyWithZone:(NSZone *)_zone {
145   /* rsrc-url associations are immutable and don't need to be copied */
146   return [self retain];
147 }
148
149 /* debugging */
150
151 - (BOOL)isDebuggingEnabled {
152   return doDebug;
153 }
154 - (NSString *)loggingPrefix {
155   return [NSString stringWithFormat:@"[rsrc:url assoc:0x%08X]", self];
156 }
157
158 /* description */
159
160 - (NSString *)description {
161   NSMutableString *str;
162
163   str = [NSMutableString stringWithCapacity:64];
164   [str appendFormat:@"<%@[0x%08X]:", NSStringFromClass([self class]), self];
165   [str appendFormat:@" rsrc='%@'", [self resourceName]];
166   [str appendString:@">"];
167   return str;
168 }
169
170 @end /* WOResourceURLAssociation */