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;
22 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
23 import gr.ebs.gss.client.exceptions.RpcException;
24 import gr.ebs.gss.server.domain.User;
25 import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
26 import gr.ebs.gss.server.domain.dto.FolderDTO;
27 import gr.ebs.gss.server.domain.dto.UserDTO;
28 import gr.ebs.gss.server.ejb.ExternalAPI;
29 import gr.ebs.gss.server.ejb.TransactionHelper;
31 import java.util.Date;
32 import java.util.concurrent.Callable;
34 import javax.naming.Context;
35 import javax.naming.InitialContext;
36 import javax.naming.NamingException;
37 import javax.rmi.PortableRemoteObject;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.bradmcevoy.http.HttpManager;
43 import com.bradmcevoy.http.Resource;
44 import com.bradmcevoy.http.ResourceFactory;
45 import com.bradmcevoy.http.SecurityManager;
46 import com.bradmcevoy.http.Request.Method;
47 import com.ettrema.http.fs.LockManager;
54 public class GSSResourceFactory implements ResourceFactory {
55 private static final Logger log = LoggerFactory.getLogger(GSSResourceFactory.class);
57 SecurityManager securityManager;
58 LockManager lockManager;
61 boolean allowDirectoryBrowsing;
63 HttpManager httpManager;
65 public Resource getResource(String host, String url) {
69 log.debug("getResource: host: " + host + " - url:" + url);
70 url = stripContext(url);
71 if(url==null||url.trim().equals("")||url.equals("/")){
73 return new GssRootFolderResource(host, this, null,"/");
75 /*//log.info("URL:"+url);
76 if(url.equals("/OthersShared")||url.equals("/OthersShared/")){
77 //log.info("[returning others]");
78 return new GssOthersResource(host, this);
80 if(url.startsWith("/OthersShared")){
85 if(HttpManager.request().getAuthorization()!=null && HttpManager.request().getAuthorization().getTag()==null){
86 String username = HttpManager.request().getAuthorization().getUser();
87 //log.info("username is:"+username);
89 user = getService().getUserByUserName(username);
91 else if(HttpManager.request().getAuthorization()!=null&&HttpManager.request().getAuthorization().getTag()!=null){
92 //log.info(HttpManager.request().getAuthorization().getUser());
93 user =(UserDTO) HttpManager.request().getAuthorization().getTag();//getService().getUserByUserName("past@ebs.gr");
97 //if(HttpManager.request().getMethod().equals(Method.PROPFIND)){
98 ////log.info("[PROP FIND RETURNING ROOT FOR:]"+url);
99 return new GssRootFolderResource(host, this, null,url);
101 ////log.info("[RETURNING NULL FOR:]"+url+" "+HttpManager.request().getMethod());
105 Object r = getResourceGss(url,user);
110 if(r instanceof FolderDTO)
111 return new GssFolderResource(host, this,r ,user);
113 return new GssFileResource(host, this,r,user);
114 } catch (RpcException e) {
119 public Long maxAgeSeconds(GssResource resource) {
120 return maxAgeSeconds;
122 protected Object getResourceGss(String path, UserDTO user) throws RpcException{
123 //log.info(path+" <--> "+HttpManager.request().getAuthorization() + HttpManager.request().getHeaders());
125 if(HttpManager.request().getAuthorization()!=null && HttpManager.request().getAuthorization().getTag()==null){
126 String username = HttpManager.request().getAuthorization().getUser();
127 //log.info("username is:"+username);
129 user = getService().getUserByUserName(username);
131 else if(HttpManager.request().getAuthorization()!=null&&HttpManager.request().getAuthorization().getTag()!=null){
132 //log.info(HttpManager.request().getAuthorization().getUser());
133 user =(UserDTO) HttpManager.request().getAuthorization().getTag();//getService().getUserByUserName("past@ebs.gr");
138 //log.info("---------------->"+path);
142 //UserDTO user =getService().getUserByUserName("past@ebs.gr");
143 /*String username = HttpManager.request().getHeaders().get("authorization");
147 username=getUsernameFromAuthHeader(username);
149 user= getService().getUserByUserName(username);
150 } catch (RpcException e) {
151 // TODO Auto-generated catch block
156 boolean exists = true;
157 Object resource = null;
158 FileHeaderDTO file = null;
159 FolderDTO folder = null;
161 resource = getService().getResourceAtPath(user.getId(), path, true);
162 } catch (ObjectNotFoundException e) {
164 } catch (RpcException e) {
178 private String stripContext( String url ) {
179 if( this.contextPath != null && contextPath.length() > 0 ) {
180 url = url.replaceFirst( '/' + contextPath, "");
181 log.debug( "stripped context: " + url);
189 * For a provided path, remove the last element and return the rest, that is
190 * the path of the parent folder.
192 * @param path the specified path
193 * @return the path of the parent folder
194 * @throws ObjectNotFoundException if the provided string contains no path
197 protected String getParentPath(String path) throws ObjectNotFoundException {
198 int lastDelimiter = path.lastIndexOf('/');
199 if (lastDelimiter == 0)
201 if (lastDelimiter == -1)
203 throw new ObjectNotFoundException("There is no parent in the path: " + path);
204 else if (lastDelimiter < path.length() - 1)
205 // Return the part before the delimiter.
206 return path.substring(0, lastDelimiter);
208 // Remove the trailing delimiter and then recurse.
209 String strippedTrail = path.substring(0, lastDelimiter);
210 return getParentPath(strippedTrail);
215 * A helper method that retrieves a reference to the ExternalAPI bean and
216 * stores it for future use.
218 * @return an ExternalAPI instance
219 * @throws RpcException in case an error occurs
221 protected ExternalAPI getService() throws RpcException {
223 final Context ctx = new InitialContext();
224 final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
225 return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
226 } catch (final NamingException e) {
227 log.error("Unable to retrieve the ExternalAPI EJB", e);
228 throw new RpcException("An error occurred while contacting the naming service");
232 private void updateAccounting(final User user, final Date date, final long bandwidthDiff) {
234 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
236 public Void call() throws Exception {
237 getService().updateAccounting(user, date, bandwidthDiff);
241 } catch (RuntimeException e) {
243 } catch (Exception e) {
244 // updateAccounting() doesn't throw any checked exceptions
251 * Retrieve the securityManager.
253 * @return the securityManager
255 public SecurityManager getSecurityManager() {
256 return securityManager;
261 * Retrieve the lockManager.
263 * @return the lockManager
265 public LockManager getLockManager() {
271 * Retrieve the maxAgeSeconds.
273 * @return the maxAgeSeconds
275 public Long getMaxAgeSeconds() {
276 return maxAgeSeconds;
281 * Retrieve the contextPath.
283 * @return the contextPath
285 public String getContextPath() {
291 * Retrieve the allowDirectoryBrowsing.
293 * @return the allowDirectoryBrowsing
295 public boolean isAllowDirectoryBrowsing() {
296 return allowDirectoryBrowsing;
301 * Retrieve the defaultPage.
303 * @return the defaultPage
305 public String getDefaultPage() {
309 public String getRealm(String host) {
310 return securityManager.getRealm(host);
315 * Modify the securityManager.
317 * @param securityManager the securityManager to set
319 public void setSecurityManager(SecurityManager securityManager) {
320 this.securityManager = securityManager;
325 * Modify the lockManager.
327 * @param lockManager the lockManager to set
329 public void setLockManager(LockManager lockManager) {
330 this.lockManager = lockManager;
335 * Modify the maxAgeSeconds.
337 * @param maxAgeSeconds the maxAgeSeconds to set
339 public void setMaxAgeSeconds(Long maxAgeSeconds) {
340 this.maxAgeSeconds = maxAgeSeconds;
345 * Modify the contextPath.
347 * @param contextPath the contextPath to set
349 public void setContextPath(String contextPath) {
350 this.contextPath = contextPath;
355 * Modify the defaultPage.
357 * @param defaultPage the defaultPage to set
359 public void setDefaultPage(String defaultPage) {
360 this.defaultPage = defaultPage;
365 * Retrieve the httpManager.
367 * @return the httpManager
369 public HttpManager getHttpManager() {
375 * Modify the httpManager.
377 * @param httpManager the httpManager to set
379 public void setHttpManager(HttpManager httpManager) {
380 this.httpManager = httpManager;
385 public static String getUsernameFromAuthHeader(String header) {
386 String first = header.split(",")[0];
387 int indx = first.indexOf("\"");
388 return first.substring(indx+1,first.length()-1);