Search in sources :

Example 11 with EntityhubException

use of org.apache.stanbol.entityhub.servicesapi.EntityhubException in project stanbol by apache.

the class BaseGoogleRefineReconcileResource method query.

@GET
public final Response query(@PathParam(value = "site") String siteId, @QueryParam(value = "query") String query, @QueryParam(value = "queries") String queries, @QueryParam(value = "callback") String callback, @Context HttpHeaders header) throws WebApplicationException {
    if (callback != null) {
        log.info("callback: {}", callback);
        try {
            return sendMetadata(siteId, callback, header);
        } catch (JSONException e) {
            throw new WebApplicationException(e);
        }
    }
    JSONObject jResult;
    if (query != null) {
        log.debug("query: {}", query);
        try {
            jResult = reconcile(siteId, ReconcileQuery.parseQuery(query, nsPrefixService));
        } catch (JSONException e) {
            throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(String.format("Error while writing Reconcilation results (%s: %s)", JSONException.class.getSimpleName(), e.getMessage())).build());
        } catch (EntityhubException e) {
            throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(String.format("Error while searching on %s (%s: %s)", getSiteName(siteId), SiteException.class.getSimpleName(), e.getMessage())).build());
        }
    } else if (queries != null) {
        log.debug("multi-query: {}", queries);
        try {
            jResult = reconcile(siteId, ReconcileQuery.parseQueries(queries, nsPrefixService));
        } catch (JSONException e) {
            throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(String.format("Error while writing Reconcilation results (%s: %s)", JSONException.class.getSimpleName(), e.getMessage())).build());
        } catch (EntityhubException e) {
            throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(String.format("Error while searching on %s (%s: %s)", getSiteName(siteId), SiteException.class.getSimpleName(), e.getMessage())).build());
        }
    } else {
        if (MediaTypeUtil.isAcceptableMediaType(header, MediaType.TEXT_HTML_TYPE)) {
            ResponseBuilder rb = Response.ok(new Viewable("index", this, BaseGoogleRefineReconcileResource.class));
            rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
            //addCORSOrigin(servletContext, rb, header);
            return rb.build();
        }
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("One of the 'query' or 'querues' or 'callback=jsonp' parameter MUST BE present!").build());
    }
    //return the results and enable Cors
    ResponseBuilder rb = Response.ok(jResult.toString()).type(MediaType.APPLICATION_JSON_TYPE);
    //CorsHelper.addCORSOrigin(servletContext, rb, header);
    return rb.build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JSONObject(org.codehaus.jettison.json.JSONObject) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) JSONException(org.codehaus.jettison.json.JSONException) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) GET(javax.ws.rs.GET)

Example 12 with EntityhubException

use of org.apache.stanbol.entityhub.servicesapi.EntityhubException in project stanbol by apache.

the class EntityhubSearcher method get.

@Override
public Representation get(String id, Set<String> includeFields) {
    if (id == null || id.isEmpty()) {
        return null;
    }
    Entityhub entityhub = getSearchService();
    if (entityhub == null) {
        throw new IllegalStateException("The Entityhub is currently not active");
    }
    Entity entity;
    try {
        entity = entityhub.getEntity(id);
    } catch (EntityhubException e) {
        throw new IllegalStateException("Exception while getting " + id + " from the Entityhub", e);
    }
    return entity == null ? null : entity.getRepresentation();
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) Entityhub(org.apache.stanbol.entityhub.servicesapi.Entityhub)

Example 13 with EntityhubException

use of org.apache.stanbol.entityhub.servicesapi.EntityhubException in project stanbol by apache.

the class EntityhubSearcher method lookup.

@Override
public Collection<? extends Representation> lookup(String field, Set<String> includeFields, List<String> search, String... languages) throws IllegalStateException {
    Entityhub entityhub = getSearchService();
    if (entityhub == null) {
        throw new IllegalStateException("The Entityhub is currently not active");
    }
    FieldQuery query = EntitySearcherUtils.createFieldQuery(entityhub.getQueryFactory(), field, includeFields, search, languages);
    QueryResultList<Representation> results;
    try {
        results = entityhub.find(query);
    } catch (EntityhubException e) {
        throw new IllegalStateException("Exception while searchign for " + search + '@' + Arrays.toString(languages) + "in the Entityhub", e);
    }
    return results.results();
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Entityhub(org.apache.stanbol.entityhub.servicesapi.Entityhub)

Example 14 with EntityhubException

use of org.apache.stanbol.entityhub.servicesapi.EntityhubException in project stanbol by apache.

the class EntityhubSearcher method get.

@Override
public Entity get(IRI id, Set<IRI> fields, String... languages) throws EntitySearcherException {
    if (id == null || id.getUnicodeString().isEmpty()) {
        return null;
    }
    Entityhub entityhub = getSearchService();
    if (entityhub == null) {
        throw new EntitySearcherException("The Entityhub is currently not active");
    }
    org.apache.stanbol.entityhub.servicesapi.model.Entity entity;
    try {
        entity = entityhub.getEntity(id.getUnicodeString());
    } catch (EntityhubException e) {
        throw new EntitySearcherException("Exception while getting " + id + " from the Entityhub", e);
    }
    if (entity != null) {
        Set<String> languageSet;
        if (languages == null || languages.length < 1) {
            languageSet = null;
        } else if (languages.length == 1) {
            languageSet = Collections.singleton(languages[0]);
        } else {
            languageSet = new HashSet<String>(Arrays.asList(languages));
        }
        return new EntityhubEntity(entity.getRepresentation(), fields, languageSet);
    } else {
        return null;
    }
}
Also used : EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) Entityhub(org.apache.stanbol.entityhub.servicesapi.Entityhub) EntitySearcherException(org.apache.stanbol.enhancer.engines.entitylinking.EntitySearcherException) HashSet(java.util.HashSet)

