All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kohsuke.github.GitHubConnectorResponseErrorHandler Maven / Gradle / Ivy

There is a newer version: 2.0.0-alpha-2
Show newest version
package org.kohsuke.github;

import org.jetbrains.annotations.NotNull;
import org.kohsuke.github.connector.GitHubConnectorResponse;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import javax.annotation.Nonnull;

import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;

// TODO: Auto-generated Javadoc
/**
 * Pluggable strategy to detect and choose what to do when errors occur during an http request.
 *
 * @author Liam Newman
 */
abstract class GitHubConnectorResponseErrorHandler {

    /**
     * Called to detect an error handled by this handler.
     *
     * @param connectorResponse
     *            the connector response
     * @return {@code true} if there is an error and {@link #onError(GitHubConnectorResponse)} should be called
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    abstract boolean isError(@Nonnull GitHubConnectorResponse connectorResponse) throws IOException;

    /**
     * Called when the library encounters HTTP error matching {@link #isError(GitHubConnectorResponse)}
     *
     * 

* Any exception thrown from this method will cause the request to fail, and the caller of github-api will receive * an exception. If this method returns normally, another request will be attempted. For that to make sense, the * implementation needs to wait for some time. * * @param connectorResponse * Response information for this request. * * @throws IOException * the io exception * @see API documentation from GitHub */ public abstract void onError(@Nonnull GitHubConnectorResponse connectorResponse) throws IOException; /** The status http bad request or greater. */ static GitHubConnectorResponseErrorHandler STATUS_HTTP_BAD_REQUEST_OR_GREATER = new GitHubConnectorResponseErrorHandler() { private static final String CONTENT_TYPE = "Content-type"; private static final String TEXT_HTML = "text/html"; private static final String UNICORN_TITLE = "Unicorn!"; @Override public boolean isError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException { return connectorResponse.statusCode() >= HTTP_BAD_REQUEST; } @Override public void onError(@NotNull GitHubConnectorResponse connectorResponse) throws IOException { if (connectorResponse.statusCode() == HTTP_NOT_FOUND) { throw new FileNotFoundException(connectorResponse.request().url().toString()); } else if (isServiceDown(connectorResponse)) { throw new ServiceDownException(connectorResponse); } else { throw new HttpException(connectorResponse); } } private boolean isServiceDown(GitHubConnectorResponse connectorResponse) throws IOException { if (connectorResponse.statusCode() < HTTP_INTERNAL_ERROR) { return false; } String contentTypeHeader = connectorResponse.header(CONTENT_TYPE); if (contentTypeHeader != null && contentTypeHeader.contains(TEXT_HTML)) { try (BufferedReader bufReader = new BufferedReader( new InputStreamReader(connectorResponse.bodyStream(), StandardCharsets.UTF_8))) { String line; int hardLineCap = 25; // <title> node is expected in the beginning anyway. // This way we do not load the raw long images' Strings, which are later in the HTML code // Regex or .contains would result in iterating the whole HTML document, if it didn't match // UNICORN_TITLE while (hardLineCap > 0 && (line = bufReader.readLine()) != null) { if (line.trim().startsWith(UNICORN_TITLE)) { return true; } hardLineCap--; } } } return false; } }; } </code></pre> <br/> <br/> <div class='clear'></div> </main> </div> <br/><br/> <div class="align-center">© 2015 - 2025 <a href="/legal-notice.php">Weber Informatics LLC</a> | <a href="/data-protection.php">Privacy Policy</a></div> <br/><br/><br/><br/><br/><br/> </body> </html>