stamd 0.1.0

Webservice for working with stand-off annotations on text (STAM)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
use axum::{
    body::Body, extract::Path, extract::Query, extract::State, http::HeaderMap, http::HeaderValue,
    http::Request, routing::get, routing::post, Form, Router,
};
use clap::Parser;
use serde::Deserialize;
use stam::FindText;
use stam::WebAnnoConfig;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::signal;
use tower_http::trace::TraceLayer;
use tracing::{debug, error};

use utoipa::{OpenApi, ToSchema};
use utoipa_swagger_ui::SwaggerUi;

use stam::{Config, Offset, QueryIter, StamError, Text};
use stamtools::view::HtmlWriter;

mod apidocs;
mod common;
mod multistore;
use common::{ApiError, ApiResponse};
use multistore::StorePool;

pub const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const FLUSH_INTERVAL: Duration = Duration::from_secs(60);
const CONTENT_TYPE_JSON: &'static str = "application/json";
const CONTENT_TYPE_JSONLD: &'static str = "application/ld+json";
const CONTENT_TYPE_HTML: &'static str = "text/html";
const CONTENT_TYPE_TEXT: &'static str = "text/plain";

#[derive(Parser, Debug)]
struct Args {
    #[arg(
        short,
        long,
        default_value_os = "127.0.0.1:8080",
        help = "The host and port to bind to"
    )]
    bind: String,

    #[arg(
        short = 'd',
        long,
        default_value_os = ".",
        help = "The base directory to serve from"
    )]
    basedir: String,

    #[arg(
        short = 'u',
        long,
        help = "The public-facing base URL. Also used as IRI for webannotations."
    )]
    baseurl: Option<String>,

    #[arg(
        short = 'e',
        long,
        default_value_os = "store.stam.json",
        help = "The extension for annotation stores"
    )]
    extension: String,

    #[arg(
        long,
        default_value_t = 600,
        help = "Number of seconds before stores are unloaded again"
    )]
    unload_time: u64,

    #[arg(
        short,
        long,
        default_value_t = false,
        help = "Sets all underlying stores as read-only"
    )]
    readonly: bool,

    #[arg(
        long,
        default_value_t = false,
        help = "Output logging info on incoming requests"
    )]
    debug: bool,

    #[arg(
        long = "add-context",
        help = "(for Web Annotation output only) URL to a JSONLD context to include"
    )]
    add_context: Vec<String>,

    #[arg(
        long = "ns",
        help = "(for Web Annotation output only) Add a namespace to the JSON-LD context, syntax is: namespace: uri"
    )]
    namespaces: Vec<String>,

    #[arg(
        long = "no-extra-target",
        help = "(for Web Annotation output only) By default, stamd adds an extra target to Web Annotations with a TextPositionSelector, this is a URL that can be resolved directly by stamd. If you don't want this behaviour, set this."
    )]
    no_extra_target: bool,
}

#[derive(OpenApi)]
#[openapi(
    paths(
        list_stores,
        get_query,
        create_store,
        create_resource,
        get_annotation_list,
        get_annotation,
        get_resource_list,
        get_resource,
        get_textselection,
    ),
    tags(
        (name = "stamd", description = "WebAPI for stam")
    )
)]
pub struct ApiDoc;

