commit | author | age
|
c22722
|
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 |
*/
|
|
16 |
package com.gitblit;
|
|
17 |
|
8c9a20
|
18 |
import java.text.MessageFormat;
|
c22722
|
19 |
import java.util.List;
|
JM |
20 |
|
|
21 |
import javax.servlet.http.HttpServlet;
|
|
22 |
|
|
23 |
import org.eclipse.jgit.lib.Repository;
|
|
24 |
import org.eclipse.jgit.revwalk.RevCommit;
|
|
25 |
import org.slf4j.Logger;
|
|
26 |
import org.slf4j.LoggerFactory;
|
|
27 |
|
|
28 |
import com.gitblit.models.RepositoryModel;
|
831469
|
29 |
import com.gitblit.utils.HttpUtils;
|
c22722
|
30 |
import com.gitblit.utils.JGitUtils;
|
JM |
31 |
import com.gitblit.utils.StringUtils;
|
|
32 |
import com.gitblit.utils.SyndicationUtils;
|
|
33 |
|
892570
|
34 |
/**
|
JM |
35 |
* SyndicationServlet generates RSS 2.0 feeds and feed links.
|
|
36 |
*
|
|
37 |
* Access to this servlet is protected by the SyndicationFilter.
|
|
38 |
*
|
|
39 |
* @author James Moger
|
|
40 |
*
|
|
41 |
*/
|
c22722
|
42 |
public class SyndicationServlet extends HttpServlet {
|
JM |
43 |
|
|
44 |
private static final long serialVersionUID = 1L;
|
|
45 |
|
|
46 |
private transient Logger logger = LoggerFactory.getLogger(SyndicationServlet.class);
|
|
47 |
|
892570
|
48 |
/**
|
JM |
49 |
* Create a feed link for the specified repository and branch/tag/commit id.
|
|
50 |
*
|
|
51 |
* @param baseURL
|
|
52 |
* @param repository
|
|
53 |
* the repository name
|
|
54 |
* @param objectId
|
|
55 |
* the branch, tag, or first commit for the feed
|
|
56 |
* @param length
|
|
57 |
* the number of commits to include in the feed
|
|
58 |
* @return an RSS feed url
|
|
59 |
*/
|
c22722
|
60 |
public static String asLink(String baseURL, String repository, String objectId, int length) {
|
8c9a20
|
61 |
if (baseURL.length() > 0 && baseURL.charAt(baseURL.length() - 1) == '/') {
|
c22722
|
62 |
baseURL = baseURL.substring(0, baseURL.length() - 1);
|
JM |
63 |
}
|
8c9a20
|
64 |
StringBuilder url = new StringBuilder();
|
JM |
65 |
url.append(baseURL);
|
5450d0
|
66 |
url.append(Constants.SYNDICATION_PATH);
|
8c9a20
|
67 |
url.append(repository);
|
JM |
68 |
if (!StringUtils.isEmpty(objectId) || length > 0) {
|
|
69 |
StringBuilder parameters = new StringBuilder("?");
|
|
70 |
if (StringUtils.isEmpty(objectId)) {
|
|
71 |
parameters.append("l=");
|
|
72 |
parameters.append(length);
|
|
73 |
} else {
|
|
74 |
parameters.append("h=");
|
|
75 |
parameters.append(objectId);
|
|
76 |
if (length > 0) {
|
|
77 |
parameters.append("&l=");
|
|
78 |
parameters.append(length);
|
|
79 |
}
|
|
80 |
}
|
|
81 |
url.append(parameters);
|
|
82 |
}
|
|
83 |
return url.toString();
|
|
84 |
}
|
85c2e6
|
85 |
|
892570
|
86 |
/**
|
JM |
87 |
* Determines the appropriate title for a feed.
|
|
88 |
*
|
|
89 |
* @param repository
|
|
90 |
* @param objectId
|
|
91 |
* @return title of the feed
|
|
92 |
*/
|
8c9a20
|
93 |
public static String getTitle(String repository, String objectId) {
|
JM |
94 |
String id = objectId;
|
|
95 |
if (!StringUtils.isEmpty(id)) {
|
|
96 |
if (id.startsWith(org.eclipse.jgit.lib.Constants.R_HEADS)) {
|
|
97 |
id = id.substring(org.eclipse.jgit.lib.Constants.R_HEADS.length());
|
|
98 |
} else if (id.startsWith(org.eclipse.jgit.lib.Constants.R_REMOTES)) {
|
|
99 |
id = id.substring(org.eclipse.jgit.lib.Constants.R_REMOTES.length());
|
|
100 |
} else if (id.startsWith(org.eclipse.jgit.lib.Constants.R_TAGS)) {
|
|
101 |
id = id.substring(org.eclipse.jgit.lib.Constants.R_TAGS.length());
|
|
102 |
}
|
|
103 |
}
|
|
104 |
return MessageFormat.format("{0} ({1})", repository, id);
|
c22722
|
105 |
}
|
JM |
106 |
|
892570
|
107 |
/**
|
JM |
108 |
* Generates the feed content.
|
|
109 |
*
|
|
110 |
* @param request
|
|
111 |
* @param response
|
|
112 |
* @throws javax.servlet.ServletException
|
|
113 |
* @throws java.io.IOException
|
|
114 |
*/
|
c22722
|
115 |
private void processRequest(javax.servlet.http.HttpServletRequest request,
|
JM |
116 |
javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
|
|
117 |
java.io.IOException {
|
8c9a20
|
118 |
|
2179fb
|
119 |
String servletUrl = request.getContextPath() + request.getServletPath();
|
JM |
120 |
String url = request.getRequestURI().substring(servletUrl.length());
|
8c9a20
|
121 |
if (url.charAt(0) == '/' && url.length() > 1) {
|
JM |
122 |
url = url.substring(1);
|
|
123 |
}
|
|
124 |
String repositoryName = url;
|
c22722
|
125 |
String objectId = request.getParameter("h");
|
JM |
126 |
String l = request.getParameter("l");
|
|
127 |
int length = GitBlit.getInteger(Keys.web.syndicationEntries, 25);
|
|
128 |
if (StringUtils.isEmpty(objectId)) {
|
|
129 |
objectId = org.eclipse.jgit.lib.Constants.HEAD;
|
|
130 |
}
|
|
131 |
if (!StringUtils.isEmpty(l)) {
|
|
132 |
try {
|
|
133 |
length = Integer.parseInt(l);
|
|
134 |
} catch (NumberFormatException x) {
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
466413
|
138 |
response.setContentType("application/rss+xml; charset=UTF-8");
|
c22722
|
139 |
Repository repository = GitBlit.self().getRepository(repositoryName);
|
JM |
140 |
RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
|
|
141 |
List<RevCommit> commits = JGitUtils.getRevLog(repository, objectId, 0, length);
|
|
142 |
try {
|
2179fb
|
143 |
SyndicationUtils.toRSS(HttpUtils.getGitblitURL(request), getTitle(model.name, objectId), model.description,
|
8c9a20
|
144 |
model.name, commits, response.getOutputStream());
|
c22722
|
145 |
} catch (Exception e) {
|
JM |
146 |
logger.error("An error occurred during feed generation", e);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
@Override
|
|
151 |
protected void doPost(javax.servlet.http.HttpServletRequest request,
|
|
152 |
javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
|
|
153 |
java.io.IOException {
|
|
154 |
processRequest(request, response);
|
|
155 |
}
|
|
156 |
|
|
157 |
@Override
|
|
158 |
protected void doGet(javax.servlet.http.HttpServletRequest request,
|
|
159 |
javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
|
|
160 |
java.io.IOException {
|
|
161 |
processRequest(request, response);
|
|
162 |
}
|
|
163 |
}
|