View Javadoc

1   /***
2    * Copyright (C) 2008 Jeremy Thomerson (jthomerson@users.sourceforge.net)
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.vcaperture.web.urls;
18  
19  import net.sf.vcaperture.web.ApertureWebApp;
20  import net.sf.vcaperture.web.BasicDirectoryViewPage;
21  import net.sf.vcaperture.web.models.RepositoryByNameModel;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.wicket.IRequestTarget;
25  import org.apache.wicket.request.RequestParameters;
26  import org.apache.wicket.request.target.coding.AbstractRequestTargetUrlCodingStrategy;
27  import org.apache.wicket.request.target.component.BookmarkablePageRequestTarget;
28  import org.apache.wicket.request.target.component.PageRequestTarget;
29  import org.apache.wicket.request.target.component.listener.IListenerInterfaceRequestTarget;
30  
31  public class BrowseStrategy extends AbstractRequestTargetUrlCodingStrategy {
32  
33  	public BrowseStrategy(String mountPath) {
34  	    super(mountPath);
35      }
36  
37  	public IRequestTarget decode(RequestParameters rp) {
38  		String repoName = null;
39  		String path = rp.getPath();
40  		if (path.startsWith(getMountPath())) {
41  			// +1 is to get rid of the trailing "/" after the mount path
42  			path = path.substring(Math.min(path.length(), getMountPath().length() + 1));
43  		}
44  		// System.out.println("PATH: " + path);
45  		if (StringUtils.isEmpty(path)) {
46  			return new BookmarkablePageRequestTarget(ApertureWebApp.get().getHomePage());
47  		}
48  		int firstSlash = path.indexOf('/');
49  		if (firstSlash > -1) {
50  			repoName = path.substring(0, firstSlash);
51  			path = path.substring(firstSlash);
52  			// System.out.println("REPO: " + repoName + "; PATH: " + path);
53  		} else {
54  			repoName = path;
55  			path = "";
56  			// System.out.println("REPO: " + repoName + "; PATH: " + path + "[no first slash]");
57  		}
58  	    return new PageRequestTarget(new BasicDirectoryViewPage(new RepositoryByNameModel(repoName), path));
59      }
60  
61  	public CharSequence encode(IRequestTarget requestTarget) {
62  		ViewDirectoryLink vdl = (ViewDirectoryLink) ((IListenerInterfaceRequestTarget) requestTarget).getTarget();
63  	    return getMountPath() + "/" + vdl.getDirectoryPath();
64      }
65  
66  	public boolean matches(IRequestTarget requestTarget) {
67  	    return requestTarget instanceof IListenerInterfaceRequestTarget
68  	    		&& ((IListenerInterfaceRequestTarget) requestTarget).getTarget() instanceof ViewDirectoryLink;
69      }
70  
71  }