~ubuntu-branches/ubuntu/edgy/sope/edgy

« back to all changes in this revision

Viewing changes to sope-appserver/NGObjWeb/DynamicElements/WOResourceURL.m

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Ley
  • Date: 2005-08-19 16:53:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050819165331-hs683wz1osm708pw
Tags: upstream-4.4rc.2
ImportĀ upstreamĀ versionĀ 4.4rc.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "common.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 hasSession]
 
135
      ? [[_ctx session] languages]
 
136
      : [[_ctx request] browserLanguages];
 
137
    uFi = [rm urlForResourceNamed:uFi
 
138
              inFramework:frameworkName
 
139
              languages:languages
 
140
              request:[_ctx request]];
 
141
    if (uFi == nil) {
 
142
      NSLog(@"%@: did not find resource '%@'", sComponent,
 
143
            [self->filename stringValueInComponent:sComponent]);
 
144
    }
 
145
    else
 
146
      [_response appendContentHTMLAttributeValue:uFi];
 
147
  }
 
148
  else if (self->data != nil) {
 
149
    NSString *kk;
 
150
    
 
151
    if ((kk = [self->key stringValueInComponent:sComponent])) {
 
152
      NSString          *url;
 
153
      WOResourceManager *rm;
 
154
      
 
155
      if ((rm = [[_ctx component] resourceManager]) == nil)
 
156
        rm = [[_ctx application] resourceManager];
 
157
    
 
158
      [rm setData:[self->data valueInComponent:sComponent]
 
159
          forKey:kk
 
160
          mimeType:[self->mimeType stringValueInComponent:sComponent]
 
161
          session:[_ctx hasSession] ? [_ctx session] : nil];
 
162
      
 
163
      url = [_ctx urlWithRequestHandlerKey:
 
164
                    [WOApplication resourceRequestHandlerKey]
 
165
                  path:[@"/" stringByAppendingString:kk]
 
166
                  queryString:nil];
 
167
      
 
168
      WOResponse_AddString(_response, url);
 
169
    }
 
170
    else {
 
171
      /* a component action link */
 
172
      uFi = [_ctx componentActionURL];
 
173
      WOResponse_AddString(_response, uFi);
 
174
    }
 
175
  }
 
176
  else {
 
177
    [sComponent logWithFormat:@"missing resource URL for element %@", self];
 
178
  }
 
179
  
 
180
  /* content */
 
181
  [self->template appendToResponse:_response inContext:_ctx];
 
182
}
 
183
 
 
184
/* description */
 
185
 
 
186
- (NSString *)associationDescription {
 
187
  NSMutableString *str = [[NSMutableString alloc] init];
 
188
 
 
189
  if (self->filename)  [str appendFormat:@" filename=%@",  self->filename];
 
190
  if (self->framework) [str appendFormat:@" framework=%@", self->framework];
 
191
  if (self->data)      [str appendFormat:@" data=%@",      self->data];
 
192
  if (self->mimeType)  [str appendFormat:@" mimeType=%@",  self->mimeType];
 
193
  if (self->key)       [str appendFormat:@" key=%@",       self->key];
 
194
  
 
195
  return AUTORELEASE(str);
 
196
}
 
197
 
 
198
@end /* WOResourceURL */