Package com.styra.opa

Class OPAResult


  • public class OPAResult
    extends java.lang.Object
    This class encapsulates a result of a previous OPA request for deferred access. It may contain an exception, a value, or both. This is intended for usage patterns where multiple requests are made, and only later does the caller want to inspect which of them succeeded or failed, and what their values may have been. This abstraction is a little unclean - typically it is preferred to throw exceptions, not pass them around as values. However, problems with the HTTP requests often generate exceptions, and those can be challenging to deal with when dispatching many requests at once.
    • Constructor Summary

      Constructors 
      Constructor Description
      OPAResult​(java.lang.Object newValue)  
      OPAResult​(java.lang.Object newValue, OPAException newException)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> T get()
      Attempt to retrieve the value stored in this result, throwing the stored exception if one is present.
      <T> T get​(com.fasterxml.jackson.core.type.TypeReference<T> toValueType)  
      <T> T get​(com.fasterxml.jackson.databind.JavaType toValueType)  
      <T> T get​(java.lang.Class<T> toValueType)  
      OPAException getException()
      Retrieves the deferred exception, if any.
      java.lang.Object getValue()
      Retrieves the deferred value if one is present.
      boolean success()
      Returns true if and only if this result contains a non-null value AND ta null exception.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • OPAResult

        public OPAResult​(java.lang.Object newValue,
                         OPAException newException)
      • OPAResult

        public OPAResult​(java.lang.Object newValue)
    • Method Detail

      • get

        public <T> T get()
                  throws OPAException
        Attempt to retrieve the value stored in this result, throwing the stored exception if one is present. NOTE: this can make it look like the exception came from this class, but that's not true, it was simply smuggling a deferred exception from somewhere else inside of it. OPAException usually wraps some other, lower-level exception - consider inspecting that to determine the true source of error.
        Throws:
        OPAException
      • get

        public <T> T get​(com.fasterxml.jackson.databind.JavaType toValueType)
                  throws OPAException
        Throws:
        OPAException
      • get

        public <T> T get​(com.fasterxml.jackson.core.type.TypeReference<T> toValueType)
                  throws OPAException
        Throws:
        OPAException
      • success

        public boolean success()
        Returns true if and only if this result contains a non-null value AND ta null exception. In other words, if this method returns true, then get() is guaranteed not to throw an exception or return null.
      • getValue

        public java.lang.Object getValue()
        Retrieves the deferred value if one is present. Does not attempt to re-throw a stored exception even if one is present.
      • getException

        public OPAException getException()
        Retrieves the deferred exception, if any.