Search in sources :

Example 1 with ReasoningServiceInputManager

use of org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputManager in project stanbol by apache.

the class RESTInputFactory method createInputManager.

@Override
public ReasoningServiceInputManager createInputManager(Map<String, List<String>> parameters) {
    ReasoningServiceInputManager inmgr = new SimpleInputManager();
    String scope = null;
    String session = null;
    for (Entry<String, List<String>> entry : parameters.entrySet()) {
        if (entry.getKey().equals("url")) {
            if (!entry.getValue().isEmpty()) {
                // We keep only the first value
                // XXX (make sense support multiple values?)
                inmgr.addInputProvider(new UrlInputProvider(entry.getValue().iterator().next()));
            } else {
                // Parameter exists with no value
                log.error("Parameter 'url' must have a value!");
                throw new WebApplicationException(Response.Status.BAD_REQUEST);
            }
        } else if (entry.getKey().equals("file")) {
            if (!entry.getValue().isEmpty()) {
                // We keep only the first value
                // FIXME We create the file once again...
                inmgr.addInputProvider(new FileInputProvider(new File(entry.getValue().iterator().next())));
            } else {
                // Parameter exists with no value
                log.error("Parameter 'url' must have a value!");
                throw new WebApplicationException(Response.Status.BAD_REQUEST);
            }
        } else if (entry.getKey().equals("scope")) {
            if (!entry.getValue().isEmpty()) {
                scope = entry.getValue().iterator().next();
            } else {
                // Parameter exists with no value
                log.error("Parameter 'scope' must have a value!");
                throw new WebApplicationException(Response.Status.BAD_REQUEST);
            }
        } else if (entry.getKey().equals("session")) {
            if (!entry.getValue().isEmpty()) {
                session = entry.getValue().iterator().next();
            } else {
                // Parameter exists with no value
                log.error("Parameter 'session' must have a value!");
                throw new WebApplicationException(Response.Status.BAD_REQUEST);
            }
        } else if (entry.getKey().equals("recipe")) {
            if (!entry.getValue().isEmpty()) {
                inmgr.addInputProvider(new RecipeInputProvider(rStore, adapterManager, entry.getValue().iterator().next()));
            } else {
                // Parameter exists with no value
                log.error("Parameter 'recipe' must have a value!");
                throw new WebApplicationException(Response.Status.BAD_REQUEST);
            }
        }
    }
    if (scope != null) {
        inmgr.addInputProvider(new OntologyManagerInputProvider(onm, sessionManager, scope, session));
    }
    return inmgr;
}
Also used : ReasoningServiceInputManager(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputManager) WebApplicationException(javax.ws.rs.WebApplicationException) FileInputProvider(org.apache.stanbol.reasoners.web.input.provider.impl.FileInputProvider) OntologyManagerInputProvider(org.apache.stanbol.reasoners.web.input.provider.impl.OntologyManagerInputProvider) List(java.util.List) UrlInputProvider(org.apache.stanbol.reasoners.web.input.provider.impl.UrlInputProvider) RecipeInputProvider(org.apache.stanbol.reasoners.web.input.provider.impl.RecipeInputProvider) File(java.io.File)

Example 2 with ReasoningServiceInputManager

use of org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputManager in project stanbol by apache.

the class ReasoningServiceTaskResource method prepareInput.

/**
     * Binds the request parameters to a list of {@see ReasoningServiceInputProvider}s, and fed a {@see
     * SimpleInputManager}. TODO In the future we may want to decouple this process from this
     * resource/submodule.
     * 
     * @return
     */
private ReasoningServiceInputManager prepareInput() {
    ReasoningServiceInputManager inmgr = new SimpleInputManager();
    String scope = null;
    String session = null;
    if (file != null) {
        inmgr.addInputProvider(new ByteArrayInputProvider(file.getContent()));
    }
    for (Entry<String, List<String>> entry : this.parameters.entrySet()) {
        if (entry.getKey().equals("url")) {
            if (!entry.getValue().isEmpty()) {
                // We keep only the first value
                // XXX (make sense support multiple values?)
                inmgr.addInputProvider(new UrlInputProvider(entry.getValue().iterator().next()));
                // We remove it form the additional parameter list
                this.parameters.remove("url");
            } else {
                // Parameter exists with no value
                log.error("Parameter 'url' must have a value!");
                throw new WebApplicationException(Response.Status.BAD_REQUEST);
            }
        } else //            } 
        if (entry.getKey().equals("scope")) {
            if (!entry.getValue().isEmpty()) {
                scope = entry.getValue().iterator().next();
            } else {
                // Parameter exists with no value
                log.error("Parameter 'scope' must have a value!");
                throw new WebApplicationException(Response.Status.BAD_REQUEST);
            }
        } else if (entry.getKey().equals("session")) {
            if (!entry.getValue().isEmpty()) {
                session = entry.getValue().iterator().next();
            } else {
                // Parameter exists with no value
                log.error("Parameter 'session' must have a value!");
                throw new WebApplicationException(Response.Status.BAD_REQUEST);
            }
        } else if (entry.getKey().equals("recipe")) {
            if (!entry.getValue().isEmpty()) {
                inmgr.addInputProvider(new RecipeInputProvider(ruleStore, adapterManager, entry.getValue().iterator().next()));
                // We remove it form the additional parameter list
                this.parameters.remove("url");
            } else {
                // Parameter exists with no value
                log.error("Parameter 'recipe' must have a value!");
                throw new WebApplicationException(Response.Status.BAD_REQUEST);
            }
        }
    }
    if (scope != null) {
        inmgr.addInputProvider(new OntologyManagerInputProvider(onm, sessionManager, scope, session));
        this.parameters.remove("scope");
        this.parameters.remove("session");
    }
    return inmgr;
}
Also used : SimpleInputManager(org.apache.stanbol.reasoners.web.input.impl.SimpleInputManager) ReasoningServiceInputManager(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputManager) WebApplicationException(javax.ws.rs.WebApplicationException) OntologyManagerInputProvider(org.apache.stanbol.reasoners.web.input.provider.impl.OntologyManagerInputProvider) List(java.util.List) ArrayList(java.util.ArrayList) UrlInputProvider(org.apache.stanbol.reasoners.web.input.provider.impl.UrlInputProvider) ByteArrayInputProvider(org.apache.stanbol.reasoners.web.input.provider.impl.ByteArrayInputProvider) RecipeInputProvider(org.apache.stanbol.reasoners.web.input.provider.impl.RecipeInputProvider)

