1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Context object for the message workflow through the filter chain.
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use crate::session::stomp_session::{StompSession, FLAG_ADMIN, FLAG_DOWNSTREAM};
use chrono::{Utc, DateTime};
//. TODO provide access to SERVER and CONFIG items here, rather than globals
pub struct Context {
/// StompSession is valid for the duration of a STOMP TCP connection or WebSocket
pub session: Arc<RwLock<StompSession>>,
/// Request attributes, valid for the life of a single message, to avoid collisions prefix the attributes with the cargo project name e.g. "romp."
pub attributes: HashMap<&'static str, String>,
// TODO how to determine this if SSL is upstream
pub is_secure: bool,
/// flag is true if incoming request has upgraded to WebSockets
pub is_web_sockets: bool,
// TODO finer grained security
/// True if this request has admin privs, from port 61616 or login credentials
is_admin: Option<bool>,
/// If true session is upstream
is_downstream: Option<bool>,
/// Cached value of time now
timestamp: Option<DateTime<Utc>>,
}
impl Context {
pub(crate) fn new(session: Arc<RwLock<StompSession>>) -> Context {
Context {
session,
attributes: Default::default(),
is_secure: false,
is_web_sockets: false,
is_admin: None,
is_downstream: None,
timestamp: None,
}
}
// for use in tests
pub fn mock() -> Context {
Context {
session: Arc::new(RwLock::new(StompSession::new())),
attributes: Default::default(),
is_secure: false,
is_web_sockets: false,
is_admin: None,
is_downstream: None,
timestamp: None,
}
}
/// returns (cached) timestamp
pub fn now(&mut self) -> DateTime<Utc> {
if let None = self.timestamp {
self.timestamp = Some(Utc::now());
}
self.timestamp.unwrap()
}
/// returns (cached) admin flag from the session
pub fn is_admin(&mut self) -> bool {
if let None = self.is_admin {
self.is_admin = Some(self.session.read().unwrap().get_flag(FLAG_ADMIN));
}
self.is_admin.unwrap()
}
/// returns (cached) downstream flag from the session
pub fn is_downstream(&mut self) -> bool {
if let None = self.is_admin {
self.is_downstream = Some(self.session.read().unwrap().get_flag(FLAG_DOWNSTREAM));
}
self.is_downstream.unwrap()
}
// OW horrible, have to clone a string to get it out from behind two read locks :(
pub(crate) fn downstream(&self) -> String {
self.session.read().unwrap().downstream_connector.as_ref().unwrap().read().unwrap().downstream.name.clone()
}
}