Package com.lpvs.util

Class LPVSPayloadUtil

java.lang.Object
com.lpvs.util.LPVSPayloadUtil

public class LPVSPayloadUtil extends Object
Utility class for processing GitHub webhook payloads and extracting relevant information. This class provides methods to parse GitHub webhook payloads, check their validity, and retrieve specific details such as organization name, repository name, repository URL, and pull request ID.
  • Constructor Details

    • LPVSPayloadUtil

      public LPVSPayloadUtil()
  • Method Details

    • createInputStreamReader

      public static InputStreamReader createInputStreamReader(InputStream inputStream) throws UnsupportedEncodingException
      Creates an InputStreamReader object with the specified input stream and UTF-8 encoding.
      Parameters:
      inputStream - The input stream to read from.
      Returns:
      An InputStreamReader object with the specified input stream.
      Throws:
      UnsupportedEncodingException - If UTF-8 encoding is not supported.
    • createBufferReader

      public static BufferedReader createBufferReader(InputStreamReader inputStreamReader)
      Creates a BufferedReader for the given InputStreamReader.
      Parameters:
      inputStreamReader - The InputStreamReader to create a BufferedReader from.
      Returns:
      A BufferedReader for the given InputStreamReader.
    • convertOsoriDbResponseToLicense

      public static LPVSLicense convertOsoriDbResponseToLicense(String payload)
      Parses the given payload from the OSORI DB and converts it into a LPVSLicense object.
      Parameters:
      payload - the JSON payload from the OSORI DB
      Returns:
      the LPVSLicense object containing the parsed information from the payload, or null if the payload is invalid
    • convertInputStreamToString

      public static String convertInputStreamToString(InputStream inputStream) throws IOException
      Convert an InputStream into a String by reading the contents line by line.
      Parameters:
      inputStream - The InputStream to convert.
      Returns:
      A String containing the contents of the InputStream.
      Throws:
      IOException - If an error occurs while reading the InputStream.
    • getGitHubWebhookConfig

      public static LPVSQueue getGitHubWebhookConfig(String payload)
      Parses the GitHub webhook payload and extracts relevant information to create an LPVSQueue object.
      Parameters:
      payload - The GitHub webhook payload in JSON format.
      Returns:
      LPVSQueue object containing information extracted from the webhook payload.
    • checkPayload

      public static boolean checkPayload(String payload)
      Checks if the provided payload represents a valid GitHub webhook event that LPVS can handle.
      Parameters:
      payload - The GitHub webhook payload in JSON format.
      Returns:
      true if the payload is valid and LPVS can handle the event, false otherwise.
    • getRepositoryOrganization

      public static String getRepositoryOrganization(LPVSQueue webhookConfig)
      Retrieves the organization name from the repository URL in the LPVSQueue object.
      Parameters:
      webhookConfig - LPVSQueue object containing repository information.
      Returns:
      The organization name.
    • getRepositoryName

      public static String getRepositoryName(LPVSQueue webhookConfig)
      Retrieves the repository name from the repository URL in the LPVSQueue object.
      Parameters:
      webhookConfig - LPVSQueue object containing repository information.
      Returns:
      The repository name.
    • getRepositoryUrl

      public static String getRepositoryUrl(LPVSQueue webhookConfig)
      Retrieves the repository URL from the LPVSQueue object.
      Parameters:
      webhookConfig - LPVSQueue object containing repository information.
      Returns:
      The repository URL.
      Throws:
      IllegalArgumentException - If the provided LPVSQueue object is null.
    • getPullRequestId

      public static String getPullRequestId(LPVSQueue webhookConfig)
      Retrieves the pull request ID from the pull request URL in the LPVSQueue object.
      Parameters:
      webhookConfig - LPVSQueue object containing pull request information.
      Returns:
      The pull request ID.
      Throws:
      IllegalArgumentException - If the provided LPVSQueue object is null or if the pull request URL is absent.
    • generateSecurityHeaders

      public static org.springframework.http.HttpHeaders generateSecurityHeaders()
      Generates a HttpHeaders object with a set of security headers for a web application. This method includes the following security headers: - Strict-Transport-Security: Enables HSTS for one year, including subdomains. - Content-Security-Policy: Minimal CSP allowing resources only from the same origin. - X-Content-Type-Options: Prevents browsers from MIME-sniffing a response. - X-Frame-Options: Prevents the content from being displayed in iframes. - X-XSS-Protection: Enables XSS protection in modern browsers. - Referrer-Policy: Specifies how much referrer information should be included with requests. - Feature-Policy: Disallows the use of various browser features. - Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers: Headers for Cross-Origin Resource Sharing (CORS) to control which origins are permitted to access resources.
      Returns:
      HttpHeaders object with a set of security headers.
    • getGitHubWebhookConfig

      public static LPVSQueue getGitHubWebhookConfig(org.kohsuke.github.GHRepository repo, org.kohsuke.github.GHPullRequest pR)
      Retrieves an LPVSQueue configuration based on the GitHub repository and pull request.
      Parameters:
      repo - The GitHub repository.
      pR - The GitHub pull request.
      Returns:
      LPVSQueue configuration for the given GitHub repository and pull request.