2 * Copyright 2011 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.server.webdav.milton;
21 import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
23 import javax.naming.Context;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import javax.rmi.PortableRemoteObject;
28 import gr.ebs.gss.client.exceptions.RpcException;
29 import gr.ebs.gss.server.domain.User;
30 import gr.ebs.gss.server.ejb.ExternalAPI;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
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;
56 public abstract class GssResource implements Resource, MoveableResource, CopyableResource, LockableResource, DigestResource {
57 private static final Logger log = LoggerFactory.getLogger(GssResource.class);
59 GSSResourceFactory factory;
66 public GssResource(String host, GSSResourceFactory factory, Object resource) {
69 this.resource=resource;
73 public Object authenticate(String user, String password) {
74 return factory.getSecurityManager().authenticate(user, password);
77 public Object authenticate( DigestResponse digestRequest ) {
78 return factory.getSecurityManager().authenticate(digestRequest);
82 public boolean isDigestAllowed() {
89 public boolean authorise(Request request, Method method, Auth auth) {
90 return factory.getSecurityManager().authorise(request, method, auth, this);
93 public String getRealm() {
94 return factory.getRealm(this.host);
97 public LockResult lock(LockTimeout timeout, LockInfo lockInfo) throws NotAuthorizedException {
98 return factory.getLockManager().lock(timeout, lockInfo, this);
101 public LockResult refreshLock(String token) throws NotAuthorizedException {
102 return factory.getLockManager().refresh(token, this);
105 public void unlock(String tokenId) throws NotAuthorizedException {
106 factory.getLockManager().unlock(tokenId, this);
109 public LockToken getCurrentLock() {
110 if( factory.getLockManager() != null ) {
111 return factory.getLockManager().getCurrentToken( this );
113 log.warn("getCurrentLock called, but no lock manager: file: " + resource);
120 * Retrieve the currentUser.
122 * @return the currentUser
124 public User getCurrentUser() {
125 if(currentUser!=null)
127 if(HttpManager.request().getAuthorization()!=null && HttpManager.request().getAuthorization().getTag()==null){
128 String username = HttpManager.request().getAuthorization().getUser();
129 //log.info("username is:"+username);
132 currentUser = getService().getUserByUserName(username);
133 } catch (RpcException e) {
134 // TODO Auto-generated catch block
135 log.error("unable to access ejb service",e);
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");
146 * A helper method that retrieves a reference to the ExternalAPI bean and
147 * stores it for future use.
149 * @return an ExternalAPI instance
150 * @throws RpcException in case an error occurs
152 protected ExternalAPI getService() throws RpcException {
154 final Context ctx = new InitialContext();
155 final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
156 return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
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");