Inject responce headers in GHObject and Exceptions.

GH has specific to GET/POST headers required for analysing in case of error.

Signed-off-by: Kanstantsin Shautsou <kanstantsin.sha@gmail.com>
This commit is contained in:
Kanstantsin Shautsou
2017-02-10 03:33:18 +03:00
parent 55b00a87f6
commit be081eec3f
6 changed files with 215 additions and 12 deletions

View File

@@ -25,6 +25,10 @@ package org.kohsuke.github;
import com.fasterxml.jackson.databind.JsonMappingException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.io.IOUtils;
import org.kohsuke.github.exception.GHFileNotFoundException;
import org.kohsuke.github.exception.GHIOException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -48,17 +52,18 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import javax.annotation.CheckForNull;
import javax.annotation.WillClose;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import static java.util.Arrays.asList;
import static java.util.logging.Level.*;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.FINEST;
import static org.kohsuke.github.GitHub.MAPPER;
/**
@@ -269,7 +274,7 @@ class Requester {
if (nextLinkMatcher.find()) {
final String link = nextLinkMatcher.group(1);
T nextResult = _to(link, type, instance);
injectInResult(nextResult);
final int resultLength = Array.getLength(result);
final int nextResultLength = Array.getLength(nextResult);
T concatResult = (T) Array.newInstance(type.getComponentType(), resultLength + nextResultLength);
@@ -279,6 +284,7 @@ class Requester {
}
}
}
injectInResult(result);
return result;
} catch (IOException e) {
handleApiError(e);
@@ -579,6 +585,7 @@ class Requester {
throw new IllegalStateException("Failed to set the request method to "+method);
}
@CheckForNull
private <T> T parse(Class<T> type, T instance) throws IOException {
InputStreamReader r = null;
int responseCode = -1;
@@ -598,12 +605,17 @@ class Requester {
String data = IOUtils.toString(r);
if (type!=null)
try {
return MAPPER.readValue(data,type);
final T readValue = MAPPER.readValue(data, type);
injectInResult(readValue);
return readValue;
} catch (JsonMappingException e) {
throw (IOException)new IOException("Failed to deserialize " +data).initCause(e);
}
if (instance!=null)
return MAPPER.readerForUpdating(instance).<T>readValue(data);
if (instance!=null) {
final T readValue = MAPPER.readerForUpdating(instance).<T>readValue(data);
injectInResult(readValue);
return readValue;
}
return null;
} catch (FileNotFoundException e) {
// java.net.URLConnection handles 404 exception has FileNotFoundException, don't wrap exception in HttpException
@@ -616,6 +628,26 @@ class Requester {
}
}
private <T> void injectInResult(T readValue) {
if (readValue instanceof GHObject[]) {
for (GHObject ghObject : (GHObject[]) readValue) {
injectInResult(ghObject);
}
} else if (readValue instanceof GHObject) {
injectInResult((GHObject) readValue);
}
}
private void injectInResult(GHObject readValue) {
try {
final Field field = GHObject.class.getDeclaredField("responseHeaderFields");
field.setAccessible(true);
field.set(readValue, uc.getHeaderFields());
} catch (NoSuchFieldException ignore) {
} catch (IllegalAccessException ignore) {
}
}
/**
* Handles the "Content-Encoding" header.
*/
@@ -663,15 +695,16 @@ class Requester {
String error = IOUtils.toString(es, "UTF-8");
if (e instanceof FileNotFoundException) {
// pass through 404 Not Found to allow the caller to handle it intelligently
throw (IOException) new FileNotFoundException(error).initCause(e);
throw (IOException) new GHFileNotFoundException(error).withResponseHeaderFields(uc).initCause(e);
} else if (e instanceof HttpException) {
HttpException http = (HttpException) e;
throw (IOException) new HttpException(error, http.getResponseCode(), http.getResponseMessage(), http.getUrl(), e);
} else {
throw (IOException) new IOException(error).initCause(e);
throw (IOException) new GHIOException(error).withResponceHeaderFields(uc).initCause(e);
}
} else
} else {
throw e;
}
} finally {
IOUtils.closeQuietly(es);
}