11package org .kohsuke .github .internal ;
22
3+ import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
34import org .apache .commons .io .IOUtils ;
4- import org .jetbrains .annotations .NotNull ;
55import org .kohsuke .github .*;
66import org .kohsuke .github .connector .GitHubConnector ;
77import org .kohsuke .github .connector .GitHubConnectorRequest ;
88import org .kohsuke .github .connector .GitHubConnectorResponse ;
99
1010import java .io .ByteArrayInputStream ;
11+ import java .io .FileNotFoundException ;
1112import java .io .IOException ;
1213import java .io .InputStream ;
1314import java .lang .reflect .Field ;
2223
2324import javax .annotation .Nonnull ;
2425
26+ import static java .net .HttpURLConnection .HTTP_BAD_REQUEST ;
27+ import static java .net .HttpURLConnection .HTTP_NOT_FOUND ;
2528import static java .util .logging .Level .*;
2629
2730/**
3134 */
3235public final class GitHubConnectorHttpConnectorAdapter implements GitHubConnector , HttpConnector {
3336
37+ /**
38+ * Internal for testing.
39+ */
3440 final HttpConnector httpConnector ;
3541
3642 /**
@@ -52,8 +58,8 @@ public GitHubConnectorHttpConnectorAdapter(HttpConnector httpConnector) {
5258 * the HttpConnector to be adapted.
5359 * @return a GitHubConnector that calls into the provided HttpConnector.
5460 */
55- @ NotNull
56- public static GitHubConnector adapt (HttpConnector connector ) {
61+ @ Nonnull
62+ public static GitHubConnector adapt (@ Nonnull HttpConnector connector ) {
5763 GitHubConnector gitHubConnector ;
5864 if (connector == HttpConnector .DEFAULT ) {
5965 gitHubConnector = GitHubConnector .DEFAULT ;
@@ -152,8 +158,6 @@ private static void setRequestMethod(String method, HttpURLConnection connection
152158 * initially received and before the body is processed.
153159 *
154160 * Implementation specific to {@link HttpURLConnection}. For internal use only.
155- *
156- *
157161 */
158162 public final static class HttpURLConnectionGitHubConnectorResponse extends GitHubConnectorResponse {
159163
@@ -177,14 +181,25 @@ public final static class HttpURLConnectionGitHubConnectorResponse extends GitHu
177181 * {@inheritDoc}
178182 */
179183 public InputStream bodyStream () throws IOException {
184+ if (connection .getResponseCode () >= HTTP_BAD_REQUEST ) {
185+ if (connection .getResponseCode () == HTTP_NOT_FOUND ) {
186+ throw new FileNotFoundException (request ().url ().toString ());
187+ } else {
188+ throw new HttpException (errorMessage (),
189+ connection .getResponseCode (),
190+ connection .getResponseMessage (),
191+ connection .getURL ().toString ());
192+ }
193+ }
194+
180195 synchronized (this ) {
181196 if (!inputStreamRead ) {
182197 try (InputStream stream = wrapStream (connection .getInputStream ())) {
183198 if (stream != null ) {
184199 inputBytes = IOUtils .toByteArray (stream );
185- inputStreamRead = true ;
186200 }
187201 }
202+ inputStreamRead = true ;
188203 }
189204 }
190205
@@ -202,9 +217,9 @@ public String errorMessage() {
202217 try (InputStream stream = wrapStream (connection .getErrorStream ())) {
203218 if (stream != null ) {
204219 errorString = new String (IOUtils .toByteArray (stream ), StandardCharsets .UTF_8 );
205- errorStreamRead = true ;
206220 }
207221 }
222+ errorStreamRead = true ;
208223 }
209224 }
210225 if (errorString != null ) {
@@ -216,6 +231,13 @@ public String errorMessage() {
216231 return result ;
217232 }
218233
234+ @ SuppressFBWarnings (value = { "EI_EXPOSE_REP" },
235+ justification = "Internal implementation class. Should not be used externally." )
236+ @ Nonnull
237+ public HttpURLConnection getConnection () {
238+ return connection ;
239+ }
240+
219241 /**
220242 * Handles the "Content-Encoding" header.
221243 *
0 commit comments