Implementation requirements
To import content from third-party repositories, you must supply the following implementations:
- HttpEndpointDatabase implementation that provides integration with a third-party service that uses an HTTP API; see HTTP endpoint database.
- ExternalItem implementation
- ExternalItemConverter implementation
ExternalItem implementation
An ExternalItem implementation represents an object referenced in a third-party repository. Extending ExternalItem, the implementation provides a bridge between data retrieved from the repository and an internal Brightspot object to which the data will be converted. An external item is identified with a unique ID, and you must implement ExternalItem#getExternalItemId to return a unique ID of an external item.
The following example shows an ExternalItem implementation that represents an image in the external Getty repository. Its setters and getters support creation of GettyImage objects with third-party data and conversion to internal Brightspot objects. Also included are methods that enable Brightspot to display Getty images in the search panel and in content edit forms.
1@Recordable.PreviewField("getPreviewFile")2@Recordable.SourceDatabaseProviderClass(GettyDatabaseProvider.class)3public class GettyImage extends ExternalItem {45@Indexed6private String title;78@ToolUi.DefaultSearchResult9private String gettyId;1011@Recordable.DisplayName("Image")12@ToolUi.NoteHtml("<span data-dynamic-html='${content.getUrlPreviewHtml()}'></span>")13private String url;1415private String caption;1617/* Getters and setters. */1819@Override20public String getExternalItemId() { return getGettyId(); }2122@Ignored(false)23@ToolUi.Hidden24public StorageItem getPreviewFile() {25return Optional.ofNullable(getUrl())26.map(url -> {27StorageItem file = new UrlStorageItem();28file.setPath(url);29return file;30})31.orElse(null);32}3334public String getUrlPreviewHtml() {35String url = getUrl();3637if (url == null) {38return "<span></span>";39}4041StringWriter stringWriter = new StringWriter();42HtmlWriter htmlWriter = new HtmlWriter(stringWriter);4344try {45htmlWriter.writeTag("img",46"src", url,47"style", htmlWriter.cssString(48"width", "auto",49"height", "500px",50"border", "solid 1px #cdcdcd",51"padding", "3px"));5253} catch (Exception error) {54/* Ignore. */55}5657return stringWriter.toString();58}5960}
- 1. Calls the internal getPreviewFile method, which returns a Getty image for display in the search panel results.
- 2. Specifies which database provider class to use to retrieve Getty images. The specified database provider creates an instance of the HttpEndpointDatabase implementation, which integrates with the Getty third-party service. For more information, see SourceDatabaseProvider implementation.
- 6. Specifies the four GettyImage fields. The fields will be populated with data retrieved from the Getty repository.
- 8. Displays the ID of the retrieved Getty image in the search results.
- 11. Uses Image as the label for the url field in the content edit form for the GettyImage type.
- 12. Uses a Java Expression Language (EL) statement that calls the internal getUrlPreviewHtml method. The annotation results in construction of an HTML tag that references the image in the Getty repository, enabling the image to be displayed in the GettyImage content edit form.
- 20. Required getExternalItemId implementation, which returns a unique ID for a Getty image.
- 24. Uses a Getty image URL to return the image that’s displayed in the search panel results.
- 45. Returns an HTML tag for Brightspot to display the image in the GettyImage content edit form.
ExternalItemConverter implementation
An ExternalItem implementation and associated database provider retrieve data from a third-party repository and make it visible in Brightspot as an ExternalItem type. For an external item to be completely imported into Brightspot, it must be converted to an internal Brightspot type. An external item converter must implement the ExternalItemConverter#convert method. The following example shows an ExternalItemConverter implementation that converts the external GettyImage type to an internal Brightspot Image type.
1public class GettyImageToImageConverter implements ExternalItemConverter<GettyImage> {23private static final Logger LOGGER = LoggerFactory.getLogger(GettyImageToImageConverter.class);45@Override6public Collection<?> convert(GettyImage gettyImage) {7Image image = new Image();8image.setTitle(gettyImage.getTitle());9image.setCaption(gettyImage.getCaption());1011String url = gettyImage.getUrl();12if (url == null) {13return Collections.singletonList(image);14}15url = url.substring(0, url.lastIndexOf("?"));1617try {18StorageItem file = StorageItem.Static.create();19file.setContentType("image/jpg");20file.setPath(new RandomUuidStorageItemPathGenerator().createPath(url));2122ByteArrayOutputStream outputStream = new ByteArrayOutputStream();23ImageIO.write(ImageIO.read(new URL(url).openStream()), "jpg", outputStream);2425file.setData(new ByteArrayInputStream(outputStream.toByteArray()));26file.save();27image.setFile(file);2829} catch (IOException error) {30LOGGER.error("Unable to save Getty Image file!", error);31}3233return Collections.singletonList(image);34}35}
- 6. Copies field data from a GettyImage object passed to the method into a new instance of Image.
- 11. Retrieves the URL that points to the Getty image.
- 17. Retrieves the Getty image from the external repository and saves it as a StorageItem object in the Brightspot repository.
- 33. Returns the image as an internal Brightspot type.