Merge with 2ad3c504ee5d73982c0ef23336276dc1fc9e165f
[pithos] / src / gr / ebs / gss / server / webdav / milton / GssResource.java
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 gr.ebs.gss.server.webdav.milton;
20
21 import gr.ebs.gss.client.exceptions.RpcException;
22 import gr.ebs.gss.server.domain.User;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.bradmcevoy.http.Auth;
28 import com.bradmcevoy.http.CopyableResource;
29 import com.bradmcevoy.http.DigestResource;
30 import com.bradmcevoy.http.HttpManager;
31 import com.bradmcevoy.http.LockInfo;
32 import com.bradmcevoy.http.LockResult;
33 import com.bradmcevoy.http.LockTimeout;
34 import com.bradmcevoy.http.LockToken;
35 import com.bradmcevoy.http.LockableResource;
36 import com.bradmcevoy.http.MoveableResource;
37 import com.bradmcevoy.http.Request;
38 import com.bradmcevoy.http.Resource;
39 import com.bradmcevoy.http.Request.Method;
40 import com.bradmcevoy.http.exceptions.NotAuthorizedException;
41 import com.bradmcevoy.http.http11.auth.DigestResponse;
42
43
44 /**
45  * @author kman
46  *
47  */
48 public abstract class GssResource implements Resource, MoveableResource, CopyableResource, LockableResource, DigestResource {
49     private static final Logger log = LoggerFactory.getLogger(GssResource.class);
50     String host;
51     GSSResourceFactory factory;
52     Object resource;
53     User currentUser;
54     
55         /**
56          * 
57          */
58         public GssResource(String host, GSSResourceFactory factory, Object resource) {
59                 this.host=host;
60                 this.factory=factory;
61                 this.resource=resource;
62                 
63         }
64         
65         public Object authenticate(String user, String password) {
66         return factory.getSecurityManager().authenticate(user, password);
67     }
68
69     public Object authenticate( DigestResponse digestRequest ) {
70         return  factory.getSecurityManager().authenticate(digestRequest);
71         
72     }
73
74     public boolean isDigestAllowed() {
75         return true;
76     }
77
78
79
80
81     public boolean authorise(Request request, Method method, Auth auth) {
82         return factory.getSecurityManager().authorise(request, method, auth, this);
83     }
84
85     public String getRealm() {
86         return factory.getRealm(this.host);
87     }
88     
89     public LockResult lock(LockTimeout timeout, LockInfo lockInfo) throws NotAuthorizedException {
90         return factory.getLockManager().lock(timeout, lockInfo, this);
91     }
92
93     public LockResult refreshLock(String token) throws NotAuthorizedException {
94         return factory.getLockManager().refresh(token, this);
95     }
96
97     public void unlock(String tokenId) throws NotAuthorizedException {
98         factory.getLockManager().unlock(tokenId, this);
99     }
100
101     public LockToken getCurrentLock() {
102         if( factory.getLockManager() != null ) {
103             return factory.getLockManager().getCurrentToken( this );
104         } else {
105             log.warn("getCurrentLock called, but no lock manager: file: " + resource);
106             return null;
107         }
108     }
109     
110     
111         /**
112          * Retrieve the currentUser.
113          *
114          * @return the currentUser
115          */
116         public User getCurrentUser() {
117                 if(currentUser!=null)
118                         return currentUser;
119                 if(HttpManager.request().getAuthorization()!=null && HttpManager.request().getAuthorization().getTag()==null){
120                         String username = HttpManager.request().getAuthorization().getUser();
121                         //log.info("username is:"+username);
122                         if(username !=null)
123                                 try {
124                                         currentUser = factory.getService().getUserByUserName(username);
125                                 } catch (RpcException e) {
126                                         // TODO Auto-generated catch block
127                                         e.printStackTrace();
128                                 }
129                 }
130                 else if(HttpManager.request().getAuthorization()!=null&&HttpManager.request().getAuthorization().getTag()!=null){
131                         //log.info(HttpManager.request().getAuthorization().getUser());
132                         currentUser =(User) HttpManager.request().getAuthorization().getTag();//getService().getUserByUserName("past@ebs.gr");
133                 }
134                 return currentUser;
135         }
136         
137
138 }