camel-processor 0.70.0

Message processors for rust-camel
Documentation
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use tower::Service;

use camel_api::body::Body;
use camel_api::{CamelError, Exchange};
use camel_json_schema::CompiledSchema;
use camel_json_schema::FormatMode;
use camel_json_schema::SchemaSource;
use camel_json_schema::SchemaSyntax;

/// Tower Service that validates the exchange body against a compiled JSON
/// Schema. Returns `CamelError::ValidationError` if the body does not match.
/// Validation runs BEFORE the inner service is called.
///
/// Non-JSON bodies (Body::Text, Body::Bytes, Body::Xml, Body::Empty,
/// Body::Stream) are passed through without validation — the validator only
/// runs against Body::Json. Callers that want to validate raw text/bytes
/// must first run an Unmarshal step that produces Body::Json.
pub struct JsonSchemaValidateService<P> {
    inner: P,
    validator: Arc<CompiledSchema>,
}

impl<P> std::fmt::Debug for JsonSchemaValidateService<P> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JsonSchemaValidateService")
            .finish_non_exhaustive()
    }
}

impl<P> Clone for JsonSchemaValidateService<P>
where
    P: Clone,
{
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            validator: self.validator.clone(),
        }
    }
}

impl<P> JsonSchemaValidateService<P> {
    /// Compile a JSON Schema and wrap the given inner service. Returns
    /// `CamelError::RouteError` if the schema is not a valid JSON Schema.
    ///
    /// The schema is an inline document, so it resolves same-document
    /// references only: every external `$ref` fails at construction with the
    /// named confinement rejection (ADR-0100).
    pub fn new(inner: P, schema: &serde_json::Value) -> Result<Self, CamelError> {
        let validator = camel_json_schema::compile(
            &SchemaSource::Inline(schema.clone()),
            SchemaSyntax::Auto,
            FormatMode::Enforce,
        )
        .map_err(|e| CamelError::RouteError(format!("invalid JSON schema: {e}")))?;
        Ok(Self {
            inner,
            validator: Arc::new(validator),
        })
    }
}

impl<P> Service<Exchange> for JsonSchemaValidateService<P>
where
    P: Service<Exchange, Response = Exchange, Error = CamelError> + Clone + Send + 'static,
    P::Future: Send,
{
    type Response = Exchange;
    type Error = CamelError;
    type Future = Pin<Box<dyn Future<Output = Result<Exchange, CamelError>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, exchange: Exchange) -> Self::Future {
        // Only Body::Json is validated. Anything else passes through.
        let result = match &exchange.input.body {
            Body::Json(v) => {
                let errors: Vec<String> = self
                    .validator
                    .violations(v)
                    .into_iter()
                    .map(|violation| {
                        format!("{} at {}", violation.message, violation.instance_path)
                    })
                    .collect();
                if errors.is_empty() {
                    Ok(())
                } else {
                    Err(errors.join("; "))
                }
            }
            _ => Ok(()),
        };
        match result {
            Ok(()) => {
                let fut = self.inner.call(exchange);
                Box::pin(fut)
            }
            Err(msg) => Box::pin(async move {
                Err(CamelError::ValidationError(format!(
                    "JSON schema validation failed: {msg}"
                )))
            }),
        }
    }
}