1 package eu.fbk.knowledgestore.server.http.jaxrs;
2
3 import eu.fbk.knowledgestore.OperationException;
4 import eu.fbk.knowledgestore.Outcome;
5 import eu.fbk.knowledgestore.data.Stream;
6 import eu.fbk.knowledgestore.internal.jaxrs.Protocol;
7 import eu.fbk.knowledgestore.server.http.CustomConfig;
8 import eu.fbk.rdfpro.*;
9 import org.codehaus.enunciate.jaxrs.TypeHint;
10 import org.openrdf.model.Statement;
11 import org.openrdf.rio.RDFHandlerException;
12
13 import javax.annotation.Nullable;
14 import javax.ws.rs.*;
15 import javax.ws.rs.core.Response;
16
17
18
19
20
21 @Path("/" + Protocol.PATH_CUSTOM)
22 public class Custom extends Resource {
23
24 @POST
25 @Path("/{customID}")
26 @Produces(Protocol.MIME_TYPES_ALL)
27 @Consumes(Protocol.MIME_TYPES_RDF)
28 @TypeHint(Stream.class)
29 public Response post(@PathParam("customID") String customID, @Nullable final Stream<Statement> statements) throws OperationException {
30
31 init(true, null);
32
33 CustomConfig customConfig = this.getApplication().getCustomConfigs().get(customID);
34 if (customConfig == null) {
35 throw new OperationException(newOutcome(Outcome.Status.ERROR_INVALID_INPUT, "Custom operation %s not found", customID));
36 }
37 if (statements == null) {
38 throw new OperationException(newOutcome(Outcome.Status.ERROR_INVALID_INPUT, "No statements"));
39 }
40
41 String command = customConfig.getCommand();
42
43 RDFSource source = RDFSources.wrap(statements);
44 try {
45 RDFProcessor processor = RDFProcessors.parse(true, command);
46 processor.apply(source, RDFHandlers.NIL, 1);
47 } catch (RDFHandlerException ex) {
48 throw new OperationException(newOutcome(Outcome.Status.ERROR_UNEXPECTED, "%s", ex.getMessage()), ex);
49 }
50
51 return newResponseBuilder(Response.Status.OK, null, null).build();
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 }
68
69 }