Example 3 with ReasoningServiceInputManager

use of org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputManager in project stanbol by apache.

the class ReasoningServiceTaskResource method processBackgroundRequest.

/**
     * Process a background request. This service use the Stanbol Commons Jobs API to start a background job.
     * Returns 201 on success, with HTTP header Location pointing to the Job resource.
     * 
     * @return
     */
private Response processBackgroundRequest() {
    // If parameters is empty it's a bad request...
    if (this.parameters.isEmpty()) {
        log.error("Cannot start job without input parameters... sending BAD REQUEST");
        throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }
    String target = getTarget();
    // Setup the input
    ReasoningServiceInputManager imngr = prepareInput();
    // The service executor
    ReasoningServiceExecutor executor = new ReasoningServiceExecutor(tcManager, imngr, getCurrentService(), getCurrentTask(), target, parameters);
    String jid = getJobManager().execute(executor);
    URI location = URI.create(getPublicBaseUri() + "jobs/" + jid);
    this.jobLocation = location.toString();
    /**
         * If everything went well, we return 201 Created We include the header Location: with the Job URL
         */
    Viewable view = new Viewable("created", this);
    return Response.created(location).entity(view).build();
}
Also used : ReasoningServiceExecutor(org.apache.stanbol.reasoners.web.utils.ReasoningServiceExecutor) ReasoningServiceInputManager(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputManager) WebApplicationException(javax.ws.rs.WebApplicationException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) URI(java.net.URI)

Example 4 with ReasoningServiceInputManager

use of org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputManager in project stanbol by apache.

the class ReasoningServiceTaskResource method processRealTimeRequest.

/**
     * Process a real-time operation. Returns 200 when the process is ready, 500 if some error occurs
     * 
     * @return
     */
private Response processRealTimeRequest() {
    // page
    if (this.parameters.isEmpty() && file == null) {
        log.debug("no parameters no input file, show default index page");
        // return Response.ok(new Viewable("index", this)).build();
        ResponseBuilder rb = Response.ok(new Viewable("index", this));
        rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
        return rb.build();
    }
    try {
        String target = getTarget();
        // Setup the input
        ReasoningServiceInputManager imngr = prepareInput();
        // The service executor
        ReasoningServiceExecutor executor = new ReasoningServiceExecutor(tcManager, imngr, getCurrentService(), getCurrentTask(), target, parameters);
        ReasoningServiceResult<?> result = executor.call();
        return new ResponseTaskBuilder(new ReasoningTaskResult(uriInfo, headers)).build(result);
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : ReasoningServiceExecutor(org.apache.stanbol.reasoners.web.utils.ReasoningServiceExecutor) ReasoningServiceInputManager(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputManager) WebApplicationException(javax.ws.rs.WebApplicationException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ResponseTaskBuilder(org.apache.stanbol.reasoners.web.utils.ResponseTaskBuilder) WebApplicationException(javax.ws.rs.WebApplicationException) UnboundReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.UnboundReasoningServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

WebApplicationException (javax.ws.rs.WebApplicationException)4 ReasoningServiceInputManager (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceInputManager)4 List (java.util.List)2 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)2 OntologyManagerInputProvider (org.apache.stanbol.reasoners.web.input.provider.impl.OntologyManagerInputProvider)2 RecipeInputProvider (org.apache.stanbol.reasoners.web.input.provider.impl.RecipeInputProvider)2 UrlInputProvider (org.apache.stanbol.reasoners.web.input.provider.impl.UrlInputProvider)2 ReasoningServiceExecutor (org.apache.stanbol.reasoners.web.utils.ReasoningServiceExecutor)2 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 UnboundReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.UnboundReasoningServiceException)1 SimpleInputManager (org.apache.stanbol.reasoners.web.input.impl.SimpleInputManager)1 ByteArrayInputProvider (org.apache.stanbol.reasoners.web.input.provider.impl.ByteArrayInputProvider)1 FileInputProvider (org.apache.stanbol.reasoners.web.input.provider.impl.FileInputProvider)1 ResponseTaskBuilder (org.apache.stanbol.reasoners.web.utils.ResponseTaskBuilder)1