Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / server / webdav / milton / GSSResourceFactory.java @ 1206:292dec4eae08

History | View | Annotate | Download (10.9 kB)

1
/*
2
 * Copyright 2011 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package org.gss_project.gss.server.webdav.milton;
20

    
21
import static org.gss_project.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22
import org.gss_project.gss.common.exceptions.ObjectNotFoundException;
23
import org.gss_project.gss.common.exceptions.RpcException;
24
import org.gss_project.gss.server.domain.FileHeader;
25
import org.gss_project.gss.server.domain.Folder;
26
import org.gss_project.gss.server.domain.User;
27
import org.gss_project.gss.server.ejb.ExternalAPI;
28
import org.gss_project.gss.server.ejb.TransactionHelper;
29

    
30
import java.util.Date;
31
import java.util.concurrent.Callable;
32

    
33
import javax.naming.Context;
34
import javax.naming.InitialContext;
35
import javax.naming.NamingException;
36
import javax.rmi.PortableRemoteObject;
37

    
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40

    
41
import com.bradmcevoy.http.HttpManager;
42
import com.bradmcevoy.http.Resource;
43
import com.bradmcevoy.http.ResourceFactory;
44
import com.bradmcevoy.http.SecurityManager;
45
import com.ettrema.http.fs.LockManager;
46

    
47

    
48
/**
49
 * @author kman
50
 *
51
 */