#[tokio::main]
async fn main() {
    let args = Args::parse();

    // set up config for webannotations
    let mut context_namespaces = Vec::new();
    for assignment in args.namespaces.iter() {
        let result: Vec<_> = assignment.splitn(2, ":").collect();
        if result.len() != 2 {
            error!("Syntax for --ns should be `ns: uri_prefix`");
        } else {
            context_namespaces.push((result[1].trim().to_string(), result[0].trim().to_string()));
        }
    }
    let webannoconfig = WebAnnoConfig {
        extra_context: args.add_context,
        context_namespaces,
        ..WebAnnoConfig::default()
    };

    let storepool = StorePool::new(
        args.basedir,
        if let Some(baseurl) = args.baseurl.as_ref() {
            baseurl.to_string()
        } else {
            format!("http://{}/", args.bind)
        },
        args.extension,
        args.readonly,
        args.unload_time,
        args.no_extra_target,
        webannoconfig,
        Config::default(),
    )
    .expect("Base directory must exist");

    if args.debug {
        tracing_subscriber::fmt()
            .with_max_level(tracing::Level::DEBUG)
            .init();
    }

    let storepool: Arc<StorePool> = storepool.into();
    let storepool_flush = storepool.clone();

    std::thread::spawn(move || loop {
        std::thread::sleep(FLUSH_INTERVAL);
        match storepool_flush.flush(false) {
            Err(e) => error!("Flush failed! {:?}", e),
            Ok(v) => {
                if args.debug {
                    debug!("Flushed {} store(s)", v.len());
                }
            }
        }
    });

    let app = Router::new()
        .route("/", get(list_stores))
        .route("/:store_id", post(create_store))
        .route("/query", post(post_query))
        .route("/:store_id", get(get_query))
        .route("/:store_id/annotations/:annotation_id", get(get_annotation))
        .route("/:store_id/annotations", get(get_annotation_list))
        .route(
            "/:store_id/resources/:resource_id/:begin/:end",
            get(get_textselection),
        )
        .route("/:store_id/resources", get(get_resource_list))
        .route("/:store_id/resources/:resource_id", get(get_resource))
        .route("/:store_id/resources/:resource_id", post(create_resource))
        .merge(SwaggerUi::new("/swagger-ui").url("/api-doc/openapi.json", ApiDoc::openapi()))
        .layer(TraceLayer::new_for_http())
        .with_state(storepool.clone());

    //allow trailing slashes as well: (conflicts with swagger-ui!)
    //let app = NormalizePathLayer::trim_trailing_slash().layer(app);

    eprintln!("[stamd] listening on {}", args.bind);
    let listener = tokio::net::TcpListener::bind(args.bind).await.unwrap();
    axum::serve(
        listener, app,
        //ServiceExt::<axum::http::Request<Body>>::into_make_service(app),
    )
    .with_graceful_shutdown(shutdown_signal(storepool))
    .await
    .unwrap();
}

async fn shutdown_signal(storepool: Arc<StorePool>) {
    let ctrl_c = async {
        signal::ctrl_c()
            .await
            .expect("failed to install Ctrl+C handler");
    };

    #[cfg(unix)]
    let terminate = async {
        signal::unix::signal(signal::unix::SignalKind::terminate())
            .expect("failed to install signal handler")
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => {
            storepool.flush(true).expect("Clean shutdown failed");
        }
        _ = terminate => {
            storepool.flush(true).expect("Clean shutdown failed");
        }
    }
}

#[utoipa::path(
    get,
    path = "/",
    responses(
        (status = 200, body = [String], description = "Returns a simple list of all available annotation stores"),
    )
)]
/// Runs all available annotation stores or provide a very simple webinterface
async fn list_stores(
    storepool: State<Arc<StorePool>>,
    request: Request<Body>,
) -> Result<ApiResponse, ApiError> {
    let extension = format!(".{}", storepool.extension());
    let mut store_ids: Vec<String> = Vec::new();
    for entry in std::fs::read_dir(storepool.basedir())
        .map_err(|_| ApiError::InternalError("Unable to read base directory"))?
    {
        let entry = entry.unwrap();
        if let Some(filename) = entry.file_name().to_str() {
            if let Some(pos) = filename.find(&extension) {
                store_ids.push(filename[0..pos].to_string());
            }
        }
    }
    match negotiate_content_type(request.headers(), &[CONTENT_TYPE_HTML, CONTENT_TYPE_JSON]) {
        Ok(CONTENT_TYPE_HTML) => Ok(ApiResponse::QueryUI(store_ids)),
        Ok(CONTENT_TYPE_JSON) => {
            let store_ids: Vec<serde_json::Value> =
                store_ids.into_iter().map(|s| s.into()).collect();
            Ok(ApiResponse::JsonList(store_ids))
        }
        _ => Err(ApiError::NotAcceptable(
            "Accept headed could not be satisfied (try application/json)",
        )),
    }
}

