public < T > T query ( MethodMap methodMap , ArgMap argMap , Class < T > responseType ) throws OpenStatesException { BufferedReader reader = null ; HttpURLConnection conn = null ; String charSet = "utf-8" ; try { if ( isCaching ( methodMap , argMap ) ) { File file = getCacheFile ( methodMap , argMap ) ; long fileLength = file . length ( ) ; logger . fine ( "Length of File in cache:" + fileLength + ": " + file . getName ( ) ) ; if ( fileLength == 0L ) { OpenStates . cacheFileFromAPI ( methodMap , argMap , file , responseType ) ; } reader = new BufferedReader ( new InputStreamReader ( new FileInputStream ( file ) , charSet ) ) ; } else { conn = OpenStates . getConnectionFromAPI ( methodMap , argMap ) ; charSet = getCharset ( conn ) ; // better check it first int rcode = conn . getResponseCode ( ) ; if ( rcode / 100 != 2 ) { String msg = conn . getResponseMessage ( ) ; conn . disconnect ( ) ; throw new OpenStatesException ( rcode , msg , methodMap , argMap , responseType ) ; } reader = new BufferedReader ( new InputStreamReader ( conn . getInputStream ( ) , charSet ) ) ; } return mapper . readValue ( reader , responseType ) ; } catch ( JsonParseException e ) { throw new OpenStatesException ( e , methodMap , argMap , responseType ) ; } catch ( JsonMappingException e ) { throw new OpenStatesException ( e , methodMap , argMap , responseType ) ; } catch ( URISyntaxException e ) { throw new OpenStatesException ( e , methodMap , argMap , responseType ) ; } catch ( IOException e ) { throw new OpenStatesException ( e , methodMap , argMap , responseType ) ; } finally { suspendCache = false ; if ( conn != null ) conn . disconnect ( ) ; if ( reader != null ) { try { reader . close ( ) ; } catch ( IOException e ) { throw new OpenStatesException ( e , methodMap , argMap , responseType ) ; } } } }
Handles the actual API calls and caching .