Implementation Requirements

To import content from third-party repositories, you must supply the following implementations:

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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@Recordable.PreviewField("getPreviewFile")
@Recordable.SourceDatabaseProviderClass(GettyDatabaseProvider.class)
public class GettyImage extends ExternalItem {

   @Indexed
   private String title;

   @ToolUi.DefaultSearchResult
   private String gettyId;

   @Recordable.DisplayName("Image")
   @ToolUi.NoteHtml("<span data-dynamic-html='${content.getUrlPreviewHtml()}'></span>")
   private String url;

   private String caption;

   public String getTitle() { return title; }
   public void setTitle(String title) { this.title = title; }

   public String getGettyId() { return gettyId; }
   public void setGettyId(String gettyId) { this.gettyId = gettyId; }

   public String getUrl() { return url; }
   public void setUrl(String url) { this.url = url; }

   public String getCaption() { return caption; }
   public void setCaption(String caption) { this.caption = caption; }

   @Override
   public String getExternalItemId() { return getGettyId(); }

   @Ignored(false)
   @ToolUi.Hidden
   public StorageItem getPreviewFile() {
      return Optional.ofNullable(getUrl())
             .map(url -> {
                 StorageItem file = new UrlStorageItem();
                 file.setPath(url);
                 return file;
             })
             .orElse(null);
   }

   public String getUrlPreviewHtml() {
      String url = getUrl();

      if (url == null) {
         return "<span></span>";
      }

      StringWriter stringWriter = new StringWriter();
      HtmlWriter htmlWriter = new HtmlWriter(stringWriter);

      try {
         htmlWriter.writeTag("img",
                 "src", url,
                 "style", htmlWriter.cssString(
                         "width", "auto",
                         "height", "500px",
                         "border", "solid 1px #cdcdcd",
                         "padding", "3px"));

      } catch (Exception error) {
         // Ignore.
      }

      return stringWriter.toString();
   }

}

In the previous snippet—

  • Line 1 is the @Recordable.PreviewField annotation that tells Brightspot to call the internal getPreviewFile method (line 34), which returns a Getty image for display in the search panel results.
  • Line 2 is the @Recordable.SourceDatabaseClass annotation that tells Brightspot 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.
  • Lines 5–15 specify the GettyImage fields. The fields will be populated with data retrieved from the Getty repository.
    • The @ToolUi.DefaultSearchResult annotation tells Brightspot to show the Id of the retrieved Getty image in the search results.
    • The @Recordable.DisplayName annotation tells Brightspot to use “Image” as the label for the url field in the content edit form for the GettyImage type.
    • The @ToolUi.NoteHtml annotation uses a Java Expression Language (EL) statement that tells Brightspot to call the internal getUrlPreviewHtml method (line 44). The annotation results in construction of an HTML <img> tag that references the image in the Getty repository, enabling the image to be displayed in the GettyImage content edit form.
  • Lines 30 is the required getExternalItemId implementation, which returns a unique Id for a Getty image.
  • Lines 34–42 use a Getty image URL to return the image that’s displayed in the search panel results.
  • Lines 44–68 return an HTML <img> 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class GettyImageToImageConverter implements ExternalItemConverter<GettyImage> {

   private static final Logger LOGGER = LoggerFactory.getLogger(GettyImageToImageConverter.class);

   @Override
   public Collection<?> convert(GettyImage gettyImage) {
      Image image = new Image();
      image.setTitle(gettyImage.getTitle());
      image.setCaption(gettyImage.getCaption());

      String url = gettyImage.getUrl();
      if (url == null) {
         return Collections.singletonList(image);
      }
      url = url.substring(0, url.lastIndexOf("?"));

      try {
         StorageItem file = StorageItem.Static.create();
         file.setContentType("image/jpg");
         file.setPath(new RandomUuidStorageItemPathGenerator().createPath(url));

         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         ImageIO.write(ImageIO.read(new URL(url).openStream()), "jpg", outputStream);

         file.setData(new ByteArrayInputStream(outputStream.toByteArray()));
         file.save();
         image.setFile(file);

      } catch (IOException error) {
         LOGGER.error("Unable to save Getty Image file!", error);
      }

      return Collections.singletonList(image);
   }
}

In the previous snippet—

  • Lines 7–9 copy field data from a GettyImage object passed to the method into a new instance of Image.
  • Lines 11–15 get the URL that points to the Getty image.
  • Lines 17–31 retrieve the Getty image from the external repository and save it as a StorageItem object in the Brightspot repository.
  • Line 33 returns the image as an internal Brightspot type.