Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
- // Call the common handler function that we use for both GET and POST calls.
- logger.info("POST received”);
- handleRequest(request, response);
- }
- private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
- // Get the GitHub event name from the request headers.
- String webhookName = "X-GitHub-Event";
- String webhookValue = request.getHeader(webhookName);
- logger.info("GitHub webhook event: " + webhookValue);
- try {
- switch (webhookValue) {
- case "push":
- // If it is a "push" request we just need to pass it on to the standard integration.
- doPush(request, response);
- break;
- default:
- // The webhook request isn't one we currently handle so we can just send a HTTP 417
- // response code back as the standard hooks do.
- logger.error("Invalid event type: " + webhookValue);
- response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
- }
- }
- catch (NullPointerException ex) {
- // This isn't a GitHub event so we can just log an error and return.
- logger.error("X-GitHub-Event not found!");
- }
- }
- private void doPush(HttpServletRequest request, HttpServletResponse response) {
- // In order to forward the request we need to use a cross-context dispatch.
- //
- // To make this work, crossContext="true" needs to be added to the Context definition
- // in tomcat/conf/context.xml file:
- //
- // <Context sessionCookiePathUsesTrailingSlash="false" crossContext="true">
- // ...
- // </Context>
- //
- // Get the ServletContext we are currently running in and that of the destination.
- ServletContext myContext = request.getServletContext();
- ServletContext cbContext = myContext.getContext("/backend");
- // Create the forwarding URL based on the project ID in the request.
- final String forwardURL = "/rest/scm/github" + Utils.getProjectID(request);
- // Forward the request.
- try {
- RequestDispatcher dispatcher = cbContext.getRequestDispatcher(forwardURL);
- if (dispatcher != null) {
- dispatcher.forward(request, response);
- } else {
- throw new NullPointerException("No dispatecher found");
- }
- }
- catch (ServletException | IOException ex)
- {
- logger.error("Oops");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment