]> err.no Git - sope/blob - sope-appserver/samples/iCalPortal/WebDAV/iCalAction.m
renamed packages as discussed in the developer list
[sope] / sope-appserver / samples / iCalPortal / WebDAV / iCalAction.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 // $Id$
22
23 #include "iCalAction.h"
24 #include "iCalPortalDatabase.h"
25 #include "iCalPortalUser.h"
26 #include "common.h"
27
28 @implementation iCalAction
29
30 - (id)initWithContext:(WOContext *)_ctx {
31   self->ctx = [_ctx retain];
32   return self;
33 }
34 - (void)dealloc {
35   [self->ctx release];
36   [super dealloc];
37 }
38
39 /* accessors */
40
41 - (WOContext *)context {
42   return self->ctx;
43 }
44 - (WORequest *)request {
45   return [self->ctx request];
46 }
47 - (id)application {
48   return [WOApplication application];
49 }
50
51 - (NSString *)requestUser {
52   NSRange  r;
53   NSString *s;
54   
55   s = [[self request] requestHandlerPath];
56   r = [s rangeOfString:@"/"];
57   if (r.length == 0) return nil;
58   return [s substringToIndex:r.location];
59 }
60 - (NSString *)requestCalendarPath {
61   NSRange  r;
62   NSString *s;
63   
64   s = [[self request] requestHandlerPath];
65   r = [s rangeOfString:@"/"];
66   if (r.length == 0) return nil;
67   return [s substringFromIndex:(r.location + 1)];
68 }
69
70 /* operation */
71
72 - (WOResponse *)run {
73   return nil;
74 }
75
76 - (NSString *)credentials {
77   WORequest *rq;
78   id        creds;
79   NSRange   r;
80   
81   if ((rq = [self->ctx request]) == nil)
82     return nil;
83   if ((creds = [rq headerForKey:@"authorization"]) == nil)
84     return nil;
85   
86   r = [creds rangeOfString:@" " options:NSBackwardsSearch];
87   if (r.length == 0) {
88     [self logWithFormat:@"invalid 'authorization' header: '%@'", creds];
89     return nil;
90   }
91   return [creds substringFromIndex:(r.location + r.length)];
92 }
93
94 - (NSString *)credentialsLogin {
95   id creds;
96
97   creds = [creds stringByDecodingBase64];
98   creds = [creds componentsSeparatedByString:@":"];
99   if ([creds count] < 2) {
100     [self logWithFormat:@"invalid credentials"];
101     return nil;
102   }
103   
104   return [creds objectAtIndex:0];
105 }
106
107 - (iCalPortalDatabase *)database {
108   return [(id)[WOApplication application] database];
109 }
110
111 - (iCalPortalUser *)user {
112   iCalPortalDatabase *db;
113   iCalPortalUser *user;
114   id       creds;
115   NSString *login, *pwd;
116   
117   if ((db = [self database]) == nil)
118     return nil;
119   
120   if ((creds = [self credentials]) == nil)
121     return nil;
122   
123   /* assuming basic authentication ... */
124   creds = [creds stringByDecodingBase64];
125   creds = [creds componentsSeparatedByString:@":"];
126   if ([creds count] < 2) {
127     [self logWithFormat:@"invalid credentials"];
128     return nil;
129   }
130   
131   login = [creds objectAtIndex:0];
132   pwd   = [creds objectAtIndex:1];
133   
134   user = [db userWithName:login password:pwd];
135   
136   return user;
137 }
138
139 - (NSString *)authRealm {
140   WOApplication *app = [self application];
141   return [app name];
142 }
143
144 - (WOResponse *)missingAuthResponse {
145   WOResponse *resp;
146   NSString *auth;
147
148   auth = [NSString stringWithFormat:@"Basic realm=\"%@\"",[self authRealm]];
149   
150   resp = [(WOResponse *)[WOResponse alloc] initWithRequest:[self request]];
151   [resp setStatus:401 /* unauthorized */];
152   [resp setHeader:auth forKey:@"www-authenticate"];
153   //[resp setHeader:@"close" forKey:@"connection"];
154   [resp setHeader:@"text/html; charset=iso-8859-1" forKey:@"content-type"];
155   [resp appendContentString:
156     @"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">"
157     @"<HTML><HEAD>"
158     @"<TITLE>401 Authorization Required</TITLE>"
159     @"</HEAD><BODY>"
160     @"<H1>Authorization Required</H1>"
161     @"<ADDRESS>Apache/1.3.26 Server at dogbert Port 9000</ADDRESS>"
162     @"</BODY></HTML>"
163    ];
164   
165   return AUTORELEASE(resp);
166 }
167
168 - (WOResponse *)accessDeniedResponse {
169   WOResponse *resp;
170   NSString *auth;
171   
172   auth = [NSString stringWithFormat:@"Basic realm=\"%@\"",[self authRealm]];
173   
174   [self logWithFormat:@"access was denied"];
175   
176   resp = [(WOResponse *)[WOResponse alloc] initWithRequest:[self request]];
177   [resp setStatus:403 /* forbidden */];
178   [resp setHeader:auth forKey:@"www-authenticate"];
179   return [resp autorelease];
180 }
181
182 - (WOResponse *)notFoundResponse {
183   WOResponse *resp;
184   
185   resp = [(WOResponse *)[WOResponse alloc] initWithRequest:[self request]];
186   [resp setStatus:404 /* not found */];
187   return [resp autorelease];
188 }
189
190 @end /* iCalAction */
191
192 @implementation iCalFakeAction
193
194 - (id)initWithContext:(WOContext *)_ctx code:(int)_status {
195   if ((self = [super initWithContext:_ctx])) {
196     self->code = _status;
197   }
198   return self;
199 }
200
201 - (id)initWithContext:(WOContext *)_ctx {
202   return [self initWithContext:_ctx code:200];
203 }
204
205 - (WOResponse *)run {
206   WOResponse *r;
207   
208   r = [WOResponse responseWithRequest:[self request]];
209   [r setStatus:self->code];
210   
211   [r setHeader:@"close" forKey:@"connection"];
212   [r setHeader:@"text/plain; charset=iso-8859-1" forKey:@"content-type"];
213   
214   [r appendContentString:@"operation executed\r\n"];
215   
216   return r;
217 }
218
219 @end /* iCalFakeAction */