-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor local web server handler class
- create handler package - add handler for own plants - contributes to #81
- Loading branch information
1 parent
23b4759
commit 9259921
Showing
7 changed files
with
172 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
app/src/main/java/eu/quelltext/mundraub/map/handler/ErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package eu.quelltext.mundraub.map.handler; | ||
|
||
public interface ErrorHandler { | ||
void handleError(Exception e); | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/eu/quelltext/mundraub/map/handler/PlantCollectionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package eu.quelltext.mundraub.map.handler; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.nanohttpd.protocols.http.IHTTPSession; | ||
import org.nanohttpd.protocols.http.response.Response; | ||
import org.nanohttpd.protocols.http.response.Status; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.util.List; | ||
|
||
import eu.quelltext.mundraub.plant.Plant; | ||
import eu.quelltext.mundraub.plant.PlantCollection; | ||
|
||
public class PlantCollectionHandler extends URIHandler { | ||
private final PlantCollection plants; | ||
|
||
public PlantCollectionHandler(String uri, PlantCollection plants, ErrorHandler errorHandler) { | ||
super(uri, errorHandler); | ||
this.plants = plants; | ||
} | ||
|
||
@Override | ||
public Response respondTo(IHTTPSession input) throws JSONException, UnsupportedEncodingException { | ||
List<Plant> plantList = plants.all(); | ||
JSONArray json = new JSONArray(); | ||
for (Plant plant: plantList) { | ||
JSONObject plantJSON = plant.toJSON(); | ||
json.put(plantJSON); | ||
} | ||
String string = json.toString(); | ||
byte[] bytes = string.getBytes("UTF-8"); | ||
Response result = Response.newFixedLengthResponse(Status.OK, "application/json", bytes); | ||
return result; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
app/src/main/java/eu/quelltext/mundraub/map/handler/TilesHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package eu.quelltext.mundraub.map.handler; | ||
|
||
import org.nanohttpd.protocols.http.IHTTPSession; | ||
import org.nanohttpd.protocols.http.NanoHTTPD; | ||
import org.nanohttpd.protocols.http.response.Response; | ||
import org.nanohttpd.protocols.http.response.Status; | ||
|
||
import eu.quelltext.mundraub.error.Logger; | ||
import eu.quelltext.mundraub.map.MundraubMapAPIForApp; | ||
import eu.quelltext.mundraub.map.TilesCache; | ||
|
||
public class TilesHandler extends URIHandler { | ||
private final Logger.Log log; | ||
private MundraubMapAPIForApp mundraubMapAPIForApp; | ||
private final TilesCache cache; | ||
|
||
public TilesHandler(MundraubMapAPIForApp mundraubMapAPIForApp, String uri, TilesCache cache, ErrorHandler errorHandler, Logger.Log log) { | ||
super(uri, errorHandler); | ||
this.mundraubMapAPIForApp = mundraubMapAPIForApp; | ||
this.cache = cache; | ||
this.log = log; | ||
} | ||
|
||
@Override | ||
public boolean wantsToServeURI(String uri) { | ||
return uri.startsWith(baseUri()); | ||
} | ||
|
||
public Response respondTo(IHTTPSession input) throws Exception { | ||
String[] zyx = input.getUri().substring(this.baseUri().length()).split("/"); | ||
if (zyx.length < 3) { | ||
return Response.newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "I need " + baseUri() + "z/y/x"); | ||
} | ||
int x = Integer.parseInt(zyx[zyx.length - 1]); | ||
int y = Integer.parseInt(zyx[zyx.length - 2]); | ||
int z = Integer.parseInt(zyx[zyx.length - 3]); | ||
TilesCache.Tile tile = cache.getTileAt(x, y, z); | ||
if (tile.isCached()) { | ||
log.d("handle tile", 200 + ": " + z + "/" + y + "/" + x); | ||
Response response = Response.newFixedLengthResponse(Status.OK, tile.contentType(), tile.bytes()); | ||
response.addHeader("Cache-Control", "no-store, must-revalidate"); // no cache from https://stackoverflow.com/a/2068407 | ||
return response; | ||
} else { | ||
log.d("handle tile", 307 + ": " + z + "/" + y + "/" + x + " -> " + tile.url()); | ||
Response response = Response.newFixedLengthResponse(Status.TEMPORARY_REDIRECT, NanoHTTPD.MIME_PLAINTEXT, "Image not found."); | ||
response.addHeader("Location", tile.url()); | ||
return response; | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
app/src/main/java/eu/quelltext/mundraub/map/handler/URIHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package eu.quelltext.mundraub.map.handler; | ||
|
||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
import org.nanohttpd.protocols.http.IHTTPSession; | ||
import org.nanohttpd.protocols.http.NanoHTTPD; | ||
import org.nanohttpd.protocols.http.request.Method; | ||
import org.nanohttpd.protocols.http.response.Response; | ||
import org.nanohttpd.protocols.http.response.Status; | ||
import org.nanohttpd.util.IHandler; | ||
|
||
public class URIHandler implements IHandler<IHTTPSession, Response> { | ||
|
||
private final String uri; | ||
private final ErrorHandler errorHandler; | ||
|
||
public URIHandler(String uri,ErrorHandler errorHandler) { | ||
this.uri = uri; | ||
this.errorHandler = errorHandler; | ||
} | ||
|
||
@Override | ||
public Response handle(IHTTPSession input) { | ||
if (wantsToServe(input)) { | ||
Response response; | ||
try { | ||
response = respondTo(input); | ||
} catch (Exception e) { | ||
response = handleError(e); | ||
} | ||
response.addHeader("Access-Control-Allow-Origin", "*"); // allow JavaScript to access the content | ||
return response; | ||
} | ||
return null; | ||
} | ||
|
||
// this should be overwritten | ||
public Response respondTo(IHTTPSession input) throws Exception { | ||
return null; | ||
} | ||
|
||
public boolean wantsToServe(IHTTPSession input) { | ||
return wantsToHandleMethod(input.getMethod()) && | ||
wantsToServeURI(input.getUri()); | ||
} | ||
|
||
public boolean wantsToServeURI(String uri) { | ||
return uri.equals(this.uri); | ||
} | ||
|
||
public boolean wantsToHandleMethod(Method method) { | ||
return method == Method.GET; | ||
} | ||
|
||
public Response handleError(Exception e) { | ||
errorHandler.handleError(e); | ||
String msg = "<html><body><h1>500 Internal server error</h1>\n" + | ||
"<pre>" + ExceptionUtils.getStackTrace(e) + "</pre></body></html>"; | ||
return Response.newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHTTPD.MIME_HTML, msg); | ||
} | ||
|
||
protected String baseUri() { | ||
return uri; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters