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.5KB

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