throw new IllegalArgumentException("The parsed ContentItem MUST NOT be NULL!"); ci.getLock().readLock().lock(); try { List<BlankNodeOrIRI> langAnnotations = getLanguageAnnotations(ci.getMetadata()); if(langAnnotations.isEmpty()){ //fallback return getString(ci.getMetadata(), ci.getUri(), DC_LANGUAGE); } else { return getString(ci.getMetadata(), langAnnotations.get(0), DC_LANGUAGE); ci.getLock().readLock().unlock();
for(IRI ref : includedMetadata){ try { Graph metadata = ci.getPart(ref, Graph.class); included.put(ref, metadata); } catch (RuntimeException e) { log.warn("Unable to add requested Metadata-ContentPart "+ref+" to" + "ContentItemBackend "+ci.getUri(),e); Graph graph; if(!included.isEmpty()){ graph = ci.getMetadata(); } else { Graph[] tcs = new Graph[included.size()+1]; tcs[0] = ci.getMetadata(); System.arraycopy(tcs, 1, included.values().toArray(), 0, included.size()); graph = new UnionGraph(tcs); this.readLock = ci.getLock().readLock();
ci.getLock().readLock().lock(); try { enhancementProperties = ci.getPart(REQUEST_PROPERTIES_URI, Map.class); } catch (NoSuchPartException e) { enhancementProperties = null; } finally{ ci.getLock().readLock().unlock(); ci.getLock().writeLock().lock(); try { //check again ... maybe an other thread has added this part enhancementProperties = ci.getPart(REQUEST_PROPERTIES_URI, Map.class); } catch (NoSuchPartException e) { enhancementProperties = ci.getPart(WEB_ENHANCEMENT_PROPERTIES_URI, Map.class); } catch (NoSuchPartException e2) { /*ignore*/} enhancementProperties = new HashMap<String,Object>(); //create ci.addPart(REQUEST_PROPERTIES_URI, enhancementProperties); } finally{ ci.getLock().writeLock().unlock();
public EngineException(EnhancementEngine ee, ContentItem ci, String message, Throwable cause) { super(String.format( "'%s' failed to process content item '%s' with type '%s': %s", ee.getClass().getSimpleName(), ci.getUri().getUnicodeString(), ci.getMimeType(), message == null ? cause : message), cause); this.enhancementEngine = ee; this.contentItem = ci; }
/** * Getter for the execution metadata content part. * @param contentItem the content item * @return the content part * @throws NoSuchPartException if no execution metadata are present in the * content part * @since 0.12.1 */ public static Graph getExecutionMetadata(ContentItem contentItem) { if(contentItem == null) { throw new IllegalArgumentException("The parsed ContentItme MUST NOT be NULL!"); } contentItem.getLock().readLock().lock(); try{ return contentItem.getPart(CHAIN_EXECUTION, Graph.class); }finally{ contentItem.getLock().readLock().unlock(); } }
/** * The ContentItem MUST NOT allow to replace the main content part (the * Blob stored at index 0) */ @Test(expected=IllegalArgumentException.class) public void replaceMainPart() throws IOException{ ContentItem ci = createContentItem(contentSource); IRI mainPart = ci.getPartUri(0); ci.addPart(mainPart, new Date()); } @Test(expected=IllegalArgumentException.class)
@Override public void computeEnhancements(ContentItem ci) throws EngineException { HtmlExtractor extractor = new HtmlExtractor(htmlExtractorRegistry, htmlParser); Graph model = new SimpleGraph(); ci.getLock().readLock().lock(); try { extractor.extract(ci.getUri().getUnicodeString(), ci.getStream(),null, ci.getMimeType(), model); } catch (ExtractorException e) { throw new EngineException("Error while processing ContentItem " + ci.getUri()+" with HtmlExtractor",e); } finally { ci.getLock().readLock().unlock(); } ClerezzaRDFUtils.urifyBlankNodes(model); // make the model single rooted if (singleRootRdf) { ClerezzaRDFUtils.makeConnected(model,ci.getUri(),new IRI(NIE_NS+"contains")); } //add the extracted triples to the metadata of the ContentItem ci.getLock().writeLock().lock(); try { LOG.info("Model: {}",model); ci.getMetadata().addAll(model); model = null; } finally { ci.getLock().writeLock().unlock(); } }
/** * Create a new instance with the types enhancer:Enhancement and * enhancer:EntityAnnotation in the metadata-graph of the content * item along with default properties (dc:creator and dc:created) and return * the IRI of the extraction so that engines can further add * * @param ci the ContentItem being under analysis * @param engine the Engine performing the analysis * @return the URI of the new enhancement instance */ public static IRI createEntityEnhancement(ContentItem ci, EnhancementEngine engine){ return createEntityEnhancement(ci.getMetadata(), engine, new IRI(ci.getUri().getUnicodeString())); } /**
@Override public String toString() { return "EnhancementJob for ContentItem "+contentItem.getUri(); }
@Override public void computeEnhancements(ContentItem ci) throws EngineException { InputStream in = ci.getBlob().getStream(); XMPPacketScanner scanner = new XMPPacketScanner(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { scanner.parse(in, baos); } catch (IOException e) { throw new EngineException(e); } byte[] bytes = baos.toByteArray(); if (bytes.length > 0) { Graph model = new IndexedGraph(); parser.parse(model, new ByteArrayInputStream(bytes), "application/rdf+xml"); GraphNode gn = new GraphNode( new IRI("http://relative-uri.fake/"), model); gn.replaceWith(ci.getUri()); ci.getLock().writeLock().lock(); try { LOG.info("Model: {}",model); ci.getMetadata().addAll(model); } finally { ci.getLock().writeLock().unlock(); } } }
@Test public void addingAndRetrieving() throws IOException{ ContentItem ci = createContentItem(contentSource); assertNotNull(ci); assertNotNull(ci.getUri()); IRI partUri = new IRI("http://foo/"); Date someObject = new Date(); ci.addPart(partUri, someObject); ci.getMetadata().add(new TripleImpl(ci.getUri(), new IRI("http://example.org/ontology#hasPart"), partUri)); ci.getMetadata().add(new TripleImpl(partUri, new IRI("http://example.org/ontology#isPartOf"),ci.getUri())); assertEquals(someObject, ci.getPart(partUri, Date.class)); assertEquals(someObject, ci.getPart(1, Date.class)); assertEquals(partUri, ci.getPartUri(1)); assertEquals(new IRI(ci.getUri().getUnicodeString()+"_main"), ci.getPartUri(0)); try { ci.getPart(2, Object.class); assertTrue("Requesting non existance part MUST throw an NoSuchPartException", false); } catch (NoSuchPartException e) {/* expected*/} try { ci.getPart(new IRI("http://foo/nonexisting"), Object.class); assertTrue("Requesting non existance part MUST throw an NoSuchPartException", false); } catch (NoSuchPartException e) {/* expected*/} try { ci.getPartUri(2); assertTrue("Requesting non existance part MUST throw an NoSuchPartException", false); } catch (NoSuchPartException e) {/* expected*/} //finally log the toString log.info("toString: {}",ci); } @Test(expected=IllegalArgumentException.class)
if (contentPart == null) { throw new IllegalStateException("No ContentPart with an supported Mimetype '" + SUPPORTED_MIMETYPES + "' found for ContentItem " + ci.getUri() + ": This is also checked in the canEnhance method! -> This " + "indicated an Bug in the implementation of the " ci.getLock().writeLock().lock(); try { holder = ci.getPart(uimaIRI, FeatureStructureListHolder.class); } catch (NoSuchPartException e) { holder = new FeatureStructureListHolder(); logger.info("Adding FeatureSet List Holder content part with uri:" + uimaUri); ci.addPart(uimaIRI, holder); logger.info(uimaUri + " content part added."); } finally { ci.getLock().writeLock().unlock(); ci.getLock().writeLock().lock(); try { holder.addFeatureStructureList(ussc.getSourceName(), featureSetList); } finally { ci.getLock().writeLock().unlock();
private void addSolrSpecificFields(ContentItem ci, SolrInputDocument doc) { String title = null; try { title = ci.getPart(TITLE_URI, String.class); } catch (NoSuchPartException e) { title = ci.getUri().getUnicodeString(); } doc.addField(SolrFieldName.TITLE.toString(), title); if (ci.getMetadata() != null) { addSemanticFields(ci, doc); addAnnotatedEntityFieldNames(ci, doc); } else { log.debug("There are no enhancements for the content item {}", ci.getUri().getUnicodeString()); } }
@Override public int canEnhance(ContentItem ci) throws EngineException { /* * Being a post-processing engine, the Refactor can enhance only content items that are previously * enhanced by other enhancement engines. */ return ci.getMetadata() == null ? CANNOT_ENHANCE : ENHANCE_SYNCHRONOUS; }
/** * Creates a {@link RDFBackend} over the {@link ContentItem#getMetadata() * metadata} and all {@link ContentItem#getPart(int, Class) content parts} * compatible to {@link Graph} * @param ci the content item * @param includeAdditionalMetadata if <code>true</code> the {@link RDFBackend} * will also include RDF data stored in content parts */ public ContentItemBackend(ContentItem ci, boolean includeAdditionalMetadata){ included = includeAdditionalMetadata ? unmodifiableMap(getContentParts(ci, Graph.class)) : EMPTY_INCLUDED; Graph graph; if(included.isEmpty()){ graph = ci.getMetadata(); } else { Graph[] tcs = new Graph[included.size()+1]; tcs[0] = ci.getMetadata(); System.arraycopy(included.values().toArray(), 0, tcs, 1, included.size()); graph = new UnionGraph(tcs); } backend = new ClerezzaBackend(graph); this.ci = ci; this.readLock = ci.getLock().readLock(); } /**
throw new IllegalArgumentException("The parsed contentItem and executionPlan MUST NOT be NULL"); this.readLock = contentItem.getLock().readLock(); this.writeLock = contentItem.getLock().writeLock(); executionMetadata = initExecutionMetadataContentPart(contentItem); if(executionMetadata.isEmpty()){ contentItem.getUri(), chainName, isDefaultChain)); chainExecutionNode = getChainExecution(executionMetadata, contentItem.getUri()); executionPlanNode = getExecutionPlanNode(executionMetadata, chainExecutionNode); executionMetadata.addAll(executionPlan);
/** * Getter for the lock used to synchronise read/write to this enhancement job. This returns the lock * provided by {@link ContentItem#getLock()} * * @return the read/write lock for this enhancement job */ public final ReadWriteLock getLock() { return contentItem.getLock(); }
IRI cpUri = null; int index = 0; ci.getLock().readLock().lock(); try { do { try { cpUri = ci.getPartUri(index); index++; try { blobs.put(cpUri, ci.getPart(cpUri, clazz)); } catch (ClassCastException e) { ci.getLock().readLock().unlock();
/** * Enhances the parsed ContentItem * @param ci the content item to enhance * @param reqProp the request properties or <code>null</code> if none * @throws EnhancementException */ protected void enhance(ContentItem ci, Map<String,Object> reqProp) throws EnhancementException { if (jobManager != null) { jobManager.enhanceContent(ci, getChain()); } Graph graph = ci.getMetadata(); Boolean includeExecutionMetadata = RequestPropertiesHelper.isIncludeExecutionMetadata(reqProp); if (includeExecutionMetadata != null && includeExecutionMetadata.booleanValue()) { try { graph.addAll(ci.getPart(ExecutionMetadata.CHAIN_EXECUTION, Graph.class)); } catch (NoSuchPartException e) { // no executionMetadata available } } }
@Override public int canEnhance(ContentItem ci) throws EngineException { // check if content is present try { if ((ContentItemHelper.getText(ci.getBlob()) == null) || (ContentItemHelper.getText(ci.getBlob()).trim().isEmpty())) { return CANNOT_ENHANCE; } } catch (IOException e) { log.error("Failed to get the text for " + "enhancement of content: " + ci.getUri(), e); throw new InvalidContentException(this, ci, e); } // default enhancement is synchronous enhancement return ENHANCE_SYNCHRONOUS; }