Search in sources :

Example 21 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class EntityhubRootResource method getSymbolMappings.

@GET
@Path("mapping/symbol")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response getSymbolMappings(@QueryParam("id") String symbol, @Context HttpHeaders headers) throws WebApplicationException {
    log.debug("getSymbolMappings() POST Request > symbol: {} > accept: {}", symbol, headers.getAcceptableMediaTypes());
    Set<String> supported = new HashSet<String>(JerseyUtils.REPRESENTATION_SUPPORTED_MEDIA_TYPES);
    supported.add(TEXT_HTML);
    MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, APPLICATION_JSON_TYPE);
    if (symbol == null || symbol.isEmpty()) {
        //if HTML -> print the docu of the restfull service
        if (TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            ResponseBuilder rb = Response.ok(new Viewable("mapping_symbol", this));
            rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
            //addCORSOrigin(servletContext, rb, headers);
            return rb.build();
        } else {
            return Response.status(Status.BAD_REQUEST).entity("No symbol given. Missing parameter id.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    //Entityhub entityhub = ContextHelper.getServiceFromContext(Entityhub.class, servletContext);
    Collection<Entity> mappings;
    try {
        mappings = entityhub.getMappingsByTarget(symbol);
    } catch (EntityhubException e) {
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    }
    if (mappings == null || mappings.isEmpty()) {
        return Response.status(Status.NOT_FOUND).entity("No mapping found for symbol '" + symbol + "'.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
    } else {
        // TODO: Implement Support for list of Signs, Representations and Strings
        // For now use a pseudo QueryResultList
        QueryResultList<Entity> mappingResultList = new QueryResultListImpl<Entity>(null, mappings, Entity.class);
        ResponseBuilder rb = Response.ok(mappingResultList);
        rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    }
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) WebApplicationException(javax.ws.rs.WebApplicationException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 22 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class EntityhubRootResource method findEntity.

@POST
@Path("/find")
@Produces({ APPLICATION_JSON, RDF_XML, N3, TURTLE, X_TURTLE, RDF_JSON, N_TRIPLE, TEXT_HTML })
public Response findEntity(@FormParam(value = "name") String name, @FormParam(value = "field") String parsedField, @FormParam(value = "lang") String language, @FormParam(value = "limit") Integer limit, @FormParam(value = "offset") Integer offset, // solution!
@FormParam(value = "select") String select, @FormParam(value = "ldpath") String ldpath, @Context HttpHeaders headers) {
    log.debug("/find Request");
    final MediaType acceptedMediaType = getAcceptableMediaType(headers, ENTITY_SUPPORTED_MEDIA_TYPE_INCL_HTML, MediaType.APPLICATION_JSON_TYPE);
    if (name == null || name.isEmpty()) {
        if (acceptedMediaType.isCompatible(TEXT_HTML_TYPE)) {
            //return HTML docu
            ResponseBuilder rb = Response.ok(new Viewable("find", this));
            rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
            //addCORSOrigin(servletContext, rb, headers);
            return rb.build();
        } else {
            return Response.status(Status.BAD_REQUEST).entity("The name must not be null nor empty for find requests. Missing parameter name.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    } else {
        final String property;
        if (parsedField == null) {
            property = DEFAULT_FIND_FIELD;
        } else {
            parsedField = parsedField.trim();
            if (parsedField.isEmpty()) {
                property = DEFAULT_FIND_FIELD;
            } else {
                property = nsPrefixService.getFullName(parsedField);
                if (property == null) {
                    String messsage = String.format("The prefix '%s' of the parsed field '%' is not " + "mapped to any namespace. Please parse the full URI instead!\n", NamespaceMappingUtils.getPrefix(parsedField), parsedField);
                    return Response.status(Status.BAD_REQUEST).entity(messsage).header(HttpHeaders.ACCEPT, acceptedMediaType).build();
                }
            }
        }
        FieldQuery query = JerseyUtils.createFieldQueryForFindRequest(name, property, language, limit == null || limit < 1 ? DEFAULT_FIND_RESULT_LIMIT : limit, offset, ldpath);
        // For the Entityhub we support to select additional fields for results
        // of find requests. For the Sites and {site} endpoint this is currently
        // deactivated because of very bad performance with OPTIONAL graph patterns
        // in SPARQL queries.
        Collection<String> additionalSelectedFields = new ArrayList<String>();
        if (select != null && !select.isEmpty()) {
            for (String selected : select.trim().split(" ")) {
                if (selected != null && !selected.isEmpty()) {
                    additionalSelectedFields.add(selected);
                }
            }
        }
        query.addSelectedFields(additionalSelectedFields);
        return executeQuery(query, headers, acceptedMediaType);
    }
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 23 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class SiteManagerRootResource method getQueryDocumentation.

@GET
@Path("/query")
public Response getQueryDocumentation(@Context HttpHeaders headers) {
    ResponseBuilder rb = Response.ok(new Viewable("query", 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) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) GET(javax.ws.rs.GET)

Example 24 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class RootResource method performShowOntology.

/**
     * Helper method to make sure a ResponseBuilder is created on every conditions, so that it is then
     * possible to enable CORS on it afterwards.
     * 
     * @param ontologyId
     * @return
     */
protected ResponseBuilder performShowOntology(String ontologyId) {
    if (ontologyId == null || ontologyId.isEmpty()) {
        return Response.status(BAD_REQUEST);
    }
    OWLOntologyID key = OntologyUtils.decode(ontologyId);
    if (ontologyProvider.listOrphans().contains(key)) {
        return Response.status(NO_CONTENT);
    }
    OWLOntology o = getOWLOntology(ontologyId, false, uriInfo.getRequestUri());
    if (o == null) {
        return Response.status(NOT_FOUND);
    }
    // Assemble dependency list
    Map<OWLOntologyID, OntologyProvider.Status> deps = new HashMap<OWLOntologyID, OntologyProvider.Status>();
    for (OWLOntologyID dep : getDescriptor().getDependencies(key)) {
        deps.put(dep, ontologyProvider.getStatus(dep));
    }
    Set<OntologyCollector> handles = new HashSet<OntologyCollector>();
    if (onManager != null) {
        for (Scope scope : onManager.getRegisteredScopes()) {
            if (scope.getCoreSpace().hasOntology(key)) {
                handles.add(scope.getCoreSpace());
            }
            if (scope.getCustomSpace().hasOntology(key)) {
                handles.add(scope.getCustomSpace());
            }
        }
    }
    if (sessionManager != null) {
        for (String sesId : sessionManager.getRegisteredSessionIDs()) {
            if (sessionManager.getSession(sesId).hasOntology(key)) {
                handles.add(sessionManager.getSession(sesId));
            }
        }
    }
    return Response.ok(new Viewable("ontology", new OntologyStats(uriInfo, key, o, ontologyProvider.listAliases(key), deps, handles)));
}
Also used : Status(javax.ws.rs.core.Response.Status) HashMap(java.util.HashMap) OntologyCollector(org.apache.stanbol.ontologymanager.servicesapi.collector.OntologyCollector) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OntologyProvider(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyProvider) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) HashSet(java.util.HashSet)

Example 25 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class ScopeResource method managedOntologyShow.

@GET
@Path("/{ontologyId:.+}")
@Produces(TEXT_HTML)
public Response managedOntologyShow(@PathParam("scopeid") String scopeid, @PathParam("ontologyId") String ontologyId, @Context HttpHeaders headers) {
    ResponseBuilder rb;
    scope = onm.getScope(scopeid);
    if (scope == null)
        rb = Response.status(NOT_FOUND);
    else if (ontologyId == null || ontologyId.isEmpty())
        rb = Response.status(BAD_REQUEST);
    else if (!ontologyProvider.hasOntology(OntologyUtils.decode(ontologyId)))
        rb = Response.status(NOT_FOUND);
    else {
        IRI prefix = IRI.create(getPublicBaseUri() + "ontonet/ontology/");
        OWLOntology o = scope.getCustomSpace().getOntology(OntologyUtils.decode(ontologyId), OWLOntology.class, false, prefix);
        if (o == null)
            o = scope.getCoreSpace().getOntology(OntologyUtils.decode(ontologyId), OWLOntology.class, false, prefix);
        if (o == null)
            rb = Response.status(NOT_FOUND);
        else
            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                o.getOWLOntologyManager().saveOntology(o, new ManchesterOWLSyntaxOntologyFormat(), out);
                rb = Response.ok(new Viewable("ontology", new OntologyPrettyPrintResource(uriInfo, out, scope)));
            } catch (OWLOntologyStorageException e) {
                throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
            }
    }
    rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
    // addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) ManchesterOWLSyntaxOntologyFormat(org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat) WebApplicationException(javax.ws.rs.WebApplicationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) OntologyPrettyPrintResource(org.apache.stanbol.ontologymanager.web.util.OntologyPrettyPrintResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Viewable (org.apache.stanbol.commons.web.viewable.Viewable)43 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)40 GET (javax.ws.rs.GET)30 Produces (javax.ws.rs.Produces)26 Path (javax.ws.rs.Path)20 WebApplicationException (javax.ws.rs.WebApplicationException)15 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)14 MediaType (javax.ws.rs.core.MediaType)12 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)12 HashSet (java.util.HashSet)11 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)7 POST (javax.ws.rs.POST)6 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)6 IRI (org.semanticweb.owlapi.model.IRI)5 Consumes (javax.ws.rs.Consumes)4 OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3