James Moger
2015-11-22 ed552ba47c02779c270ffd62841d6d1048dade70
commit | author | age
8d9750 1 /*
JM 2  * Copyright 2012 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  */
16 package com.gitblit.servlet;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Date;
21
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.eclipse.jgit.lib.Repository;
27 import org.eclipse.jgit.revwalk.RevCommit;
28
29 import com.gitblit.Constants;
30 import com.gitblit.manager.IRepositoryManager;
31 import com.gitblit.manager.IRuntimeManager;
32 import com.gitblit.utils.JGitUtils;
33 import com.google.inject.Inject;
34 import com.google.inject.Singleton;
35
36 /**
37  * Serves the content of a gh-pages branch.
38  *
39  * @author James Moger
40  *
41  */
42 @Singleton
43 public class PagesServlet extends RawServlet {
44
45     private static final long serialVersionUID = 1L;
46
47
48     /**
49      * Returns an url to this servlet for the specified parameters.
50      *
51      * @param baseURL
52      * @param repository
53      * @param path
54      * @return an url
55      */
56     public static String asLink(String baseURL, String repository, String path) {
57         if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') {
58             baseURL = baseURL.substring(0, baseURL.length() - 1);
59         }
60         return baseURL + Constants.PAGES + repository + "/" + (path == null ? "" : ("/" + path));
61     }
62
63     @Inject
64     public PagesServlet(
65             IRuntimeManager runtimeManager,
66             IRepositoryManager repositoryManager) {
67
68         super(runtimeManager, repositoryManager);
69     }
70
71     @Override
72     protected String getBranch(String repository, HttpServletRequest request) {
73         return "gh-pages";
74     }
75
76     @Override
77     protected String getPath(String repository, String branch, HttpServletRequest request) {
78         String pi = request.getPathInfo().substring(1);
79         if (pi.equals(repository)) {
80             return "";
81         }
82         String path = pi.substring(pi.indexOf(repository) + repository.length() + 1);
83         if (path.endsWith("/")) {
84             path = path.substring(0, path.length() - 1);
85         }
86         return path;
87     }
88
89     @Override
90     protected boolean renderIndex() {
91         return true;
92     }
93
94     @Override
95     protected void setContentType(HttpServletResponse response, String contentType) {
96         response.setContentType(contentType);;
97     }
98
99     @Override
248b71 100     protected boolean streamFromRepo(HttpServletRequest request, HttpServletResponse response, Repository repository,
8d9750 101             RevCommit commit, String requestedPath) throws IOException {
JM 102
103         response.setDateHeader("Last-Modified", JGitUtils.getCommitDate(commit).getTime());
104         response.setHeader("Cache-Control", "public, max-age=3600, must-revalidate");
105
248b71 106         return super.streamFromRepo(request, response, repository, commit, requestedPath);
8d9750 107     }
JM 108
109     @Override
110     protected void sendContent(HttpServletResponse response, Date date, InputStream is) throws ServletException, IOException {
111         response.setDateHeader("Last-Modified", date.getTime());
112         response.setHeader("Cache-Control", "public, max-age=3600, must-revalidate");
113
114         super.sendContent(response, date, is);
115     }
116 }