1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36
37
38
39
40
41
42
43 public class BlobUriBuilder {
44
45 private GetBlobUrl tag = new GetBlobUrl();
46 private int n = 1;
47
48
49
50
51
52
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
71
72
73
74 public BlobUriBuilder(final BlobObject blob) {
75 this(blob.getBlobAddress());
76 }
77
78
79
80
81
82
83 public BlobUriBuilder(final BlobAddress address) {
84
85 populateTag(address);
86 }
87
88
89
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
100
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
116
117
118
119 public BlobUriBuilder assembler(String s) {
120 tag.setAssembler(s);
121 return this;
122 }
123
124
125
126
127
128
129 public BlobUriBuilder fragment(String s) {
130 tag.setFragment(s);
131 return this;
132 }
133
134
135
136
137
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
148
149
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
160
161
162
163
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
178
179
180
181
182
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
195
196
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
207
208
209
210 public BlobUriBuilder parent(AssetId assetId) {
211 return parent(Long.toString(assetId.getId()));
212
213 }
214
215 }