protected < JsonType extends JsonStructure , ValueType > ValueType invoke ( String operation , String id , String action , QueryClosure queryClosure , RequestClosure < JsonType > requestClosure , ResponseClosure < JsonType , ValueType > responseClosure ) { try { WebTarget ws = wsBase . path ( operation ) ; if ( id != null ) ws = ws . path ( id ) ; if ( action != null ) ws = ws . path ( action ) ; if ( queryClosure != null ) ws = queryClosure . modify ( ws ) ; Invocation . Builder request = ws . request ( MediaType . APPLICATION_JSON_TYPE ) ; request . header ( AGENT_REQHDR , getAgentRequestHeaderValue ( ) ) ; JsonType json = requestClosure . call ( request ) ; return ( responseClosure != null ) ? responseClosure . call ( json ) : null ; } catch ( WebApplicationException e ) { Response . StatusType status = e . getResponse ( ) . getStatusInfo ( ) ; switch ( status . getStatusCode ( ) ) { case 400 : throw new BadRequestException ( operation , id , action , e ) ; case 401 : throw new MissingApiTokenException ( status . getReasonPhrase ( ) ) ; case 403 : throw new UnauthorizedException ( operation , id , action ) ; case 404 : throw new NotFoundException ( operation , id ) ; case 409 : throw new ConflictException ( operation , id , action , e ) ; case 422 : throw new CoercionException ( operation , id , action , e ) ; case 427 : throw new RateLimitedException ( operation , id , action ) ; case 429 : throw new ResponseParseException ( operation , id , action , e ) ; case 500 : { String message = "" ; Response response = e . getResponse ( ) ; String contentType = response . getHeaderString ( "Content-Type" ) ; if ( contentType . equals ( "application/json" ) ) { InputStream is = ( InputStream ) response . getEntity ( ) ; JsonObject errJson = Json . createReader ( is ) . readObject ( ) ; message = errJson . getString ( "message" ) ; } else if ( contentType . startsWith ( "text/html" ) ) { InputStream is = ( InputStream ) response . getEntity ( ) ; message = StringUtils . toString ( is ) ; } throw new ServerException ( status . getReasonPhrase ( ) + ": " + message ) ; } default : throw new ApiException ( e ) ; } } catch ( ApiException e ) { throw e ; } catch ( Exception e ) { throw new ApiException ( e ) ; } }
Performs a REST API invocation .