]> err.no Git - sope/blob - sope-appserver/NGObjWeb/WOResourceRequestHandler.m
Improved WORepetition's implementation to be more convenient in regards to the 'list...
[sope] / sope-appserver / NGObjWeb / WOResourceRequestHandler.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 <NGObjWeb/WORequestHandler.h>
23
24 @interface WOResourceRequestHandler : WORequestHandler
25 @end
26
27 #include <NGObjWeb/WOApplication.h>
28 #include <NGObjWeb/WORequest.h>
29 #include <NGObjWeb/WOResponse.h>
30 #include <NGObjWeb/WOResourceManager.h>
31 #include "common.h"
32
33 @interface WOResourceManager(PrivateKeyedAccess)
34
35 - (id)_dataForKey:(NSString *)_key sessionID:(NSString *)_sid;
36
37 @end
38
39 @implementation WOResourceRequestHandler
40
41 static BOOL debugOn = NO;
42
43 + (int)version {
44   return [super version] + 0 /* v2 */;
45 }
46 + (void)initialize {
47   NSAssert2([super version] == 2,
48             @"invalid superclass (%@) version %i !",
49             NSStringFromClass([self superclass]), [super version]);
50 }
51
52 - (WOResponse *)_handleWebServerResourcesRequest:(WORequest *)_request {
53   WOApplication *app;
54   NSArray       *handlerPath = nil;
55   WOResponse    *response   = nil;
56   NSArray       *languages;
57   NSString      *resourceName;
58   NSString      *resourcePath;
59   NSData        *data;
60   
61   if (debugOn) [self logWithFormat:@"handle WS request: %@", _request];
62   
63   if (_request == nil) return nil;
64   
65   *(&app)     = [WOApplication application];
66   handlerPath = [_request requestHandlerPathArray];
67   
68   /* check for WebServerResources requests ... */
69   
70   if ([handlerPath count] < 1) {
71     if (debugOn) [self logWithFormat:@"path to short: '%@'", handlerPath];
72     return nil;
73   }
74   if (debugOn) [self logWithFormat:@"  check path: '%@'", handlerPath];
75   
76   /* ok, found a resource request */
77
78   if ([handlerPath count] > 1) {
79     NSString *lang;
80     
81     lang      = [handlerPath objectAtIndex:0];
82     lang      = [lang stringByDeletingPathExtension];
83     languages = [NSArray arrayWithObject:lang];
84     languages = [languages arrayByAddingObjectsFromArray:languages];
85     
86     resourceName = [handlerPath objectAtIndex:1];
87   }
88   else {
89     languages    = [_request browserLanguages];
90     resourceName = [handlerPath objectAtIndex:0];
91   }
92   
93   resourcePath = [[app resourceManager]
94                        pathForResourceNamed:resourceName
95                        inFramework:nil
96                        languages:languages];
97   if (debugOn) [self logWithFormat:@"  resource path: '%@'", resourcePath];
98
99   data = (resourcePath != nil)
100     ? [NSData dataWithContentsOfFile:resourcePath]
101     : nil;
102   
103   if (data == nil) {
104     response = [WOResponse responseWithRequest:_request];
105     [response setStatus:404]; /* not found */
106     [response setHeader:@"text/html" forKey:@"content-type"];
107     [response appendContentString:@"<h3>Resource not found</h3><pre>"];
108     [response appendContentHTMLString:@"Name: "];
109     [response appendContentHTMLString:resourceName];
110     [response appendContentHTMLString:@"\nLanguages: "];
111     [response appendContentHTMLString:[languages description]];
112     [response appendContentHTMLString:@"\nResourceManager: "];
113     [response appendContentHTMLString:[[app resourceManager] description]];
114     [response appendContentString:@"</pre>"];
115     return response;
116   }
117   
118   //NSLog(@"shall deliver %@", resourcePath);
119   
120   response = [WOResponse responseWithRequest:_request];
121   
122   /* determine content-type */
123   {
124     NSString *ctype;
125     NSString *pathExtension;
126     
127     pathExtension = [resourcePath pathExtension];
128     ctype         = @"application/octet-stream";
129     
130     if ([pathExtension isEqualToString:@"html"])
131       ctype = @"text/html";
132     else if ([pathExtension isEqualToString:@"gif"])
133       ctype = @"image/gif";
134     
135     [response setHeader:ctype forKey:@"content-type"];
136   }
137   
138   [response setContent:data];
139   
140   return response;
141 }
142
143 - (WOResponse *)handleRequest:(WORequest *)_request {
144   NSArray *handlerPath = nil;
145   
146   if (debugOn) [self logWithFormat:@"handle request: %@", _request];
147   
148   if ([[_request requestHandlerKey] isEqualToString:@"WebServerResources"])
149     return [self _handleWebServerResourcesRequest:_request];
150
151   handlerPath = [_request requestHandlerPathArray];
152   
153   if ([handlerPath count] > 0) {
154     NSString *rmkey;
155     
156     rmkey = [handlerPath objectAtIndex:0];
157     if ([rmkey length] > 0) {
158       WOResourceManager *rm;
159       NSDictionary *data;
160       
161       rm = [[WOApplication application] resourceManager];
162       
163       if ((data = [rm _dataForKey:rmkey sessionID:nil]) != nil) {
164         WOResponse *response;
165         
166         response = [WOResponse responseWithRequest:_request];
167         [response setHeader:[data objectForKey:@"mimeType"]
168                   forKey:@"content-type"];
169         [response setContent:[data objectForKey:@"data"]];
170         return response;
171       }
172       else {
173         [[WOApplication application]
174                         logWithFormat:@"WOResourceRequestHandler: "
175                           @"didn't find data for resource key '%@'",
176                           rmkey];
177       }
178     }
179   }
180   
181   /* if everything fails, try locating resource in WebServerResources */
182   return [self _handleWebServerResourcesRequest:_request];
183 }
184
185 /* logging */
186
187 - (NSString *)loggingPrefix {
188   return @"[resource-handler]";
189 }
190
191 @end /* WOResourceRequestHandler */