You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by su...@apache.org on 2011/10/04 14:13:59 UTC
svn commit: r1178765 [5/12] - in /incubator/stanbol/trunk/contenthub: ./
api/ api/src/ api/src/main/ api/src/main/java/ api/src/main/java/org/
api/src/main/java/org/apache/ api/src/main/java/org/apache/stanbol/
api/src/main/java/org/apache/stanbol/cont...
Added: incubator/stanbol/trunk/contenthub/search/engines/ontologyresource/src/main/java/org/apache/stanbol/contenthub/search/engines/ontologyresource/OntologyResourceSearchEngine.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/contenthub/search/engines/ontologyresource/src/main/java/org/apache/stanbol/contenthub/search/engines/ontologyresource/OntologyResourceSearchEngine.java?rev=1178765&view=auto
==============================================================================
--- incubator/stanbol/trunk/contenthub/search/engines/ontologyresource/src/main/java/org/apache/stanbol/contenthub/search/engines/ontologyresource/OntologyResourceSearchEngine.java (added)
+++ incubator/stanbol/trunk/contenthub/search/engines/ontologyresource/src/main/java/org/apache/stanbol/contenthub/search/engines/ontologyresource/OntologyResourceSearchEngine.java Tue Oct 4 12:13:49 2011
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.stanbol.contenthub.search.engines.ontologyresource;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.stanbol.contenthub.core.utils.ClosureHelper;
+import org.apache.stanbol.contenthub.servicesapi.search.engine.SearchEngine;
+import org.apache.stanbol.contenthub.servicesapi.search.engine.SearchEngineException;
+import org.apache.stanbol.contenthub.servicesapi.search.execution.ClassResource;
+import org.apache.stanbol.contenthub.servicesapi.search.execution.IndividualResource;
+import org.apache.stanbol.contenthub.servicesapi.search.execution.Keyword;
+import org.apache.stanbol.contenthub.servicesapi.search.execution.QueryKeyword;
+import org.apache.stanbol.contenthub.servicesapi.search.execution.SearchContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.hp.hpl.jena.query.Query;
+import com.hp.hpl.jena.query.QueryExecution;
+import com.hp.hpl.jena.query.QueryExecutionFactory;
+import com.hp.hpl.jena.query.QuerySolution;
+import com.hp.hpl.jena.query.ResultSet;
+import com.hp.hpl.jena.query.larq.IndexLARQ;
+import com.hp.hpl.jena.query.larq.LARQ;
+import com.hp.hpl.jena.rdf.model.RDFNode;
+
+/**
+ *
+ * @author cihan
+ *
+ */
+@Component
+@Service
+public class OntologyResourceSearchEngine implements SearchEngine {
+ private static final Logger logger = LoggerFactory.getLogger(OntologyResourceSearchEngine.class);
+
+ @Override
+ public void search(SearchContext searchContext) throws SearchEngineException {
+ for (QueryKeyword qk : searchContext.getQueryKeyWords()) {
+ searchForKeyword(qk, searchContext);
+ for (Keyword kw : qk.getRelatedKeywords()) {
+ searchForKeyword(kw, searchContext);
+ }
+ }
+ }
+
+ private void searchForKeyword(Keyword kw, SearchContext searchContext) {
+ // index of the search model is obtained and checked
+ IndexLARQ index = searchContext.getIndex();
+ if (index != null) {
+ // classes
+ Query query = QueryFactory.getClassQuery(kw.getKeyword());
+ QueryExecution classQExec = QueryExecutionFactory.create(query, searchContext.getSearchModel());
+ LARQ.setDefaultIndex(classQExec.getContext(), index);
+ ResultSet result = classQExec.execSelect();
+ processClassResultSet(result, kw, searchContext);
+
+ // individuals
+ query = QueryFactory.getIndividualQuery(kw.getKeyword());
+ QueryExecution individualQExec = QueryExecutionFactory.create(query,
+ searchContext.getSearchModel());
+ LARQ.setDefaultIndex(individualQExec.getContext(), index);
+ result = individualQExec.execSelect();
+ processIndividualResultSet(result, kw, searchContext);
+ } else {
+ logger.warn("Keyword Engine skipped since no index for search model");
+ }
+
+ }
+
+ private void processClassResultSet(ResultSet result, Keyword kw, SearchContext context) {
+ Map<String,Double> results = new HashMap<String,Double>();
+ while (result.hasNext()) {
+ QuerySolution resultBinding = result.nextSolution();
+ RDFNode rdfNode = resultBinding.get("class");
+ double score = resultBinding.getLiteral("score").getDouble();
+ if (rdfNode.isURIResource()) {
+ String uri = rdfNode.asResource().getURI();
+ results.put(uri, score);
+ }
+ }
+ for (String uri : results.keySet()) {
+ ClassResource cr = context.getFactory().createClassResource(uri, 1.0, results.get(uri), kw);
+ ClosureHelper.getInstance(context).computeClassClosure(cr, 6, 1.5, kw);
+ }
+ }
+
+ private void processIndividualResultSet(ResultSet result, Keyword kw, SearchContext context) {
+ Map<String,Double> results = new HashMap<String,Double>();
+ while (result.hasNext()) {
+ QuerySolution resultBinding = result.nextSolution();
+ RDFNode rdfNode = resultBinding.get("individual");
+ double score = resultBinding.getLiteral("score").getDouble();
+ if (rdfNode.isURIResource()) {
+ String uri = rdfNode.asResource().getURI();
+ results.put(uri, score);
+ }
+ }
+ for (String uri : results.keySet()) {
+ IndividualResource ir = context.getFactory().createIndividualResource(uri, 1.0, results.get(uri),
+ kw);
+ ClosureHelper.getInstance(context).computeIndividualClosure(ir, 6, 1.5, kw);
+ }
+ }
+}
Added: incubator/stanbol/trunk/contenthub/search/engines/ontologyresource/src/main/java/org/apache/stanbol/contenthub/search/engines/ontologyresource/QueryFactory.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/contenthub/search/engines/ontologyresource/src/main/java/org/apache/stanbol/contenthub/search/engines/ontologyresource/QueryFactory.java?rev=1178765&view=auto
==============================================================================
--- incubator/stanbol/trunk/contenthub/search/engines/ontologyresource/src/main/java/org/apache/stanbol/contenthub/search/engines/ontologyresource/QueryFactory.java (added)
+++ incubator/stanbol/trunk/contenthub/search/engines/ontologyresource/src/main/java/org/apache/stanbol/contenthub/search/engines/ontologyresource/QueryFactory.java Tue Oct 4 12:13:49 2011
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.stanbol.contenthub.search.engines.ontologyresource;
+
+import org.apache.stanbol.contenthub.servicesapi.search.vocabulary.SearchVocabulary;
+
+import com.hp.hpl.jena.query.Query;
+
+/**
+ *
+ * @author cihan
+ *
+ */
+public final class QueryFactory {
+
+ private static final String OWL = "PREFIX owl: <http://www.w3.org/2002/07/owl#>";
+ // private static final String XSD = "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>";
+ // private static final String RDFS = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>";
+ private static final String RDF = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>";
+ // private static final String FOAF = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>";
+ // private static final String DC = "PREFIX dc: <http://purl.org/dc/elements/1.1/>";
+ // private static final String DBPEDIA_RES = "PREFIX dbpedia1: <http://dbpedia.org/resource/>";
+ // private static final String DBPEDIA_PROP = "PREFIX dbpedia2: <http://dbpedia.org/property/>";
+ // private static final String DBPEDIA = "PREFIX dbpedia: <http://dbpedia.org/>";
+ // private static final String SKOS = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>";
+ private static final String PF = "PREFIX pf: <http://jena.hpl.hp.com/ARQ/property#>";
+ private static final String SEARCH = "PREFIX ss: <" + SearchVocabulary.getUri() + ">";
+
+ public static final Query getClassQuery(String keyword) {
+
+ StringBuilder sb = new StringBuilder();
+ sb.append(RDF).append("\n");
+ sb.append(OWL).append("\n");
+ sb.append(PF).append("\n");
+ sb.append(SEARCH).append("\n");
+ sb.append("SELECT ?class ?score WHERE {\n");
+ sb.append("\t?class rdf:type owl:Class.\n");
+ sb.append("\t?class ss:hasLocalName ?name.\n");
+ sb.append("\t(?name ?score) pf:textMatch '+" + normalizeKeyword(keyword) + "'.\n");
+ sb.append("}");
+ return com.hp.hpl.jena.query.QueryFactory.create(sb.toString());
+ }
+
+ public static final Query getIndividualQuery(String keyword) {
+ StringBuilder sb = new StringBuilder();
+ sb.append(RDF).append("\n");
+ sb.append(OWL).append("\n");
+ sb.append(PF).append("\n");
+ sb.append(SEARCH).append("\n");
+ sb.append("SELECT ?individual ?score WHERE {\n");
+ sb.append("\t?individual rdf:type ?type.\n");
+ sb.append("\t?type rdf:type owl:Class.\n");
+ sb.append("\t?individual ss:hasLocalName ?name.\n");
+ sb.append("\t(?name ?score) pf:textMatch '+" + normalizeKeyword(keyword) + "'.\n");
+ sb.append("}");
+ return com.hp.hpl.jena.query.QueryFactory.create(sb.toString());
+ }
+
+ private static String normalizeKeyword(String keyword) {
+ return keyword.replace("'", "");
+ }
+}
Propchange: incubator/stanbol/trunk/contenthub/search/engines/solr/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Oct 4 12:13:49 2011
@@ -0,0 +1,4 @@
+target
+.classpath
+.project
+.settings
Added: incubator/stanbol/trunk/contenthub/search/engines/solr/pom.xml
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/contenthub/search/engines/solr/pom.xml?rev=1178765&view=auto
==============================================================================
--- incubator/stanbol/trunk/contenthub/search/engines/solr/pom.xml (added)
+++ incubator/stanbol/trunk/contenthub/search/engines/solr/pom.xml Tue Oct 4 12:13:49 2011
@@ -0,0 +1,97 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.contenthub.parent</artifactId>
+ <version>0.9.0-incubating-SNAPSHOT</version>
+ <relativePath>../../../parent</relativePath>
+ </parent>
+
+ <artifactId>org.apache.stanbol.contenthub.search.engines.solr</artifactId>
+ <packaging>bundle</packaging>
+
+ <name>Apache Stanbol Search Solr Searcher</name>
+
+ <description>Finds indexed document from Solr according to field constraints</description>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Embed-Dependency>jaws</Embed-Dependency>
+ </instructions>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.contenthub.servicesapi</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.contenthub.core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.commons.solr</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.scr.annotations</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.hp.hpl.jena</groupId>
+ <artifactId>jena</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.hp.hpl.jena</groupId>
+ <artifactId>arq</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </dependency>
+ <!-- Solr Bundles -->
+ <dependency>
+ <groupId>org.apache.solr</groupId>
+ <artifactId>solr-solrj</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.solr</groupId>
+ <artifactId>solr-core</artifactId>
+ </dependency>
+ </dependencies>
+</project>
Added: incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrQueryFactory.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrQueryFactory.java?rev=1178765&view=auto
==============================================================================
--- incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrQueryFactory.java (added)
+++ incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrQueryFactory.java Tue Oct 4 12:13:49 2011
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.stanbol.contenthub.search.engines.solr;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.solr.client.solrj.SolrQuery;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ * @author anil.pacaci
+ *
+ */
+public class SolrQueryFactory {
+ private final static Logger logger = LoggerFactory.getLogger(SolrQueryFactory.class);
+ private final static String and = " AND ";
+ private final static String delimiter = ":";
+
+ public static SolrQuery keywordQueryWithFacets(String keyword, Map<String,List<Object>> constraints) {
+ SolrQuery query = new SolrQuery();
+ String queryString = keyword;
+
+ try {
+ for (Entry<String,List<Object>> entry : constraints.entrySet()) {
+ String fieldName = entry.getKey();
+ for (Object value : entry.getValue()) {
+ queryString = queryString + and + fieldName + delimiter + value;
+ }
+ }
+ } catch (Exception e) {
+ logger.warn("Facet constraints could not be added to Query", e);
+ }
+ queryString += "&fl=*,score";
+ query.setQuery(queryString);
+ return query;
+ }
+
+}
\ No newline at end of file
Added: incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrSearchEngine.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrSearchEngine.java?rev=1178765&view=auto
==============================================================================
--- incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrSearchEngine.java (added)
+++ incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrSearchEngine.java Tue Oct 4 12:13:49 2011
@@ -0,0 +1,293 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.stanbol.contenthub.search.engines.solr;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.SolrServer;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrDocumentList;
+import org.apache.stanbol.commons.solr.SolrDirectoryManager;
+import org.apache.stanbol.commons.solr.SolrServerProviderManager;
+import org.apache.stanbol.commons.solr.SolrServerTypeEnum;
+import org.apache.stanbol.contenthub.core.search.execution.SearchContextImpl;
+import org.apache.stanbol.contenthub.servicesapi.search.engine.EngineProperties;
+import org.apache.stanbol.contenthub.servicesapi.search.engine.SearchEngine;
+import org.apache.stanbol.contenthub.servicesapi.search.engine.SearchEngineException;
+import org.apache.stanbol.contenthub.servicesapi.search.execution.ClassResource;
+import org.apache.stanbol.contenthub.servicesapi.search.execution.IndividualResource;
+import org.apache.stanbol.contenthub.servicesapi.search.execution.Keyword;
+import org.apache.stanbol.contenthub.servicesapi.search.execution.QueryKeyword;
+import org.apache.stanbol.contenthub.servicesapi.search.execution.SearchContext;
+import org.apache.stanbol.contenthub.servicesapi.search.execution.SearchContextFactory;
+import org.apache.stanbol.contenthub.servicesapi.search.vocabulary.SearchVocabulary;
+import org.apache.stanbol.contenthub.servicesapi.store.vocabulary.SolrVocabulary;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.hp.hpl.jena.ontology.OntModel;
+import com.hp.hpl.jena.rdf.model.RDFNode;
+import com.hp.hpl.jena.rdf.model.ResIterator;
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.rdf.model.impl.Util;
+import com.hp.hpl.jena.util.iterator.Filter;
+import com.hp.hpl.jena.vocabulary.RDF;
+
+/**
+ *
+ * @author anil.pacaci
+ *
+ */
+@Component
+@Service
+public class SolrSearchEngine implements SearchEngine, EngineProperties {
+
+ private static final Logger logger = LoggerFactory.getLogger(SolrSearchEngine.class);
+
+ private static final Map<String,Object> properties;
+ static {
+ properties = new HashMap<String,Object>();
+ properties.put(PROCESSING_ORDER, PROCESSING_POST);
+ }
+
+ private final static String SERVER_NAME = "contenthub";
+ /**
+ * for EmbeddedSolr instance, it is tried to obtained EmbbeddedSolr at activator if it can get the
+ * instance, server becomes null and this SolrSearchEngine does not work
+ */
+ private SolrServer server;
+
+ @Reference
+ SolrServerProviderManager solrServerProviderManager;
+
+ @Reference
+ SolrDirectoryManager solrDirectoryManager;
+
+ @Activate
+ /**
+ * Tries to connect EmbeddedSolr at startup, if can not, server becomes null and no query is executed on server
+ * @param cc
+ */
+ public void activate(ComponentContext cc) {
+ try {
+ if (solrDirectoryManager != null) {
+ File indexDirectory = solrDirectoryManager.getSolrIndexDirectory(SERVER_NAME);
+ if (indexDirectory == null) {
+ indexDirectory = solrDirectoryManager.createSolrDirectory(SERVER_NAME, SERVER_NAME, null);
+ }
+ String serverLocation = indexDirectory.toString();
+
+ server = solrServerProviderManager.getSolrServer(SolrServerTypeEnum.EMBEDDED, serverLocation);
+ }
+ logger.warn("Could not get the EmbeddedSolr Instance since there is no SolrDirectoryManager");
+ } catch (Exception e) {
+ logger.warn("Could not get the EmbeddedSolr Instance at location : {}", SERVER_NAME, e);
+ server = null;
+ }
+ }
+
+ @Override
+ /**
+ * gets the keywords from search context and then queries solr with these keywords and facet constraints,
+ * when it found any result, adds to searchContext as documentResouce.
+ * After searching for all keywords, omits the results founded by other engines and having non matching field constraints
+ */
+ public void search(SearchContext searchContext) throws SearchEngineException {
+ if (server == null) {
+ logger.warn("No EmbeddedSolr, so SolrSearchEngine does not work");
+ } else {
+ for (QueryKeyword qk : searchContext.getQueryKeyWords()) {
+ searchForKeyword(qk, searchContext);
+ for (Keyword kw : qk.getRelatedKeywords()) {
+ searchForKeyword(kw, searchContext);
+ }
+ }
+ if(searchContext.getConstraints() != null && !searchContext.getConstraints().isEmpty()) {
+ omitNonMatchingResult(searchContext);
+ }
+ }
+ }
+
+ /**
+ * @param kw
+ * the keyword to use in query
+ * @param searchContext
+ */
+ private void searchForKeyword(Keyword kw, SearchContext searchContext) {
+ String keyword = kw.getKeyword();
+ SolrQuery query = SolrSearchEngineHelper.keywordQueryWithFacets(keyword,
+ searchContext.getConstraints());
+
+ // Finding document resources by querying keyword with the facets
+ try {
+ QueryResponse solrResult = server.query(query);
+ processSolrResult(searchContext, kw, solrResult);
+ } catch (SolrServerException e) {
+ logger.warn("Server could not be queried", e);
+ }
+
+ // Finding document resources by querying related Class Resources about
+ // the keyword with facets
+ List<ClassResource> classResources = kw.getRelatedClassResources();
+ try {
+ for (int i = 0; i < classResources.size(); i++) {
+ ClassResource classResource = classResources.get(i);
+ String classURI = classResource.getClassURI();
+ String className = classURI.substring(Util.splitNamespace(classURI));
+ if (className != null) {
+ query = SolrSearchEngineHelper.keywordQueryWithFacets(className,
+ searchContext.getConstraints());
+ QueryResponse solrResult = server.query(query);
+ processSolrResult(searchContext, kw, solrResult);
+ } else {
+ logger.info("Name of class could not be extracted from from class Resource : ",
+ classResource);
+ }
+ }
+ } catch (Exception e) {
+ logger.warn("Error while querying solr with relatedClass resources", e);
+ }
+
+ // Finding document resources by querying related Individual Resources
+ // about the keyword with facets
+ List<IndividualResource> individualResources = kw.getRelatedIndividualResources();
+ try {
+ for (int i = 0; i < individualResources.size(); i++) {
+ IndividualResource individualResource = individualResources.get(i);
+ String individualURI = individualResource.getIndividualURI();
+ String individualName = individualURI.substring(Util.splitNamespace(individualURI));
+ if (individualName != null) {
+ query = SolrSearchEngineHelper.keywordQueryWithFacets(individualName,
+ searchContext.getConstraints());
+ QueryResponse solrResult = server.query(query);
+ processSolrResult(searchContext, kw, solrResult);
+ } else {
+ logger.info("Name of individual could not be extracted from individual Resource : ",
+ individualResource);
+ }
+ }
+ } catch (Exception e) {
+ logger.warn("Error while querying solr with relatedIndividual resources", e);
+ }
+
+ }
+
+ private void processSolrResult(SearchContext searchContext, Keyword keyword, QueryResponse solrResult) {
+ SearchContextFactory factory = searchContext.getFactory();
+ SolrDocumentList resultDocuments = solrResult.getResults();
+ Double maxScore = resultDocuments.getMaxScore().doubleValue();
+
+ for (int i = 0; i < resultDocuments.size(); i++) {
+ SolrDocument resultDoc = resultDocuments.get(i);
+ double score = Double.parseDouble(resultDoc.getFieldValue(SolrSearchEngineHelper.SCORE_FIELD)
+ .toString()) / maxScore;
+ score = score > 1.0 ? 1.0 : score;
+
+ String contenthubId = (String) resultDoc.getFieldValue(SolrVocabulary.SOLR_FIELD_NAME_ID);
+ /*
+ * String cmsId = (String) resultDoc.getFieldValue(SolrSearchEngineHelper.CMSID_FIELD); cmsId =
+ * cmsId == null ? "" : cmsId;
+ */
+
+ String selectionText = (String) resultDoc.getFieldValue(SolrVocabulary.SOLR_FIELD_NAME_CONTENT);
+
+ // score of the keyword is used as a weight for newly found document
+ factory.createDocumentResource(contenthubId, 1.0, keyword.getScore() * score, keyword,
+ selectionText/* , cmsId */);
+ }
+ }
+
+ private void omitNonMatchingResult(SearchContext searchContext) {
+ OntModel contextModel = (SearchContextImpl) searchContext;
+ ResIterator docResources = contextModel.listResourcesWithProperty(RDF.type,
+ SearchVocabulary.DOCUMENT_RESOURCE);
+ List<String> solrResultIds = new ArrayList<String>();
+
+ SolrQuery query = SolrSearchEngineHelper
+ .keywordQueryWithFacets("*:*", searchContext.getConstraints());
+ QueryResponse solrResult = null;
+ try {
+ solrResult = server.query(query);
+ } catch (SolrServerException e) {
+ logger.warn("Error while querying with query : {} ", query, e);
+ }
+ // means query is done successfully, no problem about server or query
+ if (solrResult != null) {
+ SolrDocumentList resultDocuments = solrResult.getResults();
+ for (int i = 0; i < resultDocuments.size(); i++) {
+ SolrDocument resultDocument = resultDocuments.get(i);
+ String docID = (String) resultDocument.getFieldValue("id");
+ if (docID != null && !docID.equals("")) {
+ solrResultIds.add(docID);
+ }
+ }
+ }
+
+ while (docResources.hasNext()) {
+ Resource docResource = docResources.next();
+ String relatedDocument = docResource.getProperty(SearchVocabulary.RELATED_DOCUMENT).getObject()
+ .toString();
+ if (!solrResultIds.contains(relatedDocument)) {
+ /*
+ * means that documentResource is related with such a document that document does not apply
+ * facet constraints so that document resource and its external resources will be omitted from
+ * searchContext
+ */
+ Filter<Statement> f = new Filter<Statement>() {
+ @Override
+ public boolean accept(Statement s) {
+ return s.getSubject().getPropertyResourceValue(RDF.type)
+ .equals(SearchVocabulary.EXTERNAL_RESOURCE);
+ }
+ };
+ List<Statement> omitEntity = contextModel
+ .listStatements(null, SearchVocabulary.RELATED_DOCUMENT, docResource).filterKeep(f)
+ .toList();
+ // now all statements whose subject is document will be omitted
+ List<Statement> omitDocument = contextModel.listStatements(docResource, null, (RDFNode) null)
+ .toList();
+
+ try {
+ contextModel.remove(omitEntity);
+ contextModel.remove(omitDocument);
+ } catch (Exception e) {
+ logger.warn("Resources could not be omitted from search context", e);
+ }
+ }
+ }
+ }
+
+ @Override
+ public Map<String,Object> getEngineProperties() {
+ return properties;
+ }
+
+}
Added: incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrSearchEngineHelper.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrSearchEngineHelper.java?rev=1178765&view=auto
==============================================================================
--- incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrSearchEngineHelper.java (added)
+++ incubator/stanbol/trunk/contenthub/search/engines/solr/src/main/java/org/apache/stanbol/contenthub/search/engines/solr/SolrSearchEngineHelper.java Tue Oct 4 12:13:49 2011
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.stanbol.contenthub.search.engines.solr;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.util.ClientUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ * @author anil.pacaci
+ *
+ */
+public class SolrSearchEngineHelper {
+ public static final String CONTENT_FIELD = "content_t";
+ public static final String ID_FIELD = "id";
+ public static final String SCORE_FIELD = "score";
+
+ private final static Logger logger = LoggerFactory.getLogger(SolrSearchEngineHelper.class);
+ private final static String and = " AND ";
+ private final static String queryDelimiter = ":";
+
+ public static SolrQuery keywordQueryWithFacets(String keyword, Map<String,List<Object>> constraints) {
+ SolrQuery query = new SolrQuery();
+ String queryString = keyword;
+
+ if(constraints != null) {
+ try {
+ for (Entry<String,List<Object>> entry : constraints.entrySet()) {
+ String fieldName = ClientUtils.escapeQueryChars(entry.getKey());
+ for (Object value : entry.getValue()) {
+ queryString = queryString + and + fieldName + queryDelimiter + ClientUtils.escapeQueryChars((String) value);
+ }
+ }
+ } catch (Exception e) {
+ logger.warn("Facet constraints could not be added to Query", e);
+ }
+ }
+
+ query.setQuery(queryString);
+ query.setFields("*", "score");
+ return query;
+ }
+
+ private SolrSearchEngineHelper() {}
+
+}
Added: incubator/stanbol/trunk/contenthub/search/pom.xml
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/contenthub/search/pom.xml?rev=1178765&view=auto
==============================================================================
--- incubator/stanbol/trunk/contenthub/search/pom.xml (added)
+++ incubator/stanbol/trunk/contenthub/search/pom.xml Tue Oct 4 12:13:49 2011
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.contenthub.parent</artifactId>
+ <version>0.9.0-incubating-SNAPSHOT</version>
+ <relativePath>../parent</relativePath>
+ </parent>
+
+ <artifactId>org.apache.stanbol.contenthub.search.reactor</artifactId>
+ <packaging>pom</packaging>
+ <version>${search-version}</version>
+
+ <name>Apache Stanbol Search Reactor</name>
+ <description>
+ Pseudo project to build the complete semantic search project
+ </description>
+
+ <inceptionYear>2011</inceptionYear>
+
+ <scm>
+ <connection>
+ scm:svn:http://svn.apache.org/repos/asf/incubator/stanbol/trunk/contenthub/search
+ </connection>
+ <developerConnection>
+ scm:svn:https://svn.apache.org/repos/asf/incubator/stanbol/trunk/contenthub/search
+ </developerConnection>
+ </scm>
+
+ <modules>
+ <module>engines/solr</module>
+ <module>engines/ontologyresource</module>
+ <module>engines/enhancement</module>
+ </modules>
+</project>
Modified: incubator/stanbol/trunk/contenthub/web/pom.xml
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/contenthub/web/pom.xml?rev=1178765&r1=1178764&r2=1178765&view=diff
==============================================================================
--- incubator/stanbol/trunk/contenthub/web/pom.xml (original)
+++ incubator/stanbol/trunk/contenthub/web/pom.xml Tue Oct 4 12:13:49 2011
@@ -1,180 +1,195 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.apache.stanbol</groupId>
- <artifactId>stanbol-parent</artifactId>
- <version>0.9.0-incubating-SNAPSHOT</version>
- <relativePath>../../parent/pom.xml</relativePath>
- </parent>
- <artifactId>org.apache.stanbol.contenthub.web</artifactId>
- <packaging>bundle</packaging>
+ <parent>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.contenthub.parent</artifactId>
+ <version>0.9.0-incubating-SNAPSHOT</version>
+ <relativePath>../parent</relativePath>
+ </parent>
- <name>Apache Stanbol Content Hub JAX-RS API</name>
- <description>HTTP endpoint to create and search for content items enriched
+ <artifactId>org.apache.stanbol.contenthub.web</artifactId>
+ <packaging>bundle</packaging>
+
+ <name>Apache Stanbol Content Hub JAX-RS API</name>
+ <description>HTTP endpoint to create and search for content items enriched
with enhancer's annotations and links to entities from the entity hub.
</description>
- <scm>
- <connection>
+ <scm>
+ <connection>
scm:svn:http://svn.apache.org/repos/asf/incubator/stanbol/trunk/contenthub/web/
</connection>
- <developerConnection>
+ <developerConnection>
scm:svn:https://svn.apache.org/repos/asf/incubator/stanbol/trunk/contentub/web/
</developerConnection>
- <url>http://incubator.apache.org/stanbol/</url>
- </scm>
+ <url>http://incubator.apache.org/stanbol/</url>
+ </scm>
- <build>
- <!-- make it an OSGi bundle -->
- <plugins>
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-scr-plugin</artifactId>
- </plugin>
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-bundle-plugin</artifactId>
- <extensions>true</extensions>
- <configuration>
- <instructions>
- <Export-Package>
- org.apache.stanbol.contenthub.web.*
- </Export-Package>
- <Import-Package>*</Import-Package>
- </instructions>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
- <dependencies>
-
- <!-- dependencies on other IKS modules -->
- <dependency>
- <groupId>org.apache.stanbol</groupId>
- <artifactId>org.apache.stanbol.enhancer.servicesapi</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.stanbol</groupId>
- <artifactId>org.apache.stanbol.enhancer.standalone</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.stanbol</groupId>
- <artifactId>org.apache.stanbol.commons.web.base</artifactId>
- </dependency>
-
- <!-- needed for the ContentItem resource -->
- <dependency>
- <groupId>org.apache.stanbol</groupId>
- <artifactId>org.apache.stanbol.enhancer.jersey</artifactId>
- </dependency>
- <!-- Clerezza dependencies -->
- <dependency>
- <groupId>org.apache.clerezza</groupId>
- <artifactId>rdf.core</artifactId>
- </dependency>
-
- <!-- Jersey -->
- <dependency>
- <groupId>com.sun.jersey</groupId>
- <artifactId>jersey-server</artifactId>
- </dependency>
- <dependency>
- <groupId>com.sun.jersey</groupId>
- <artifactId>jersey-core</artifactId>
- </dependency>
- <dependency>
- <groupId>com.sun.jersey.contribs</groupId>
- <artifactId>jersey-multipart</artifactId>
- </dependency>
-<!--
- <dependency>
- <groupId>com.sun.jersey</groupId>
- <artifactId>jersey-json</artifactId>
- </dependency>
- <dependency>
- <groupId>com.sun.jersey</groupId>
- <artifactId>jersey-client</artifactId>
- </dependency> -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- </dependency>
- <!-- indirect dependency for freemarker -->
- <dependency>
- <groupId>org.codehaus.jettison</groupId>
- <artifactId>jettison</artifactId>
- </dependency>
-
- <!-- generic tax -->
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- </dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- </dependency>
- <dependency>
+ <build>
+ <!-- make it an OSGi bundle -->
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-scr-plugin</artifactId>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+
+ <!-- dependencies on other Stanbol modules -->
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.enhancer.servicesapi</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.enhancer.standalone</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.commons.web.base</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.contenthub.servicesapi</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.contenthub.core</artifactId>
+ </dependency>
+
+ <!-- Search Helper -->
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.contenthub.helper.cnn</artifactId>
+ </dependency>
+
+ <!-- needed for the ContentItem resource -->
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.enhancer.jersey</artifactId>
+ </dependency>
+
+ <!-- Clerezza dependencies -->
+ <dependency>
+ <groupId>org.apache.clerezza</groupId>
+ <artifactId>rdf.core</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>com.hp.hpl.jena</groupId>
+ <artifactId>jena</artifactId>
+ </dependency>
+
+ <!-- Jersey -->
+ <dependency>
+ <groupId>com.sun.jersey</groupId>
+ <artifactId>jersey-server</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.sun.jersey</groupId>
+ <artifactId>jersey-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.sun.jersey.contribs</groupId>
+ <artifactId>jersey-multipart</artifactId>
+ </dependency>
+ <!-- <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId>
+ </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId>
+ </dependency> -->
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.freemarker</groupId>
+ <artifactId>freemarker</artifactId>
+ </dependency>
+ <!-- indirect dependency for freemarker -->
+ <dependency>
+ <groupId>org.codehaus.jettison</groupId>
+ <artifactId>jettison</artifactId>
+ </dependency>
+
+ <!-- generic tax -->
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-lang</groupId>
+ <artifactId>commons-lang</artifactId>
+ </dependency>
+ <dependency>
<groupId>eu.medsea.mimeutil</groupId>
<artifactId>mime-util</artifactId>
- </dependency>
-
+ </dependency>
+
- <!-- OSGi tax -->
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.core</artifactId>
- </dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.compendium</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.scr.annotations</artifactId>
- </dependency>
-
- <!-- for tests -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-simple</artifactId>
- <scope>test</scope>
- </dependency>
+ <!-- OSGi tax -->
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.scr.annotations</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.solr</groupId>
+ <artifactId>solr-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.commons.solr</artifactId>
+ </dependency>
+ <!-- for tests -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <scope>test</scope>
+ </dependency>
- </dependencies>
+ </dependencies>
</project>
Added: incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/fragment/ContenthubWebFragment.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/fragment/ContenthubWebFragment.java?rev=1178765&view=auto
==============================================================================
--- incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/fragment/ContenthubWebFragment.java (added)
+++ incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/fragment/ContenthubWebFragment.java Tue Oct 4 12:13:49 2011
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.stanbol.contenthub.web.fragment;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.clerezza.rdf.core.access.TcManager;
+import org.apache.clerezza.rdf.core.serializedform.Serializer;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.stanbol.commons.web.base.LinkResource;
+import org.apache.stanbol.commons.web.base.NavigationLink;
+import org.apache.stanbol.commons.web.base.ScriptResource;
+import org.apache.stanbol.commons.web.base.WebFragment;
+import org.apache.stanbol.contenthub.web.resources.CNNImporterResource;
+import org.apache.stanbol.contenthub.web.resources.ContenthubStoreResource;
+import org.apache.stanbol.contenthub.web.resources.SearchResource;
+import org.apache.stanbol.enhancer.servicesapi.EnhancementJobManager;
+import org.apache.stanbol.enhancer.servicesapi.Store;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.component.ComponentContext;
+
+import freemarker.cache.ClassTemplateLoader;
+import freemarker.cache.TemplateLoader;
+
+/**
+ * Statically define the list of available resources and providers to be contributed to the the Stanbol JAX-RS
+ * Endpoint.
+ */
+@Component(immediate = true, metatype = true)
+@Service
+public class ContenthubWebFragment implements WebFragment {
+
+ private static final String NAME = "contenthub";
+
+ private static final String TEMPLATE_PATH = "/org/apache/stanbol/contenthub/web/templates";
+ private static final String STATIC_RESOURCE_PATH = "/org/apache/stanbol/contenthub/web/static";
+
+ private BundleContext bundleContext;
+
+ @Reference
+ TcManager tcManager;
+
+ @Reference
+ Store store;
+
+ @Reference
+ EnhancementJobManager jobManager;
+
+ @Reference
+ Serializer serializer;
+
+ @Override
+ public String getName() {
+ return NAME;
+ }
+
+ @Activate
+ protected void activate(ComponentContext ctx) {
+ this.bundleContext = ctx.getBundleContext();
+ }
+
+ @Override
+ public Set<Class<?>> getJaxrsResourceClasses() {
+ Set<Class<?>> classes = new HashSet<Class<?>>();
+ classes.add(ContenthubStoreResource.class);
+ classes.add(CNNImporterResource.class);
+ classes.add(SearchResource.class);
+ return classes;
+ }
+
+ @Override
+ public Set<Object> getJaxrsResourceSingletons() {
+ return Collections.emptySet();
+ }
+
+ @Override
+ public String getStaticResourceClassPath() {
+ return STATIC_RESOURCE_PATH;
+ }
+
+ @Override
+ public TemplateLoader getTemplateLoader() {
+ return new ClassTemplateLoader(getClass(), TEMPLATE_PATH);
+ }
+
+ @Override
+ public List<LinkResource> getLinkResources() {
+ List<LinkResource> resources = new ArrayList<LinkResource>();
+ resources.add(new LinkResource("stylesheet", "style/search.css", this, 0));
+ resources.add(new LinkResource("stylesheet", "style/jquery-ui-1.8.11.custom.css", this, 1));
+ return resources;
+ }
+
+ @Override
+ public List<ScriptResource> getScriptResources() {
+ List<ScriptResource> resources = new ArrayList<ScriptResource>();
+ resources.add(new ScriptResource("text/javascript", "scripts/prettify/prettify.js", this, 0));
+ resources.add(new ScriptResource("text/javascript", "scripts/jit.js", this, 1));
+ resources.add(new ScriptResource("text/javascript", "scripts/jquery-1.5.1.min.js", this, 2));
+ resources
+ .add(new ScriptResource("text/javascript", "scripts/jquery-ui-1.8.11.custom.min.js", this, 3));
+ return resources;
+ }
+
+ @Override
+ public List<NavigationLink> getNavigationLinks() {
+ List<NavigationLink> links = new ArrayList<NavigationLink>();
+ links.add(new NavigationLink("contenthub", "contenthub", "/imports/contenthubDescription.ftl", 20));
+ links.add(new NavigationLink("contenthub/search", "search", "/imports/searchDescription.ftl", 70));
+ return links;
+ }
+
+ @Override
+ public BundleContext getBundleContext() {
+ return bundleContext;
+ }
+
+}
Added: incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/processor/FreemarkerViewProcessor.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/processor/FreemarkerViewProcessor.java?rev=1178765&view=auto
==============================================================================
--- incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/processor/FreemarkerViewProcessor.java (added)
+++ incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/processor/FreemarkerViewProcessor.java Tue Oct 4 12:13:49 2011
@@ -0,0 +1,230 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.stanbol.contenthub.web.processor;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintStream;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.ext.Provider;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.sun.jersey.api.view.Viewable;
+import com.sun.jersey.spi.resource.Singleton;
+import com.sun.jersey.spi.template.ViewProcessor;
+
+import freemarker.cache.ClassTemplateLoader;
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+
+/**
+ * Match a Viewable-named view with a Freemarker template.
+ *
+ * This class is based on the following original implementation: http://github.com/cwinters/jersey-freemarker/
+ *
+ * <p>
+ * You can configure the location of your templates with the context param 'freemarker.template.path'. If not
+ * assigned we'll use a default of <tt>WEB-INF/templates</tt>. Note that this uses Freemarker's
+ * {@link freemarker.cache.WebappTemplateLoader} to load/cache the templates, so check its docs (or crank up
+ * the logging under the 'freemarker.cache' package) if your templates aren't getting loaded.
+ * </p>
+ *
+ * <p>
+ * This will put your Viewable's model object in the template variable "it", unless the model is a Map. If so,
+ * the values will be assigned to the template assuming the map is of type <tt>Map<String,Object></tt>.
+ * </p>
+ *
+ * <p>
+ * There are a number of methods you can override to change the behavior, such as handling processing
+ * exceptions, changing the default template extension, or adding variables to be assigned to every template
+ * context.
+ * </p>
+ *
+ * @author Chris Winters <ch...@cwinters.com> // original code
+ * @author Olivier Grisel <og...@nuxeo.com> // ViewProcessor refactoring
+ */
+@Singleton
+@Provider
+public class FreemarkerViewProcessor implements ViewProcessor<Template> {
+
+ public static final String FREEMARKER_TEMPLATE_PATH_INIT_PARAM = "freemarker.template.path";
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ protected Configuration freemarkerConfig;
+
+ protected String rootPath;
+
+ @Context
+ protected ServletContext context;
+
+ public FreemarkerViewProcessor() {}
+
+ /**
+ * @return extension for templates, ".ftl" by default; if we don't see this at the end of your view we'll
+ * append it so we can find the template resource
+ */
+ protected String getDefaultExtension() {
+ return ".ftl";
+ }
+
+ /**
+ * Define additional variables to make available to the template.
+ *
+ * @param viewableVariables
+ * variables provided by whomever generated the viewable object; these are provided for
+ * reference only, there will be no effect if you modify this map
+ * @return new variables for the template context, which will override any defaults provided
+ */
+ protected Map<String,Object> getVariablesForTemplate(final Map<String,Object> viewableVariables) {
+ return Collections.emptyMap();
+ }
+
+ /**
+ * Catch any exception generated during template processing.
+ *
+ * @param t
+ * throwable caught
+ * @param templatePath
+ * path of template we're executing
+ * @param templateContext
+ * context use when evaluating this template
+ * @param out
+ * output stream from servlet container
+ * @throws IOException
+ * on any write errors, or if you want to rethrow
+ */
+ protected void onProcessException(final Throwable t,
+ final Template template,
+ final Map<String,Object> templateContext,
+ final OutputStream out) throws IOException {
+ log.error("Error processing freemarker template @ " + template.getName() + ": " + t.getMessage(), t);
+ out.write("<pre>".getBytes());
+ t.printStackTrace(new PrintStream(out));
+ out.write("</pre>".getBytes());
+ }
+
+ /**
+ * Modify freemarker configuration after we've created it and applied any settings from
+ * 'freemarker.properties' on the classpath.
+ *
+ * @param config
+ * configuration we've created so far
+ * @param context
+ * servlet context used to create the configuration
+ */
+ protected void assignFreemarkerConfig(final Configuration config, final ServletContext context) {
+ // TODO read those parameters from context instead of hardcoding them
+
+ // don't always put a ',' in numbers (e.g., id=2000 vs id=2,000)
+ config.setNumberFormat("0");
+
+ // don't look for list.en.ftl when list.ftl requested
+ config.setLocalizedLookup(false);
+
+ // don't cache for more that 2s
+ config.setTemplateUpdateDelay(2);
+ log.info("Assigned default freemarker configuration");
+ }
+
+ protected Configuration getConfig() {
+ if (freemarkerConfig == null) {
+ // deferred initialization of the freemarker config to ensure that
+ // the injected ServletContext is fully functional and for some
+ // reason the #getInitParam access does not work hence using the
+ // #getAttribute access after servlet init.
+ Configuration config = new Configuration();
+ if (context != null) {
+ rootPath = (String) context.getAttribute(FREEMARKER_TEMPLATE_PATH_INIT_PARAM);
+ }
+ if (rootPath == null || rootPath.trim().length() == 0) {
+ log.info("No 'freemarker.template.path' context-param, "
+ + "defaulting to '/WEB-INF/templates'");
+ rootPath = "/WEB-INF/templates";
+ }
+ rootPath = rootPath.replaceAll("/$", "");
+ config.setTemplateLoader(new ClassTemplateLoader(getClass(), rootPath));
+
+ // TODO: make the usage of a freemaker properties file an explicit
+ // parameter declared in the servlet context instead of magic
+ // classloading auto-detect. That way the application could
+ // explicitly override the defaults
+ final InputStream fmProps = context.getResourceAsStream("freemarker.properties");
+ boolean loadDefaults = true;
+ if (fmProps != null) {
+ try {
+ config.setSettings(fmProps);
+ log.info("Assigned freemarker configuration from 'freemarker.properties'");
+ loadDefaults = false;
+ } catch (Throwable t) {
+ log.warn("Failed to load/assign freemarker.properties, will"
+ + " use default settings instead: " + t.getMessage(), t);
+ }
+ }
+ if (loadDefaults) {
+ assignFreemarkerConfig(config, context);
+ }
+ freemarkerConfig = config;
+ }
+ return freemarkerConfig;
+ }
+
+ public Template resolve(final String path) {
+ // accept both '/path/to/template' and '/path/to/template.ftl'
+ final String defaultExtension = getDefaultExtension();
+ final String filePath = path.endsWith(defaultExtension) ? path : path + defaultExtension;
+ try {
+ return getConfig().getTemplate(filePath);
+ } catch (IOException e) {
+ log.error("Failed to load freemaker template: " + rootPath + filePath);
+ return null;
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public void writeTo(Template template, Viewable viewable, OutputStream out) throws IOException {
+ out.flush(); // send status + headers
+
+ Object model = viewable.getModel();
+ final Map<String,Object> vars = new HashMap<String,Object>();
+ if (model instanceof Map<?,?>) {
+ vars.putAll((Map<String,Object>) model);
+ } else {
+ vars.put("it", model);
+ }
+ // override custom variables if any
+ vars.putAll(getVariablesForTemplate(vars));
+
+ final OutputStreamWriter writer = new OutputStreamWriter(out);
+ try {
+ template.process(vars, writer);
+ } catch (Throwable t) {
+ onProcessException(t, template, vars, out);
+ }
+ }
+
+}
Added: incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/CNNImporterResource.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/CNNImporterResource.java?rev=1178765&view=auto
==============================================================================
--- incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/CNNImporterResource.java (added)
+++ incubator/stanbol/trunk/contenthub/web/src/main/java/org/apache/stanbol/contenthub/web/resources/CNNImporterResource.java Tue Oct 4 12:13:49 2011
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.stanbol.contenthub.web.resources;
+
+import static javax.ws.rs.core.MediaType.TEXT_HTML;
+
+import java.net.URI;
+import java.util.List;
+
+import javax.servlet.ServletContext;
+import javax.ws.rs.FormParam;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+
+import org.apache.stanbol.commons.web.base.ContextHelper;
+import org.apache.stanbol.commons.web.base.resource.BaseStanbolResource;
+import org.apache.stanbol.contenthub.helper.cnn.CNNImporter;
+import org.apache.stanbol.contenthub.web.search.model.TopicNews;
+
+import com.sun.jersey.api.view.Viewable;
+
+/**
+ *
+ * @author cihan
+ *
+ */
+@Path("/contenthub/import/cnn")
+public class CNNImporterResource extends BaseStanbolResource {
+
+ private CNNImporter cnnImporter;
+ private Object templateData = null;
+
+ public CNNImporterResource(@Context ServletContext context) {
+ cnnImporter = ContextHelper.getServiceFromContext(CNNImporter.class, context);
+ }
+
+ private TopicNews importCNNNews(String topic, Integer max, Boolean full) {
+ if (topic == null || topic.isEmpty()) {
+ return null;
+ }
+ if (max == null) {
+ max = 10;
+ }
+ if (full == null) {
+ full = false;
+ }
+
+ List<URI> uris = cnnImporter.importCNNNews(topic, max, full);
+ TopicNews tn = new TopicNews();
+ tn.setTopic(topic);
+ tn.setUris(uris);
+
+ return tn;
+ }
+
+ @GET
+ @Produces(TEXT_HTML)
+ public Response importCNNNewsHTML() {
+ return Response.ok(new Viewable("index", this), TEXT_HTML).build();
+ }
+
+ @POST
+ @Produces(TEXT_HTML)
+ public Response importCNNNewsHTMLPOST(@FormParam("topic") String topic,
+ @FormParam("max") Integer max,
+ @FormParam("full") Boolean full) {
+ this.templateData = importCNNNews(topic, max, full);
+ return Response.ok(new Viewable("index", this), TEXT_HTML).build();
+ }
+
+ public Object getTemplateData() {
+ return templateData;
+ }
+}