|
1 | | -/*jslint node: true*/ |
2 | | -"use strict"; |
3 | | - |
4 | | -var path = require('path'); |
5 | | -var $rdf = require('rdflib'); |
6 | | -var S = require('string'); |
| 1 | +exports.handler = handler; |
7 | 2 |
|
8 | 3 | var debug = require('../debug').handlers; |
9 | 4 | var utils = require('../utils.js'); |
10 | 5 | var header = require('../header.js'); |
11 | | -var metadata = require('../metadata.js'); |
12 | 6 | var patch = require('./patch.js'); |
| 7 | +var error = require('../http-error'); |
13 | 8 |
|
14 | | -var ldpVocab = require('../vocab/ldp.js'); |
15 | | -var rdfVocab = require('../vocab/rdf.js'); |
16 | | -var HttpError = require('../http-error'); |
17 | | - |
18 | | -function handler(req, res, next) { |
19 | | - var ldp = req.app.locals.ldp; |
20 | | - var contentType = req.get('content-type'); |
21 | | - |
22 | | - // Handle SPARQL query |
23 | | - if (contentType === 'application/sparql') { |
24 | | - debug("POST -- Handling sparql query"); |
25 | | - return patch.handler(req, res, next); |
26 | | - } |
27 | | - |
28 | | - // Handle SPARQL-update query |
29 | | - if (contentType === 'application/sparql-update') { |
30 | | - debug("POST -- Handling sparql-update query"); |
31 | | - return patch.handler(req, res, next); |
32 | | - } |
33 | | - |
34 | | - // Error on invalid content-type |
35 | | - if (contentType != 'text/turtle' && |
36 | | - contentType != 'text/n3' && |
37 | | - contentType != 'application/rdf+xml' && |
38 | | - contentType != 'application/ld+json' && |
39 | | - contentType != 'application/nquads' && |
40 | | - contentType != 'application/n-quads') { |
41 | | - debug("POST -- Invalid Content Type: " + contentType); |
42 | | - |
43 | | - return next(new HttpError({ |
44 | | - status: 415, |
45 | | - message: "Invalid Content Type" |
46 | | - })); |
47 | | - } |
48 | | - |
49 | | - var root = !ldp.idp ? ldp.root : ldp.root + req.hostname + '/'; |
50 | | - var containerPath = utils.uriToFilename(req.path, root); |
51 | | - debug("POST -- Container path: " + containerPath); |
| 9 | +function handler (req, res, next) { |
| 10 | + var ldp = req.app.locals.ldp; |
| 11 | + var contentType = req.get('content-type'); |
52 | 12 |
|
53 | | - // Not a container |
54 | | - if (containerPath[containerPath.length - 1] != '/') { |
55 | | - debug("POST -- Requested resource is not a container"); |
56 | | - res.set('Allow', 'GET,HEAD,PUT,DELETE'); |
| 13 | + // Handle SPARQL(-update?) query |
| 14 | + if (contentType === 'application/sparql' || |
| 15 | + contentType === 'application/sparql-update') { |
57 | 16 |
|
58 | | - return next(new HttpError({ |
59 | | - status: 405, |
60 | | - message: "Requested resource is not a container" |
61 | | - })); |
62 | | - } |
| 17 | + debug('POST -- Handling sparql query via PATCH'); |
| 18 | + return patch.handler(req, res, next); |
| 19 | + } |
63 | 20 |
|
64 | | - debug("POST -- Content Type: " + contentType); |
| 21 | + var containerPath = req.path; |
| 22 | + debug('POST -- On parent: ' + containerPath); |
65 | 23 |
|
66 | | - var resourceMetadata = header.parseMetadataFromHeader(req.get('Link')); |
| 24 | + // Not a container |
| 25 | + if (containerPath[containerPath.length - 1] !== '/') { |
| 26 | + debug('POST -- Path is not a container'); |
| 27 | + res.set('Allow', 'GET,HEAD,PUT,DELETE'); |
| 28 | + return next(error(405, 'Requested resource is not a container')); |
| 29 | + } |
67 | 30 |
|
68 | | - // Create resource URI |
69 | | - var resourcePath = ldp.createResourceUri( |
70 | | - containerPath, |
71 | | - req.get('Slug'), |
72 | | - resourceMetadata.isBasicContainer); |
| 31 | + debug('POST -- Content Type: ' + contentType); |
73 | 32 |
|
74 | | - // Check if URI is already in use |
75 | | - if (resourcePath === null) { |
76 | | - ldp.releaseResourceUri(resourcePath); |
77 | | - debug("POST -- URI already exists or in use"); |
| 33 | + var linkHeader = header.parseMetadataFromHeader(req.get('Link')); |
78 | 34 |
|
79 | | - return next(new HttpError({ |
80 | | - status: 400, |
81 | | - message: "URI already exists or in use" |
82 | | - })); |
| 35 | + // TODO: possibly package this in ldp.post |
| 36 | + ldp.getAvailablePath(req.hostname, containerPath, req.get('Slug'), function (resourcePath) { |
| 37 | + debug('POST -- Will create at: ' + resourcePath); |
| 38 | + var meta = ''; |
| 39 | + if (linkHeader.isBasicContainer) { |
| 40 | + resourcePath += '/'; |
| 41 | + meta = ldp.suffixMeta; |
83 | 42 | } |
84 | 43 |
|
85 | | - // Creating a graph and add the req text |
86 | | - var resourceGraph = $rdf.graph(); |
87 | | - var requestText = req.convertedText || req.text; |
88 | | - var uri = utils.uriBase(req); |
89 | | - var resourceBaseUri = utils.filenameToBaseUri( |
90 | | - resourcePath, |
91 | | - uri, |
92 | | - root); |
| 44 | + ldp.put(req.hostname, resourcePath + meta, req.text, function (err) { |
| 45 | + if (err) { |
| 46 | + next(err); |
| 47 | + } |
93 | 48 |
|
94 | | - try { |
95 | | - $rdf.parse( |
96 | | - requestText, |
97 | | - resourceGraph, |
98 | | - resourceBaseUri, |
99 | | - contentType); |
100 | | - } catch (parseErr) { |
101 | | - debug("POST -- Error parsing resource: " + parseErr); |
102 | | - ldp.releaseResourceUri(resourcePath); |
103 | | - return next(new HttpError({ |
104 | | - status: 400, |
105 | | - message: "Error parsing resource" |
106 | | - })); |
107 | | - } |
108 | | - |
109 | | - // Add header link to the resource |
110 | | - header.addLinks(res, resourceMetadata); |
111 | | - |
112 | | - // Finally, either create the new resource or container |
113 | | - if (resourceMetadata.isBasicContainer) { |
114 | | - resourcePath += '/'; |
115 | | - resourceBaseUri += '/'; |
116 | | - ldp.createNewContainer( |
117 | | - uri, |
118 | | - resourcePath, |
119 | | - resourceGraph, |
120 | | - containerCallback); |
121 | | - } else { |
122 | | - ldp.createNewResource( |
123 | | - req.hostname, |
124 | | - uri, |
125 | | - resourcePath, |
126 | | - resourceGraph, |
127 | | - resourceCallback); |
128 | | - } |
129 | | - |
130 | | - function containerCallback(err) { |
131 | | - if (err) { |
132 | | - debug("POST -- Error creating new container: " + err.message); |
133 | | - return next(err); |
134 | | - } |
135 | | - |
136 | | - debug("POST -- Created new container " + resourceBaseUri); |
137 | | - res.set('Location', resourceBaseUri); |
138 | | - res.sendStatus(201); |
139 | | - return next(); |
140 | | - } |
141 | | - |
142 | | - function resourceCallback(err) { |
143 | | - if (err) { |
144 | | - debug("POST -- Error creating resource: " + err.message); |
145 | | - return next(err); |
146 | | - } |
147 | | - res.set('Location', resourceBaseUri); |
148 | | - res.sendStatus(201); |
149 | | - return next(); |
150 | | - } |
| 49 | + header.addLinks(res, linkHeader); |
| 50 | + res.set('Location', resourcePath); |
| 51 | + res.sendStatus(201); |
| 52 | + }); |
| 53 | + }); |
151 | 54 | } |
152 | | - |
153 | | -exports.handler = handler; |
0 commit comments