52
public class GSSResourceFactory implements ResourceFactory {
53
        private static final Logger log = LoggerFactory.getLogger(GSSResourceFactory.class);
54
        public static final String OTHERS="OthersShared";
55
        SecurityManager securityManager;
56
    LockManager lockManager;
57
    Long maxAgeSeconds;
58
    String contextPath;
59
    boolean allowDirectoryBrowsing;
60
    String defaultPage;
61
    HttpManager httpManager;
62
        @Override
63
        public Resource getResource(String host, String url) {
64
                
65

    
66

    
67
                log.debug("getResource: host: " + host + " - url:" + url);
68
        url = stripContext(url);
69
        if(url==null||url.trim().equals("")||url.equals("/")){
70
                url="/";
71
        }
72
        /*//log.info("URL:"+url);
73
        if(url.equals("/OthersShared")||url.equals("/OthersShared/")){
74
                //log.info("[returning others]");
75
                return new GssOthersResource(host, this);
76
        }
77
        if(url.startsWith("/OthersShared")){
78
                
79
        }*/
80
        try {
81
                User user =null;
82
                    if(HttpManager.request().getAuthorization()!=null && HttpManager.request().getAuthorization().getTag()==null){
83
                            String username = HttpManager.request().getAuthorization().getUser();
84
                            if(username !=null)
85
                                    user = getService().getUserByUserName(username);
86
                    }
87
                    else if(HttpManager.request().getAuthorization()!=null&&HttpManager.request().getAuthorization().getTag()!=null){
88
                            user =(User) HttpManager.request().getAuthorization().getTag();
89
                    }
90
            
91
                    if(user==null){
92
                                return new GssRootFolderResource(host, this, null,url);
93
                    }
94
                    if(url.equals("/"+OTHERS)||url.equals("/"+OTHERS+"/")){
95
                            return new GssOthersResource(host, this);
96
                    }
97
                    if(url.startsWith("/"+OTHERS)&&url.split("/").length==3){
98
                            String username = url.split("/")[2];
99
                            User userother = getService().getUserByUserName(username);
100
                            if(userother!=null)
101
                                    return new GssOtherUserResource(host, this,userother );
102
                            return null;
103
                    }
104
                    if(url.startsWith("/"+OTHERS)&&url.split("/").length>3){
105
                            String username = url.split("/")[2];
106
                            User userother = getService().getUserByUserName(username);
107
                            String newUrl = url.replace("/"+OTHERS+"/"+username, "");
108
                            if(userother==null)
109
                                    return null;
110
                            Object r = getResourceGss(newUrl,userother);
111
                    if(r==null){
112
                            
113
                            return null;
114
                    }
115
                    if(r instanceof Folder){
116
                            
117
                            return new GssFolderResource(host, this,r ,user);
118
                    }
119
                    else
120
                            return new GssFileResource(host, this,r,user);
121
                    }
122
                Object r = getResourceGss(url,user);
123
                if(r==null){
124
                        
125
                        return null;
126
                }
127
                if(r instanceof Folder){
128
                        
129
                        return new GssFolderResource(host, this,r ,user);
130
                }
131
                else
132
                        return new GssFileResource(host, this,r,user);
133
                } catch (RpcException e) {
134
                        log.error("unable to access ejb service",e);
135
                }
136
                return null;
137
    }
138
        public Long maxAgeSeconds(GssResource resource) {
139
        return maxAgeSeconds;
140
    }
141
        protected Object getResourceGss(String path, User user) throws RpcException{
142

    
143
                if(user ==null){
144
                        if(HttpManager.request().getAuthorization()!=null && HttpManager.request().getAuthorization().getTag()==null){
145
                                String username = HttpManager.request().getAuthorization().getUser();
146
                                if(username !=null)
147
                                        user = getService().getUserByUserName(username);
148
                        }
149
                        else if(HttpManager.request().getAuthorization()!=null&&HttpManager.request().getAuthorization().getTag()!=null){
150
                                user =(User) HttpManager.request().getAuthorization().getTag();
151
                        }
152
                }
153
                
154
                if(user==null){
155
                        return null;
156
                }
157
                boolean exists = true;
158
                Object resource = null;
159
                try {
160
                        resource = getService().getResourceAtPath(user.getId(), path, true);
161
                } catch (ObjectNotFoundException e) {
162
                        exists = false;
163
                } catch (RpcException e) {
164
                        
165
                        return null;
166
                }
167

    
168
                if (!exists) {
169
                        
170
                        return null;
171
                }
172
                if(resource instanceof Folder){
173
                        try {
174
                                resource = getService().expandFolder((Folder) resource);
175
                        } catch (ObjectNotFoundException e) {
176
                                // TODO Auto-generated catch block
177
                                return null;
178
                        }
179
                }
180
                else if(resource instanceof FileHeader){
181
                        try {
182
                                resource = getService().expandFile((FileHeader) resource);
183
                        } catch (ObjectNotFoundException e) {
184
                                // TODO Auto-generated catch block
185
                                return null;
186
                        }
187
                }
188
                return resource;
189
        }
190
        
191
        
192
         private String stripContext( String url ) {
193
                if( this.contextPath != null && contextPath.length() > 0 ) {
194
                    url = url.replaceFirst( '/' + contextPath, "");
195
                    log.debug( "stripped context: " + url);
196
                    return url;
197
                } else {
198
                    return url;
199
                }
200
            }
201
         
202
         /**
203
                 * For a provided path, remove the last element and return the rest, that is
204
                 * the path of the parent folder.
205
                 *
206
                 * @param path the specified path
207
                 * @return the path of the parent folder
208
                 * @throws ObjectNotFoundException if the provided string contains no path
209
                 *             delimiters
210
                 */
211
                protected String getParentPath(String path) throws ObjectNotFoundException {
212
                        int lastDelimiter = path.lastIndexOf('/');
213
                        if (lastDelimiter == 0)
214
                                return "/";
215
                        if (lastDelimiter == -1)
216
                                // No path found.
217
                                throw new ObjectNotFoundException("There is no parent in the path: " + path);
218
                        else if (lastDelimiter < path.length() - 1)
219
                                // Return the part before the delimiter.
220
                                return path.substring(0, lastDelimiter);
221
                        else {
222
                                // Remove the trailing delimiter and then recurse.
223
                                String strippedTrail = path.substring(0, lastDelimiter);
224
                                return getParentPath(strippedTrail);
225
                        }
226
                }
227

    
228
                /**
229
                 * A helper method that retrieves a reference to the ExternalAPI bean and
230
                 * stores it for future use.
231
                 *
232
                 * @return an ExternalAPI instance
233
                 * @throws RpcException in case an error occurs
234
                 */
235
                protected ExternalAPI getService() throws RpcException {
236
                        try {
237
                                final Context ctx = new InitialContext();
238
                                final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
239
                                return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
240
                                
241
                        } catch (final NamingException e) {
242
                                log.error("Unable to retrieve the ExternalAPI EJB", e);
243
                                throw new RpcException("An error occurred while contacting the naming service");
244
                        }
245
                }
246

    
247
                private void updateAccounting(final User user, final Date date, final long bandwidthDiff) {
248
                        try {
249
                                new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
250
                                        @Override
251
                                        public Void call() throws Exception {
252
                                                getService().updateAccounting(user, date, bandwidthDiff);
253
                                                return null;
254
                                        }
255
                                });
256
                        } catch (RuntimeException e) {
257
                                throw e;
258
                        } catch (Exception e) {
259
                                // updateAccounting() doesn't throw any checked exceptions
260
                                assert false;
261
                        }
