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.FileHeader;
25 import gr.ebs.gss.server.domain.Folder;
26 import gr.ebs.gss.server.domain.User;
27 import gr.ebs.gss.server.ejb.ExternalAPI;
28 import gr.ebs.gss.server.ejb.TransactionHelper;
30 import java.util.Date;
31 import java.util.concurrent.Callable;
33 import javax.naming.Context;
34 import javax.naming.InitialContext;
35 import javax.naming.NamingException;
36 import javax.rmi.PortableRemoteObject;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.bradmcevoy.http.HttpManager;
42 import com.bradmcevoy.http.Resource;
43 import com.bradmcevoy.http.ResourceFactory;
44 import com.bradmcevoy.http.SecurityManager;
45 import com.ettrema.http.fs.LockManager;
52 public class GSSResourceFactory implements ResourceFactory {
53 private static final Logger log = LoggerFactory.getLogger(GSSResourceFactory.class);
55 SecurityManager securityManager;
56 LockManager lockManager;
59 boolean allowDirectoryBrowsing;
61 HttpManager httpManager;
63 public Resource getResource(String host, String url) {
67 log.debug("getResource: host: " + host + " - url:" + url);
68 url = stripContext(url);
69 if(url==null||url.trim().equals("")||url.equals("/")){
72 /*//log.info("URL:"+url);
73 if(url.equals("/OthersShared")||url.equals("/OthersShared/")){
74 //log.info("[returning others]");
75 return new GssOthersResource(host, this);
77 if(url.startsWith("/OthersShared")){
82 if(HttpManager.request().getAuthorization()!=null && HttpManager.request().getAuthorization().getTag()==null){
83 String username = HttpManager.request().getAuthorization().getUser();
85 user = getService().getUserByUserName(username);
87 else if(HttpManager.request().getAuthorization()!=null&&HttpManager.request().getAuthorization().getTag()!=null){
88 user =(User) HttpManager.request().getAuthorization().getTag();
92 //create a resource based on path if no resource exists at this path it will be handled by subsequent webdav method calls
93 return new GssRootFolderResource(host, this, null,url);
96 Object r = getResourceGss(url,user);
101 if(r instanceof Folder){
103 return new GssFolderResource(host, this,r ,user);
106 return new GssFileResource(host, this,r,user);
107 } catch (RpcException e) {
108 log.error("unable to access ejb service",e);
112 public Long maxAgeSeconds(GssResource resource) {
113 return maxAgeSeconds;
115 protected Object getResourceGss(String path, User user) throws RpcException{
118 if(HttpManager.request().getAuthorization()!=null && HttpManager.request().getAuthorization().getTag()==null){
119 String username = HttpManager.request().getAuthorization().getUser();
121 user = getService().getUserByUserName(username);
123 else if(HttpManager.request().getAuthorization()!=null&&HttpManager.request().getAuthorization().getTag()!=null){
124 user =(User) HttpManager.request().getAuthorization().getTag();
131 boolean exists = true;
132 Object resource = null;
134 resource = getService().getResourceAtPath(user.getId(), path, true);
135 } catch (ObjectNotFoundException e) {
137 } catch (RpcException e) {
146 if(resource instanceof Folder){
148 resource = getService().expandFolder((Folder) resource);
149 } catch (ObjectNotFoundException e) {
150 // TODO Auto-generated catch block
154 else if(resource instanceof FileHeader){
156 resource = getService().expandFile((FileHeader) resource);
157 } catch (ObjectNotFoundException e) {
158 // TODO Auto-generated catch block
166 private String stripContext( String url ) {
167 if( this.contextPath != null && contextPath.length() > 0 ) {
168 url = url.replaceFirst( '/' + contextPath, "");
169 log.debug( "stripped context: " + url);
177 * For a provided path, remove the last element and return the rest, that is
178 * the path of the parent folder.
180 * @param path the specified path
181 * @return the path of the parent folder
182 * @throws ObjectNotFoundException if the provided string contains no path
185 protected String getParentPath(String path) throws ObjectNotFoundException {
186 int lastDelimiter = path.lastIndexOf('/');
187 if (lastDelimiter == 0)
189 if (lastDelimiter == -1)
191 throw new ObjectNotFoundException("There is no parent in the path: " + path);
192 else if (lastDelimiter < path.length() - 1)
193 // Return the part before the delimiter.
194 return path.substring(0, lastDelimiter);
196 // Remove the trailing delimiter and then recurse.
197 String strippedTrail = path.substring(0, lastDelimiter);
198 return getParentPath(strippedTrail);
203 * A helper method that retrieves a reference to the ExternalAPI bean and
204 * stores it for future use.
206 * @return an ExternalAPI instance
207 * @throws RpcException in case an error occurs
209 protected ExternalAPI getService() throws RpcException {
211 final Context ctx = new InitialContext();
212 final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
213 return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
215 } catch (final NamingException e) {
216 log.error("Unable to retrieve the ExternalAPI EJB", e);
217 throw new RpcException("An error occurred while contacting the naming service");
221 private void updateAccounting(final User user, final Date date, final long bandwidthDiff) {
223 new TransactionHelper<Void>().tryExecute(new Callable<Void>() {
225 public Void call() throws Exception {
226 getService().updateAccounting(user, date, bandwidthDiff);
230 } catch (RuntimeException e) {
232 } catch (Exception e) {
233 // updateAccounting() doesn't throw any checked exceptions
240 * Retrieve the securityManager.
242 * @return the securityManager
244 public SecurityManager getSecurityManager() {
245 return securityManager;
250 * Retrieve the lockManager.
252 * @return the lockManager
254 public LockManager getLockManager() {
260 * Retrieve the maxAgeSeconds.
262 * @return the maxAgeSeconds
264 public Long getMaxAgeSeconds() {
265 return maxAgeSeconds;
270 * Retrieve the contextPath.
272 * @return the contextPath
274 public String getContextPath() {
280 * Retrieve the allowDirectoryBrowsing.
282 * @return the allowDirectoryBrowsing
284 public boolean isAllowDirectoryBrowsing() {
285 return allowDirectoryBrowsing;
290 * Retrieve the defaultPage.
292 * @return the defaultPage
294 public String getDefaultPage() {
298 public String getRealm(String host) {
299 return securityManager.getRealm(host);
304 * Modify the securityManager.
306 * @param securityManager the securityManager to set
308 public void setSecurityManager(SecurityManager securityManager) {
309 this.securityManager = securityManager;
314 * Modify the lockManager.
316 * @param lockManager the lockManager to set
318 public void setLockManager(LockManager lockManager) {
319 this.lockManager = lockManager;
324 * Modify the maxAgeSeconds.
326 * @param maxAgeSeconds the maxAgeSeconds to set
328 public void setMaxAgeSeconds(Long maxAgeSeconds) {
329 this.maxAgeSeconds = maxAgeSeconds;
334 * Modify the contextPath.
336 * @param contextPath the contextPath to set
338 public void setContextPath(String contextPath) {
339 this.contextPath = contextPath;
344 * Modify the defaultPage.
346 * @param defaultPage the defaultPage to set
348 public void setDefaultPage(String defaultPage) {
349 this.defaultPage = defaultPage;
354 * Retrieve the httpManager.
356 * @return the httpManager
358 public HttpManager getHttpManager() {
364 * Modify the httpManager.
366 * @param httpManager the httpManager to set
368 public void setHttpManager(HttpManager httpManager) {
369 this.httpManager = httpManager;
374 public static String getUsernameFromAuthHeader(String header) {
375 String first = header.split(",")[0];
376 int indx = first.indexOf("\"");
377 return first.substring(indx+1,first.length()-1);