Imported changesets 6ad7cf34a8f5, d535941636f3, f3a4422f7b1a from the default branch
[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 static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22
23 import javax.naming.Context;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import javax.rmi.PortableRemoteObject;
27
28 import gr.ebs.gss.client.exceptions.RpcException;
29 import gr.ebs.gss.server.domain.User;
30 import gr.ebs.gss.server.ejb.ExternalAPI;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.bradmcevoy.http.Auth;
36 import com.bradmcevoy.http.CopyableResource;
37 import com.bradmcevoy.http.DigestResource;
38 import com.bradmcevoy.http.HttpManager;
39 import com.bradmcevoy.http.LockInfo;
40 import com.bradmcevoy.http.LockResult;
41 import com.bradmcevoy.http.LockTimeout;
42 import com.bradmcevoy.http.LockToken;
43 import com.bradmcevoy.http.LockableResource;
44 import com.bradmcevoy.http.MoveableResource;
45 import com.bradmcevoy.http.Request;
46 import com.bradmcevoy.http.Resource;
47 import com.bradmcevoy.http.Request.Method;
48 import com.bradmcevoy.http.exceptions.NotAuthorizedException;
49 import com.bradmcevoy.http.http11.auth.DigestResponse;
50
51
52 /**
53  * @author kman
54  *
55  */
56 public abstract class GssResource implements Resource, MoveableResource, CopyableResource, LockableResource, DigestResource {
57     private static final Logger log = LoggerFactory.getLogger(GssResource.class);
58     String host;
59     GSSResourceFactory factory;
60     Object resource;
61     User currentUser;
62     
63         /**
64          * 
65          */
66         public GssResource(String host, GSSResourceFactory factory, Object resource) {
67                 this.host=host;
68                 this.factory=factory;
69                 this.resource=resource;
70                 
71         }
72         
73         public Object authenticate(String user, String password) {
74         return factory.getSecurityManager().authenticate(user, password);
75     }
76
77     public Object authenticate( DigestResponse digestRequest ) {
78         return  factory.getSecurityManager().authenticate(digestRequest);
79         
80     }
81
82     public boolean isDigestAllowed() {
83         return true;
84     }
85
86
87
88
89     public boolean authorise(Request request, Method method, Auth auth) {
90         return factory.getSecurityManager().authorise(request, method, auth, this);
91     }
92
93     public String getRealm() {
94         return factory.getRealm(this.host);
95     }
96     
97     public LockResult lock(LockTimeout timeout, LockInfo lockInfo) throws NotAuthorizedException {
98         return factory.getLockManager().lock(timeout, lockInfo, this);
99     }
100
101     public LockResult refreshLock(String token) throws NotAuthorizedException {
102         return factory.getLockManager().refresh(token, this);
103     }
104
105     public void unlock(String tokenId) throws NotAuthorizedException {
106         factory.getLockManager().unlock(tokenId, this);
107     }
108
109     public LockToken getCurrentLock() {
110         if( factory.getLockManager() != null ) {
111             return factory.getLockManager().getCurrentToken( this );
112         } else {
113             log.warn("getCurrentLock called, but no lock manager: file: " + resource);
114             return null;
115         }
116     }
117     
118     
119         /**
120          * Retrieve the currentUser.
121          *
122          * @return the currentUser
123          */
124         public User getCurrentUser() {
125                 if(currentUser!=null)
126                         return currentUser;
127                 if(HttpManager.request().getAuthorization()!=null && HttpManager.request().getAuthorization().getTag()==null){
128                         String username = HttpManager.request().getAuthorization().getUser();
129                         //log.info("username is:"+username);
130                         if(username !=null)
131                                 try {
132                                         currentUser = getService().getUserByUserName(username);
133                                 } catch (RpcException e) {
134                                         // TODO Auto-generated catch block
135                                         log.error("unable to access ejb service",e);
136                                 }
137                 }
138                 else if(HttpManager.request().getAuthorization()!=null&&HttpManager.request().getAuthorization().getTag()!=null){
139                         //log.info(HttpManager.request().getAuthorization().getUser());
140                         currentUser =(User) HttpManager.request().getAuthorization().getTag();//getService().getUserByUserName("past@ebs.gr");
141                 }
142                 return currentUser;
143         }
144         
145         /**
146          * A helper method that retrieves a reference to the ExternalAPI bean and
147          * stores it for future use.
148          *
149          * @return an ExternalAPI instance
150          * @throws RpcException in case an error occurs
151          */
152         protected ExternalAPI getService() throws RpcException {
153                 try {
154                         final Context ctx = new InitialContext();
155                         final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
156                         return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
157                         
158                 } catch (final NamingException e) {
159                         log.error("Unable to retrieve the ExternalAPI EJB", e);
160                         throw new RpcException("An error occurred while contacting the naming service");
161                 }
162         }
163         
164
165 }