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 com.fatwire.gst.foundation.facade.uri;
18  
19  import org.apache.commons.lang.StringUtils;
20  
21  import COM.FutureTense.Interfaces.ICS;
22  
23  import com.fatwire.assetapi.data.AssetData;
24  import com.fatwire.assetapi.data.AssetId;
25  import com.fatwire.assetapi.data.AttributeData;
26  import com.fatwire.assetapi.data.BlobObject;
27  import com.fatwire.assetapi.data.BlobObject.BlobAddress;
28  import com.fatwire.gst.foundation.DebugHelper;
29  import com.fatwire.gst.foundation.facade.assetapi.AttributeDataUtils;
30  import com.fatwire.gst.foundation.facade.runtag.render.GetBlobUrl;
31  
32  /**
33   * Builder support for blob urls.
34   * 
35   * <pre>
36   * new BlobUriBuilder(blob).mimeType(&quot;image/jpeg&quot;).parent(&quot;12345&quot;).toURI(ics);
37   * </pre>
38   * 
39   * @author Dolf Dijkstra
40   * @since Feb 15, 2011
41   * 
42   */
43  public class BlobUriBuilder {
44  
45      private GetBlobUrl tag = new GetBlobUrl();
46      private int n = 1;
47  
48      /**
49       * Constructor that accepts AssetData and an attribute name.
50       * 
51       * @param data the asset.
52       * @param attributeName the name of the attribute containing the blob.
53       */
54      public BlobUriBuilder(final AssetData data, String attributeName) {
55          AttributeData attr = data.getAttributeData(attributeName);
56          if (attr == null)
57              throw new IllegalStateException("Can't find attribute " + attributeName + " on asset "
58                      + DebugHelper.toString(data.getAssetId()));
59          BlobObject blob = AttributeDataUtils.asBlob(attr);
60          if (blob == null)
61              throw new IllegalStateException("Attribute " + attributeName + " on asset "
62                      + DebugHelper.toString(data.getAssetId()) + " is not found.");
63  
64          populateTag(blob.getBlobAddress());
65  
66          parent(data.getAssetId());
67      }
68  
69      /**
70       * Constructor accepting a BlobObject.
71       * 
72       * @param blob
73       */
74      public BlobUriBuilder(final BlobObject blob) {
75          this(blob.getBlobAddress());
76      }
77  
78      /**
79       * Constructor accepting a BlobAddress.
80       * 
81       * @param address
82       */
83      public BlobUriBuilder(final BlobAddress address) {
84  
85          populateTag(address);
86      }
87  
88      /**
89       * @param address
90       */
91      private void populateTag(final BlobAddress address) {
92          tag.setBlobCol(address.getColumnName());
93          tag.setBlobKey(address.getIdentifierColumnName());
94          tag.setBlobTable(address.getTableName());
95          tag.setBlobWhere(address.getIdentifier().toString());
96      }
97  
98      /**
99       * @param ics
100      * @return the URI
101      */
102     public String toURI(ICS ics) {
103         tag.setOutstr("foo_");
104         String ret;
105         try {
106             tag.execute(ics);
107             ret = ics.GetVar("foo_");
108         } finally {
109             ics.RemoveVar("foo_");
110         }
111         return ret;
112     }
113 
114     /**
115      * @param s
116      * @return this
117      * @see com.fatwire.gst.foundation.facade.runtag.render.GetBlobUrl#setAssembler(java.lang.String)
118      */
119     public BlobUriBuilder assembler(String s) {
120         tag.setAssembler(s);
121         return this;
122     }
123 
124     /**
125      * @param s
126      * @return this
127      * @see com.fatwire.gst.foundation.facade.runtag.render.GetBlobUrl#setFragment(java.lang.String)
128      */
129     public BlobUriBuilder fragment(String s) {
130         tag.setFragment(s);
131         return this;
132     }
133 
134     /**
135      * @param s
136      * @return this
137      * @see com.fatwire.gst.foundation.facade.runtag.render.GetBlobUrl#setBobHeader(java.lang.String)
138      */
139     public BlobUriBuilder mimeType(String s) {
140         if (StringUtils.isBlank(s))
141             throw new IllegalArgumentException("Value cannot be blank.");
142         tag.setBobHeader(s);
143         return this;
144     }
145 
146     /**
147      * @param s
148      * @return this
149      * @see com.fatwire.gst.foundation.facade.runtag.render.GetBlobUrl#setBlobNoCache(java.lang.String)
150      */
151     public BlobUriBuilder blobNoCache(String s) {
152         if (StringUtils.isBlank(s))
153             throw new IllegalArgumentException("Value cannot be blank.");
154         tag.setBlobNoCache(s);
155         return this;
156     }
157 
158     /**
159      * @param name
160      * @param value
161      * @return this
162      * @see com.fatwire.gst.foundation.facade.runtag.render.GetBlobUrl#setBlobHeaderName(int,
163      *      java.lang.String)
164      */
165     public BlobUriBuilder header(String name, String value) {
166         if (StringUtils.isBlank(name))
167             throw new IllegalArgumentException("Name cannot be blank.");
168         if (StringUtils.isBlank(value))
169             throw new IllegalArgumentException("Value cannot be blank.");
170         tag.setBlobHeaderName(n, name);
171         tag.setBlobHeaderValue(n, value);
172         n++;
173         return this;
174     }
175 
176     /**
177      * Sets the Cache-Control: max-age http response header for this blob.
178      * 
179      * @param value the max-age value as per http specification.
180      * @return this
181      * @see com.fatwire.gst.foundation.facade.runtag.render.GetBlobUrl#setBlobHeaderName(int,
182      *      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         tag.setBlobHeaderName(n, "Cache-Control");
188         tag.setBlobHeaderValue(n, "max-age=" + value);
189         n++;
190         return this;
191     }
192 
193     /**
194      * @param s
195      * @return this
196      * @see com.fatwire.gst.foundation.facade.runtag.render.GetBlobUrl#setParentId(java.lang.String)
197      */
198     public BlobUriBuilder parent(String s) {
199         if (StringUtils.isBlank(s))
200             throw new IllegalArgumentException("Value cannot be blank.");
201         tag.setParentId(s);
202         return this;
203     }
204 
205     /**
206      * @param assetId
207      * @see com.fatwire.gst.foundation.facade.runtag.render.GetBlobUrl#setParentId(java.lang.String)
208      * @return this
209      */
210     public BlobUriBuilder parent(AssetId assetId) {
211         return parent(Long.toString(assetId.getId()));
212 
213     }
214 
215 }