View Javadoc

1   package com.atlassian.plugin.webresource.transformer;
2   
3   import com.atlassian.plugin.servlet.DownloadException;
4   import com.atlassian.plugin.servlet.DownloadableResource;
5   import org.apache.commons.io.IOUtils;
6   
7   import java.io.*;
8   
9   /**
10   * Abstract class that makes it easy to create transforms that go from string to string.  Override
11   * {@link #getEncoding()} to customize the character encoding of the underlying content and transformed content.
12   * <p>
13   * For example, here is a minimal transformer that prepends text to the underlying resource:
14   * <pre>
15   * public class PrefixTransformer implements WebResourceTransformer
16   *   {
17   *       public DownloadableResource transform(Element configElement, ResourceLocation location, String filePath, DownloadableResource nextResource)
18   *       {
19   *           return new AbstractStringTransformedDownloadableResource(nextResource)
20   *           {
21   *               protected String transform(String originalContent)
22   *               {
23   *                   return "Prefix: "  + originalContent;
24   *               }
25   *           };
26   *       }
27   *    }
28   * </pre>
29   *
30   * @since 2.5.0
31   */
32  public abstract class AbstractStringTransformedDownloadableResource extends AbstractTransformedDownloadableResource
33  {
34  
35      public AbstractStringTransformedDownloadableResource(DownloadableResource originalResource)
36      {
37          super(originalResource);
38      }
39  
40      public void streamResource(OutputStream out) throws DownloadException
41      {
42          ByteArrayOutputStream delegateOut = new ByteArrayOutputStream();
43          try
44          {
45              getOriginalResource().streamResource(delegateOut);
46          }
47          catch (DownloadException e)
48          {
49              throw e;
50          }
51          try
52          {
53              String originalContent = new String(delegateOut.toByteArray(), getEncoding());
54              String transformedContent = transform(originalContent);
55              IOUtils.copy(new StringReader(transformedContent.toString()), out, getEncoding());
56          }
57          catch (UnsupportedEncodingException e)
58          {
59              // should never happen
60              throw new DownloadException(e);
61          }
62          catch (IOException e)
63          {
64              throw new DownloadException("Unable to stream to the output", e);
65          }
66  
67      }
68  
69      /**
70       * @return the encoding used to read the original resource and encode the transformed string
71       */
72      protected String getEncoding()
73      {
74          return "UTF-8";
75      }
76  
77      /**
78       * Override this method to transform the original content into a new format.
79       *
80       * @param originalContent The original content from the original downloadable resource.
81       * @return The transformed content you want returned
82       */
83      protected abstract String transform(String originalContent);
84  }