-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/met 5806 support embeddable resources profile (#675)
* MET-5806 add ombed detection for webresources * MET-5806 add tika detection for oEmbedded items * MET-5806 refactor unit tests * MET-5806 add complementary unit tests * MET-5806 correct file detection implementation and update unit tests * MET-5806 added code review comments * MET-5806 remove unused imports and optimizations * MET-5806 remove chain of responsability pattern, start refactor * MET-5806 add xpath condition for oembed objects, pending fix unit tests * MET-5806 updated textprocessor and oembedprocessor with unit tests * MET-5806 add sonarqube recommendations * MET-5806 sonarcloud recommendations improvements * MET-5806 sonarcloud improvement recommendations part II * MET-5806 sonarcloud recomendations part III * MET-5806 Fix sonar issues --------- Co-authored-by: Simon Tzanakis <[email protected]>
- Loading branch information
1 parent
6457c23
commit 09d064e
Showing
32 changed files
with
2,037 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
303 changes: 177 additions & 126 deletions
303
...s-media-service/src/main/java/eu/europeana/metis/mediaprocessing/RdfDeserializerImpl.java
Large diffs are not rendered by default.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
metis-media-service/src/main/java/eu/europeana/metis/mediaprocessing/RdfXpathConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package eu.europeana.metis.mediaprocessing; | ||
|
||
/** | ||
* Rdf xpath string constants. | ||
*/ | ||
public final class RdfXpathConstants { | ||
|
||
public static final String RDF_NAMESPACE = "/rdf:RDF"; | ||
public static final String ORE_AGGREGATION = RDF_NAMESPACE + "/ore:Aggregation"; | ||
public static final String EDM_OBJECT = ORE_AGGREGATION + "/edm:object/@rdf:resource"; | ||
public static final String EDM_IS_SHOWN_BY = ORE_AGGREGATION + "/edm:isShownBy/@rdf:resource"; | ||
public static final String EDM_HAS_VIEW = ORE_AGGREGATION + "/edm:hasView/@rdf:resource"; | ||
public static final String EDM_IS_SHOWN_AT = ORE_AGGREGATION + "/edm:isShownAt/@rdf:resource"; | ||
|
||
private RdfXpathConstants() {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...-service/src/main/java/eu/europeana/metis/mediaprocessing/extraction/OEmbedProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package eu.europeana.metis.mediaprocessing.extraction; | ||
|
||
import static eu.europeana.metis.mediaprocessing.extraction.oembed.OEmbedValidation.checkValidWidthAndHeightDimensions; | ||
import static eu.europeana.metis.mediaprocessing.extraction.oembed.OEmbedValidation.getDurationFromModel; | ||
import static eu.europeana.metis.mediaprocessing.extraction.oembed.OEmbedValidation.getOEmbedModelFromJson; | ||
import static eu.europeana.metis.mediaprocessing.extraction.oembed.OEmbedValidation.getOEmbedModelFromXml; | ||
import static eu.europeana.metis.mediaprocessing.extraction.oembed.OEmbedValidation.isValidOEmbedPhotoOrVideo; | ||
|
||
import eu.europeana.metis.mediaprocessing.exception.MediaExtractionException; | ||
import eu.europeana.metis.mediaprocessing.extraction.oembed.OEmbedModel; | ||
import eu.europeana.metis.mediaprocessing.model.ImageResourceMetadata; | ||
import eu.europeana.metis.mediaprocessing.model.Resource; | ||
import eu.europeana.metis.mediaprocessing.model.ResourceExtractionResult; | ||
import eu.europeana.metis.mediaprocessing.model.ResourceExtractionResultImpl; | ||
import eu.europeana.metis.mediaprocessing.model.VideoResourceMetadata; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Locale; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The type Oembed processor. | ||
*/ | ||
public class OEmbedProcessor implements MediaProcessor { | ||
|
||
/** | ||
* The constant LOGGER. | ||
*/ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(OEmbedProcessor.class); | ||
|
||
/** | ||
* Process a resource by extracting the metadata from the content. | ||
* | ||
* @param resource The resource to process. Note that the resource may not have content (see | ||
* {@link MediaExtractorImpl#shouldDownloadForFullProcessing(String)}). | ||
* @param detectedMimeType The mime type that was detected for this resource (may deviate from the mime type that was provided | ||
* by the server and which is stored in {@link Resource#getProvidedMimeType()}). | ||
* @param mainThumbnailAvailable Whether the main thumbnail for this record is available. This may influence the decision on | ||
* whether to generate a thumbnail for this resource. | ||
* @return The result of the processing. | ||
* @throws MediaExtractionException In case something went wrong during the extraction. | ||
*/ | ||
@Override | ||
public ResourceExtractionResult extractMetadata(Resource resource, String detectedMimeType, boolean mainThumbnailAvailable) | ||
throws MediaExtractionException { | ||
|
||
ResourceExtractionResult resourceExtractionResult; | ||
// the content for this oembed needs to be downloaded to be examined | ||
if (resource.getContentPath() != null) { | ||
try { | ||
OEmbedModel embedModel = null; | ||
if (detectedMimeType.startsWith("application/json")) { | ||
embedModel = getOEmbedModelFromJson(Files.readAllBytes(Paths.get(resource.getContentPath().toString()))); | ||
} else if (detectedMimeType.startsWith("application/xml")) { | ||
embedModel = getOEmbedModelFromXml(Files.readAllBytes(Paths.get(resource.getContentPath().toString()))); | ||
} | ||
if (isValidOEmbedPhotoOrVideo(embedModel)) { | ||
checkValidWidthAndHeightDimensions(embedModel, resource.getResourceUrl()); | ||
resourceExtractionResult = getResourceExtractionResult(resource, detectedMimeType, embedModel); | ||
} else { | ||
LOGGER.warn("No oembed model found"); | ||
resourceExtractionResult = null; | ||
} | ||
} catch (IOException e) { | ||
throw new MediaExtractionException("Unable to read OEmbedded resource", e); | ||
} | ||
} else { | ||
resourceExtractionResult = null; | ||
} | ||
|
||
return resourceExtractionResult; | ||
} | ||
|
||
/** | ||
* Process a resource by copying the metadata from the input without performing any extraction. | ||
* | ||
* @param resource The resource to process. The resource is not expected to have content. | ||
* @param detectedMimeType The mime type that was detected for this resource (may deviate from the mime type that was provided | ||
* by the server and which is stored in {@link Resource#getProvidedMimeType()}). | ||
* @return The result of the processing. | ||
* @throws MediaExtractionException In case something went wrong during the extraction. | ||
*/ | ||
@Override | ||
public ResourceExtractionResult copyMetadata(Resource resource, String detectedMimeType) throws MediaExtractionException { | ||
return null; | ||
} | ||
|
||
/** | ||
* @return Whether the processor needs the downloaded resource for full processing. | ||
*/ | ||
@Override | ||
public boolean downloadResourceForFullProcessing() { | ||
return true; | ||
} | ||
|
||
private ResourceExtractionResult getResourceExtractionResult(Resource resource, String detectedMimeType, | ||
OEmbedModel oEmbedModel) throws MediaExtractionException { | ||
ResourceExtractionResult resourceExtractionResult; | ||
if (oEmbedModel != null) { | ||
switch (oEmbedModel.getType().toLowerCase(Locale.US)) { | ||
case "photo" -> { | ||
ImageResourceMetadata imageResourceMetadata = new ImageResourceMetadata(detectedMimeType, | ||
resource.getResourceUrl(), | ||
resource.getProvidedFileSize(), oEmbedModel.getWidth(), oEmbedModel.getHeight(), null, null, null); | ||
resourceExtractionResult = new ResourceExtractionResultImpl(imageResourceMetadata); | ||
} | ||
case "video" -> { | ||
Double duration = getDurationFromModel(oEmbedModel); | ||
VideoResourceMetadata videoResourceMetadata = new VideoResourceMetadata(detectedMimeType, | ||
resource.getResourceUrl(), | ||
resource.getProvidedFileSize(), duration, null, oEmbedModel.getWidth(), oEmbedModel.getHeight(), null, null); | ||
resourceExtractionResult = new ResourceExtractionResultImpl(videoResourceMetadata); | ||
} | ||
default -> resourceExtractionResult = null; | ||
} | ||
} else { | ||
resourceExtractionResult = null; | ||
} | ||
return resourceExtractionResult; | ||
} | ||
} |
Oops, something went wrong.