Search in sources :

Example 61 with ResponseBuilder

use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.

the class RegistryManagerResource method getHtmlInfo.

@GET
@Produces(value = MediaType.TEXT_HTML)
public Response getHtmlInfo(@Context HttpHeaders headers) {
    ResponseBuilder rb = Response.ok(new Viewable("index", this));
    rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
    //        addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 62 with ResponseBuilder

use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.

the class RulesResource method adaptTo.

/**
     * Return the String representation of a recipe adapted to another format, e.g., Jena rules, SPARQL
     * CONSTRUCTs, Clerezza, SWRL, etc.
     * 
     * @param recipe
     *            {The ID of the recipe}
     * @param format
     *            {The canonical name of the class we want have back, e.g.,
     *            org.apache.stanbol.rules.base.api.Rule for Jena Rules}
     * @param headers
     *            {@link HttpHeaders}
     * @return <ul>
     *         <li>200: it works properly and the string representation of the recipe according to the format
     *         provided is returned</li>
     *         <li>204: the recipe does not exist in the store</li>
     *         <li>403: a class exists for the format provided but there is no adapter for that</li>
     *         <li>404: no class exists in the context for the format provided</li>
     *         <li>406: some error occurred while converting a rule of the recipe</li>
     *         <li>409: some atom of a rule in the recipe cannot be converted to the format provided</li>
     *         </ul>
     */
@GET
@Produces(value = { KRFormat.RDF_JSON })
@Path("/adapters/{recipe:.+}")
public Response adaptTo(@PathParam("recipe") String recipe, @QueryParam("format") String format, @Context HttpHeaders headers) {
    ResponseBuilder responseBuilder = null;
    Class<?> classToLoad;
    try {
        // ClassLoader loader = Thread.currentThread().getContextClassLoader();
        // classToLoad = loader.loadClass(format);
        classToLoad = Class.forName(format);
        URI uri = new URI(recipe);
        if (uri.getScheme() == null) {
            recipe = "urn:" + recipe;
            log.info("The recipe ID is a URI without scheme. The ID is set to " + recipe);
        }
        Recipe rcp = ruleStore.getRecipe(new IRI(recipe));
        RuleAdapter adapter = adapterManager.getAdapter(rcp, classToLoad);
        Object adaptedRecipe = adapter.adaptTo(rcp, classToLoad);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("recipe", rcp.getRecipeID().toString());
            jsonObject.put("adaptedTo", format);
            jsonObject.put("result", adaptedRecipe.toString());
        } catch (JSONException e) {
            log.error(e.getMessage(), e);
        }
        responseBuilder = Response.ok(jsonObject.toString());
    } catch (ClassNotFoundException e) {
        responseBuilder = Response.status(Status.NOT_FOUND);
        log.error(e.getMessage(), e);
    } catch (NoSuchRecipeException e) {
        responseBuilder = Response.status(Status.NO_CONTENT);
        log.error(e.getMessage(), e);
    } catch (RecipeConstructionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnavailableRuleObjectException e) {
        responseBuilder = Response.status(Status.NOT_ACCEPTABLE);
        log.error(e.getMessage(), e);
    } catch (RuleAtomCallExeption e) {
        responseBuilder = Response.status(Status.CONFLICT);
        log.error(e.getMessage(), e);
    } catch (UnsupportedTypeForExportException e) {
        responseBuilder = Response.status(Status.FORBIDDEN);
        log.error(e.getMessage(), e);
    } catch (URISyntaxException e) {
        responseBuilder = Response.status(Status.NOT_ACCEPTABLE);
        log.error(e.getMessage(), e);
    }
    //        addCORSOrigin(servletContext, responseBuilder, headers);
    return responseBuilder.build();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) JSONException(org.codehaus.jettison.json.JSONException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) JSONObject(org.codehaus.jettison.json.JSONObject) UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) JSONObject(org.codehaus.jettison.json.JSONObject) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) RuleAdapter(org.apache.stanbol.rules.base.api.RuleAdapter) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 63 with ResponseBuilder

use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.

the class RulesResource method removeRecipe.

/**
     * This method allows to delete a recipe or a rule from the store.<br/>
     * If the optional rule identifier id provided as second parameter that the rule is deleted. Otherwise it
     * is the whole recipe to be deleted.
     * 
     * @param recipe
     *            {@link String}
     * @param rule
     *            {@link String} - OPTIONAL
     * @param headers
     *            {@link HttpHeaders}
     * @return <ul>
     *         <li>200 - if either the recipe or the rule is deleted</li>
     *         <li>204 - it is not possible to delete the rule because the internal construction of the recipe
     *         failed</li>
     *         <li>404 - the rule does not exist</li>
     *         <li>412 - the recipe to which the rule belongs does not exist</li>
     *         <li>500 - if a {@link RecipeEliminationException} exception is thrown</li>
     *         </ul>
     */
