parsec_service/back/
dispatcher.rs

1// Copyright 2019 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3//! Dispatch requests to the correct backend
4//!
5//! The dispatcher's role is to direct requests to the provider they specify, if
6//! said provider is available on the system, thus acting as a multiplexer.
7use super::backend_handler::BackEndHandler;
8use crate::authenticators::Application;
9use log::trace;
10use parsec_interface::requests::request::Request;
11use parsec_interface::requests::ProviderId;
12use parsec_interface::requests::{Response, ResponseStatus};
13use std::collections::HashMap;
14use std::io::{Error, ErrorKind, Result};
15
16/// Dispatcher to backend
17///
18/// Component tasked with identifying the backend handler that can
19/// service a request.
20///
21/// As such, it owns all the backend handlers and attempts to match
22/// the fields in the request header to the properties of the handlers.
23#[derive(Debug)]
24pub struct Dispatcher {
25    backends: HashMap<ProviderId, BackEndHandler>,
26}
27
28impl Dispatcher {
29    /// Parses the `provider` field of the request header and attempts to find
30    /// the backend handler to which the request must be dispatched.
31    ///
32    /// Returns either the response coming from the backend handler, or a response
33    /// containing a status code consistent with the error encountered during
34    /// processing.
35    pub fn dispatch_request(&self, request: Request, app: Option<Application>) -> Response {
36        trace!("dispatch_request ingress");
37        if let Some(backend) = self.backends.get(&request.header.provider) {
38            if let Err(status) = backend.is_capable(&request) {
39                Response::from_request_header(request.header, status)
40            } else {
41                {
42                    let response = backend.execute_request(request, app);
43                    trace!("execute_request egress");
44                    response
45                }
46            }
47        } else {
48            Response::from_request_header(request.header, ResponseStatus::ProviderNotRegistered)
49        }
50    }
51}
52
53/// `Dispatcher` builder
54#[derive(Debug, Default)]
55pub struct DispatcherBuilder {
56    backends: Option<HashMap<ProviderId, BackEndHandler>>,
57}
58
59impl DispatcherBuilder {
60    /// Create a new Dispatcher builder
61    pub fn new() -> Self {
62        DispatcherBuilder { backends: None }
63    }
64
65    /// Add a BackEndHandler with a specific Provider ID to the dispatcher
66    pub fn with_backend(
67        mut self,
68        provider_id: ProviderId,
69        backend_handler: BackEndHandler,
70    ) -> Self {
71        let mut backends = self.backends.unwrap_or_default();
72        let _ = backends.insert(provider_id, backend_handler);
73        self.backends = Some(backends);
74
75        self
76    }
77
78    /// Add multiple BackEndHandler to the dispatcher in one call
79    pub fn with_backends(mut self, new_backends: HashMap<ProviderId, BackEndHandler>) -> Self {
80        let mut backends = self.backends.unwrap_or_default();
81        backends.extend(new_backends);
82        self.backends = Some(backends);
83
84        self
85    }
86
87    /// Build the builder into a dispatcher
88    pub fn build(self) -> Result<Dispatcher> {
89        Ok(Dispatcher {
90            backends: self
91                .backends
92                .ok_or_else(|| Error::new(ErrorKind::InvalidData, "backends is missing"))?,
93        })
94    }
95}