You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SparkleShareInviteServlet.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2013 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. import java.io.IOException;
  18. import java.text.MessageFormat;
  19. import javax.servlet.ServletException;
  20. import javax.servlet.http.HttpServlet;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import com.gitblit.manager.IRepositoryManager;
  24. import com.gitblit.manager.IRuntimeManager;
  25. import com.gitblit.manager.ISessionManager;
  26. import com.gitblit.manager.IUserManager;
  27. import com.gitblit.models.RepositoryModel;
  28. import com.gitblit.models.UserModel;
  29. import com.gitblit.utils.StringUtils;
  30. /**
  31. * Handles requests for Sparkleshare Invites
  32. *
  33. * @author James Moger
  34. *
  35. */
  36. public class SparkleShareInviteServlet extends HttpServlet {
  37. private static final long serialVersionUID = 1L;
  38. public SparkleShareInviteServlet() {
  39. super();
  40. }
  41. @Override
  42. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  43. throws ServletException, java.io.IOException {
  44. processRequest(request, response);
  45. }
  46. @Override
  47. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  48. throws ServletException, IOException {
  49. processRequest(request, response);
  50. }
  51. protected void processRequest(javax.servlet.http.HttpServletRequest request,
  52. javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
  53. java.io.IOException {
  54. IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings();
  55. IUserManager userManager = GitBlit.getManager(IUserManager.class);
  56. ISessionManager sessionManager = GitBlit.getManager(ISessionManager.class);
  57. IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class);
  58. // extract repo name from request
  59. String repoUrl = request.getPathInfo().substring(1);
  60. // trim trailing .xml
  61. if (repoUrl.endsWith(".xml")) {
  62. repoUrl = repoUrl.substring(0, repoUrl.length() - 4);
  63. }
  64. String servletPath = Constants.GIT_PATH;
  65. int schemeIndex = repoUrl.indexOf("://") + 3;
  66. String host = repoUrl.substring(0, repoUrl.indexOf('/', schemeIndex));
  67. String path = repoUrl.substring(repoUrl.indexOf(servletPath) + servletPath.length());
  68. String username = null;
  69. int fetchIndex = repoUrl.indexOf('@');
  70. if (fetchIndex > -1) {
  71. username = repoUrl.substring(schemeIndex, fetchIndex);
  72. }
  73. UserModel user;
  74. if (StringUtils.isEmpty(username)) {
  75. user = sessionManager.authenticate(request);
  76. } else {
  77. user = userManager.getUserModel(username);
  78. }
  79. if (user == null) {
  80. user = UserModel.ANONYMOUS;
  81. username = "";
  82. }
  83. // ensure that the requested repository exists
  84. RepositoryModel model = repositoryManager.getRepositoryModel(path);
  85. if (model == null) {
  86. response.setStatus(HttpServletResponse.SC_NOT_FOUND);
  87. response.getWriter().append(MessageFormat.format("Repository \"{0}\" not found!", path));
  88. return;
  89. }
  90. StringBuilder sb = new StringBuilder();
  91. sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  92. sb.append("<sparkleshare><invite>\n");
  93. sb.append(MessageFormat.format("<address>{0}</address>\n", host));
  94. sb.append(MessageFormat.format("<remote_path>{0}{1}</remote_path>\n", servletPath, model.name));
  95. if (settings.getInteger(Keys.fanout.port, 0) > 0) {
  96. // Gitblit is running it's own fanout service for pubsub notifications
  97. sb.append(MessageFormat.format("<announcements_url>tcp://{0}:{1}</announcements_url>\n", request.getServerName(), settings.getString(Keys.fanout.port, "")));
  98. }
  99. sb.append("</invite></sparkleshare>\n");
  100. // write invite to client
  101. response.setContentType("application/xml");
  102. response.setContentLength(sb.length());
  103. response.getWriter().append(sb.toString());
  104. }
  105. }