Skip to content

Commit

Permalink
Merge pull request clojure-liberator#291 from brogowski/master
Browse files Browse the repository at this point in the history
Remove javax.xml.ws dependency
  • Loading branch information
ordnungswidrig authored Oct 17, 2017
2 parents cb6c4cb + 4642348 commit ed41cf6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGES.markdown
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# Unreleased

* Removed javax.xml.ws dependency

# New in 0.15.1

## Bugs fixed
Expand Down
8 changes: 4 additions & 4 deletions src/liberator/conneg.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns liberator.conneg
(:require [clojure.string :as string])
(:import (javax.xml.ws ProtocolException)))
(:require [clojure.string :as string]
[liberator.util :refer [protocol-exception]]))

;;; TODO: sort by level for text/html. Maybe also sort by charset.
;;; Finally, compare by precedence rules:
Expand Down Expand Up @@ -170,11 +170,11 @@
(when (and
(not (nil? q))
(> q 1.0))
(throw (ProtocolException. "Quality value of header exceeds 1")))
(throw (protocol-exception "Quality value of header exceeds 1")))
(when (and
(not (nil? q))
(< q 0))
(throw (ProtocolException. "Quality value of header is less than 0")))
(throw (protocol-exception "Quality value of header is less than 0")))
[charset (or q 1)]))

(defn parse-accepts-header [accepts-header]
Expand Down
25 changes: 15 additions & 10 deletions src/liberator/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
[liberator.representation :refer
[Representation as-response ring-response]]
[liberator.util :refer
[as-date http-date parse-http-date combine make-function]]
[as-date http-date parse-http-date
combine make-function is-protocol-exception?]]
[clojure.string :refer [join upper-case]])
(:import (javax.xml.ws ProtocolException)))
(:import (clojure.lang ExceptionInfo)))

(defmulti coll-validator
"Return a function that evaluaties if the give argument
Expand Down Expand Up @@ -406,9 +407,11 @@

(defmacro try-header [header & body]
`(try ~@body
(catch ProtocolException e#
(throw (ProtocolException.
(format "Malformed %s header" ~header) e#)))))
(catch ExceptionInfo e#
(if (is-protocol-exception? e#)
(throw (ex-info (format "Malformed %s header" ~header)
{:inner-exception e#}))
(throw e#)))))

(defdecision accept-encoding-exists? (partial header-exists? "accept-encoding")
encoding-available? processable?)
Expand Down Expand Up @@ -593,11 +596,13 @@
:resource (map-values make-function (merge default-functions kvs))
:representation {}})

(catch ProtocolException e ; this indicates a client error
{:status 400
:headers {"Content-Type" "text/plain"}
:body (.getMessage e)
::throwable e}))) ; ::throwable gets picked up by an error renderer
(catch ExceptionInfo e
(if (is-protocol-exception? e) ; this indicates a client error
{:status 400
:headers {"Content-Type" "text/plain"}
:body (.getMessage e)
::throwable e} ; ::throwable gets picked up by an error renderer
(throw e)))))


(defn get-options
Expand Down
10 changes: 10 additions & 0 deletions src/liberator/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,13 @@
(and (set? curr) (coll? newval)) (set (concat curr newval))
:otherwise newval))

(defn is-protocol-exception?
"Detects if given exception is a protocol exception."
[exception]
(= (:type (ex-data exception)) :protocol))

(defn protocol-exception
"Creates new protocol exception"
[msg]
(ex-info msg
{:type :protocol}))

0 comments on commit ed41cf6

Please sign in to comment.