Example 15 with EntityhubException

use of org.apache.stanbol.entityhub.servicesapi.EntityhubException in project stanbol by apache.

the class NamedEntityTaggingEngine method computeEnhancements.

public void computeEnhancements(ContentItem ci) throws EngineException {
    final Site site;
    if (referencedSiteID != null) {
        // lookup the referenced site
        site = siteManager.getSite(referencedSiteID);
        // ensure that it is present
        if (site == null) {
            String msg = String.format("Unable to enhance %s because Referenced Site %s is currently not active!", ci.getUri().getUnicodeString(), referencedSiteID);
            log.warn(msg);
            // throw new EngineException(msg);
            return;
        }
        // and that it supports offline mode if required
        if (isOfflineMode() && !site.supportsLocalMode()) {
            log.warn("Unable to enhance ci {} because OfflineMode is not supported by ReferencedSite {}.", ci.getUri().getUnicodeString(), site.getId());
            return;
        }
    } else {
        // null indicates to use the Entityhub to lookup Entities
        site = null;
    }
    Graph graph = ci.getMetadata();
    LiteralFactory literalFactory = LiteralFactory.getInstance();
    // Retrieve the existing text annotations (requires read lock)
    Map<NamedEntity, List<IRI>> textAnnotations = new HashMap<NamedEntity, List<IRI>>();
    // the language extracted for the parsed content or NULL if not
    // available
    String contentLangauge;
    ci.getLock().readLock().lock();
    try {
        contentLangauge = EnhancementEngineHelper.getLanguage(ci);
        for (Iterator<Triple> it = graph.filter(null, RDF_TYPE, TechnicalClasses.ENHANCER_TEXTANNOTATION); it.hasNext(); ) {
            IRI uri = (IRI) it.next().getSubject();
            if (graph.filter(uri, Properties.DC_RELATION, null).hasNext()) {
                // skip
                continue;
            }
            NamedEntity namedEntity = NamedEntity.createFromTextAnnotation(graph, uri);
            if (namedEntity != null) {
                // This is a first occurrence, collect any subsumed
                // annotations
                List<IRI> subsumed = new ArrayList<IRI>();
                for (Iterator<Triple> it2 = graph.filter(null, Properties.DC_RELATION, uri); it2.hasNext(); ) {
                    subsumed.add((IRI) it2.next().getSubject());
                }
                textAnnotations.put(namedEntity, subsumed);
            }
        }
    } finally {
        ci.getLock().readLock().unlock();
    }
    // search the suggestions
    Map<NamedEntity, List<Suggestion>> suggestions = new HashMap<NamedEntity, List<Suggestion>>(textAnnotations.size());
    for (Entry<NamedEntity, List<IRI>> entry : textAnnotations.entrySet()) {
        try {
            List<Suggestion> entitySuggestions = computeEntityRecommentations(site, entry.getKey(), entry.getValue(), contentLangauge);
            if (entitySuggestions != null && !entitySuggestions.isEmpty()) {
                suggestions.put(entry.getKey(), entitySuggestions);
            }
        } catch (EntityhubException e) {
            throw new EngineException(this, ci, e);
        }
    }
    // now write the results (requires write lock)
    ci.getLock().writeLock().lock();
    try {
        RdfValueFactory factory = RdfValueFactory.getInstance();
        Map<String, Representation> entityData = new HashMap<String, Representation>();
        for (Entry<NamedEntity, List<Suggestion>> entitySuggestions : suggestions.entrySet()) {
            List<IRI> subsumed = textAnnotations.get(entitySuggestions.getKey());
            List<BlankNodeOrIRI> annotationsToRelate = new ArrayList<BlankNodeOrIRI>(subsumed);
            annotationsToRelate.add(entitySuggestions.getKey().getEntity());
            for (Suggestion suggestion : entitySuggestions.getValue()) {
                log.debug("Add Suggestion {} for {}", suggestion.getEntity().getId(), entitySuggestions.getKey());
                EnhancementRDFUtils.writeEntityAnnotation(this, literalFactory, graph, ci.getUri(), annotationsToRelate, suggestion, nameField, // header)?!
                contentLangauge == null ? DEFAULT_LANGUAGE : contentLangauge);
                if (dereferenceEntities) {
                    entityData.put(suggestion.getEntity().getId(), suggestion.getEntity().getRepresentation());
                }
            }
        }
        // Representations to add! If false entityData will be empty
        for (Representation rep : entityData.values()) {
            graph.addAll(factory.toRdfRepresentation(rep).getRdfGraph());
        }
    } finally {
        ci.getLock().writeLock().unlock();
    }
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) List(java.util.List) ArrayList(java.util.ArrayList) QueryResultList(org.apache.stanbol.entityhub.servicesapi.query.QueryResultList) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) LiteralFactory(org.apache.clerezza.rdf.core.LiteralFactory) Triple(org.apache.clerezza.commons.rdf.Triple) Graph(org.apache.clerezza.commons.rdf.Graph) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)

Aggregations

EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)19 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)10 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)9 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)8 MediaType (javax.ws.rs.core.MediaType)7 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)7 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)7 HashSet (java.util.HashSet)6 GET (javax.ws.rs.GET)6 Path (javax.ws.rs.Path)6 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)6 Produces (javax.ws.rs.Produces)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 Graph (org.apache.clerezza.commons.rdf.Graph)3 Entityhub (org.apache.stanbol.entityhub.servicesapi.Entityhub)3 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)3 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 IRI (org.apache.clerezza.commons.rdf.IRI)2