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