#[utoipa::path(
    post,
    path = "/{store_id}",
    responses(
        (status = 201, description = "Returned when successfully created"),
        (status = 403, body = apidocs::ApiError, description = "Returned with name `PermissionDenied` when permission is denied, for instance the store is configured as read-only or the store already exists", content_type = "application/json")
    )
)]
/// Create a new annotation store
async fn create_store(
    Path(store_id): Path<String>,
    storepool: State<Arc<StorePool>>,
) -> Result<ApiResponse, ApiError> {
    storepool.new_store(&store_id)?;
    Ok(ApiResponse::Created())
}

#[utoipa::path(
    post,
    path = "/{store_id}/resources/{resource_id}",
    request_body(content_type = "text/plain", description = "The full text of the resource"),
    responses(
        (status = 201, description = "Returned when successfully created"),
        (status = 403, body = apidocs::ApiError, description = "Returned with name `PermissionDenied` when permission is denied, for instance the store is configured as read-only or the resource already exists", content_type = "application/json")
    )
)]
/// Create a new text resource, the request body contains the text.
async fn create_resource(
    Path((store_id, resource_id)): Path<(String, String)>,
    storepool: State<Arc<StorePool>>,
    text: String,
) -> Result<ApiResponse, ApiError> {
    storepool.new_resource(&store_id, &resource_id, text)?;
    Ok(ApiResponse::Created())
}

#[utoipa::path(
    get,
    path = "/{store_id}",
    params(
        ("store_id" = String, Path, description = "The identifier of the store"),
        ("query" = String, Query, description = "A query in STAMQL, see <https://github.com/annotation/stam/tree/master/extensions/stam-query> for the syntax.", allow_reserved),
        ("use" = Option<String>, Query, description = "Select a single variable from the query (by name, without '?' prefix), to constrain the result set accordingly.")
    ),
    responses(
        (status = 200, description = "Query result. Several return types are supported via content negotation, but not all content types can be used for all queries. Most notably, the plain text type only works if the query produces a single item that holds text as result.",content(
            ([BTreeMap<String,apidocs::StamJson>] = "application/json"),
            ([apidocs::StamJson] = "application/json"),
            (String = "text/html"),
            (String = "text/plain"),
        )),
        (status = 406, body = apidocs::ApiError, description = "This is returned if the requested content-type (Accept) could not be delivered for your query.", content_type = "application/json"),
        (status = 404, body = apidocs::StamError, description = "Return when the query is invalid or another error occurs", content_type = "application/json"),
        (status = 404, body = apidocs::ApiError, description = "Returned with name `MissingArgument` if you forget the 'query' parameter", content_type = "application/json"),
        (status = 404, body = apidocs::ApiError, description = "Returned with name `NotFound` if the store does not exist", content_type = "application/json"),
        (status = 403, body = apidocs::ApiError, description = "Returned with name `PermissionDenied` when permission is denied, for instance when you send a query that edits the data but the store is configured as read-only", content_type = "application/json")
    )
)]
/// Run a query on an annotation store. The query is formulated in STAMQL.
async fn get_query(
    Path(store_id): Path<String>,
    Query(params): Query<HashMap<String, String>>,
    storepool: State<Arc<StorePool>>,
    request: Request<Body>,
) -> Result<ApiResponse, ApiError> {
    if let Some(querystring) = params.get("query") {
        run_query(
            store_id.as_str(),
            querystring,
            params.get("use").map(|s| s.as_str()),
            storepool,
            request.headers(),
        )
    } else {
        Err(ApiError::MissingArgument("query"))
    }
}

