Massaging the change a bit

This commit is contained in:
Kohsuke Kawaguchi
2017-09-09 13:03:18 -07:00
parent 92caf98683
commit 692dccf110
5 changed files with 33 additions and 34 deletions

View File

@@ -1,4 +1,4 @@
package org.kohsuke.github.exception;
package org.kohsuke.github;
import javax.annotation.CheckForNull;
import java.io.FileNotFoundException;
@@ -27,7 +27,7 @@ public class GHFileNotFoundException extends FileNotFoundException {
return responseHeaderFields;
}
public GHFileNotFoundException withResponseHeaderFields(HttpURLConnection urlConnection) {
GHFileNotFoundException withResponseHeaderFields(HttpURLConnection urlConnection) {
this.responseHeaderFields = urlConnection.getHeaderFields();
return this;
}

View File

@@ -1,4 +1,4 @@
package org.kohsuke.github.exception;
package org.kohsuke.github;
import javax.annotation.CheckForNull;
import java.io.IOException;
@@ -13,7 +13,7 @@ import java.util.Map;
* @author Kanstantsin Shautsou
*/
public class GHIOException extends IOException {
protected Map<String, List<String>> responceHeaderFields;
protected Map<String, List<String>> responseHeaderFields;
public GHIOException() {
}
@@ -31,12 +31,12 @@ public class GHIOException extends IOException {
}
@CheckForNull
public Map<String, List<String>> getResponceHeaderFields() {
return responceHeaderFields;
public Map<String, List<String>> getResponseHeaderFields() {
return responseHeaderFields;
}
public GHIOException withResponceHeaderFields(HttpURLConnection urlConnection) {
this.responceHeaderFields = urlConnection.getHeaderFields();
GHIOException withResponseHeaderFields(HttpURLConnection urlConnection) {
this.responseHeaderFields = urlConnection.getHeaderFields();
return this;
}
}

View File

@@ -19,7 +19,9 @@ import java.util.Map;
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
"NP_UNWRITTEN_FIELD"}, justification = "JSON API")
public abstract class GHObject {
// not data but information related to data from responce
/**
* Capture response HTTP headers on the state object.
*/
protected Map<String, List<String>> responseHeaderFields;
protected String url;
@@ -30,7 +32,17 @@ public abstract class GHObject {
/*package*/ GHObject() {
}
@CheckForNull
/**
* Returns the HTTP response headers given along with the state of this object.
*
* <p>
* Some of the HTTP headers have nothing to do with the object, for example "Cache-Control"
* and others are different depending on how this object was retrieved.
*
* This method was added as a kind of hack to allow the caller to retrieve OAuth scopes and such.
* Use with caution. The method might be removed in the future.
*/
@CheckForNull @Deprecated
public Map<String, List<String>> getResponseHeaderFields() {
return responseHeaderFields;
}

View File

@@ -26,8 +26,6 @@ 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;
@@ -274,7 +272,7 @@ class Requester {
if (nextLinkMatcher.find()) {
final String link = nextLinkMatcher.group(1);
T nextResult = _to(link, type, instance);
injectInResult(nextResult);
setResponseHeaders(nextResult);
final int resultLength = Array.getLength(result);
final int nextResultLength = Array.getLength(nextResult);
T concatResult = (T) Array.newInstance(type.getComponentType(), resultLength + nextResultLength);
@@ -284,8 +282,7 @@ class Requester {
}
}
}
injectInResult(result);
return result;
return setResponseHeaders(result);
} catch (IOException e) {
handleApiError(e);
} finally {
@@ -605,16 +602,12 @@ class Requester {
String data = IOUtils.toString(r);
if (type!=null)
try {
final T readValue = MAPPER.readValue(data, type);
injectInResult(readValue);
return readValue;
return setResponseHeaders(MAPPER.readValue(data, type));
} catch (JsonMappingException e) {
throw (IOException)new IOException("Failed to deserialize " +data).initCause(e);
}
if (instance!=null) {
final T readValue = MAPPER.readerForUpdating(instance).<T>readValue(data);
injectInResult(readValue);
return readValue;
return setResponseHeaders(MAPPER.readerForUpdating(instance).<T>readValue(data));
}
return null;
} catch (FileNotFoundException e) {
@@ -628,24 +621,19 @@ class Requester {
}
}
private <T> void injectInResult(T readValue) {
private <T> T setResponseHeaders(T readValue) {
if (readValue instanceof GHObject[]) {
for (GHObject ghObject : (GHObject[]) readValue) {
injectInResult(ghObject);
setResponseHeaders(ghObject);
}
} else if (readValue instanceof GHObject) {
injectInResult((GHObject) readValue);
setResponseHeaders((GHObject) readValue);
}
return 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) {
}
private void setResponseHeaders(GHObject readValue) {
readValue.responseHeaderFields = uc.getHeaderFields();
}
/**
@@ -700,7 +688,7 @@ class Requester {
HttpException http = (HttpException) e;
throw (IOException) new HttpException(error, http.getResponseCode(), http.getResponseMessage(), http.getUrl(), e);
} else {
throw (IOException) new GHIOException(error).withResponceHeaderFields(uc).initCause(e);
throw (IOException) new GHIOException(error).withResponseHeaderFields(uc).initCause(e);
}
} else {
throw e;

View File

@@ -3,7 +3,6 @@ package org.kohsuke.github;
import org.apache.commons.lang.StringUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.kohsuke.github.exception.GHFileNotFoundException;
import java.io.IOException;
import java.util.Arrays;