diff options
author | Victor Nikiforov <victor.nike@gmail.com> | 2015-08-24 13:35:25 +0300 |
---|---|---|
committer | Victor Nikiforov <victor.nike@gmail.com> | 2015-08-24 13:35:25 +0300 |
commit | eaf89cc0dabb96b443895f02625fd699ee740f5c (patch) | |
tree | ba8dc44ccd4684a65c5b4765c9ebc0c494a8faad /samples/src/main/java/fi/iki | |
parent | e97508b4a9eb932f452772bfd12aa1a763c2d54a (diff) | |
download | nanohttpd-eaf89cc0dabb96b443895f02625fd699ee740f5c.tar.gz |
Issue #214 - NanoHTTPD uri router. Single file
Diffstat (limited to 'samples/src/main/java/fi/iki')
-rw-r--r-- | samples/src/main/java/fi/iki/elonen/router/AppNanolets.java (renamed from samples/src/main/java/fi/iki/elonen/router/App.java) | 6 | ||||
-rw-r--r-- | samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java | 78 | ||||
-rw-r--r-- | samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java | 82 | ||||
-rw-r--r-- | samples/src/main/java/fi/iki/elonen/router/RouterResponse.java | 80 | ||||
-rw-r--r-- | samples/src/main/java/fi/iki/elonen/router/StringUtils.java | 54 | ||||
-rw-r--r-- | samples/src/main/java/fi/iki/elonen/router/UriPart.java | 63 | ||||
-rw-r--r-- | samples/src/main/java/fi/iki/elonen/router/UriResource.java | 149 | ||||
-rw-r--r-- | samples/src/main/java/fi/iki/elonen/router/UriResponder.java | 50 | ||||
-rw-r--r-- | samples/src/main/java/fi/iki/elonen/router/UriRouter.java | 182 | ||||
-rw-r--r-- | samples/src/main/java/fi/iki/elonen/router/UserHandler.java | 88 |
10 files changed, 3 insertions, 829 deletions
diff --git a/samples/src/main/java/fi/iki/elonen/router/App.java b/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java index e43e025..ac35723 100644 --- a/samples/src/main/java/fi/iki/elonen/router/App.java +++ b/samples/src/main/java/fi/iki/elonen/router/AppNanolets.java @@ -42,14 +42,14 @@ import fi.iki.elonen.ServerRunner; import java.io.IOException; -public class App extends RouterNanoHTTPD { +public class AppNanolets extends RouterNanoHTTPD { private static final int PORT = 8081; /** Create the server instance */ - public App() throws IOException { + public AppNanolets() throws IOException { super(PORT); addMappings(); System.out.println("\nRunning! Point your browers to http://localhost:" + PORT + "/ \n"); @@ -78,7 +78,7 @@ public class App extends RouterNanoHTTPD { */ public static void main(String[] args) { try { - ServerRunner.run(App.class); + ServerRunner.run(AppNanolets.class); } catch (Exception ioe) { System.err.println("Couldn't start server:\n" + ioe); } diff --git a/samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java b/samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java deleted file mode 100644 index 57e7b47..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/DefaultHandler.java +++ /dev/null @@ -1,78 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * Copyright (C) 2012 - 2015 nanohttpd - * %% - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the nanohttpd nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * #L% - */ - -import fi.iki.elonen.NanoHTTPD; - -import java.io.ByteArrayInputStream; -import java.util.Map; - -/** - * Created by vnnv on 7/21/15. - * - */ -public abstract class DefaultHandler implements UriResponder { - - public abstract String getText(); - public abstract String getMimeType(); - public abstract NanoHTTPD.Response.IStatus getStatus(); - - public RouterResponse get(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - String text = getText(); - ByteArrayInputStream inp = new ByteArrayInputStream(text.getBytes()); - - RouterResponse result = new RouterResponse(); - result.setData(inp); - result.setMimeType(getMimeType()); - result.setSize(text.getBytes().length); - result.setStatus(getStatus()); - - return result; - } - - public RouterResponse post(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public RouterResponse put(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public RouterResponse delete(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - public RouterResponse other(String method, UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java b/samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java deleted file mode 100644 index 4e7f56a..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/GeneralHandler.java +++ /dev/null @@ -1,82 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * Copyright (C) 2012 - 2015 nanohttpd - * %% - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the nanohttpd nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * #L% - */ - -import fi.iki.elonen.NanoHTTPD; - -import java.io.ByteArrayInputStream; -import java.util.Map; - -/** - * Created by vnnv on 7/22/15. - */ -public class GeneralHandler extends DefaultHandler { - - @Override - public String getText() { - return null; - } - - @Override - public String getMimeType() { - return "text/html"; - } - - @Override - public NanoHTTPD.Response.IStatus getStatus() { - return NanoHTTPD.Response.Status.OK; - } - - public RouterResponse get(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - String text = "<html><body>"; - text += "<h1>Url: " + session.getUri() + "</h1><br>"; - Map<String, String> queryParams = session.getParms(); - if (queryParams.size() > 0) { - for (Map.Entry<String, String> entry : queryParams.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - text += "<p>Param '" + key + "' = " + value + "</p>"; - } - }else{ - text +="<p>no params in url</p><br>"; - } - - RouterResponse res = new RouterResponse(); - res.setData(new ByteArrayInputStream(text.getBytes())); - res.setSize(text.getBytes().length); - res.setStatus(getStatus()); - res.setMimeType(getMimeType()); - return res; - } - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/RouterResponse.java b/samples/src/main/java/fi/iki/elonen/router/RouterResponse.java deleted file mode 100644 index ed16524..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/RouterResponse.java +++ /dev/null @@ -1,80 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * Copyright (C) 2012 - 2015 nanohttpd - * %% - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the nanohttpd nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * #L% - */ - -import fi.iki.elonen.NanoHTTPD; - -import java.io.InputStream; - -/** - * Created by vnnv on 7/21/15. - * Transport class because NanoHTTPD.Response has protected constructor - */ -public class RouterResponse { - private NanoHTTPD.Response.IStatus status; - private InputStream data; - private long size; - private String mimeType; - - public NanoHTTPD.Response.IStatus getStatus() { - return status; - } - - public void setStatus(NanoHTTPD.Response.IStatus status) { - this.status = status; - } - - public InputStream getData() { - return data; - } - - public void setData(InputStream data) { - this.data = data; - } - - public long getSize() { - return size; - } - - public void setSize(long size) { - this.size = size; - } - - public String getMimeType() { - return mimeType; - } - - public void setMimeType(String mimeType) { - this.mimeType = mimeType; - } -} diff --git a/samples/src/main/java/fi/iki/elonen/router/StringUtils.java b/samples/src/main/java/fi/iki/elonen/router/StringUtils.java deleted file mode 100644 index bacaceb..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/StringUtils.java +++ /dev/null @@ -1,54 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * Copyright (C) 2012 - 2015 nanohttpd - * %% - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the nanohttpd nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * #L% - */ - -/** - * Created by victor on 7/20/15. - * Util methods - */ -public class StringUtils { - public static String normalizeUri(String value) { - if (value == null) { - return value; - } - if (value.startsWith("/")) { - value = value.substring(1); - } - if (value.endsWith("/")) { - value = value.substring(0, value.length() - 1); - } - return value; - - } - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriPart.java b/samples/src/main/java/fi/iki/elonen/router/UriPart.java deleted file mode 100644 index 19225b3..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/UriPart.java +++ /dev/null @@ -1,63 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * Copyright (C) 2012 - 2015 nanohttpd - * %% - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the nanohttpd nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * #L% - */ - -/** - * Created by vnnv on 7/20/15. - */ -public class UriPart { - private String name; - private boolean isParam; - - public UriPart(String name, boolean isParam) { - this.name = name; - this.isParam = isParam; - } - - @Override - public String toString() { - return "UriPart{" + - "name='" + name + '\'' + - ", isParam=" + isParam + - '}'; - } - - public boolean isParam() { - return isParam; - } - - public String getName() { - return name; - } - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriResource.java b/samples/src/main/java/fi/iki/elonen/router/UriResource.java deleted file mode 100644 index 0ec45fe..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/UriResource.java +++ /dev/null @@ -1,149 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * Copyright (C) 2012 - 2015 nanohttpd - * %% - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the nanohttpd nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * #L% - */ - -import fi.iki.elonen.NanoHTTPD; - -import java.io.ByteArrayInputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -/** - * Created by vnnv on 7/20/15. - * Represent single uri - */ -public class UriResource { - private boolean hasParameters; - private int uriParamsCount; - private String uri; - private List<UriPart> uriParts; - private Class handler; - - public UriResource(String uri, Class<?> handler) { - this.hasParameters = false; - this.handler = handler; - uriParamsCount = 0; - if (uri != null) { - this.uri = StringUtils.normalizeUri(uri); - parse(); - } - } - - private void parse(){ - String[] parts = uri.split("/"); - uriParts = new ArrayList<UriPart>(); - for (String part : parts) { - boolean isParam = part.startsWith(":"); - UriPart uriPart = null; - if (isParam) { - hasParameters = true; - uriParamsCount++; - uriPart = new UriPart(part.substring(1), true); - }else{ - uriPart = new UriPart(part, false); - } - uriParts.add(uriPart); - } - - } - - public RouterResponse process(Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - String error = "General error!"; - if (handler != null) { - try { - Object object = handler.newInstance(); - if (object instanceof UriResponder) { - UriResponder responder = (UriResponder) object; - switch (session.getMethod()) { - case GET: return responder.get(this, urlParams, session); - case POST: return responder.post(this, urlParams, session); - case PUT: return responder.put(this, urlParams, session); - case DELETE: return responder.delete(this, urlParams, session); - default: return responder.other(session.getMethod().toString(), this, urlParams, session); - } - }else { - // return toString() - String text = "Return: " + handler.getCanonicalName() + ".toString() -> " + object.toString(); - RouterResponse res = new RouterResponse(); - res.setStatus(NanoHTTPD.Response.Status.OK); - res.setMimeType("text/plain"); - res.setData(new ByteArrayInputStream(text.getBytes())); - res.setSize(text.getBytes().length); - return res; - } - } catch (InstantiationException e) { - error = "Error: " + InstantiationException.class.getName() + " : " + e.getMessage(); - e.printStackTrace(); - } catch (IllegalAccessException e) { - error = "Error: " + IllegalAccessException.class.getName() + " : " + e.getMessage(); - e.printStackTrace(); - } - } - - RouterResponse res = new RouterResponse(); - res.setStatus(NanoHTTPD.Response.Status.INTERNAL_ERROR); - res.setMimeType("text/plain"); - res.setData(new ByteArrayInputStream(error.getBytes())); - res.setSize(error.getBytes().length); - return res; - } - - - @Override - public String toString() { - return "UrlResource{" + - "hasParameters=" + hasParameters + - ", uriParamsCount=" + uriParamsCount + - ", uri='" + (uri != null ? "/" : "") + uri + '\'' + - ", urlParts=" + uriParts + - '}'; - } - - public boolean hasParameters() { - return hasParameters; - } - - public String getUri() { - return uri; - } - - public List<UriPart> getUriParts() { - return uriParts; - } - - public int getUriParamsCount() { - return uriParamsCount; - } - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriResponder.java b/samples/src/main/java/fi/iki/elonen/router/UriResponder.java deleted file mode 100644 index 1015250..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/UriResponder.java +++ /dev/null @@ -1,50 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * Copyright (C) 2012 - 2015 nanohttpd - * %% - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the nanohttpd nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * #L% - */ - -import fi.iki.elonen.NanoHTTPD; - -import java.util.Map; - -/** - * Created by vnnv on 7/20/15. - */ -public interface UriResponder { - - public RouterResponse get(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session); - public RouterResponse put(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session); - public RouterResponse post(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session); - public RouterResponse delete(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session); - public RouterResponse other(String method, UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session); - -} diff --git a/samples/src/main/java/fi/iki/elonen/router/UriRouter.java b/samples/src/main/java/fi/iki/elonen/router/UriRouter.java deleted file mode 100644 index c28cecf..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/UriRouter.java +++ /dev/null @@ -1,182 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * Copyright (C) 2012 - 2015 nanohttpd - * %% - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the nanohttpd nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * #L% - */ - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Created by vnnv on 7/17/15. - */ -public class UriRouter { - - private List<UriResource> mappings; - private UriResource error404Url; - private Class<?> notImplemented; - - public UriRouter() { - mappings = new ArrayList<UriResource>(); - } - - /** - * Search in the mappings if the given url matches some of the rules - * If there are more than one marches returns the rule with less parameters - * e.g. - * mapping 1 = /user/:id - * mapping 2 = /user/help - * - * if the incoming uri is www.example.com/user/help - mapping 2 is returned - * if the incoming uri is www.example.com/user/3232 - mapping 1 is returned - * - * @param url - * @return - */ - public UriResource matchUrl(String url) { - - String work = StringUtils.normalizeUri(url); - - String[] parts = work.split("/"); - List<UriResource> resultList = new ArrayList<UriResource>(); - - for (UriResource u : mappings) { - - // Check if count of parts is the same - if (parts.length != u.getUriParts().size()) { - continue; // different - } - - List<UriPart> uriParts = u.getUriParts(); - - boolean match = true; - for (int i = 0; i < parts.length; i++) { - String currentPart = parts[i]; - UriPart uriPart = uriParts.get(i); - boolean goOn = false; - - if (currentPart.equals(uriPart.getName())) { - // exact match - goOn = true; - }else { - // not match - if (uriPart.isParam()){ - goOn = true; - }else{ - match = false; - goOn = false; - } - } - if (!goOn) { - match = false; - break; - } - } // for - iterate incoming url parts - if (match) { - resultList.add(u); // current match - } - } // end iterate over all rules - if (resultList.size() > 0) { - // some results - UriResource result = null; - if (resultList.size() > 1) { - //return the rule with less parameters - int params = 1024; - for (UriResource u : resultList) { - if (!u.hasParameters()) { - result = u; - break; - }else{ - if (u.getUriParamsCount() <= params) { - result = u; - } - } - } - return result; - }else{ - return resultList.get(0); - } - } - return error404Url; - } - - public void addRoute(String url, Class<?> handler) { - if (url != null) { - if (handler != null) { - mappings.add(new UriResource(url, handler)); - }else{ - mappings.add(new UriResource(url, notImplemented)); - } - } - - } - - public void removeRoute(String url) { - if (mappings.contains(url)) { - mappings.remove(url); - } - } - - public void setNotFoundHandler(Class<?> handler) { - error404Url = new UriResource(null, handler); - } - - public void setNotImplemented(Class<?> handler) { - notImplemented = handler; - } - - /** - * Extract parameters and their values for the given route - * @param route - * @param uri - * @return - */ - public Map<String, String> getUrlParams(UriResource route, String uri) { - Map<String, String> result = new HashMap<String, String>(); - if (route.getUri() == null) { - return result; - } - - String workUri = StringUtils.normalizeUri(uri); - String[] parts = workUri.split("/"); - - - for (int i = 0; i < parts.length; i++ ) { - UriPart currentPart = route.getUriParts().get(i); - if (currentPart.isParam()) { - result.put(currentPart.getName(), parts[i]); - } - } - return result; - } -} diff --git a/samples/src/main/java/fi/iki/elonen/router/UserHandler.java b/samples/src/main/java/fi/iki/elonen/router/UserHandler.java deleted file mode 100644 index 8999793..0000000 --- a/samples/src/main/java/fi/iki/elonen/router/UserHandler.java +++ /dev/null @@ -1,88 +0,0 @@ -package fi.iki.elonen.router; -/* - * #%L - * NanoHttpd-Samples - * %% - * Copyright (C) 2012 - 2015 nanohttpd - * %% - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the nanohttpd nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE - * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED - * OF THE POSSIBILITY OF SUCH DAMAGE. - * #L% - */ - -import fi.iki.elonen.NanoHTTPD; - -import java.io.ByteArrayInputStream; -import java.util.Map; - - - -/** - * Created by vnnv on 7/21/15. - */ -public class UserHandler implements UriResponder { - public RouterResponse get(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - String text = "<html><body>User handler. Method: " + session.getMethod().toString() + "<br>"; - text += "<h1>Uri parameters:</h1>"; - for (Map.Entry<String, String> entry : urlParams.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - text += "<div> Param: " + key + " Value: " + value + "</div>"; - } - text += "<h1>Query parameters:</h1>"; - for (Map.Entry<String, String> entry : session.getParms().entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - text += "<div> Query Param: " + key + " Value: " + value + "</div>"; - } - text += "</body></html>"; - - ByteArrayInputStream inp = new ByteArrayInputStream(text.getBytes()); - - RouterResponse result = new RouterResponse(); - result.setData(inp); - result.setMimeType("text/html"); - result.setSize(text.getBytes().length); - result.setStatus(NanoHTTPD.Response.Status.OK); - - return result; - } - - public RouterResponse post(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public RouterResponse put(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public RouterResponse delete(UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } - - public RouterResponse other(String method, UriResource uriResource, Map<String, String> urlParams, NanoHTTPD.IHTTPSession session) { - return get(uriResource, urlParams, session); - } -} |