1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35
36
37
38
39 public class BlobUriBuilder {
40
41 private GetBlobUrl tag = new GetBlobUrl();
42 private int n = 1;
43
44
45
46
47
48
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
69
70
71
72 public BlobUriBuilder(final BlobObject blob) {
73 this(blob.getBlobAddress());
74 }
75
76
77
78
79
80
81 public BlobUriBuilder(final BlobAddress address) {
82
83 populateTag(address);
84 }
85
86
87
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
98
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
114
115
116
117 public BlobUriBuilder assembler(String s) {
118 tag.setAssembler(s);
119 return this;
120 }
121
122
123
124
125
126
127 public BlobUriBuilder fragment(String s) {
128 tag.setFragment(s);
129 return this;
130 }
131
132
133
134
135
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
147
148
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
160
161
162
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
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 }
188 tag.setBlobHeaderName(n, "Cache-Control");
189 tag.setBlobHeaderValue(n, "max-age=" + value);
190 n++;
191 return this;
192 }
193
194
195
196
197
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
209
210
211
212 public BlobUriBuilder parent(AssetId assetId) {
213 return parent(Long.toString(assetId.getId()));
214
215 }
216
217 }