Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Minor improvements. #4

Merged
merged 2 commits into from

2 participants

@ghoseb

No description provided.

@niclasmeier niclasmeier merged commit 54788e9 into HEROLABS:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 2, 2013
  1. @ghoseb
  2. @ghoseb

    Update .gitignore

    ghoseb authored
This page is out of date. Refresh to see the latest.
View
18 .gitignore
@@ -1,14 +1,10 @@
+/target
+/lib
+/classes
+/checkouts
pom.xml
-*jar
-*class
-*iml
-/lib/
-/classes/
-/newrelic/logs/
+*.jar
+*.class
.lein-deps-sum
.lein-failures
-/.idea/
-*.iml
-.project
-.classpath
-.settings/
+.lein-plugins
View
74 src/herolabs/apns/feedback.clj
@@ -14,25 +14,23 @@
[java.util.concurrent Executors ExecutorService ThreadFactory]
[java.util.concurrent LinkedBlockingQueue TimeUnit]
[java.net InetSocketAddress]
- [javax.net.ssl SSLContext]
- )
- )
+ [javax.net.ssl SSLContext]))
(def ^:private default-thread-pool* (atom nil))
(defn default-thread-pool []
(or @default-thread-pool*
- (swap! default-thread-pool*
- (fn [_] (Executors/newCachedThreadPool
- (let [number (atom 1)
- sm (System/getSecurityManager)
- group (if sm (.getThreadGroup sm) (.getThreadGroup (Thread/currentThread)))]
- (reify ThreadFactory
- (newThread [_ r] (let [t (Thread. group r (str "apns-feedback-" (swap! number inc)) 0)
- t (if (.isDaemon t) (.setDaemon t false) t)
- t (if (not= Thread/NORM_PRIORITY (.getPriority t)) (.setPriority t Thread/NORM_PRIORITY) t)]
- t)))))))))
+ (swap! default-thread-pool*
+ (fn [_] (Executors/newCachedThreadPool
+ (let [number (atom 1)
+ sm (System/getSecurityManager)
+ group (if sm (.getThreadGroup sm) (.getThreadGroup (Thread/currentThread)))]
+ (reify ThreadFactory
+ (newThread [_ r] (let [t (Thread. group r (str "apns-feedback-" (swap! number inc)) 0)
+ t (if (.isDaemon t) (.setDaemon t false) t)
+ t (if (not= Thread/NORM_PRIORITY (.getPriority t)) (.setPriority t Thread/NORM_PRIORITY) t)]
+ t)))))))))
(def ^:private timer* (ref nil))
@@ -45,19 +43,17 @@
(channelConnected [^ChannelHandlerContext ctx ^ChannelStateEvent event]
(debug "channelConnected")
(let [ssl-handler (-> ctx
- (.getPipeline)
- (.get SslHandler))]
- (.handshake ssl-handler)
- ))
+ (.getPipeline)
+ (.get SslHandler))]
+ (.handshake ssl-handler)))
(messageReceived [^ChannelHandlerContext ctx ^MessageEvent event]
(debug "messageReceived - " (.getMessage event))
(.put queue (.getMessage event)))
(exceptionCaught [^ChannelHandlerContext ctx ^ExceptionEvent event]
(debug (.getCause event) "exceptionCaught")
(-> event
- (.getChannel)
- (.close))
- )
+ (.getChannel)
+ (.close)))
(channelClosed [^ChannelHandlerContext ctx ^ChannelStateEvent event]
(debug "channelClosed"))))
@@ -68,11 +64,11 @@
(reify
ChannelPipelineFactory
(getPipeline [this]
- (doto (Channels/pipeline)
- (.addLast "ssl" (SslHandler. ssl-engine))
- (.addLast "decoder" (feedback-decoder))
- (.addLast "timeout" (ReadTimeoutHandler. (timer) (int (if time-out time-out 300))))
- (.addLast "handler" handler)))))
+ (doto (Channels/pipeline)
+ (.addLast "ssl" (SslHandler. ssl-engine))
+ (.addLast "decoder" (feedback-decoder))
+ (.addLast "timeout" (ReadTimeoutHandler. (timer) (int (if time-out time-out 300))))
+ (.addLast "handler" handler)))))
(defn- connect [^InetSocketAddress address ^SSLContext ssl-context time-out queue boss-executor worker-executor]
"creates a netty Channel to connect to the server."
@@ -80,22 +76,18 @@
(let [engine (ssl-engine ssl-context :use-client-mode true)
pipeline-factory (create-pipeline-factory engine (handler queue) time-out)
bootstrap (doto (-> (NioClientSocketChannelFactory. boss-executor worker-executor)
- (ClientBootstrap.))
- (.setOption "connectTimeoutMillis" 5000)
- (.setPipelineFactory pipeline-factory))
+ (ClientBootstrap.))
+ (.setOption "connectTimeoutMillis" 5000)
+ (.setPipelineFactory pipeline-factory))
future (.connect bootstrap address)
channel (-> future
- (.awaitUninterruptibly)
- (.getChannel)
- )]
+ (.awaitUninterruptibly)
+ (.getChannel))]
(if (.isSuccess future)
channel
(do
(.releaseExternalResources bootstrap)
- nil
- )
- )
- )
+ nil)))
(catch java.lang.Exception e
(warn e "Error"))))
@@ -103,11 +95,11 @@
(defn- read-feedback [queue channel]
"Internal function to create a lazy-seq returning the data from the feedback service"
(lazy-seq
- (if-let [next (.poll queue 10 TimeUnit/SECONDS)]
- (cons next (read-feedback queue channel))
- (when (.isConnected channel)
- (.close channel)
- nil))))
+ (if-let [next (.poll queue 10 TimeUnit/SECONDS)]
+ (cons next (read-feedback queue channel))
+ (when (.isConnected channel)
+ (.close channel)
+ nil))))
(defn feedback [^InetSocketAddress address ^SSLContext ssl-context & {:keys [time-out boss-executor worker-executor]
:or {time-out 300
@@ -120,4 +112,4 @@
(defn dev-address [] (InetSocketAddress. "feedback.sandbox.push.apple.com" 2196))
-(defn prod-address [] (InetSocketAddress. "feedback.push.apple.com" 2196))
+(defn prod-address [] (InetSocketAddress. "feedback.push.apple.com" 2196))
View
22 src/herolabs/apns/message.clj
@@ -4,41 +4,33 @@
(defn with-badge [message number] (assoc-in message [:aps :badge ] number))
(defn with-sound [message sound]
- (assoc-in message [:aps :sound ] (name sound))
- )
+ (assoc-in message [:aps :sound ] (name sound)))
(defn with-standard-alert [message body]
- (assoc-in message [:aps :alert ] body)
- )
+ (assoc-in message [:aps :alert ] body))
(defn with-action-loc-key [message key]
(if key
(assoc-in message [:aps :alert :action-loc-key ] key)
- message)
- )
+ message))
(defn with-loc-key [message key]
(if key
(assoc-in message [:aps :alert :loc-key ] key)
- message)
- )
+ message))
(defn with-loc-args [message args]
(if-not (empty? args)
(assoc-in message [:aps :alert :loc-args ] (if (sequential? args) args (list args)))
- message)
- )
+ message))
(defn with-payload [message payload]
(if-not (empty? payload)
(merge (dissoc payload :aps) message)
- message)
- )
+ message))
(defn with-alert-body [message body]
(if key
(assoc-in message [:aps :alert :body ] body)
- message)
- )
-
+ message))
View
69 src/herolabs/apns/protocol.clj
@@ -4,8 +4,7 @@
[org.jboss.netty.buffer ChannelBuffer ChannelBuffers]
[java.nio ByteOrder]
[org.apache.commons.codec.binary Hex]
- [java.util.concurrent.atomic AtomicInteger]
- ))
+ [java.util.concurrent.atomic AtomicInteger]))
;; some constants
(def ^:private standard-head (byte-array 1 (byte 0)))
@@ -19,8 +18,7 @@
(byte 5) :invalid-token-size
(byte 6) :invalid-topic-size
(byte 7) :invalid-payload-size
- (byte 8) :invalid-token
- })
+ (byte 8) :invalid-token})
(def ^:dynamic *coercions* json/*coercions*)
@@ -28,14 +26,11 @@
(defn- serialize [msg]
"Serializes the map into a JSON representation"
(binding [json/*coercions* *coercions*]
- (json/generate-string msg)
- )
- )
+ (json/generate-string msg)))
(defn- dynamic-buffer [len]
"Creates a dynamic buffer."
- (ChannelBuffers/dynamicBuffer ByteOrder/BIG_ENDIAN len)
- )
+ (ChannelBuffers/dynamicBuffer ByteOrder/BIG_ENDIAN len))
(defn- encode-message [^String device-token msg]
"Encodes a message into the standard APNS protocol format
@@ -43,15 +38,12 @@
(let [token (.decode hex-codec device-token)
serialized (serialize msg)
buffer (doto (dynamic-buffer (+ 1 2 (count token) 2 (count serialized)))
- (.writeBytes standard-head)
- (.writeShort (int (count token)))
- (.writeBytes token)
- (.writeShort (int (count serialized)))
- (.writeBytes (.getBytes serialized)))
- ]
- buffer
- )
- )
+ (.writeBytes standard-head)
+ (.writeShort (int (count token)))
+ (.writeBytes token)
+ (.writeShort (int (count serialized)))
+ (.writeBytes (.getBytes serialized)))]
+ buffer))
@@ -64,17 +56,14 @@
expires (get m :expires Integer/MAX_VALUE)
serialized (serialize msg)
buffer (doto (dynamic-buffer (+ 1 4 4 2 (count token) 2 (count serialized)))
- (.writeBytes enhanced-head)
- (.writeInt id)
- (.writeInt (int expires))
- (.writeShort (int (count token)))
- (.writeBytes token)
- (.writeShort (int (count serialized)))
- (.writeBytes (.getBytes serialized)))
- ]
- buffer
- )
- )
+ (.writeBytes enhanced-head)
+ (.writeInt id)
+ (.writeInt (int expires))
+ (.writeShort (int (count token)))
+ (.writeBytes token)
+ (.writeShort (int (count serialized)))
+ (.writeBytes (.getBytes serialized)))]
+ buffer))
@@ -87,14 +76,8 @@
(if-let [device-token (get (meta msg) :device-token )]
(if (= :enhanced (get (meta msg) :format ))
(encode-enhanced-message id-gen device-token msg)
- (encode-message device-token msg)
- )
- (throw (IllegalArgumentException. "Message must contain a :device-token as meta."))
- )
- )
- )
- )
- )
+ (encode-message device-token msg))
+ (throw (IllegalArgumentException. "Message must contain a :device-token as meta.")))))))
(defn decoder []
@@ -104,11 +87,7 @@
(let [command (.readByte msg)
status (.readByte msg)
id (.readInt msg)]
- {:status (get status-dictionary status :unknown ) :id id}
- )
- )
- )
- )
+ {:status (get status-dictionary status :unknown ) :id id}))))
(defn feedback-decoder []
"Creates an decoder for the APNS protocol."
@@ -119,8 +98,4 @@
token-bytes (byte-array token-len)]
(.readBytes msg token-bytes)
(let [token (Hex/encodeHexString token-bytes)]
- [token time]
- ))
- )
- )
- )
+ [token time])))))
View
62 src/herolabs/apns/push.clj
@@ -22,16 +22,16 @@
(defn default-thread-pool []
(or @default-thread-pool*
- (swap! default-thread-pool*
- (fn [_] (Executors/newCachedThreadPool
- (let [number (atom 1)
- sm (System/getSecurityManager)
- group (if sm (.getThreadGroup sm) (.getThreadGroup (Thread/currentThread)))]
- (reify ThreadFactory
- (newThread [_ r] (let [t (Thread. group r (str "apns-push-pool-" (swap! number inc)) 0)]
- (when (.isDaemon t) (.setDaemon t false))
- (when (not= Thread/NORM_PRIORITY (.getPriority t)) (.setPriority t Thread/NORM_PRIORITY))
- t)))))))))
+ (swap! default-thread-pool*
+ (fn [_] (Executors/newCachedThreadPool
+ (let [number (atom 1)
+ sm (System/getSecurityManager)
+ group (if sm (.getThreadGroup sm) (.getThreadGroup (Thread/currentThread)))]
+ (reify ThreadFactory
+ (newThread [_ r] (let [t (Thread. group r (str "apns-push-pool-" (swap! number inc)) 0)]
+ (when (.isDaemon t) (.setDaemon t false))
+ (when (not= Thread/NORM_PRIORITY (.getPriority t)) (.setPriority t Thread/NORM_PRIORITY))
+ t)))))))))
(def ^:private timer* (ref nil))
@@ -39,12 +39,12 @@
(defmacro future-listener [params & body]
(cond
- (not (vector? params)) (throw (IllegalArgumentException. "Parameter have to be a vector."))
- (not= 1 (count params)) (throw (IllegalArgumentException. "Parameter may only contain one element."))
- (empty? body) nil
- :else (let [future (first params)]
- `(reify org.jboss.netty.channel.ChannelFutureListener
- (operationComplete [this# ^ChannelFuture ~future] ~@body)))))
+ (not (vector? params)) (throw (IllegalArgumentException. "Parameter have to be a vector."))
+ (not= 1 (count params)) (throw (IllegalArgumentException. "Parameter may only contain one element."))
+ (empty? body) nil
+ :else (let [future (first params)]
+ `(reify org.jboss.netty.channel.ChannelFutureListener
+ (operationComplete [this# ^ChannelFuture ~future] ~@body)))))
(defn- handler
"Function to create a ChannelUpstreamHandler"
@@ -53,29 +53,26 @@
(channelConnected [^ChannelHandlerContext ctx ^ChannelStateEvent event]
(trace "channelConnected")
(let [ssl-handler (-> ctx
- (.getPipeline)
- (.get SslHandler))]
- (.handshake ssl-handler)
- ))
+ (.getPipeline)
+ (.get SslHandler))]
+ (.handshake ssl-handler)))
(channelDisconnected [^ChannelHandlerContext ctx ^ChannelStateEvent event]
(trace "channelDisconnected" this))
(messageReceived [^ChannelHandlerContext ctx ^MessageEvent event]
- (trace "messageReceived -" (.getMessage event))
- )
+ (trace "messageReceived -" (.getMessage event)))
(exceptionCaught [^ChannelHandlerContext ctx ^ExceptionEvent event]
(trace (.getCause event) "exceptionCaught")
(when exception-handler (exception-handler (.getCause event)))
(-> event
- (.getChannel)
- (.close))
- )
+ (.getChannel)
+ (.close)))
(channelClosed [^ChannelHandlerContext ctx ^ChannelStateEvent event]
(trace "channelClosed")
(let [new-handler (ssl-handler-factory)
pipeline (.getPipeline ctx)
ssl-handler (.replace pipeline SslHandler "ssl" new-handler)]
(-> (.connect bootstrap) (.addListener (future-listener [f]
- (swap! client-handle (fn [_] (.getChannel f))))))))))
+ (swap! client-handle (fn [_] (.getChannel f))))))))))
(defn- create-ssl-handler-factory [ssl-engine-factory] (fn [] (SslHandler. (ssl-engine-factory))))
@@ -99,18 +96,17 @@
"Creates a Netty Channel to connect to the server."
(let [engine-factory (ssl-engine-factory ssl-context :use-client-mode true)
bootstrap (-> (NioClientSocketChannelFactory.
- boss-executor worker-executor) (ClientBootstrap.))
+ boss-executor worker-executor) (ClientBootstrap.))
ssl-handler-factory (create-ssl-handler-factory engine-factory)
client-handle (atom nil)
pipeline-factory (create-pipeline-factory ssl-handler-factory (handler bootstrap ssl-handler-factory client-handle
- exception-handler) (timer) time-out)
+ exception-handler) (timer) time-out)
bootstrap (doto bootstrap
- (.setOption "connectTimeoutMillis" 5000)
- (.setPipelineFactory pipeline-factory)
- (.setOption "remoteAddress" address))
+ (.setOption "connectTimeoutMillis" 5000)
+ (.setPipelineFactory pipeline-factory)
+ (.setOption "remoteAddress" address))
future (.connect bootstrap)
- channel (-> future (.awaitUninterruptibly) (.getChannel))
- ]
+ channel (-> future (.awaitUninterruptibly) (.getChannel))]
(if (.isSuccess future)
(do
(swap! client-handle (fn [_] channel))
View
36 src/herolabs/apns/ssl.clj
@@ -5,8 +5,7 @@
(:import [org.apache.commons.codec.digest DigestUtils]
[java.security Security KeyStore]
[javax.net.ssl KeyManager KeyManagerFactory SSLContext SSLEngine TrustManager TrustManagerFactory X509TrustManager]
- [java.security.cert Certificate X509Certificate CertificateException CertificateFactory])
- )
+ [java.security.cert Certificate X509Certificate CertificateException CertificateFactory]))
(defn load-keystore
@@ -25,17 +24,13 @@
store-type "JCEKS"}}]
(let [keystore (if keystore keystore (load-keystore store-path store-pass store-type))
kmf (doto (KeyManagerFactory/getInstance algorithm)
- (.init keystore (char-array key-pass)))
+ (.init keystore (char-array key-pass)))
tms (if trust-managers trust-managers (.getTrustManagers (doto
- (TrustManagerFactory/getInstance algorithm)
- (.init keystore)
- )))
+ (TrustManagerFactory/getInstance algorithm)
+ (.init keystore))))
context (doto (SSLContext/getInstance "TLS")
- (.init (.getKeyManagers kmf) tms nil))
- ]
- context
- )
- )
+ (.init (.getKeyManagers kmf) tms nil))]
+ context))
(defn aliases [keystore]
(letfn [(get-aliases [enum] (lazy-seq
@@ -64,17 +59,14 @@
(let [engine (.createSSLEngine context)]
(if use-client-mode
(doto engine (.setUseClientMode use-client-mode))
- engine)
- )
- )
+ engine)))
(defn ssl-engine-factory [context & {:keys [use-client-mode] :or {use-client-mode true}}]
"Creates an SSL engine"
(fn [] (let [engine (.createSSLEngine context)]
- (if use-client-mode
- (doto engine (.setUseClientMode use-client-mode))
- engine)
- )))
+ (if use-client-mode
+ (doto engine (.setUseClientMode use-client-mode))
+ engine))))
@@ -83,10 +75,6 @@
(into-array (list (proxy [javax.net.ssl.X509TrustManager] []
(getAcceptedIssuers [] (make-array X509Certificate 0))
(checkClientTrusted [chain auth-type]
- (when trace (info "Unknown client certificate:" (.getSubjectDN (get chain 0))))
- )
+ (when trace (info "Unknown client certificate:" (.getSubjectDN (get chain 0)))))
(checkServerTrusted [chain auth-type]
- (when trace (info "Unknown server certificate:" (.getSubjectDN (get chain 0))))
- )
- )))
- )
+ (when trace (info "Unknown server certificate:" (.getSubjectDN (get chain 0)))))))))
Something went wrong with that request. Please try again.