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   * Created by alessio on 28/09/15.
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  //		System.out.println(command);
54  //		// Validate preconditions and handle probe requests here, before body is consumed
55  //		// POST URI does not support GET, hence no tag and last modified
56  //		init(true, null);
57  //
58  //		Outcome outcome = getSession().sparqldelete().statements(statements).exec();
59  //
60  //		// Setup the response stream
61  //		final int httpStatus = outcome.getStatus().getHTTPStatus();
62  //		final Stream<Outcome> entity = Stream.create(outcome);
63  //
64  //		// Stream the result to the client
65  //		return newResponseBuilder(httpStatus, entity, Protocol.STREAM_OF_OUTCOMES).build();
66  //		return null;
67  	}
68  
69  }