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