#[derive(Deserialize, ToSchema)]
struct QueryForm {
    /// The ID of the store to query
    store: String,

    /// A query in STAMQL
    query: String,

    /// A variable from the above query to return in the result set (without leading ?)
    r#use: Option<String>,
}

#[utoipa::path(
    post,
    path = "/query",
    request_body( content_type = "multipart/form-data", content = QueryForm),
    responses(
        (status = 200, description = "Query result. Several return types are supported via content negotation, but not all content types can be used for all queries. Most notably, the plain text type only works if the query produces a single item that holds text as result.",content(
            ([BTreeMap<String,apidocs::StamJson>] = "application/json"),
            ([apidocs::StamJson] = "application/json"),
            (String = "text/html"),
            (String = "text/plain"),
        )),
        (status = 406, body = apidocs::ApiError, description = "This is returned if the requested content-type (Accept) could not be delivered for your query.", content_type = "application/json"),
        (status = 404, body = apidocs::StamError, description = "Return when the query is invalid or another error occurs", content_type = "application/json"),
        (status = 404, body = apidocs::ApiError, description = "Returned with name `MissingArgument` if you forget the 'query' parameter", content_type = "application/json"),
        (status = 404, body = apidocs::ApiError, description = "Returned with name `NotFound` if the store does not exist", content_type = "application/json"),
        (status = 403, body = apidocs::ApiError, description = "Returned with name `PermissionDenied` when permission is denied, for instance when you send a query that edits the data but the store is configured as read-only", content_type = "application/json")
    )
)]
/// Run a query on an annotation store. The query is formulated in STAMQL.
async fn post_query(
    storepool: State<Arc<StorePool>>,
    headers: HeaderMap,
    Form(queryform): Form<QueryForm>,
) -> Result<ApiResponse, ApiError> {
    run_query(
        queryform.store.as_str(),
        queryform.query.as_str(),
        queryform.r#use.as_ref().map(|s| s.as_str()),
        storepool,
        &headers,
    )
}

#[utoipa::path(
    get,
    path = "/{store_id}/annotations",
    params(
        ("store_id" = String, Path, description = "The identifier of the store"),
    ),
    responses(
        (status = 200, body = [String], description = "Returns a simple list of all available annotations (IDs), for the given store"),
        (status = 404, body = apidocs::ApiError, description = "Returned with name `NotFound` if the store does not exist", content_type = "application/json"),
    )
)]
/// Returns the public identifiers of all available annotations in a given annotation store
async fn get_annotation_list(
    Path(store_id): Path<String>,
    storepool: State<Arc<StorePool>>,
    request: Request<Body>,
) -> Result<ApiResponse, ApiError> {
    storepool.map(&store_id, |store| {
        match negotiate_content_type(request.headers(), &[CONTENT_TYPE_JSON]) {
            Ok(CONTENT_TYPE_JSON) => {
                //TODO: may be a fairly expensive copy if there are lots of annotations, no pagination either here
                let annotations: Vec<serde_json::Value> = store
                    .annotations()
                    .filter_map(|a| a.id().map(|s| s.into()))
                    .collect();
                Ok(ApiResponse::JsonList(annotations))
            }
            _ => Err(ApiError::NotAcceptable(
                "Accept headed could not be satisfied (try application/json)",
            )),
        }
    })
}

#[utoipa::path(
    get,
    path = "/{store_id}/resources",
    params(
        ("store_id" = String, Path, description = "The identifier of the store"),
    ),
    responses(
        (status = 200, body = [String], description = "Returns a simple list of all available resources (IDs), for the given store"),
        (status = 404, body = apidocs::ApiError, description = "Returned with name `NotFound` if the store does not exist", content_type = "application/json"),
    )
)]
/// Returns the public identifiers of all available resources in a given annotation store
async fn get_resource_list(
    Path(store_id): Path<String>,
    storepool: State<Arc<StorePool>>,
    request: Request<Body>,
) -> Result<ApiResponse, ApiError> {
    storepool.map(&store_id, |store| {
        match negotiate_content_type(request.headers(), &[CONTENT_TYPE_JSON]) {
            Ok(CONTENT_TYPE_JSON) => {
                //TODO: may be a fairly expensive copy if there are lots of resources, no pagination either here
                let resources: Vec<serde_json::Value> = store
                    .resources()
                    .filter_map(|r| r.id().map(|s| s.into()))
                    .collect();
                Ok(ApiResponse::JsonList(resources))
            }
            _ => Err(ApiError::NotAcceptable(
                "Accept headed could not be satisfied (try application/json)",
            )),
        }
    })
}

