2011-07-21 20:21:08 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) 2010, Sebastian Sdorra
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
* 3. Neither the name of SCM-Manager; nor the names of its
|
|
|
|
|
* contributors may be used to endorse or promote products derived from this
|
|
|
|
|
* software without specific prior written permission.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*
|
|
|
|
|
* http://bitbucket.org/sdorra/scm-manager
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-07 16:35:51 +02:00
|
|
|
package sonia.scm.web;
|
2011-07-21 20:21:08 +02:00
|
|
|
|
|
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
2013-07-27 17:18:38 +02:00
|
|
|
import com.google.common.base.Strings;
|
2013-04-02 21:57:23 +02:00
|
|
|
import com.google.common.io.Closeables;
|
2011-07-21 22:13:42 +02:00
|
|
|
import com.google.inject.Inject;
|
2011-10-08 15:31:10 +02:00
|
|
|
import com.google.inject.Provider;
|
2011-08-07 16:35:51 +02:00
|
|
|
import com.google.inject.Singleton;
|
2012-08-30 12:08:16 +02:00
|
|
|
import org.apache.shiro.SecurityUtils;
|
|
|
|
|
import org.apache.shiro.subject.Subject;
|
2011-07-21 20:21:08 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-10-18 16:08:49 +02:00
|
|
|
import sonia.scm.NotFoundException;
|
2011-10-08 15:31:10 +02:00
|
|
|
import sonia.scm.repository.HgContext;
|
2011-07-22 13:08:12 +02:00
|
|
|
import sonia.scm.repository.HgHookManager;
|
2011-07-21 22:13:42 +02:00
|
|
|
import sonia.scm.repository.HgRepositoryHandler;
|
2018-11-23 16:12:35 +01:00
|
|
|
import sonia.scm.repository.Repository;
|
2018-11-23 10:13:47 +01:00
|
|
|
import sonia.scm.repository.RepositoryDAO;
|
2011-09-28 09:42:16 +02:00
|
|
|
import sonia.scm.repository.RepositoryHookType;
|
2013-07-24 21:03:01 +02:00
|
|
|
import sonia.scm.repository.api.HgHookMessage;
|
|
|
|
|
import sonia.scm.repository.api.HgHookMessage.Severity;
|
2013-07-27 17:18:38 +02:00
|
|
|
import sonia.scm.repository.spi.HgHookContextProvider;
|
|
|
|
|
import sonia.scm.repository.spi.HookEventFacade;
|
2011-10-15 15:55:17 +02:00
|
|
|
import sonia.scm.security.CipherUtil;
|
2012-10-11 16:12:06 +02:00
|
|
|
import sonia.scm.security.Tokens;
|
2011-08-07 16:35:51 +02:00
|
|
|
import sonia.scm.util.HttpUtil;
|
2011-08-23 15:43:15 +02:00
|
|
|
import sonia.scm.util.Util;
|
2011-07-21 22:13:42 +02:00
|
|
|
|
2018-07-10 15:31:18 +02:00
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
2018-11-23 10:13:47 +01:00
|
|
|
import java.io.File;
|
2011-08-07 16:35:51 +02:00
|
|
|
import java.io.IOException;
|
2013-04-02 21:57:23 +02:00
|
|
|
import java.io.PrintWriter;
|
2013-07-22 11:40:03 +02:00
|
|
|
import java.util.List;
|
2011-08-07 16:35:51 +02:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
2018-07-10 15:31:18 +02:00
|
|
|
//~--- JDK imports ------------------------------------------------------------
|
2011-07-21 20:21:08 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
2011-08-07 16:35:51 +02:00
|
|
|
@Singleton
|
|
|
|
|
public class HgHookCallbackServlet extends HttpServlet
|
2011-07-21 20:21:08 +02:00
|
|
|
{
|
|
|
|
|
|
2011-09-28 09:42:16 +02:00
|
|
|
/** Field description */
|
|
|
|
|
public static final String HGHOOK_POST_RECEIVE = "changegroup";
|
|
|
|
|
|
|
|
|
|
/** Field description */
|
|
|
|
|
public static final String HGHOOK_PRE_RECEIVE = "pretxnchangegroup";
|
|
|
|
|
|
2011-10-23 16:39:09 +02:00
|
|
|
/** Field description */
|
|
|
|
|
public static final String PARAM_REPOSITORYPATH = "repositoryPath";
|
|
|
|
|
|
2011-08-07 16:35:51 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private static final String PARAM_CHALLENGE = "challenge";
|
|
|
|
|
|
2011-10-15 15:55:17 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private static final String PARAM_CREDENTIALS = "credentials";
|
|
|
|
|
|
2011-08-07 16:35:51 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private static final String PARAM_NODE = "node";
|
|
|
|
|
|
2012-06-02 14:42:46 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private static final String PARAM_PING = "ping";
|
|
|
|
|
|
2011-08-07 16:35:51 +02:00
|
|
|
/** Field description */
|
|
|
|
|
private static final Pattern REGEX_URL =
|
2011-10-23 16:39:09 +02:00
|
|
|
Pattern.compile("^/hook/hg/([^/]+)$");
|
2011-08-07 16:35:51 +02:00
|
|
|
|
|
|
|
|
/** the logger for HgHookCallbackServlet */
|
2011-07-21 20:21:08 +02:00
|
|
|
private static final Logger logger =
|
2011-08-07 16:35:51 +02:00
|
|
|
LoggerFactory.getLogger(HgHookCallbackServlet.class);
|
2011-07-21 20:21:08 +02:00
|
|
|
|
2011-12-30 11:17:05 +01:00
|
|
|
/** Field description */
|
|
|
|
|
private static final long serialVersionUID = 3531596724828189353L;
|
|
|
|
|
|
2011-07-21 22:13:42 +02:00
|
|
|
//~--- constructors ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
@Inject
|
2013-07-22 11:40:03 +02:00
|
|
|
public HgHookCallbackServlet(HookEventFacade hookEventFacade,
|
2018-11-23 10:13:47 +01:00
|
|
|
HgRepositoryHandler handler, HgHookManager hookManager,
|
|
|
|
|
Provider<HgContext> contextProvider, RepositoryDAO repositoryDAO)
|
2011-07-21 22:13:42 +02:00
|
|
|
{
|
2013-07-22 11:40:03 +02:00
|
|
|
this.hookEventFacade = hookEventFacade;
|
2011-07-21 22:13:42 +02:00
|
|
|
this.handler = handler;
|
2011-07-22 13:08:12 +02:00
|
|
|
this.hookManager = hookManager;
|
2011-10-08 15:31:10 +02:00
|
|
|
this.contextProvider = contextProvider;
|
2018-11-23 10:13:47 +01:00
|
|
|
this.repositoryDAO = repositoryDAO;
|
2011-07-21 22:13:42 +02:00
|
|
|
}
|
|
|
|
|
|
2011-07-21 20:21:08 +02:00
|
|
|
//~--- methods --------------------------------------------------------------
|
|
|
|
|
|
2012-06-02 14:42:46 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param request
|
|
|
|
|
* @param response
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws ServletException
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
{
|
|
|
|
|
String ping = request.getParameter(PARAM_PING);
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(ping) && Boolean.parseBoolean(ping))
|
|
|
|
|
{
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-23 15:43:15 +02:00
|
|
|
@Override
|
2018-07-16 13:51:37 +02:00
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
|
|
try {
|
|
|
|
|
handlePostRequest(request, response);
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
logger.warn("error in hook callback execution, sending internal server error", ex);
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void handlePostRequest(HttpServletRequest request, HttpServletResponse response) throws IOException
|
2011-08-23 15:43:15 +02:00
|
|
|
{
|
|
|
|
|
String strippedURI = HttpUtil.getStrippedURI(request);
|
|
|
|
|
Matcher m = REGEX_URL.matcher(strippedURI);
|
|
|
|
|
|
|
|
|
|
if (m.matches())
|
|
|
|
|
{
|
2018-11-23 16:12:35 +01:00
|
|
|
Repository repository = getRepositoryId(request);
|
2011-10-23 16:39:09 +02:00
|
|
|
String type = m.group(1);
|
2011-08-23 15:43:15 +02:00
|
|
|
String challenge = request.getParameter(PARAM_CHALLENGE);
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(challenge))
|
|
|
|
|
{
|
|
|
|
|
String node = request.getParameter(PARAM_NODE);
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(node))
|
|
|
|
|
{
|
2011-10-15 15:55:17 +02:00
|
|
|
String credentials = request.getParameter(PARAM_CREDENTIALS);
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(credentials))
|
|
|
|
|
{
|
2012-10-11 16:12:06 +02:00
|
|
|
authenticate(request, credentials);
|
2011-10-15 15:55:17 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-23 16:12:35 +01:00
|
|
|
hookCallback(response, repository, type, challenge, node);
|
2011-08-23 15:43:15 +02:00
|
|
|
}
|
|
|
|
|
else if (logger.isDebugEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.debug("node parameter not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (logger.isDebugEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.debug("challenge parameter not found");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (logger.isDebugEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.debug("url does not match");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-11 16:12:06 +02:00
|
|
|
private void authenticate(HttpServletRequest request, String credentials)
|
2011-10-15 15:55:17 +02:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
credentials = CipherUtil.getInstance().decode(credentials);
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(credentials))
|
|
|
|
|
{
|
2014-01-17 15:56:18 +01:00
|
|
|
int index = credentials.indexOf(':');
|
2011-10-15 15:55:17 +02:00
|
|
|
|
2014-01-17 15:56:18 +01:00
|
|
|
if (index > 0 && index < credentials.length())
|
2011-10-15 15:55:17 +02:00
|
|
|
{
|
2012-08-30 12:08:16 +02:00
|
|
|
Subject subject = SecurityUtils.getSubject();
|
2011-10-15 15:55:17 +02:00
|
|
|
|
2014-01-17 15:56:18 +01:00
|
|
|
//J-
|
|
|
|
|
subject.login(
|
|
|
|
|
Tokens.createAuthenticationToken(
|
|
|
|
|
request,
|
|
|
|
|
credentials.substring(0, index),
|
|
|
|
|
credentials.substring(index + 1)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
//J+
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.error("could not find delimiter");
|
2011-10-15 15:55:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.error("could not authenticate user", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 16:12:35 +01:00
|
|
|
private void fireHook(HttpServletResponse response, Repository repository,
|
2012-08-30 12:08:16 +02:00
|
|
|
String node, RepositoryHookType type)
|
|
|
|
|
throws IOException
|
2011-09-28 09:42:16 +02:00
|
|
|
{
|
2013-07-27 17:18:38 +02:00
|
|
|
HgHookContextProvider context = null;
|
|
|
|
|
|
2011-09-28 09:42:16 +02:00
|
|
|
try
|
|
|
|
|
{
|
2011-10-08 15:31:10 +02:00
|
|
|
if (type == RepositoryHookType.PRE_RECEIVE)
|
|
|
|
|
{
|
|
|
|
|
contextProvider.get().setPending(true);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 16:12:35 +01:00
|
|
|
context = new HgHookContextProvider(handler, repository, hookManager,
|
2013-07-27 17:18:38 +02:00
|
|
|
node, type);
|
2013-07-22 11:40:03 +02:00
|
|
|
|
2018-11-23 16:12:35 +01:00
|
|
|
hookEventFacade.handle(repository).fireHookEvent(type, context);
|
2013-07-22 11:40:03 +02:00
|
|
|
|
|
|
|
|
printMessages(response, context);
|
2011-09-28 09:42:16 +02:00
|
|
|
}
|
2018-10-18 16:08:49 +02:00
|
|
|
catch (NotFoundException ex)
|
2011-09-28 09:42:16 +02:00
|
|
|
{
|
2018-10-18 16:08:49 +02:00
|
|
|
logger.error(ex.getMessage());
|
2011-09-28 09:42:16 +02:00
|
|
|
|
2018-10-18 16:08:49 +02:00
|
|
|
logger.trace("repository not found", ex);
|
2011-09-28 09:42:16 +02:00
|
|
|
|
|
|
|
|
response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
|
|
|
|
}
|
2013-04-02 21:57:23 +02:00
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-07-27 17:18:38 +02:00
|
|
|
sendError(response, context, ex);
|
2013-04-02 21:57:23 +02:00
|
|
|
}
|
2011-09-28 09:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-23 16:12:35 +01:00
|
|
|
private void hookCallback(HttpServletResponse response, Repository repository, String typeName, String challenge, String node) throws IOException {
|
2011-07-22 13:08:12 +02:00
|
|
|
if (hookManager.isAcceptAble(challenge))
|
2011-07-21 22:13:42 +02:00
|
|
|
{
|
2011-09-28 09:42:16 +02:00
|
|
|
RepositoryHookType type = null;
|
|
|
|
|
|
|
|
|
|
if (HGHOOK_PRE_RECEIVE.equals(typeName))
|
2011-07-21 22:13:42 +02:00
|
|
|
{
|
2011-09-28 09:42:16 +02:00
|
|
|
type = RepositoryHookType.PRE_RECEIVE;
|
2011-07-22 13:08:12 +02:00
|
|
|
}
|
2011-09-28 09:42:16 +02:00
|
|
|
else if (HGHOOK_POST_RECEIVE.equals(typeName))
|
2011-07-22 13:08:12 +02:00
|
|
|
{
|
2011-09-28 09:42:16 +02:00
|
|
|
type = RepositoryHookType.POST_RECEIVE;
|
|
|
|
|
}
|
2011-07-22 13:08:12 +02:00
|
|
|
|
2011-09-28 09:42:16 +02:00
|
|
|
if (type != null)
|
|
|
|
|
{
|
2018-11-23 16:12:35 +01:00
|
|
|
fireHook(response, repository, node, type);
|
2011-09-28 09:42:16 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (logger.isWarnEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.warn("unknown hook type {}", typeName);
|
2011-07-21 22:13:42 +02:00
|
|
|
}
|
2011-07-22 13:08:12 +02:00
|
|
|
|
2011-09-28 09:42:16 +02:00
|
|
|
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
|
2011-07-22 13:08:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (logger.isWarnEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.warn("hg hook challenge is not accept able");
|
2011-07-21 22:13:42 +02:00
|
|
|
}
|
2011-07-21 20:21:08 +02:00
|
|
|
|
2011-08-07 16:35:51 +02:00
|
|
|
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
|
2011-07-21 22:13:42 +02:00
|
|
|
}
|
2011-08-07 16:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-27 17:18:38 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param writer
|
|
|
|
|
* @param msg
|
|
|
|
|
*/
|
|
|
|
|
private void printMessage(PrintWriter writer, HgHookMessage msg)
|
|
|
|
|
{
|
|
|
|
|
writer.append('_');
|
|
|
|
|
|
|
|
|
|
if (msg.getSeverity() == Severity.ERROR)
|
|
|
|
|
{
|
|
|
|
|
writer.append("e[SCM] Error: ");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
writer.append("n[SCM] ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writer.println(msg.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-22 11:40:03 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
2018-07-10 15:31:18 +02:00
|
|
|
* @param response
|
2013-07-22 11:40:03 +02:00
|
|
|
* @param context
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
2018-07-10 15:31:18 +02:00
|
|
|
private void printMessages(HttpServletResponse response,
|
2013-07-22 11:40:03 +02:00
|
|
|
HgHookContextProvider context)
|
|
|
|
|
throws IOException
|
|
|
|
|
{
|
|
|
|
|
List<HgHookMessage> msgs = context.getHgMessageProvider().getMessages();
|
|
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(msgs))
|
|
|
|
|
{
|
|
|
|
|
PrintWriter writer = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-07-10 15:31:18 +02:00
|
|
|
writer = response.getWriter();
|
2013-07-22 11:40:03 +02:00
|
|
|
|
2013-07-27 17:18:38 +02:00
|
|
|
printMessages(writer, msgs);
|
2013-07-22 11:40:03 +02:00
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Closeables.close(writer, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-27 17:18:38 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param writer
|
|
|
|
|
* @param msgs
|
|
|
|
|
*/
|
|
|
|
|
private void printMessages(PrintWriter writer, List<HgHookMessage> msgs)
|
|
|
|
|
{
|
|
|
|
|
for (HgHookMessage msg : msgs)
|
|
|
|
|
{
|
|
|
|
|
printMessage(writer, msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-02 21:57:23 +02:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param response
|
2013-07-27 17:18:38 +02:00
|
|
|
* @param context
|
2013-04-02 21:57:23 +02:00
|
|
|
* @param ex
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
2013-07-27 17:18:38 +02:00
|
|
|
private void sendError(HttpServletResponse response,
|
|
|
|
|
HgHookContextProvider context, Exception ex)
|
2013-04-02 21:57:23 +02:00
|
|
|
throws IOException
|
|
|
|
|
{
|
|
|
|
|
logger.warn("hook ended with exception", ex);
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_CONFLICT);
|
|
|
|
|
|
|
|
|
|
String msg = ex.getMessage();
|
2013-07-27 17:18:38 +02:00
|
|
|
List<HgHookMessage> msgs = null;
|
|
|
|
|
|
|
|
|
|
if (context != null)
|
|
|
|
|
{
|
|
|
|
|
msgs = context.getHgMessageProvider().getMessages();
|
|
|
|
|
}
|
2013-04-02 21:57:23 +02:00
|
|
|
|
2013-07-27 17:18:38 +02:00
|
|
|
if (!Strings.isNullOrEmpty(msg) || Util.isNotEmpty(msgs))
|
2013-04-02 21:57:23 +02:00
|
|
|
{
|
|
|
|
|
PrintWriter writer = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
writer = response.getWriter();
|
2013-07-27 17:18:38 +02:00
|
|
|
|
|
|
|
|
if (Util.isNotEmpty(msgs))
|
|
|
|
|
{
|
|
|
|
|
printMessages(writer, msgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Strings.isNullOrEmpty(msg))
|
|
|
|
|
{
|
|
|
|
|
printMessage(writer, new HgHookMessage(Severity.ERROR, msg));
|
|
|
|
|
}
|
2013-04-02 21:57:23 +02:00
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2014-03-29 10:30:19 +01:00
|
|
|
Closeables.close(writer, true);
|
2013-04-02 21:57:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-23 16:39:09 +02:00
|
|
|
//~--- get methods ----------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param request
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2018-11-23 12:46:24 +01:00
|
|
|
@SuppressWarnings("squid:S2083") // we do nothing with the path given, so this should be no issue
|
2018-11-23 16:12:35 +01:00
|
|
|
private Repository getRepositoryId(HttpServletRequest request)
|
2011-10-23 16:39:09 +02:00
|
|
|
{
|
2018-11-23 16:12:35 +01:00
|
|
|
Repository repository = null;
|
2011-10-23 16:39:09 +02:00
|
|
|
String path = request.getParameter(PARAM_REPOSITORYPATH);
|
|
|
|
|
|
2018-11-23 10:13:47 +01:00
|
|
|
if (Util.isNotEmpty(path)) {
|
2018-11-23 16:12:35 +01:00
|
|
|
repository = repositoryDAO.getRepositoryForDirectory(new File(path));
|
2011-10-23 16:39:09 +02:00
|
|
|
}
|
|
|
|
|
else if (logger.isWarnEnabled())
|
|
|
|
|
{
|
|
|
|
|
logger.warn("no repository path parameter found");
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 16:12:35 +01:00
|
|
|
return repository;
|
2011-10-23 16:39:09 +02:00
|
|
|
}
|
|
|
|
|
|
2011-07-21 22:13:42 +02:00
|
|
|
//~--- fields ---------------------------------------------------------------
|
|
|
|
|
|
2011-10-08 15:31:10 +02:00
|
|
|
/** Field description */
|
2014-03-29 10:30:19 +01:00
|
|
|
private final Provider<HgContext> contextProvider;
|
2011-10-08 15:31:10 +02:00
|
|
|
|
2011-07-21 22:13:42 +02:00
|
|
|
/** Field description */
|
2014-03-29 10:30:19 +01:00
|
|
|
private final HgRepositoryHandler handler;
|
2011-07-21 22:13:42 +02:00
|
|
|
|
2011-07-22 13:08:12 +02:00
|
|
|
/** Field description */
|
2014-03-29 10:30:19 +01:00
|
|
|
private final HookEventFacade hookEventFacade;
|
2011-07-22 13:08:12 +02:00
|
|
|
|
2011-07-21 22:13:42 +02:00
|
|
|
/** Field description */
|
2014-03-29 10:30:19 +01:00
|
|
|
private final HgHookManager hookManager;
|
2018-11-23 10:13:47 +01:00
|
|
|
|
|
|
|
|
private final RepositoryDAO repositoryDAO;
|
2011-07-21 20:21:08 +02:00
|
|
|
}
|