v1.js 2.41 KB
Newer Older
Athanasios's avatar
Athanasios committed
1
2
const express = require("express");
const { check, validationResult, oneOf } = require("express-validator")
Athanasios's avatar
Athanasios committed
3
const { toLower } = require("../src/customSanitizers");
Athanasios's avatar
Athanasios committed
4
5
const { errorHandler } = require("../src/errorHandler");
const checkFactory = require("../src/validationCheckFactory");
Athanasios's avatar
Athanasios committed
6
7
8
9
10
const { getSceneHandler } = require("../src/getSceneHandler");
const { getCapabilitiesHandler } = require("../src/getCapabilitiesHandler");
const redirectTo = require("../src/redirectTo");
const assets = require("../src/assets");
const bb = require("../src/boundingbox");
Athanasios's avatar
Athanasios committed
11
12
13
14
15
16
17
18

let router = express.Router();

const checks = [
  ...checkFactory.commonChecks(),
  check("request")
    .exists().withMessage("MissingParameterValue")
    .customSanitizer(toLower)
Athanasios's avatar
Athanasios committed
19
    .isIn(["getscene", "getcapabilities"]).withMessage("InvalidParameterValue")
Athanasios's avatar
Athanasios committed
20
21
];

Athanasios's avatar
Athanasios committed
22
router.route("/").get(checks, (req, res) => {
Athanasios's avatar
Athanasios committed
23

Athanasios's avatar
Athanasios committed
24
25
26
27
28
29
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    let exception = errorHandler(errors);
    res.set("Content-Type", "application/xml");
    return res.status(400).send(exception);
  }
Athanasios's avatar
Athanasios committed
30

Athanasios's avatar
Athanasios committed
31
  redirectTo(req.query.request, req, res);
Athanasios's avatar
Athanasios committed
32

Athanasios's avatar
Athanasios committed
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
});

const capabilitiesChecks = [
  check("request")
    .exists().withMessage("MissingParameterValue")
    .customSanitizer(toLower)
    .equals("getcapabilities").withMessage("InvalidParameterValue")
];

router.route("/capabilities").get(capabilitiesChecks, (req, res) => {

  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    let exception = errorHandler(errors);
    res.set("Content-Type", "application/xml");
    return res.status(400).send(exception);
  }

  getCapabilitiesHandler(req, res);

});

const sceneChecks = [
  check("boundingbox")
    .exists().withMessage("MissingParameterValue")
    .matches(/^((\-?\d+(\.\d+)?),){3}(\-?\d+(\.\d+)?)$/).withMessage("InvalidParameterValue")
    .custom(value => bb.isValid(value)).withMessage("InvalidParameterValue"),
  check("layers")
    .exists().withMessage("MissingParameterValue")
    .custom(value => assets.allLayersExist(value)).withMessage("UnknownLayer")
];

router.route("/scene").get(sceneChecks, (req, res) => {

  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    let exception = errorHandler(errors);
    res.set("Content-Type", "application/xml");
    return res.status(400).send(exception);
  }

  getSceneHandler(req, res);

});
Athanasios's avatar
Athanasios committed
77
78
79


module.exports = router;