#[utoipa::path(
    get,
    path = "/{store_id}/annotations/{annotation_id}",
    params(
        ("store_id" = String, Path, description = "The identifier of the store the annotation is in"),
        ("annotation_id" = String, Path, description = "The identifier of the annotation"),
    ),
    responses(
        (status = 200, description = "The annotation. Several return types are supported via content negotation.",content(
            (apidocs::StamJson = "application/json"),
            (apidocs::WebAnnotation = "application/ld+json"),
            (String = "text/plain"),
        )),
        (status = 406, body = apidocs::ApiError, description = "This is returned if the requested content-type (Accept) could not be delivered", content_type = "application/json"),
        (status = 404, body = apidocs::ApiError, description = "Returned with name `NotFound` if the store or annotation does not exist", content_type = "application/json"),
        (status = 404, body = apidocs::StamError, description = "Returned when a STAM error occurs", content_type = "application/json"),
    )
)]
/// Returns an annotation given its identifier
async fn get_annotation(
    Path((store_id, annotation_id)): Path<(String, String)>,
    storepool: State<Arc<StorePool>>,
    request: Request<Body>,
) -> Result<ApiResponse, ApiError> {
    storepool.map(&store_id, |store| match store.annotation(annotation_id) {
        None => Err(ApiError::NotFound("No such annotation")),
        Some(annotation) => {
            match negotiate_content_type(
                request.headers(),
                &[CONTENT_TYPE_JSON, CONTENT_TYPE_JSONLD, CONTENT_TYPE_TEXT],
            ) {
                Ok(CONTENT_TYPE_JSON) => Ok(ApiResponse::RawJson(
                    annotation.as_ref().to_json_string(store)?,
                )),
                Ok(CONTENT_TYPE_JSONLD) => {
                    if let Ok(webannoconfigs) = storepool.webannoconfigs().read() {
                        if let Some(webannoconfig) = webannoconfigs.get(&store_id) {
                            Ok(ApiResponse::RawJsonLd(
                                annotation.to_webannotation(webannoconfig).to_string(),
                            ))
                        } else {
                            Err(ApiError::InternalError("Webannoconfig must exist"))
                        }
                    } else {
                        Err(ApiError::InternalError("Webannoconfigs lock poisoned"))
                    }
                }
                Ok(CONTENT_TYPE_TEXT) => Ok(ApiResponse::Text(annotation.text_join("\t"))),
                _ => Err(ApiError::NotAcceptable(
                    "Accept headed could not be satisfied (try application/json)",
                )),
            }
        }
    })
}

