java.lang.Object
de.turnertech.problemdetails.Problem

public class Problem extends Object
A POJO representation of the urn:ietf:rfc:7807 problem type.
  • Field Details

  • Constructor Details

    • Problem

      public Problem()
      Constructs an empty Problem with the type "about:blank" (RFC 9457 - 4.2.1.).
    • Problem

      public Problem(URI type)
      Constructs a Problem with the provided parameters. Note that type may not be null.
      Parameters:
      type - RFC 9457 - 3.1.1.
      Throws:
      NullPointerException - if type is null.
    • Problem

      public Problem(URI type, URI instance) throws NullPointerException
      Constructs a Problem with the provided parameters. Note that type may not be null.
      Parameters:
      type - RFC 9457 - 3.1.1.
      instance - RFC 9457 - 3.1.5.
      Throws:
      NullPointerException - if type is null.
    • Problem

      public Problem(Problem other)
      Constructs a deep copy of the provided problem. If you extend this class, make sure that your implementation of the copy constructor also returns deep copies.
      Parameters:
      other - problem to copy.
  • Method Details

    • getType

      public URI getType()
      Gets the problem type.
      Returns:
      the problem type.
    • setType

      public void setType(URI type) throws NullPointerException
      Sets the problem type.
      Parameters:
      type - the problem type.
      Throws:
      NullPointerException - if type is null
    • getStatus

      public Integer getStatus()
      Gets the problem status code (HTTP Status Code).
      Returns:
      the problem status code (HTTP Status Code)
    • setStatus

      public void setStatus(Integer status)
      Sets the problem status code (HTTP Status Code). If set, this must be the same as the one used in your HTTP Response.
      Parameters:
      status - the problem status code (HTTP Status Code).
      See Also:
    • getTitle

      public String getTitle()
      Gets the problem title.
      Returns:
      the problem title.
    • setTitle

      public void setTitle(String title)
      Sets the problem title.
      Parameters:
      title - the short human readable title.
      See Also:
    • getDetail

      public String getDetail()
      Gets the problem detail.
      Returns:
      the problem detail.
    • setDetail

      public void setDetail(String detail)
      Sets the problem detail.
      Parameters:
      detail - the problem detail.
    • getInstance

      public URI getInstance()
      Gets the problem instance.
      Returns:
      the problem instance.
    • setInstance

      public void setInstance(URI instance)
      Sets the problem instance.
      Parameters:
      instance - the problem instance.
    • findStatusPhrase

      public static String findStatusPhrase(int statusCode)
      Helper for retrieving the HTTP Status Phrase for a HTTP Status Code.
      Parameters:
      statusCode - the HTTP Status Code.
      Returns:
      the HTTP Status Phrase in English, may be null.
      See Also:
    • findStatusPhrase

      public static String findStatusPhrase(int statusCode, Locale locale)
      Helper for retrieving the localised HTTP Status Phrase for a HTTP Status Code.
      Parameters:
      statusCode - the HTTP Status Code.
      locale - the desired language.
      Returns:
      the HTTP Status Phrase, may be null.
      See Also:
    • toJson

      public String toJson() throws IOException
      Converts the contents to a String containing the JSON representation of this Problem.
      Returns:
      the JSON String.
      Throws:
      IOException - if there are problems with extending the JSON.
      See Also:
    • toJson

      public void toJson(OutputStream outputStream) throws IOException
      Writes the Problem to the supplied stream in JSON format using UTF-8.
      Parameters:
      outputStream - the stream to write to.
      Throws:
      IOException - if there are problems with extending the JSON.
      See Also:
    • toJson

      public void toJson(OutputStream outputStream, Charset charset) throws IOException
      Writes the Problem to the supplied stream in JSON format using the supplied Charset.
      Parameters:
      outputStream - the stream to write to.
      charset - the Charset to use.
      Throws:
      IOException - if there are problems with extending the JSON.
    • extendJson

      protected boolean extendJson(OutputStream outputStream, Charset charset)

      Override this if you wish to extend the JSON response. This function is called directly before closing the problem element. Pay carefull attention to the namespaces!

      Parameters:
      outputStream - the stream to write to
      charset - the charset this XML is being written in
      Returns:
      true if an element was inserted (so that the caller can decide whether or not to write a ",")
    • toXml

      public String toXml() throws XMLStreamException
      Converts the contents to a String containing the XML representation of this Problem.
      Returns:
      the XML String.
      Throws:
      XMLStreamException - if there are problems with extending the XML.
      See Also:
    • toXml

      public void toXml(OutputStream outputStream, Charset charset, boolean writeStartDocument) throws XMLStreamException
      Writes the Problem to the supplied stream in XML format using the supplied charset.
      Parameters:
      outputStream - to write to.
      charset - to write using.
      writeStartDocument - to indicate if the xml start document should also be written.
      Throws:
      XMLStreamException - if there are problems with extending the XML.
    • toXml

      public void toXml(XMLStreamWriter xmlStreamWriter, Charset charset, boolean writeStartDocument) throws XMLStreamException
      Writes the Problem to the supplied writer in XML format using the supplied charset.
      Parameters:
      xmlStreamWriter - to write using.
      charset - to write using.
      writeStartDocument - to indicate if the xml start document should also be written.
      Throws:
      XMLStreamException - if there are problems with extending the XML.
    • extendXml

      protected void extendXml(XMLStreamWriter xmlStreamWriter, Charset charset) throws XMLStreamException
      Override this if you wish to extend the XML response. This function is called directly before closing the problem element. Pay carefull attention to the namespaces!
      Parameters:
      xmlStreamWriter - the writer you must use to write to the stream
      charset - the charset this XML is being written in
      Throws:
      XMLStreamException - if there are problems with extending the XML.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object