gwt: update UrlTileSource
This commit is contained in:
parent
5c277f4d54
commit
5ad68ff2c7
@ -16,9 +16,11 @@ package org.oscim.tiling.source;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.layers.tile.MapTile;
|
||||
|
||||
import com.google.gwt.typedarrays.client.Uint8ArrayNative;
|
||||
import com.google.gwt.typedarrays.shared.Uint8Array;
|
||||
@ -26,20 +28,18 @@ import com.google.gwt.xhr.client.ReadyStateChangeHandler;
|
||||
import com.google.gwt.xhr.client.XMLHttpRequest;
|
||||
import com.google.gwt.xhr.client.XMLHttpRequest.ResponseType;
|
||||
|
||||
public class LwHttp {
|
||||
public class LwHttp implements HttpEngine {
|
||||
//static final Logger log = LoggerFactory.getLogger(LwHttp.class);
|
||||
|
||||
private final String mUrlPath;
|
||||
private final byte[] mRequestBuffer;
|
||||
|
||||
private int mContentLength = -1;
|
||||
private XMLHttpRequest mHttpRequest;
|
||||
|
||||
private ReadyStateChangeHandler mResponseHandler;
|
||||
|
||||
public LwHttp(URL url) {
|
||||
public LwHttp(UrlTileSource tileSource) {
|
||||
mTileSource = tileSource;
|
||||
URL url = tileSource.getUrl();
|
||||
mUrlPath = url.toString();
|
||||
mRequestBuffer = new byte[1024];
|
||||
}
|
||||
|
||||
static class Buffer extends InputStream {
|
||||
@ -63,21 +63,18 @@ public class LwHttp {
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (mHttpRequest != null)
|
||||
mHttpRequest.abort();
|
||||
if (mHttpRequest == null)
|
||||
return;
|
||||
|
||||
mHttpRequest.abort();
|
||||
mHttpRequest = null;
|
||||
}
|
||||
|
||||
private UrlTileDataSource mDataSource;
|
||||
private UrlTileSource mTileSource;
|
||||
|
||||
public boolean sendRequest(Tile tile, UrlTileDataSource dataSource) throws IOException {
|
||||
mDataSource = dataSource;
|
||||
public boolean sendRequest(MapTile tile, final UrlTileDataSource dataSource) {
|
||||
|
||||
byte[] request = mRequestBuffer;
|
||||
int pos = 0;
|
||||
|
||||
pos = dataSource.getTileSource().formatTilePath(tile, request, pos);
|
||||
|
||||
String url = mUrlPath + (new String(request, 0, pos));
|
||||
String url = mUrlPath + mTileSource.formatTilePath(tile);
|
||||
|
||||
mHttpRequest = XMLHttpRequest.create();
|
||||
mHttpRequest.open("GET", url);
|
||||
@ -91,16 +88,13 @@ public class LwHttp {
|
||||
//log.debug(mCurrentUrl + "response " + status + "/" + state);
|
||||
|
||||
if (state == XMLHttpRequest.DONE) {
|
||||
|
||||
int status = xhr.getStatus();
|
||||
|
||||
if (status == 200) {
|
||||
if (xhr.getStatus() == 200) {
|
||||
Uint8Array buf = Uint8ArrayNative.create(xhr.getResponseArrayBuffer());
|
||||
|
||||
mDataSource.process(new Buffer(buf));
|
||||
dataSource.process(new Buffer(buf));
|
||||
} else {
|
||||
mDataSource.process(null);
|
||||
dataSource.process(null);
|
||||
}
|
||||
mHttpRequest = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -111,43 +105,32 @@ public class LwHttp {
|
||||
return true;
|
||||
}
|
||||
|
||||
// write (positive) integer to byte array
|
||||
protected static int writeInt(int val, int pos, byte[] buf) {
|
||||
if (val == 0) {
|
||||
buf[pos] = '0';
|
||||
return pos + 1;
|
||||
public static class LwHttpFactory implements HttpEngine.Factory {
|
||||
|
||||
@Override
|
||||
public HttpEngine create(UrlTileSource tileSource) {
|
||||
return new LwHttp(tileSource);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (int n = val; n > 0; n = n / 10, i++)
|
||||
buf[pos + i] = (byte) ('0' + n % 10);
|
||||
|
||||
// reverse bytes
|
||||
for (int j = pos, end = pos + i - 1, mid = pos + i / 2; j < mid; j++, end--) {
|
||||
byte tmp = buf[j];
|
||||
buf[j] = buf[end];
|
||||
buf[end] = tmp;
|
||||
}
|
||||
|
||||
return pos + i;
|
||||
}
|
||||
|
||||
// parse (positive) integer from byte array
|
||||
protected static int parseInt(byte[] buf, int pos, int end) {
|
||||
int val = 0;
|
||||
for (; pos < end; pos++)
|
||||
val = val * 10 + (buf[pos]) - '0';
|
||||
|
||||
return val;
|
||||
@Override
|
||||
public InputStream read() throws IOException {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void requestCompleted() {
|
||||
|
||||
mHttpRequest.clearOnReadyStateChange();
|
||||
mHttpRequest = null;
|
||||
@Override
|
||||
public void setCache(OutputStream os) {
|
||||
}
|
||||
|
||||
public int getContentLength() {
|
||||
return mContentLength;
|
||||
@Override
|
||||
public boolean requestCompleted(boolean success) {
|
||||
// mHttpRequest.clearOnReadyStateChange();
|
||||
// mHttpRequest = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendRequest(Tile tile) throws IOException {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -35,65 +35,52 @@ public class UrlTileDataSource implements ITileDataSource {
|
||||
protected final ITileDecoder mTileDecoder;
|
||||
protected final UrlTileSource mTileSource;
|
||||
|
||||
public UrlTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder, LwHttp conn) {
|
||||
mTileSource = tileSource;
|
||||
mTileDecoder = tileDecoder;
|
||||
mConn = conn;
|
||||
}
|
||||
|
||||
UrlTileSource getTileSource() {
|
||||
return mTileSource;
|
||||
}
|
||||
|
||||
private ITileDataSink mSink;
|
||||
private MapTile mTile;
|
||||
|
||||
public UrlTileDataSource(UrlTileSource tileSource, ITileDecoder tileDecoder, HttpEngine conn) {
|
||||
mTileSource = tileSource;
|
||||
mTileDecoder = tileDecoder;
|
||||
mConn = (LwHttp) conn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void query(MapTile tile, ITileDataSink sink) {
|
||||
mTile = tile;
|
||||
mSink = sink;
|
||||
try {
|
||||
mConn.sendRequest(tile, this);
|
||||
} catch (Exception e) {
|
||||
///e.printStackTrace();
|
||||
log.error("{} {}", mTile, e.getMessage());
|
||||
sink.completed(FAILED);
|
||||
}
|
||||
mConn.sendRequest(tile, this);
|
||||
}
|
||||
|
||||
public void process(final InputStream is) {
|
||||
TileLoader.postLoadDelay(new LoadDelayTask<InputStream>(mTile, mSink, is) {
|
||||
if (is == null) {
|
||||
log.debug("{} no inputstream", mTile);
|
||||
mSink.completed(FAILED);
|
||||
mTile = null;
|
||||
mSink = null;
|
||||
return;
|
||||
}
|
||||
|
||||
TileLoader.postLoadDelay(new LoadDelayTask<InputStream>(mTile, mSink, is) {
|
||||
@Override
|
||||
public void continueLoading() {
|
||||
|
||||
boolean win = false;
|
||||
if (tile.state(MapTile.State.LOADING)) {
|
||||
boolean win = false;
|
||||
if (is != null) {
|
||||
try {
|
||||
win = mTileDecoder.decode(tile, sink, data);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
win = mTileDecoder.decode(tile, sink, data);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (!win)
|
||||
log.debug("{} failed", tile);
|
||||
|
||||
// FIXME
|
||||
mConn.requestCompleted();
|
||||
|
||||
sink.completed(win ? SUCCESS : FAILED);
|
||||
} else {
|
||||
// FIXME
|
||||
mConn.requestCompleted();
|
||||
sink.completed(FAILED);
|
||||
}
|
||||
|
||||
mTile = null;
|
||||
mSink = null;
|
||||
if (win) {
|
||||
sink.completed(SUCCESS);
|
||||
} else {
|
||||
sink.completed(FAILED);
|
||||
log.debug("{} decode failed", tile);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
mTile = null;
|
||||
mSink = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -31,9 +31,17 @@ public class BitmapTileSource extends UrlTileSource {
|
||||
* Use e.g. setExtension(".jpg") to overide ending or
|
||||
* implement getUrlString() for custom formatting.
|
||||
*/
|
||||
|
||||
public BitmapTileSource(String url, int zoomMin, int zoomMax) {
|
||||
super(url, zoomMin, zoomMax);
|
||||
setExtension(".png");
|
||||
super(url, "/{Z}/{X}/{Y}.png", zoomMin, zoomMax);
|
||||
}
|
||||
|
||||
public BitmapTileSource(String url, int zoomMin, int zoomMax, String extension) {
|
||||
super(url, "/{Z}/{X}/{Y}" + extension, zoomMin, zoomMax);
|
||||
}
|
||||
|
||||
public BitmapTileSource(String url, String tilePath, int zoomMin, int zoomMax) {
|
||||
super(url, tilePath, zoomMin, zoomMax);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -41,10 +49,9 @@ public class BitmapTileSource extends UrlTileSource {
|
||||
return new BitmapTileDataSource(this);
|
||||
}
|
||||
|
||||
public static class BitmapTileDataSource implements ITileDataSource {
|
||||
public class BitmapTileDataSource implements ITileDataSource {
|
||||
|
||||
protected final UrlTileSource mTileSource;
|
||||
private final byte[] mRequestBuffer = new byte[1024];
|
||||
|
||||
public BitmapTileDataSource(BitmapTileSource bitmapTileSource) {
|
||||
mTileSource = bitmapTileSource;
|
||||
@ -53,10 +60,8 @@ public class BitmapTileSource extends UrlTileSource {
|
||||
@Override
|
||||
public void query(final MapTile tile, final ITileDataSink sink) {
|
||||
|
||||
int pos = mTileSource.formatTilePath(tile, mRequestBuffer, 0);
|
||||
|
||||
String url = mTileSource.getUrl()
|
||||
+ (new String(mRequestBuffer, 0, pos));
|
||||
+ BitmapTileSource.this.formatTilePath(tile);
|
||||
|
||||
SafeUri uri = UriUtils.fromTrustedString(url);
|
||||
|
||||
|
@ -30,8 +30,7 @@ public abstract class GeoJsonTileSource extends UrlTileSource {
|
||||
static final Logger log = LoggerFactory.getLogger(GeoJsonTileSource.class);
|
||||
|
||||
public GeoJsonTileSource(String url) {
|
||||
super(url);
|
||||
setExtension(".json");
|
||||
super(url, "/{Z}/{X}/{Y}.json");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -41,8 +41,6 @@ public class JsonTileDataSource implements ITileDataSource {
|
||||
protected final GeoJsonTileDecoder mTileDecoder;
|
||||
protected final UrlTileSource mTileSource;
|
||||
|
||||
private final byte[] mRequestBuffer = new byte[1024];
|
||||
|
||||
public JsonTileDataSource(GeoJsonTileSource tileSource) {
|
||||
mTileSource = tileSource;
|
||||
mTileDecoder = new GeoJsonTileDecoder(tileSource);
|
||||
@ -61,10 +59,8 @@ public class JsonTileDataSource implements ITileDataSource {
|
||||
mSink = sink;
|
||||
|
||||
try {
|
||||
int pos = mTileSource.formatTilePath(tile, mRequestBuffer, 0);
|
||||
|
||||
String url = mTileSource.getUrl()
|
||||
+ (new String(mRequestBuffer, 0, pos));
|
||||
+ mTileSource.formatTilePath(tile);
|
||||
|
||||
doGet(url);
|
||||
} catch (Exception e) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user