Skip to content

Commit 692dccf

Browse files
committed
Massaging the change a bit
1 parent 92caf98 commit 692dccf

5 files changed

Lines changed: 33 additions & 34 deletions

File tree

src/main/java/org/kohsuke/github/exception/GHFileNotFoundException.java renamed to src/main/java/org/kohsuke/github/GHFileNotFoundException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.kohsuke.github.exception;
1+
package org.kohsuke.github;
22

33
import javax.annotation.CheckForNull;
44
import java.io.FileNotFoundException;
@@ -27,7 +27,7 @@ public Map<String, List<String>> getResponseHeaderFields() {
2727
return responseHeaderFields;
2828
}
2929

30-
public GHFileNotFoundException withResponseHeaderFields(HttpURLConnection urlConnection) {
30+
GHFileNotFoundException withResponseHeaderFields(HttpURLConnection urlConnection) {
3131
this.responseHeaderFields = urlConnection.getHeaderFields();
3232
return this;
3333
}

src/main/java/org/kohsuke/github/exception/GHIOException.java renamed to src/main/java/org/kohsuke/github/GHIOException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.kohsuke.github.exception;
1+
package org.kohsuke.github;
22

33
import javax.annotation.CheckForNull;
44
import java.io.IOException;
@@ -13,7 +13,7 @@
1313
* @author Kanstantsin Shautsou
1414
*/
1515
public class GHIOException extends IOException {
16-
protected Map<String, List<String>> responceHeaderFields;
16+
protected Map<String, List<String>> responseHeaderFields;
1717

1818
public GHIOException() {
1919
}
@@ -31,12 +31,12 @@ public GHIOException(Throwable cause) {
3131
}
3232

3333
@CheckForNull
34-
public Map<String, List<String>> getResponceHeaderFields() {
35-
return responceHeaderFields;
34+
public Map<String, List<String>> getResponseHeaderFields() {
35+
return responseHeaderFields;
3636
}
3737

38-
public GHIOException withResponceHeaderFields(HttpURLConnection urlConnection) {
39-
this.responceHeaderFields = urlConnection.getHeaderFields();
38+
GHIOException withResponseHeaderFields(HttpURLConnection urlConnection) {
39+
this.responseHeaderFields = urlConnection.getHeaderFields();
4040
return this;
4141
}
4242
}

src/main/java/org/kohsuke/github/GHObject.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
@SuppressFBWarnings(value = {"UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
2020
"NP_UNWRITTEN_FIELD"}, justification = "JSON API")
2121
public abstract class GHObject {
22-
// not data but information related to data from responce
22+
/**
23+
* Capture response HTTP headers on the state object.
24+
*/
2325
protected Map<String, List<String>> responseHeaderFields;
2426

2527
protected String url;
@@ -30,7 +32,17 @@ public abstract class GHObject {
3032
/*package*/ GHObject() {
3133
}
3234

33-
@CheckForNull
35+
/**
36+
* Returns the HTTP response headers given along with the state of this object.
37+
*
38+
* <p>
39+
* Some of the HTTP headers have nothing to do with the object, for example "Cache-Control"
40+
* and others are different depending on how this object was retrieved.
41+
*
42+
* This method was added as a kind of hack to allow the caller to retrieve OAuth scopes and such.
43+
* Use with caution. The method might be removed in the future.
44+
*/
45+
@CheckForNull @Deprecated
3446
public Map<String, List<String>> getResponseHeaderFields() {
3547
return responseHeaderFields;
3648
}

src/main/java/org/kohsuke/github/Requester.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import com.fasterxml.jackson.databind.JsonMappingException;
2727
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2828
import org.apache.commons.io.IOUtils;
29-
import org.kohsuke.github.exception.GHFileNotFoundException;
30-
import org.kohsuke.github.exception.GHIOException;
3129

3230
import java.io.FileNotFoundException;
3331
import java.io.IOException;
@@ -274,7 +272,7 @@ private <T> T _to(String tailApiUrl, Class<T> type, T instance) throws IOExcepti
274272
if (nextLinkMatcher.find()) {
275273
final String link = nextLinkMatcher.group(1);
276274
T nextResult = _to(link, type, instance);
277-
injectInResult(nextResult);
275+
setResponseHeaders(nextResult);
278276
final int resultLength = Array.getLength(result);
279277
final int nextResultLength = Array.getLength(nextResult);
280278
T concatResult = (T) Array.newInstance(type.getComponentType(), resultLength + nextResultLength);
@@ -284,8 +282,7 @@ private <T> T _to(String tailApiUrl, Class<T> type, T instance) throws IOExcepti
284282
}
285283
}
286284
}
287-
injectInResult(result);
288-
return result;
285+
return setResponseHeaders(result);
289286
} catch (IOException e) {
290287
handleApiError(e);
291288
} finally {
@@ -605,16 +602,12 @@ private <T> T parse(Class<T> type, T instance) throws IOException {
605602
String data = IOUtils.toString(r);
606603
if (type!=null)
607604
try {
608-
final T readValue = MAPPER.readValue(data, type);
609-
injectInResult(readValue);
610-
return readValue;
605+
return setResponseHeaders(MAPPER.readValue(data, type));
611606
} catch (JsonMappingException e) {
612607
throw (IOException)new IOException("Failed to deserialize " +data).initCause(e);
613608
}
614609
if (instance!=null) {
615-
final T readValue = MAPPER.readerForUpdating(instance).<T>readValue(data);
616-
injectInResult(readValue);
617-
return readValue;
610+
return setResponseHeaders(MAPPER.readerForUpdating(instance).<T>readValue(data));
618611
}
619612
return null;
620613
} catch (FileNotFoundException e) {
@@ -628,24 +621,19 @@ private <T> T parse(Class<T> type, T instance) throws IOException {
628621
}
629622
}
630623

631-
private <T> void injectInResult(T readValue) {
624+
private <T> T setResponseHeaders(T readValue) {
632625
if (readValue instanceof GHObject[]) {
633626
for (GHObject ghObject : (GHObject[]) readValue) {
634-
injectInResult(ghObject);
627+
setResponseHeaders(ghObject);
635628
}
636629
} else if (readValue instanceof GHObject) {
637-
injectInResult((GHObject) readValue);
630+
setResponseHeaders((GHObject) readValue);
638631
}
632+
return readValue;
639633
}
640634

641-
private void injectInResult(GHObject readValue) {
642-
try {
643-
final Field field = GHObject.class.getDeclaredField("responseHeaderFields");
644-
field.setAccessible(true);
645-
field.set(readValue, uc.getHeaderFields());
646-
} catch (NoSuchFieldException ignore) {
647-
} catch (IllegalAccessException ignore) {
648-
}
635+
private void setResponseHeaders(GHObject readValue) {
636+
readValue.responseHeaderFields = uc.getHeaderFields();
649637
}
650638

651639
/**
@@ -700,7 +688,7 @@ private InputStream wrapStream(InputStream in) throws IOException {
700688
HttpException http = (HttpException) e;
701689
throw (IOException) new HttpException(error, http.getResponseCode(), http.getResponseMessage(), http.getUrl(), e);
702690
} else {
703-
throw (IOException) new GHIOException(error).withResponceHeaderFields(uc).initCause(e);
691+
throw (IOException) new GHIOException(error).withResponseHeaderFields(uc).initCause(e);
704692
}
705693
} else {
706694
throw e;

src/test/java/org/kohsuke/github/GHHookTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.apache.commons.lang.StringUtils;
44
import org.junit.Ignore;
55
import org.junit.Test;
6-
import org.kohsuke.github.exception.GHFileNotFoundException;
76

87
import java.io.IOException;
98
import java.util.Arrays;

0 commit comments

Comments
 (0)