-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathRPFRetriever.java
More file actions
359 lines (307 loc) · 9.52 KB
/
RPFRetriever.java
File metadata and controls
359 lines (307 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright 2006-2009, 2017, 2020 United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The NASA World Wind Java (WWJ) platform is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* NASA World Wind Java (WWJ) also contains the following 3rd party Open Source
* software:
*
* Jackson Parser – Licensed under Apache 2.0
* GDAL – Licensed under MIT
* JOGL – Licensed under Berkeley Software Distribution (BSD)
* Gluegen – Licensed under Berkeley Software Distribution (BSD)
*
* A complete listing of 3rd Party software notices and licenses included in
* NASA World Wind Java (WWJ) can be found in the WorldWindJava-v2.2 3rd-party
* notices and licenses PDF found in code directory.
*/
package gov.nasa.worldwind.layers.rpf;
import gov.nasa.worldwind.WWObjectImpl;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.formats.dds.DDSCompressor;
import gov.nasa.worldwind.retrieve.*;
import gov.nasa.worldwind.util.Logging;
import java.awt.image.*;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
/**
* @author dcollins
* @version $Id: RPFRetriever.java 1171 2013-02-11 21:45:02Z dcollins $
*/
class RPFRetriever extends WWObjectImpl implements Retriever
{
private volatile ByteBuffer byteBuffer;
private volatile int contentLength = 0;
private AtomicInteger contentLengthRead = new AtomicInteger(0);
private volatile String state = RETRIEVER_STATE_NOT_STARTED;
private volatile String contentType;
private long submitTime;
private long beginTime;
private long endTime;
private int connectTimeout = -1;
private int readTimeout = -1;
private int staleRequestLimit = -1;
private final RPFGenerator.RPFServiceInstance service;
private final URL url;
private final RetrievalPostProcessor postProcessor;
private int responseCode;
public static final int RESPONSE_CODE_OK = 1;
public static final int RESPONSE_CODE_NO_CONTENT = 2;
protected String basicAuthenticationEncodedString;
public RPFRetriever(RPFGenerator.RPFServiceInstance service, URL url, RetrievalPostProcessor postProcessor)
{
if (service == null)
{
String message = "Service is null";
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (url == null)
{
String message = Logging.getMessage("nullValue.URLIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (postProcessor == null)
{
String message = Logging.getMessage("nullValue.PostProcessorIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
this.service = service;
this.url = url;
this.postProcessor = postProcessor;
}
public final ByteBuffer getBuffer()
{
return this.byteBuffer;
}
public final int getContentLength()
{
return this.contentLength;
}
public final int getContentLengthRead()
{
return this.contentLengthRead.get();
}
protected void setContentLengthRead(int length)
{
this.contentLengthRead.set(length);
}
public final String getName()
{
return this.url.toString();
}
public final String getState()
{
return this.state;
}
public final String getContentType()
{
return this.contentType;
}
/**
* {@inheritDoc}
*
* @return Always returns zero (no expiration).
*/
public long getExpirationTime()
{
return 0;
}
public long getSubmitTime()
{
return this.submitTime;
}
public void setSubmitTime(long submitTime)
{
this.submitTime = submitTime;
}
public long getBeginTime()
{
return this.beginTime;
}
public void setBeginTime(long beginTime)
{
this.beginTime = beginTime;
}
public long getEndTime()
{
return this.endTime;
}
public void setEndTime(long endTime)
{
this.endTime = endTime;
}
public int getConnectTimeout()
{
return this.connectTimeout;
}
public void setConnectTimeout(int connectTimeout)
{
this.connectTimeout = connectTimeout;
}
public int getReadTimeout()
{
return this.readTimeout;
}
public void setReadTimeout(int readTimeout)
{
this.readTimeout = readTimeout;
}
public int getStaleRequestLimit()
{
return this.staleRequestLimit;
}
public void setStaleRequestLimit(int staleRequestLimit)
{
this.staleRequestLimit = staleRequestLimit;
}
@Override
public void setBasicAuthentication(String basicAuthorizationString) {
this.basicAuthenticationEncodedString = basicAuthorizationString;
}
public final RPFGenerator.RPFServiceInstance getService()
{
return this.service;
}
public final URL getURL()
{
return this.url;
}
public final RetrievalPostProcessor getPostProcessor()
{
return this.postProcessor;
}
public int getResponseCode()
{
return this.responseCode;
}
public final Retriever call() throws Exception
{
if (interrupted())
return this;
try
{
setState(RETRIEVER_STATE_STARTED);
// Simulate connected state.
if (!interrupted())
setState(RETRIEVER_STATE_CONNECTING);
if (!interrupted())
{
setState(RETRIEVER_STATE_READING);
this.byteBuffer = read();
}
if (!interrupted())
setState(RETRIEVER_STATE_SUCCESSFUL);
}
catch (Exception e)
{
setState(RETRIEVER_STATE_ERROR);
Logging.logger().log(Level.SEVERE,
Logging.getMessage("URLRetriever.ErrorAttemptingToRetrieve", this.url.toString()), e);
}
finally
{
end();
}
return this;
}
private void setState(String state)
{
String oldState = this.state;
this.state = state;
this.firePropertyChange(AVKey.RETRIEVER_STATE, oldState, this.state);
}
private boolean interrupted()
{
if (Thread.currentThread().isInterrupted())
{
setState(RETRIEVER_STATE_INTERRUPTED);
String message = Logging.getMessage("URLRetriever.RetrievalInterruptedFor", this.url.toString());
Logging.logger().fine(message);
return true;
}
return false;
}
private void end() throws Exception
{
try
{
if (this.postProcessor != null)
{
this.byteBuffer = this.postProcessor.run(this);
}
}
catch (Exception e)
{
setState(RETRIEVER_STATE_ERROR);
Logging.logger().log(Level.SEVERE,
Logging.getMessage("Retriever.ErrorPostProcessing", this.url.toString()), e);
throw e;
}
}
private ByteBuffer read() throws Exception
{
ByteBuffer buffer = this.doRead(this.service, this.url);
if (buffer == null)
this.contentLength = 0;
return buffer;
}
protected ByteBuffer doRead(RPFGenerator.RPFServiceInstance service, URL url) throws Exception
{
ByteBuffer buffer = null;
BufferedImage bufferedImage = service.serviceRequest(url);
if (bufferedImage != null)
{
// TODO: format parameter should determine image type
buffer = DDSCompressor.compressImage(bufferedImage);
if (buffer != null)
{
int length = buffer.limit();
this.contentType = "image/dds";
this.contentLength = length;
setContentLengthRead(length);
}
}
// TODO: service should provide response code
this.responseCode = buffer != null ? RESPONSE_CODE_OK : RESPONSE_CODE_NO_CONTENT;
return buffer;
}
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
final RPFRetriever that = (RPFRetriever) o;
// Retrievers are considered identical if they are for the same URL. This convention is used by the
// retrieval service to filter out duplicate retreival requests.
return !(url != null ? !url.toString().contentEquals(that.url.toString()) : that.url != null);
}
@Override
public int hashCode()
{
int result;
result = (url != null ? url.hashCode() : 0);
return result;
}
@Override
public String toString()
{
return this.getName() != null ? this.getName() : super.toString();
}
}