Skip to content

Commit 35b60b3

Browse files
author
nicola
committed
cleaning handlers/post.js code
1 parent 45e3d09 commit 35b60b3

2 files changed

Lines changed: 100 additions & 83 deletions

File tree

‎handlers/post.js‎

Lines changed: 98 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,106 +17,122 @@ var rdfVocab = require('../vocab/rdf.js');
1717

1818
function handler(req, res) {
1919
var options = req.app.locals.ldp;
20-
if (req.is('application/sparql')) {
20+
var contentType = req.get('content-type');
21+
22+
// Handle SPARQL query
23+
if (contentType === 'application/sparql') {
2124
debug("POST -- Handling sparql query");
2225
return patch.handler(req, res);
23-
} else if (req.is('application/sparql-update')) {
26+
}
27+
28+
// Handle SPARQL-update query
29+
if (contentType === 'application/sparql-update') {
2430
debug("POST -- Handling sparql-update query");
2531
return patch.handler(req, res);
32+
}
33+
34+
// Error on invalid content-type
35+
if (contentType != 'text/turtle' &&
36+
contentType != 'text/n3' &&
37+
contentType != 'application/rdf+xml') {
38+
//TODO Handle json and nquad content types
39+
debug("POST -- Invalid Content Type: " + contentType);
40+
return res.status(415).send("Invalid Content Type");
41+
}
42+
43+
44+
var containerPath = file.uriToFilename(req.path, options.base);
45+
debug("POST -- Container path: " + containerPath);
46+
47+
// Container not found/invalid
48+
if (metadata.isMetadataFile(containerPath)) {
49+
debug("POST -- Invalid container.");
50+
return res.sendStatus(404);
51+
}
52+
53+
// Not a container
54+
if (containerPath[containerPath.length - 1] != '/') {
55+
debug("POST -- Requested resource is not a container");
56+
return res.set('Allow', 'GET,HEAD,PUT,DELETE')
57+
.sendStatus(405);
58+
}
59+
60+
debug("POST -- Content Type: " + contentType);
61+
62+
var slug = req.get('Slug');
63+
var resourceMetadata = header.parseMetadataFromHeader(req.get('Link'));
64+
var resourcePath = container.createResourceUri(
65+
options,
66+
containerPath,
67+
slug,
68+
resourceMetadata.isBasicContainer);
69+
70+
if (resourcePath === null) {
71+
container.releaseResourceUri(options, resourcePath);
72+
debug("POST -- URI already exists or in use");
73+
return res.sendStatus(400);
74+
}
75+
var resourceGraph = $rdf.graph();
76+
77+
// Get the request text
78+
// TODO make sure correct text is selected
79+
var requestText = req.convertedText || req.text;
80+
81+
var resourceBaseUri;
82+
try {
83+
resourceBaseUri = file.filenameToBaseUri(
84+
resourcePath,
85+
options.uri,
86+
options.base);
87+
88+
$rdf.parse(
89+
requestText,
90+
resourceGraph,
91+
resourceBaseUri,
92+
'text/turtle');
93+
} catch (parseErr) {
94+
debug("POST -- Error parsing resource: " + parseErr);
95+
container.releaseResourceUri(options, resourcePath);
96+
return res.sendStatus(400);
97+
}
98+
99+
header.addLinks(res, resourceMetadata);
100+
101+
if (resourceMetadata.isBasicContainer) {
102+
resourcePath += '/';
103+
resourceBaseUri += '/';
104+
container.createNewContainer(
105+
options,
106+
resourcePath,
107+
resourceGraph,
108+
containerCallback);
26109
} else {
27-
var containerPath = file.uriToFilename(req.path, options.base);
28-
debug("POST -- Container path: " + containerPath);
29-
if (metadata.isMetadataFile(containerPath)) {
30-
debug("POST -- Invalid container.");
31-
return res.status(404).send();
32-
}
33-
if (S(containerPath).endsWith('/')) {
34-
var contentType = "";
35-
if (req.is('text/turtle'))
36-
contentType = 'text/turtle';
37-
else if (req.is('text/n3'))
38-
contentType = 'text/n3';
39-
else if (req.is('application/rdf+xml'))
40-
contentType = 'application/rdf+xml';
41-
else {
42-
//TODO Handle json and nquad content types
43-
debug("POST -- Invalid Content Type");
44-
return res.status(415).send("Invalid Content Type");
45-
}
46-
debug("POST -- Content Type: " + contentType);
47-
48-
var slug = req.get('Slug');
49-
var resourceMetadata = header.parseMetadataFromHeader(req.get('Link'));
50-
var resourcePath = container.createResourceUri(options, containerPath, slug,
51-
resourceMetadata.isBasicContainer);
52-
var resourceGraph = $rdf.graph();
53-
54-
if (resourcePath === null) {
55-
container.releaseResourceUri(options, resourcePath);
56-
debug("POST -- URI already exists or in use");
57-
return res.sendStatus(400);
58-
}
59-
60-
// Get the request text
61-
// TODO make sure correct text is selected
62-
var requestText;
63-
if (req.convertedText) {
64-
requestText = req.convertedText;
65-
} else {
66-
requestText = req.text;
67-
}
68-
69-
var resourceBaseUri;
70-
try {
71-
resourceBaseUri = file.filenameToBaseUri(resourcePath, options.uri, options.base);
72-
$rdf.parse(requestText, resourceGraph,
73-
resourceBaseUri, 'text/turtle');
74-
} catch (parseErr) {
75-
debug("POST -- Error parsing resource: " + parseErr);
76-
container.releaseResourceUri(options, resourcePath);
77-
return res.sendStatus(400);
78-
}
79-
80-
header.addLinks(res, resourceMetadata);
81-
82-
if (resourceMetadata.isBasicContainer) {
83-
resourcePath += '/';
84-
resourceBaseUri += '/';
85-
container.createNewContainer(options, resourcePath, resourceGraph,
86-
containerCallback);
87-
} else {
88-
container.createNewResource(options, resourcePath,
89-
resourceGraph, resourceCallback);
90-
}
91-
} else {
92-
debug("POST -- Requested resource is not a container");
93-
return res.set('Allow', 'GET,HEAD,PUT,DELETE').sendStatus(405);
94-
}
110+
container.createNewResource(
111+
options,
112+
resourcePath,
113+
resourceGraph,
114+
resourceCallback);
95115
}
96116

97117
function containerCallback(err) {
98118
if (err) {
99-
debug(
100-
"POST -- Error creating new container: " + err);
119+
debug("POST -- Error creating new container: " + err);
101120
return res.sendStatus(500);
102-
} else {
103-
debug(
104-
"POST -- Created new container " + resourceBaseUri);
105-
res.set('Location', resourceBaseUri);
106-
return res.sendStatus(201);
107121
}
122+
debug("POST -- Created new container " + resourceBaseUri);
123+
res.set('Location', resourceBaseUri);
124+
return res.sendStatus(201);
108125
}
109126

110127
function resourceCallback(err) {
111128
if (err) {
112-
debug(
113-
"POST -- Error creating resource: " + err);
129+
debug("POST -- Error creating resource: " + err);
114130
return res.sendStatus(500);
115-
} else {
116-
res.set('Location', resourceBaseUri);
117-
return res.sendStatus(201);
118131
}
132+
res.set('Location', resourceBaseUri);
133+
return res.sendStatus(201);
119134
}
120135
}
121136

137+
122138
exports.handler = handler;

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
"start": "node bin/ldnode.js",
5959
"test": "DEBUG='ldnode:*' ./node_modules/mocha/bin/mocha ./test/*.js",
6060
"test-acl": "./node_modules/mocha/bin/mocha ./test/secure-test.js",
61-
"test-params": "./node_modules/mocha/bin/mocha ./test/params.js"
61+
"test-params": "./node_modules/mocha/bin/mocha ./test/params.js",
62+
"test-http": "./node_modules/mocha/bin/mocha ./test/test.js"
6263
},
6364
"bin": {
6465
"ldnode": "./bin/ldnode.js"

0 commit comments

Comments
 (0)