@DELETE
@Path("/recipe/{recipe:.+}")
public Response removeRecipe(@PathParam("recipe") String recipe, @QueryParam("rule") String rule, @Context HttpHeaders headers) {
    ResponseBuilder responseBuilder;
    boolean stop = false;
    URI uri = null;
    try {
        uri = new URI(recipe);
    } catch (URISyntaxException e1) {
        log.error(e1.getMessage(), e1);
        responseBuilder = Response.status(Status.NOT_ACCEPTABLE);
        stop = true;
    }
    if (!stop) {
        if (uri != null && uri.getScheme() == null) {
            recipe = "urn:" + recipe;
            log.info("The recipe ID is a URI without scheme. The ID is set to " + recipe);
        }
        log.info("The recipe ID is : " + recipe);
        if (rule != null && !rule.isEmpty()) {
            Recipe rcp;
            try {
                rcp = ruleStore.getRecipe(new IRI(recipe));
                Rule rl = ruleStore.getRule(rcp, new IRI(rule));
                ruleStore.removeRule(rcp, rl);
            } catch (NoSuchRecipeException e) {
                log.error(e.getMessage(), e);
                responseBuilder = Response.status(Status.PRECONDITION_FAILED);
            } catch (RecipeConstructionException e) {
                log.error(e.getMessage(), e);
                responseBuilder = Response.status(Status.NO_CONTENT);
            } catch (NoSuchRuleInRecipeException e) {
                log.error(e.getMessage(), e);
                responseBuilder = Response.status(Status.NOT_FOUND);
            }
        } else {
            try {
                ruleStore.removeRecipe(new IRI(recipe));
            } catch (RecipeEliminationException e) {
                log.error(e.getMessage(), e);
                responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR);
            }
        }
    }
    responseBuilder = Response.ok();
    //        addCORSOrigin(servletContext, responseBuilder, headers);
    return responseBuilder.build();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RecipeEliminationException(org.apache.stanbol.rules.base.api.RecipeEliminationException) NoSuchRuleInRecipeException(org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) URISyntaxException(java.net.URISyntaxException) Rule(org.apache.stanbol.rules.base.api.Rule) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) URI(java.net.URI) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 64 with ResponseBuilder

use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.

the class RulesResource method listRecipes.

@GET
@Path("/recipe")
@Produces(value = { KRFormat.RDF_XML, KRFormat.TURTLE, KRFormat.OWL_XML, KRFormat.RDF_JSON, KRFormat.FUNCTIONAL_OWL, KRFormat.MANCHESTER_OWL })
public Response listRecipes(@Context HttpHeaders headers) {
    ResponseBuilder responseBuilder = null;
    try {
        RecipeList recipeList = getListRecipes();
        responseBuilder = Response.ok(recipeList);
    } catch (NoSuchRecipeException e) {
        log.error(e.getMessage(), e);
        responseBuilder = Response.status(Status.NOT_FOUND);
    } catch (RecipeConstructionException e) {
        log.error(e.getMessage(), e);
        responseBuilder = Response.status(Status.INTERNAL_SERVER_ERROR);
    }
    //        addCORSOrigin(servletContext, responseBuilder, headers);
    return responseBuilder.build();
}
Also used : RecipeList(org.apache.stanbol.rules.base.api.util.RecipeList) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 65 with ResponseBuilder

use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.

the class RefactorResource method performRefactoringLazyCreateGraph.

@GET
public Response performRefactoringLazyCreateGraph(@QueryParam("recipe") String recipe, @QueryParam("input-graph") String inputGraph, @QueryParam("output-graph") String outputGraph, @Context HttpHeaders headers) {
    log.info("recipe: {}", recipe);
    log.info("input-graph: {}", inputGraph);
    log.info("output-graph: {}", outputGraph);
    IRI recipeID = new IRI(recipe);
    IRI inputGraphID = new IRI(inputGraph);
    IRI outputGraphID = new IRI(outputGraph);
    // Refactorer semionRefactorer = semionManager.getRegisteredRefactorer();
    ResponseBuilder responseBuilder = null;
    try {
        refactorer.graphRefactoring(outputGraphID, inputGraphID, recipeID);
        responseBuilder = Response.ok();
    } catch (RefactoringException e) {
        // refactoring exceptions are re-thrown
        log.error(e.getMessage(), e);
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    } catch (NoSuchRecipeException e) {
        log.error(e.getMessage(), e);
        responseBuilder = Response.status(NOT_FOUND);
    }
    MediaType mediaType = MediaTypeUtil.getAcceptableMediaType(headers, null);
    if (mediaType != null)
        responseBuilder.header(HttpHeaders.CONTENT_TYPE, mediaType);
    //        addCORSOrigin(servletContext, responseBuilder, headers);
    return responseBuilder.build();
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) WebApplicationException(javax.ws.rs.WebApplicationException) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) MediaType(javax.ws.rs.core.MediaType) RefactoringException(org.apache.stanbol.rules.refactor.api.RefactoringException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) GET(javax.ws.rs.GET)

Aggregations

ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)175 GET (javax.ws.rs.GET)84 Produces (javax.ws.rs.Produces)81 Path (javax.ws.rs.Path)69 WebApplicationException (javax.ws.rs.WebApplicationException)47 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)40 IOException (java.io.IOException)30 MediaType (javax.ws.rs.core.MediaType)29 POST (javax.ws.rs.POST)23 IRI (org.semanticweb.owlapi.model.IRI)22 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)21 Response (javax.ws.rs.core.Response)20 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)19 URI (java.net.URI)18 Consumes (javax.ws.rs.Consumes)18 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)16 HashSet (java.util.HashSet)14 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)12