Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: HTTPBuilder-0.5.0
-
Fix Version/s: HTTPBuilder-0.5.0
-
Component/s: HttpBuilder
-
Labels:None
-
Environment:Nullpointer in ParserRegistry.getCharset()
-
Number of attachments :
Description
I've encountered a nullpointer in ParserRegistry.getCharset(), when no Header was present.
This is was the executing code:
private static String URL = 'https://sandbox.itunes.apple.com/verifyReceipt' // TODO: impl me public boolean isValid(String receipt, boolean sandbox = false) { //return true HTTPBuilder http = new HTTPBuilder(URL) boolean result = false http.request(POST, JSON) { req -> //body = ['receipt-data': receipt] body = ['receipt-data': 'hallo'] response.success = { resp, json -> println 's' //log.debug('success') result = true } response.failure = { resp, json -> println 'f' //log.debug('failure') result = false } } result }
And here is the fix:
/** * Helper method to get the charset from the response. This should be done * when manually parsing any text response to ensure it is decoded using the * correct charset. For instance:<pre> * Reader reader = new InputStreamReader( resp.getEntity().getContent(), * ParserRegistry.getCharset( resp ) );</pre> * @param resp */ public static String getCharset( HttpResponse resp ) { /* old code yields NP if resp.getEntity().getContentType() is null NameValuePair charset = resp.getEntity().getContentType().getElements()[0].getParameterByName("charset"); return ( charset == null || charset.getValue().trim().equals("") ) ? Charset.defaultCharset().name() : charset.getValue(); */ // FIX: NP NameValuePair charset = null; Header header = resp.getEntity().getContentType(); if (header != null) { HeaderElement[] headerElements = header.getElements(); if (headerElements != null && headerElements.length > 0) { HeaderElement headerElement = headerElements[0]; charset = headerElement.getParameterByName("charset"); } } return ( charset == null || charset.getValue().trim().equals("") ) ? Charset.defaultCharset().name() : charset.getValue(); }