View Javadoc
1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package tools.gsf.facade.uri;
18  
19  import COM.FutureTense.Interfaces.ICS;
20  import com.fatwire.assetapi.data.AssetData;
21  import com.fatwire.assetapi.data.AssetId;
22  import com.fatwire.assetapi.data.AttributeData;
23  import com.fatwire.assetapi.data.BlobObject;
24  import com.fatwire.assetapi.data.BlobObject.BlobAddress;
25  import org.apache.commons.lang3.StringUtils;
26  import tools.gsf.facade.assetapi.AttributeDataUtils;
27  import tools.gsf.facade.runtag.render.GetBlobUrl;
28  import tools.gsf.runtime.DebugHelper;
29  
30  /**
31   * Builder support for blob urls.
32   * <pre>
33   * new BlobUriBuilder(blob).mimeType(&quot;image/jpeg&quot;).parent(&quot;12345&quot;).toURI(ics);
34   * </pre>
35   *
36   * @author Dolf Dijkstra
37   * @since Feb 15, 2011
38   */
39  public class BlobUriBuilder {
40  
41      private GetBlobUrl tag = new GetBlobUrl();
42      private int n = 1;
43  
44      /**
45       * Constructor that accepts AssetData and an attribute name.
46       *
47       * @param data          the asset.
48       * @param attributeName the name of the attribute containing the blob.
49       */
50      public BlobUriBuilder(final AssetData data, String attributeName) {
51          AttributeData attr = data.getAttributeData(attributeName);
52          if (attr == null) {
53              throw new IllegalStateException("Can't find attribute " + attributeName + " on asset "
54                      + DebugHelper.toString(data.getAssetId()));
55          }
56          BlobObject blob = AttributeDataUtils.asBlob(attr);
57          if (blob == null) {
58              throw new IllegalStateException("Attribute " + attributeName + " on asset "
59                      + DebugHelper.toString(data.getAssetId()) + " is not found.");
60          }
61  
62          populateTag(blob.getBlobAddress());
63  
64          parent(data.getAssetId());
65      }
66  
67      /**
68       * Constructor accepting a BlobObject.
69       *
70       * @param blob blob
71       */
72      public BlobUriBuilder(final BlobObject blob) {
73          this(blob.getBlobAddress());
74      }
75  
76      /**
77       * Constructor accepting a BlobAddress.
78       *
79       * @param address blob address
80       */
81      public BlobUriBuilder(final BlobAddress address) {
82  
83          populateTag(address);
84      }
85  
86      /**
87       * @param address
88       */
89      private void populateTag(final BlobAddress address) {
90          tag.setBlobCol(address.getColumnName());
91          tag.setBlobKey(address.getIdentifierColumnName());
92          tag.setBlobTable(address.getTableName());
93          tag.setBlobWhere(address.getIdentifier().toString());
94      }
95  
96      /**
97       * @param ics Content Server context object
98       * @return the URI
99       */
100     public String toURI(ICS ics) {
101         tag.setOutstr("foo_");
102         String ret;
103         try {
104             tag.execute(ics);
105             ret = ics.GetVar("foo_");
106         } finally {
107             ics.RemoveVar("foo_");
108         }
109         return ret;
110     }
111 
112     /**
113      * @param s string value for assembler
114      * @return this
115      * @see "tools.gsf.facade.runtag.render.GetBlobUrl#setAssembler(java.lang.String)"
116      */
117     public BlobUriBuilder assembler(String s) {
118         tag.setAssembler(s);
119         return this;
120     }
121 
122     /**
123      * @param s string value for fragment
124      * @return this
125      * @see "tools.gsf.facade.runtag.render.GetBlobUrl#setFragment(java.lang.String)"
126      */
127     public BlobUriBuilder fragment(String s) {
128         tag.setFragment(s);
129         return this;
130     }
131 
132     /**
133      * @param s string value for bob header
134      * @return this
135      * @see "tools.gsf.facade.runtag.render.GetBlobUrl#setBobHeader(java.lang.String)"
136      */
137     public BlobUriBuilder mimeType(String s) {
138         if (StringUtils.isBlank(s)) {
139             throw new IllegalArgumentException("Value cannot be blank.");
140         }
141         tag.setBobHeader(s);
142         return this;
143     }
144 
145     /**
146      * @param s string value for blob no cache
147      * @return this
148      * @see "tools.gsf.facade.runtag.render.GetBlobUrl#setBlobNoCache(java.lang.String)"
149      */
150     public BlobUriBuilder blobNoCache(String s) {
151         if (StringUtils.isBlank(s)) {
152             throw new IllegalArgumentException("Value cannot be blank.");
153         }
154         tag.setBlobNoCache(s);
155         return this;
156     }
157 
158     /**
159      * @param name  blob header name
160      * @param value blob header value
161      * @return this
162      * @see "tools.gsf.facade.runtag.render.GetBlobUrl#setBlobHeaderName(int, java.lang.String)"
163      */
164     public BlobUriBuilder header(String name, String value) {
165         if (StringUtils.isBlank(name)) {
166             throw new IllegalArgumentException("Name cannot be blank.");
167         }
168         if (StringUtils.isBlank(value)) {
169             throw new IllegalArgumentException("Value cannot be blank.");
170         }
171         tag.setBlobHeaderName(n, name);
172         tag.setBlobHeaderValue(n, value);
173         n++;
174         return this;
175     }
176 
177     /**
178      * Sets the Cache-Control: max-age http response header for this blob.
179      *
180      * @param value the max-age value as per http specification.
181      * @return this
182      * @see "tools.gsf.facade.runtag.render.GetBlobUrl#setBlobHeaderName(int, java.lang.String)"
183      */
184     public BlobUriBuilder maxAge(int value) {
185         if (value < 0) {
186             throw new IllegalArgumentException("Cache-Control: max-age can not be negative");
187         }
188         tag.setBlobHeaderName(n, "Cache-Control");
189         tag.setBlobHeaderValue(n, "max-age=" + value);
190         n++;
191         return this;
192     }
193 
194     /**
195      * @param s string value for parent id
196      * @return this
197      * @see "tools.gsf.facade.runtag.render.GetBlobUrl#setParentId(java.lang.String)"
198      */
199     public BlobUriBuilder parent(String s) {
200         if (StringUtils.isBlank(s)) {
201             throw new IllegalArgumentException("Value cannot be blank.");
202         }
203         tag.setParentId(s);
204         return this;
205     }
206 
207     /**
208      * @param assetId asset id object
209      * @return this
210      * @see "tools.gsf.facade.runtag.render.GetBlobUrl#setParentId(java.lang.String)"
211      */
212     public BlobUriBuilder parent(AssetId assetId) {
213         return parent(Long.toString(assetId.getId()));
214 
215     }
216 
217 }