Skip to content

Commit

Permalink
[ issue #12 ] Additional messages in MessageCatalog
Browse files Browse the repository at this point in the history
  • Loading branch information
agazzarini committed Apr 9, 2015
1 parent ef8ae73 commit 83bf6a7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -28,6 +29,8 @@
import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.gazzax.labs.solrdf.Names;
import org.gazzax.labs.solrdf.graph.SolRDFDatasetGraph;
import org.gazzax.labs.solrdf.log.MessageCatalog;
import org.gazzax.labs.solrdf.log.MessageFactory;

import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.NodeFactory;
Expand All @@ -36,7 +39,7 @@
import com.hp.hpl.jena.sparql.core.Quad;

/**
* A subclass of {@link UpdateRequestHandler} for handlong RDF bulk loadings.
* A subclass of {@link UpdateRequestHandler} for handling RDF bulk loadings.
*
* @author Andrea Gazzarini
* @since 1.0
Expand Down Expand Up @@ -81,6 +84,11 @@ public void run() {
dataset.add(iterator.next());
}
}

@Override
public String toString() {
return "Quads Loader";
};
};

final ContentStreamLoader triplesLoader = new ContentStreamLoader() {
Expand All @@ -107,7 +115,10 @@ public void run() {
}
}
});


// Graph Store Protocol indicates the target graph URI separately.
// So the incoming Content-type here is one that maps "Triples Loader" but
// the indexed tuple could be a Quad.
final String graphUri = request.getParams().get(Names.GRAPH_URI_ATTRIBUTE_NAME);

final DatasetGraph dataset = new SolRDFDatasetGraph(request, response, null, null);
Expand All @@ -118,6 +129,11 @@ public void run() {
defaultGraph.add(iterator.next());
}
}

@Override
public String toString() {
return "Triples Loader";
};
};

@Override
Expand All @@ -127,22 +143,33 @@ public void load(
final ContentStream stream,
final UpdateRequestProcessor processor) throws Exception {

// Default ContentStream implementation starts reading the stream and
// if it starts with '<' then it assumes a content type of "application/xml",
// if it starts with '{' then it assumes a content type of "application/json"
// This behaviour is wrong is SolRDF and maybe we need a custom ContentStream here
// At the moment this is just a workaround:
final String contentType = stream.getContentType() != null
&& !"application/xml".equals(stream.getContentType())
&& !"application/json".equals(stream.getContentType())
? stream.getContentType()
: request.getParams().get(UpdateParams.ASSUME_CONTENT_TYPE);

log.debug(MessageCatalog._00094_BULK_LOADER_CT, contentType);

final Lang lang = RDFLanguages.contentTypeToLang(contentType);
if (lang == null) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown Content-type: " + contentType);
final String message = MessageFactory.createMessage(MessageCatalog._00095_INVALID_CT, contentType);
log.error(message);
throw new SolrException(ErrorCode.BAD_REQUEST, message);
}

final ContentStreamLoader delegate =
(lang == Lang.NQ || lang == Lang.NQUADS || lang == Lang.TRIG)
? quadsLoader
: triplesLoader;

log.debug(MessageCatalog._00096_SELECTED_BULK_LOADER, contentType, delegate);

delegate.load(
request,
response,
Expand Down Expand Up @@ -191,6 +218,10 @@ protected Map<String, ContentStreamLoader> createDefaultLoaders(final NamedList
}
registry.put(WebContent.contentTypeSPARQLUpdate, new Sparql11UpdateRdfDataLoader());

if (log.isDebugEnabled()) {
prettyPrint(registry);
}

return registry;
}

Expand All @@ -203,4 +234,13 @@ public String getDescription() {
public String getSource() {
return "$https://github.com/agazzarini/SolRDF/blob/master/solrdf/src/main/java/org/gazzax/labs/solrdf/handler/update/RdfBulkUpdateRequestHandler.java $";
}

/**
* Debugs the registry content.
*/
void prettyPrint(final Map<String, ContentStreamLoader> registry) {
for (final Entry<String, ContentStreamLoader> entry : registry.entrySet()) {
log.debug(MessageCatalog._00097_BULK_LOADER_REGISTRY_ENTRY, entry.getKey(), entry.getValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.jena.riot.WebContent;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.handler.UpdateRequestHandler;
import org.apache.solr.handler.loader.ContentStreamLoader;
import org.gazzax.labs.solrdf.log.MessageCatalog;

/**
* An {@link UpdateRequestHandler} implementation for handling SPARQL updates.
Expand All @@ -24,6 +26,11 @@ protected Map<String, ContentStreamLoader> createDefaultLoaders(final NamedList
final Map<String, ContentStreamLoader> registry = new HashMap<String, ContentStreamLoader>();
registry.put("application/rdf+xml", new Sparql11UpdateRdfDataLoader());
registry.put(WebContent.contentTypeSPARQLUpdate, new Sparql11UpdateRdfDataLoader());

if (log.isDebugEnabled()) {
prettyPrint(registry);
}

return registry;
}

Expand All @@ -36,4 +43,14 @@ public String getDescription() {
public String getSource() {
return "https://github.com/agazzarini/SolRDF";
}

/**
* Debugs the registry content.
*/
void prettyPrint(final Map<String, ContentStreamLoader> registry) {
for (final Entry<String, ContentStreamLoader> entry : registry.entrySet()) {
log.debug(MessageCatalog._00098_UPDATE_HANDLER_REGISTRY_ENTRY, entry.getKey(), entry.getValue());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.gazzax.labs.solrdf.graph.SolRDFDatasetGraph;
import org.gazzax.labs.solrdf.log.Log;
import org.gazzax.labs.solrdf.log.MessageCatalog;
import org.slf4j.LoggerFactory;

import com.hp.hpl.jena.update.UpdateAction;
import com.hp.hpl.jena.update.UpdateFactory;
Expand All @@ -25,7 +28,7 @@
* @since 1.0
*/
class Sparql11UpdateRdfDataLoader extends ContentStreamLoader {
static final int READ_BUFFER_DEFAULT_SIZE = 512;
private final static Log LOGGER = new Log(LoggerFactory.getLogger(Sparql11UpdateRdfDataLoader.class));

@Override
public void load(
Expand All @@ -36,12 +39,12 @@ public void load(

final String q = request.getParams().get(CommonParams.Q);
if (isNullOrEmpty(q)) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Invalid query : " + q);
LOGGER.error(MessageCatalog._00099_INVALID_UPDATE_QUERY);
throw new SolrException(ErrorCode.BAD_REQUEST, MessageCatalog._00099_INVALID_UPDATE_QUERY);
}

UpdateAction.execute(
UpdateFactory.create(
request.getParams().get(CommonParams.Q)),
new SolRDFDatasetGraph(request, response));
UpdateFactory.create(q),
new SolRDFDatasetGraph(request, response));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public interface MessageCatalog {
String _00091_NULL_QUERY_OR_EXECUTION = PREFIX + "-00091> : Query or QueryExecution cannot be null.";
String _00092_NEGOTIATED_CONTENT_TYPE = PREFIX + "-00092> : Query type %s, incoming Accept header is %s, applied Content-type is %s";
String _00093_GSP_REQUEST = PREFIX + "-00093> : Incoming GraphStoreProtocol %s request on %s Graph.";

String _00094_BULK_LOADER_CT = PREFIX + "-00094> : Content-type of the incoming stream: %s";
String _00095_INVALID_CT = PREFIX + "-00095> : Unsupported / Unknown Content-type: %s";
String _00096_SELECTED_BULK_LOADER = PREFIX + "-00096> : Incoming stream with Content-type %s has been associated with %s";
String _00097_BULK_LOADER_REGISTRY_ENTRY = PREFIX + "-00097> : New Bulk Loader registry entry: %s => %s";
String _00098_UPDATE_HANDLER_REGISTRY_ENTRY = PREFIX + "-00098> : New Bulk Loader registry entry: %s => %s";
String _00099_INVALID_UPDATE_QUERY = PREFIX + "-00099> : Invalid (empty or null) query.";
}

0 comments on commit 83bf6a7

Please sign in to comment.