262
                }
263

    
264
                
265
                /**
266
                 * Retrieve the securityManager.
267
                 *
268
                 * @return the securityManager
269
                 */
270
                public SecurityManager getSecurityManager() {
271
                        return securityManager;
272
                }
273

    
274
                
275
                /**
276
                 * Retrieve the lockManager.
277
                 *
278
                 * @return the lockManager
279
                 */
280
                public LockManager getLockManager() {
281
                        return lockManager;
282
                }
283

    
284
                
285
                /**
286
                 * Retrieve the maxAgeSeconds.
287
                 *
288
                 * @return the maxAgeSeconds
289
                 */
290
                public Long getMaxAgeSeconds() {
291
                        return maxAgeSeconds;
292
                }
293

    
294
                
295
                /**
296
                 * Retrieve the contextPath.
297
                 *
298
                 * @return the contextPath
299
                 */
300
                public String getContextPath() {
301
                        return contextPath;
302
                }
303

    
304
                
305
                /**
306
                 * Retrieve the allowDirectoryBrowsing.
307
                 *
308
                 * @return the allowDirectoryBrowsing
309
                 */
310
                public boolean isAllowDirectoryBrowsing() {
311
                        return allowDirectoryBrowsing;
312
                }
313

    
314
                
315
                /**
316
                 * Retrieve the defaultPage.
317
                 *
318
                 * @return the defaultPage
319
                 */
320
                public String getDefaultPage() {
321
                        return defaultPage;
322
                }
323
                
324
                public String getRealm(String host) {
325
                return securityManager.getRealm(host);
326
            }
327

    
328
                
329
                /**
330
                 * Modify the securityManager.
331
                 *
332
                 * @param securityManager the securityManager to set
333
                 */
334
                public void setSecurityManager(SecurityManager securityManager) {
335
                        this.securityManager = securityManager;
336
                }
337

    
338
                
339
                /**
340
                 * Modify the lockManager.
341
                 *
342
                 * @param lockManager the lockManager to set
343
                 */
344
                public void setLockManager(LockManager lockManager) {
345
                        this.lockManager = lockManager;
346
                }
347

    
348
                
349
                /**
350
                 * Modify the maxAgeSeconds.
351
                 *
352
                 * @param maxAgeSeconds the maxAgeSeconds to set
353
                 */
354
                public void setMaxAgeSeconds(Long maxAgeSeconds) {
355
                        this.maxAgeSeconds = maxAgeSeconds;
356
                }
357

    
358
                
359
                /**
360
                 * Modify the contextPath.
361
                 *
362
                 * @param contextPath the contextPath to set
363
                 */
364
                public void setContextPath(String contextPath) {
365
                        this.contextPath = contextPath;
366
                }
367

    
368
                
369
                /**
370
                 * Modify the defaultPage.
371
                 *
372
                 * @param defaultPage the defaultPage to set
373
                 */
374
                public void setDefaultPage(String defaultPage) {
375
                        this.defaultPage = defaultPage;
376
                }
377
                
378
                
379
                /**
380
                 * Retrieve the httpManager.
381
                 *
382
                 * @return the httpManager
383
                 */
384
                public HttpManager getHttpManager() {
385
                        return httpManager;
386
                }
387
                
388
                
389
                /**
390
                 * Modify the httpManager.
391
                 *
392
                 * @param httpManager the httpManager to set
393
                 */
394
                public void setHttpManager(HttpManager httpManager) {
395
                        this.httpManager = httpManager;
396
                }
397
                
398
                
399
        
400
                public static String getUsernameFromAuthHeader(String header) {
401
                        String first = header.split(",")[0];
402
                        int indx = first.indexOf("\"");
403
                        return first.substring(indx+1,first.length()-1);
404
                }
405
}