#[utoipa::path(
    get,
    path = "/{store_id}/resources/{resource_id}",
    params(
        ("store_id" = String, Path, description = "The identifier of the store the resource is in"),
        ("resource_id" = String, Path, description = "The identifier of the resource"),
    ),
    responses(
        (status = 200, description = "The resource. Several return types are supported via content negotation.",content(
            (apidocs::StamJson = "application/json"),
            (String = "text/plain"),
        )),
        (status = 406, body = apidocs::ApiError, description = "This is returned if the requested content-type (Accept) could not be delivered", content_type = "application/json"),
        (status = 404, body = apidocs::ApiError, description = "An ApiError with name 'NotFound` is returned if the store or resource does not exist", content_type = "application/json"),
        (status = 404, body = apidocs::StamError, description = "Returned when a STAM error occurs", content_type = "application/json"),
    )
)]
/// Returns a text resource given its identifier
async fn get_resource(
    Path((store_id, resource_id)): Path<(String, String)>,
    storepool: State<Arc<StorePool>>,
    request: Request<Body>,
) -> Result<ApiResponse, ApiError> {
    storepool.map(&store_id, |store| match store.resource(resource_id) {
        None => Err(ApiError::NotFound("No such resource")),
        Some(resource) => match negotiate_content_type(request.headers(), &[CONTENT_TYPE_TEXT]) {
            Ok(CONTENT_TYPE_TEXT) => Ok(ApiResponse::Text(resource.text().to_string())),
            _ => Err(ApiError::NotAcceptable(
                "Accept headed could not be satisfied (try application/json)",
            )),
        },
    })
}

#[utoipa::path(
    get,
    path = "/{store_id}/resources/{resource_id}/{begin}/{end}",
    params(
        ("store_id" = String, Path, description = "The identifier of the store the resource is in"),
        ("resource_id" = String, Path, description = "The identifier of the resource"),
        ("begin" = isize, Path, description = "An integer indicating the begin offset in unicode points (0-indexed). This may be a negative integer for end-aligned cursors."),
        ("end" = isize, Path, description = "An integer indicating the non-inclusive end offset in unicode points (0-indexed). This may be a negative integer for end-aligned cursors. `-0` is a special value in this context, which means until the very end."),
    ),
    responses(
        (status = 200, description = "The resource. Several return types are supported via content negotation.",content(
            (apidocs::StamJson = "application/json"),
            (String = "text/plain"),
        )),
        (status = 406, body = apidocs::ApiError, description = "This is returned if the requested content-type (Accept) could not be delivered", content_type = "application/json"),
        (status = 404, body = apidocs::ApiError, description = "An ApiError with name 'NotFound` is returned if the store or resource does not exist", content_type = "application/json"),
        (status = 404, body = apidocs::StamError, description = "Returned when a STAM error occurs, such as invalid offsets.", content_type = "application/json"),
    )
)]
/// Returns an text selection given a resource identifier and an offset
async fn get_textselection(
    Path((store_id, resource_id, begin, end)): Path<(String, String, String, String)>,
    storepool: State<Arc<StorePool>>,
    request: Request<Body>,
) -> Result<ApiResponse, ApiError> {
    let offset = Offset::new(begin.as_str().try_into()?, end.as_str().try_into()?);
    storepool.map(&store_id, |store| match store.resource(resource_id) {
        None => Err(ApiError::NotFound("No such resource")),
        Some(resource) => {
            let textselection = resource.textselection(&offset)?;
            match negotiate_content_type(request.headers(), &[CONTENT_TYPE_JSON, CONTENT_TYPE_TEXT])
            {
                Ok(CONTENT_TYPE_JSON) => Ok(ApiResponse::RawJson(textselection.to_json_string()?)),
                Ok(CONTENT_TYPE_TEXT) => Ok(ApiResponse::Text(textselection.text().to_string())),
                _ => Err(ApiError::NotAcceptable(
                    "Accept headed could not be satisfied (try application/json)",
                )),
            }
        }
    })
}

fn negotiate_content_type(
    headers: &HeaderMap<HeaderValue>,
    offer_types: &[&'static str],
) -> Result<&'static str, ApiError> {
    if let Some(accept_types) = headers.get(axum::http::header::ACCEPT) {
        let mut match_accept_index = None;
        let mut matching_offer = None;
        for (i, accept_type) in accept_types
            .to_str()
            .map_err(|_| ApiError::NotAcceptable("Invalid Accept header"))
            .unwrap_or(CONTENT_TYPE_JSON)
            .split(",")
            .enumerate()
        {
            let accept_type = accept_type.split(";").next().unwrap();
            for offer_type in offer_types.iter() {
                if *offer_type == accept_type || accept_type == "*/*" {
                    if match_accept_index.is_none()
                        || (match_accept_index.is_some() && match_accept_index.unwrap() > i)
                    {
                        match_accept_index = Some(i);
                        matching_offer = Some(*offer_type);
                    }
                }
            }
        }
        if let Some(matching_offer) = matching_offer {
            Ok(matching_offer)
        } else {
            Err(ApiError::NotAcceptable("No matching content type on offer"))
        }
    } else {
        Ok(offer_types[0])
    }
}

