Paul Martin
2016-03-21 77c38a1865564a4ea4544bfb1f52bedf4326d481
commit | author | age
f13c4c 1 /*
JM 2  * Copyright 2011 gitblit.com.
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  */
5fe7df 16 package com.gitblit.wicket.pages;
JM 17
77c38a 18 import java.io.OutputStream;
5fe7df 19 import java.util.List;
JM 20
21 import org.apache.wicket.PageParameters;
22 import org.apache.wicket.markup.html.basic.Label;
1a3fc5 23 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
ff17f7 24 import org.apache.wicket.markup.html.link.ExternalLink;
77c38a 25 import org.apache.wicket.markup.html.link.Link;
1a3fc5 26 import org.apache.wicket.markup.html.panel.Fragment;
5fe7df 27 import org.apache.wicket.markup.repeater.Item;
JM 28 import org.apache.wicket.markup.repeater.data.DataView;
29 import org.apache.wicket.markup.repeater.data.ListDataProvider;
77c38a 30 import org.apache.wicket.request.target.resource.ResourceStreamRequestTarget;
PM 31 import org.apache.wicket.util.resource.AbstractResourceStreamWriter;
32 import org.apache.wicket.util.resource.IResourceStream;
f08c1c 33 import org.eclipse.jgit.lib.FileMode;
5fe7df 34 import org.eclipse.jgit.lib.Repository;
JM 35 import org.eclipse.jgit.revwalk.RevCommit;
36
1f9dae 37 import com.gitblit.models.PathModel;
eb870f 38 import com.gitblit.models.SubmoduleModel;
77c38a 39 import com.gitblit.models.UserModel;
ff17f7 40 import com.gitblit.servlet.RawServlet;
5fe7df 41 import com.gitblit.utils.ByteFormat;
JM 42 import com.gitblit.utils.JGitUtils;
a7db57 43 import com.gitblit.wicket.CacheControl;
77c38a 44 import com.gitblit.wicket.GitBlitWebSession;
a7db57 45 import com.gitblit.wicket.CacheControl.LastModified;
5fe7df 46 import com.gitblit.wicket.WicketUtils;
c1c3c6 47 import com.gitblit.wicket.panels.CommitHeaderPanel;
59b817 48 import com.gitblit.wicket.panels.CompressedDownloadsPanel;
1f9dae 49 import com.gitblit.wicket.panels.LinkPanel;
5fe7df 50 import com.gitblit.wicket.panels.PathBreadcrumbsPanel;
JM 51
a7db57 52 @CacheControl(LastModified.BOOT)
5fe7df 53 public class TreePage extends RepositoryPage {
JM 54
55     public TreePage(PageParameters params) {
cebf45 56         super(params);
5fe7df 57
f602a2 58         final String path = WicketUtils.getPath(params);
5fe7df 59
JM 60         Repository r = getRepository();
bc9d4a 61         RevCommit commit = getCommit();
a9a2ff 62         List<PathModel> paths = JGitUtils.getFilesInPath2(r, path, commit);
5fe7df 63
JM 64         // tree page links
2a7306 65         add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
JM 66                 WicketUtils.newPathParameter(repositoryName, objectId, path)));
59b817 67         add(new CompressedDownloadsPanel("compressedLinks", getRequest()
JM 68                 .getRelativePathPrefixToContextRoot(), repositoryName, objectId, path));
608ece 69
c1c3c6 70         add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
5fe7df 71
JM 72         // breadcrumbs
f602a2 73         add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, path, objectId));
JM 74         if (path != null && path.trim().length() > 0) {
28d6b2 75             // add .. parent path entry
JM 76             String parentPath = null;
77             if (path.lastIndexOf('/') > -1) {
78                 parentPath = path.substring(0, path.lastIndexOf('/'));
79             }
46f33f 80             PathModel model = new PathModel("..", parentPath, null, 0, FileMode.TREE.getBits(), null, objectId);
28d6b2 81             model.isParentPath = true;
JM 82             paths.add(0, model);
5fe7df 83         }
JM 84
ab1e11 85         final String id = getBestCommitId(commit);
46f33f 86         
5fe7df 87         final ByteFormat byteFormat = new ByteFormat();
2179fb 88         final String baseUrl = WicketUtils.getGitblitURL(getRequest());
JM 89
155bf7 90         // changed paths list
5fe7df 91         ListDataProvider<PathModel> pathsDp = new ListDataProvider<PathModel>(paths);
JM 92         DataView<PathModel> pathsView = new DataView<PathModel>("changedPath", pathsDp) {
93             private static final long serialVersionUID = 1L;
2a7306 94             int counter;
5fe7df 95
699e71 96             @Override
5fe7df 97             public void populateItem(final Item<PathModel> item) {
77c38a 98                 final PathModel entry = item.getModelObject();
46f33f 99                 
5fe7df 100                 item.add(new Label("pathPermissions", JGitUtils.getPermissionsFromMode(entry.mode)));
46f33f 101                 
5fe7df 102                 if (entry.isParentPath) {
JM 103                     // parent .. path
1e8390 104                     item.add(WicketUtils.newBlankImage("pathIcon"));
fc8426 105                     item.add(new Label("pathSize", ""));
2a7306 106                     item.add(new LinkPanel("pathName", null, entry.name, TreePage.class,
a2709d 107                             WicketUtils
ab1e11 108                                     .newPathParameter(repositoryName, id, entry.path)));
46f33f 109                     item.add(new Label("filestore", getString("gb.filestore")).setVisible(false));
1a3fc5 110                     item.add(new Label("pathLinks", ""));
5fe7df 111                 } else {
JM 112                     if (entry.isTree()) {
113                         // folder/tree link
1e8390 114                         item.add(WicketUtils.newImage("pathIcon", "folder_16x16.png"));
fc8426 115                         item.add(new Label("pathSize", ""));
2a7306 116                         item.add(new LinkPanel("pathName", "list", entry.name, TreePage.class,
ab1e11 117                                 WicketUtils.newPathParameter(repositoryName, id,
a2709d 118                                         entry.path)));
46f33f 119
PM 120                         item.add(new Label("filestore", getString("gb.filestore")).setVisible(false));
155bf7 121
1a3fc5 122                         // links
JM 123                         Fragment links = new Fragment("pathLinks", "treeLinks", this);
2a7306 124                         links.add(new BookmarkablePageLink<Void>("tree", TreePage.class,
ab1e11 125                                 WicketUtils.newPathParameter(repositoryName, id,
2a7306 126                                         entry.path)));
JM 127                         links.add(new BookmarkablePageLink<Void>("history", HistoryPage.class,
ab1e11 128                                 WicketUtils.newPathParameter(repositoryName, id,
699e71 129                                         entry.path)));
59b817 130                         links.add(new CompressedDownloadsPanel("compressedLinks", baseUrl,
JM 131                                 repositoryName, objectId, entry.path));
132
1a3fc5 133                         item.add(links);
eb870f 134                     } else if (entry.isSubmodule()) {
JM 135                         // submodule
699e71 136                         String submoduleId = entry.objectId;
eb870f 137                         String submodulePath;
JM 138                         boolean hasSubmodule = false;
139                         SubmoduleModel submodule = getSubmodule(entry.path);
140                         submodulePath = submodule.gitblitPath;
141                         hasSubmodule = submodule.hasSubmodule;
699e71 142
eb870f 143                         item.add(WicketUtils.newImage("pathIcon", "git-orange-16x16.png"));
JM 144                         item.add(new Label("pathSize", ""));
699e71 145                         item.add(new LinkPanel("pathName", "list", entry.name + " @ " +
eb870f 146                                 getShortObjectId(submoduleId), TreePage.class,
JM 147                                 WicketUtils.newPathParameter(submodulePath, submoduleId, "")).setEnabled(hasSubmodule));
699e71 148
46f33f 149                         item.add(new Label("filestore", getString("gb.filestore")).setVisible(false));
PM 150                         
eb870f 151                         Fragment links = new Fragment("pathLinks", "submoduleLinks", this);
JM 152                         links.add(new BookmarkablePageLink<Void>("view", SummaryPage.class,
153                                 WicketUtils.newRepositoryParameter(submodulePath)).setEnabled(hasSubmodule));
154                         links.add(new BookmarkablePageLink<Void>("tree", TreePage.class,
155                                 WicketUtils.newPathParameter(submodulePath, submoduleId,
156                                         "")).setEnabled(hasSubmodule));
157                         links.add(new BookmarkablePageLink<Void>("history", HistoryPage.class,
ab1e11 158                                 WicketUtils.newPathParameter(repositoryName, id,
9cc56a 159                                         entry.path)));
59b817 160                         links.add(new CompressedDownloadsPanel("compressedLinks", baseUrl,
JM 161                                 submodulePath, submoduleId, "").setEnabled(hasSubmodule));
699e71 162                         item.add(links);
5fe7df 163                     } else {
JM 164                         // blob link
7670a0 165                         String displayPath = entry.name;
e5662e 166                         String path = entry.path;
JM 167                         if (entry.isSymlink()) {
168                             path = JGitUtils.getStringContent(getRepository(), getCommit().getTree(), path);
7670a0 169                             displayPath = entry.name + " -> " + path;
e5662e 170                         }
c1c3c6 171                         item.add(WicketUtils.getFileImage("pathIcon", entry.name));
5fe7df 172                         item.add(new Label("pathSize", byteFormat.format(entry.size)));
46f33f 173                         
1a3fc5 174                         // links
JM 175                         Fragment links = new Fragment("pathLinks", "blobLinks", this);
46f33f 176                         
PM 177                         if (entry.isFilestoreItem()) {
178                             item.add(new Label("filestore", getString("gb.filestore")).setVisible(true));
179                             
77c38a 180                             item.add(new LinkPanel("pathName", "list", displayPath, new Link<Object>("link", null) {
PM 181                                  
182                                 private static final long serialVersionUID = 1L;
183
184                                 @Override
185                                 public void onClick() {
186                              
187                                      IResourceStream resourceStream = new AbstractResourceStreamWriter() {
188                                                                                  
189                                         private static final long serialVersionUID = 1L;
190
191                                         @Override 
192                                           public void write(OutputStream output) {
193                                                 UserModel user =  GitBlitWebSession.get().getUser();
194                                              user = user == null ? UserModel.ANONYMOUS : user;
195                                                 
196                                             app().filestore().downloadBlob(entry.getFilestoreOid(), user, getRepositoryModel(), output);
197                                           }
198                                       };
199                                           
200                                     
201                                     getRequestCycle().setRequestTarget(new ResourceStreamRequestTarget(resourceStream, entry.path));
202                                 }}));
46f33f 203                             
77c38a 204                             links.add(new Link<Object>("view", null) {
PM 205                                  
206                                 private static final long serialVersionUID = 1L;
207
208                                 @Override
209                                 public void onClick() {
210                              
211                                      IResourceStream resourceStream = new AbstractResourceStreamWriter() {
212                                                                                  
213                                         private static final long serialVersionUID = 1L;
214
215                                         @Override 
216                                           public void write(OutputStream output) {
217                                                 UserModel user =  GitBlitWebSession.get().getUser();
218                                              user = user == null ? UserModel.ANONYMOUS : user;
219                                                 
220                                             app().filestore().downloadBlob(entry.getFilestoreOid(), user, getRepositoryModel(), output);
221                                           }
222                                       };
223                                           
224                                     
225                                     getRequestCycle().setRequestTarget(new ResourceStreamRequestTarget(resourceStream, entry.path));
226                                 }});
227                             
228                             links.add(new Link<Object>("raw", null) {
229                                  
230                                 private static final long serialVersionUID = 1L;
231
232                                 @Override
233                                 public void onClick() {
234                              
235                                      IResourceStream resourceStream = new AbstractResourceStreamWriter() {
236                                                                                  
237                                         private static final long serialVersionUID = 1L;
238
239                                         @Override 
240                                           public void write(OutputStream output) {
241                                                 UserModel user =  GitBlitWebSession.get().getUser();
242                                              user = user == null ? UserModel.ANONYMOUS : user;
243                                                 
244                                             app().filestore().downloadBlob(entry.getFilestoreOid(), user, getRepositoryModel(), output);
245                                           }
246                                       };
247                                           
248                                     
249                                     getRequestCycle().setRequestTarget(new ResourceStreamRequestTarget(resourceStream, entry.path));
250                                 }});
46f33f 251                             
PM 252                         } else {
253                             item.add(new Label("filestore", getString("gb.filestore")).setVisible(false));
254                             
255                             item.add(new LinkPanel("pathName", "list", displayPath, BlobPage.class,
256                                     WicketUtils.newPathParameter(repositoryName, id,
257                                             path)));
258                             
259                             links.add(new BookmarkablePageLink<Void>("view", BlobPage.class,
260                                     WicketUtils.newPathParameter(repositoryName, id,
261                                             path)));
262                             String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, id, path);
263                             links.add(new ExternalLink("raw", rawUrl));
264                         }
265                         
716745 266                         links.add(new BookmarkablePageLink<Void>("blame", BlamePage.class,
ab1e11 267                                 WicketUtils.newPathParameter(repositoryName, id,
e5662e 268                                         path)));
2a7306 269                         links.add(new BookmarkablePageLink<Void>("history", HistoryPage.class,
ab1e11 270                                 WicketUtils.newPathParameter(repositoryName, id,
e5662e 271                                         path)));
1a3fc5 272                         item.add(links);
5fe7df 273                     }
JM 274                 }
1a3fc5 275                 WicketUtils.setAlternatingBackground(item, counter);
5fe7df 276                 counter++;
JM 277             }
278         };
279         add(pathsView);
280     }
155bf7 281
cebf45 282     @Override
JM 283     protected String getPageName() {
1e47ab 284         return getString("gb.tree");
cebf45 285     }
5d5e55 286
JM 287     @Override
288     protected boolean isCommitPage() {
289         return true;
290     }
291
5fe7df 292 }