andyp73

Untitled

Aug 14th, 2023
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | Source Code | 0 0
  1. @Override
  2. public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
  3.     // Call the common handler function that we use for both GET and POST calls.
  4.     logger.info("POST received”);
  5.    handleRequest(request, response);
  6. }
  7.  
  8. private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
  9.     // Get the GitHub event name from the request headers.
  10.     String webhookName = "X-GitHub-Event";
  11.     String webhookValue = request.getHeader(webhookName);
  12.     logger.info("GitHub webhook event: " + webhookValue);
  13.  
  14.     try {
  15.         switch (webhookValue) {
  16.             case "push":
  17.                 // If it is a "push" request we just need to pass it on to the standard integration.
  18.                 doPush(request, response);
  19.                 break;
  20.  
  21.             default:
  22.                 // The webhook request isn't one we currently handle so we can just send a HTTP 417
  23.                 // response code back as the standard  hooks do.
  24.                 logger.error("Invalid event type: " + webhookValue);
  25.                 response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
  26.         }
  27.     }
  28.  
  29.     catch (NullPointerException ex) {
  30.         // This isn't a GitHub event so we can just log an error and return.
  31.         logger.error("X-GitHub-Event not found!");
  32.     }
  33. }
  34.  
  35. private void doPush(HttpServletRequest request, HttpServletResponse response) {
  36.     // In order to forward the request we need to use a cross-context dispatch.
  37.     //
  38.     // To make this work, crossContext="true" needs to be added to the Context definition
  39.     // in tomcat/conf/context.xml file:
  40.     //
  41.     // <Context sessionCookiePathUsesTrailingSlash="false" crossContext="true">
  42.     //     ...
  43.     // </Context>
  44.     //
  45.     // Get the ServletContext we are currently running in and that of the destination.
  46.     ServletContext myContext = request.getServletContext();
  47.     ServletContext cbContext = myContext.getContext("/backend");
  48.  
  49.     // Create the forwarding URL based on the project ID in the request.
  50.     final String forwardURL = "/rest/scm/github" + Utils.getProjectID(request);
  51.  
  52.     // Forward the request.
  53.     try {
  54.         RequestDispatcher dispatcher = cbContext.getRequestDispatcher(forwardURL);
  55.         if (dispatcher != null) {
  56.             dispatcher.forward(request, response);
  57.         } else {
  58.             throw new NullPointerException("No dispatecher found");
  59.         }
  60.     }
  61.  
  62.     catch (ServletException | IOException ex)
  63.     {
  64.         logger.error("Oops");
  65.     }
  66. }
  67.  
  68.  
Advertisement
Add Comment
Please, Sign In to add comment