fn run_query(
    store_id: &str,
    querystring: &str,
    use_variable: Option<&str>,
    storepool: State<Arc<StorePool>>,
    headers: &HeaderMap<HeaderValue>,
) -> Result<ApiResponse, ApiError> {
    let (query, _) = stam::Query::parse(querystring)?;
    if let Ok(CONTENT_TYPE_HTML) = negotiate_content_type(
        headers,
        &[CONTENT_TYPE_JSON, CONTENT_TYPE_HTML, CONTENT_TYPE_TEXT],
    ) {
        storepool.map(&store_id, |store| {
            let htmlwriter = HtmlWriter::new(&store, query, use_variable)
                .map_err(|e| ApiError::CustomNotFound(e))?;
            Ok(ApiResponse::Html(htmlwriter.to_string()))
        })
    } else if query.querytype().readonly() {
        storepool.map(&store_id, |store| match store.query(query) {
            Err(err) => Err(ApiError::StamError(err)),
            Ok(queryiter) => query_results(queryiter, headers, use_variable),
        })
    } else {
        storepool.map_mut(&store_id, |store| match store.query_mut(query) {
            Err(err) => Err(ApiError::StamError(err)),
            Ok(queryiter) => query_results(queryiter, headers, use_variable),
        })
    }
}

fn query_results(
    queryiter: QueryIter,
    headers: &HeaderMap<HeaderValue>,
    use_variable: Option<&str>,
) -> Result<ApiResponse, ApiError> {
    match negotiate_content_type(headers, &[CONTENT_TYPE_JSON, CONTENT_TYPE_TEXT]) {
        Ok(CONTENT_TYPE_JSON) => {
            if let Some(use_variable) = use_variable {
                //output only one variable
                let mut ser_results = Vec::new();
                for resultitems in queryiter {
                    if let Ok(result) = resultitems.get_by_name(use_variable) {
                        ser_results.push(result.to_json_value()?);
                    }
                }
                Ok(ApiResponse::JsonList(ser_results))
            } else {
                //output all variables
                let mut ser_results = Vec::new();
                for resultitems in queryiter {
                    let mut responsemap = BTreeMap::new();
                    for (i, (result, name)) in
                        resultitems.iter().zip(resultitems.names()).enumerate()
                    {
                        responsemap.insert(
                            name.map(|s| s.to_string()).unwrap_or(format!("{i}")),
                            result.to_json_value()?,
                        );
                    }
                    ser_results.push(responsemap);
                }
                Ok(ApiResponse::JsonMap(ser_results))
            }
        }
        Ok(CONTENT_TYPE_TEXT) => {
            for (i, resultitems) in queryiter.enumerate() {
                if i > 0 {
                    return Err(ApiError::NotAcceptable(
                        "Plain text can not be returned for queries with multiple results (try application/json instead)",
                    ));
                }
                if let Ok(result) = resultitems.get_by_name_or_first(use_variable) {
                    return Ok(ApiResponse::Text(result.text(Some("\t"))?.to_string()));
                } else {
                    return Err(ApiError::NotFound("No results found"));
                }
            }
            Err(ApiError::NotFound("No results found"))
        }
        _ => Err(ApiError::NotAcceptable(
            "Requested accept type can not be accommodated (try application/json instead)",
        )),
    }
}

impl From<StamError> for ApiError {
    fn from(e: StamError) -> ApiError {
        ApiError::StamError(e)
    }
}