From b22569a7c0aa667a4cf4845cc4574aaaa979c90f Mon Sep 17 00:00:00 2001 From: Elias N Vasylenko Date: Fri, 24 Mar 2017 10:15:08 +0000 Subject: [PATCH 1/2] Simple response to feature request #68, enable primary rest endpoint. If a method is named for the verb with no path given, provide from the root of the endpoint. Signed-off-by: Elias N Vasylenko --- .../src/osgi/enroute/rest/simple/provider/RestMapper.java | 8 +++----- .../osgi/enroute/rest/simple/provider/RestServlet.java | 8 -------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/RestMapper.java b/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/RestMapper.java index 0d7844a..bea5137 100644 --- a/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/RestMapper.java +++ b/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/RestMapper.java @@ -193,7 +193,7 @@ static String decode(String path, boolean toLower) { // // Skip last _ so we can escape keywords // - if (sb.charAt(sb.length() - 1) == '-') + if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '-') sb.setLength(sb.length() - 1); return sb.toString(); } @@ -229,10 +229,8 @@ public boolean execute(HttpServletRequest rq, HttpServletResponse rsp) try { String path = rq.getPathInfo(); if (path == null) - throw new IllegalArgumentException( - "The rest servlet requires that the name of the resource follows the servlet path ('rest'), like /rest/aQute.service.library.Program[/...]*[?...]"); - - if (path.startsWith("/")) + path = ""; + else if (path.startsWith("/")) path = path.substring(1); if (path.equals("openapi.json")) { diff --git a/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/RestServlet.java b/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/RestServlet.java index bb80989..7576d2f 100644 --- a/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/RestServlet.java +++ b/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/RestServlet.java @@ -49,14 +49,6 @@ void setCloseable(Closeable c) { } public void service(HttpServletRequest rq, HttpServletResponse rsp) throws IOException, ServletException { - String pathInfo = rq.getPathInfo(); - if (pathInfo == null) { - rsp.getWriter().println( - "The rest servlet requires that the name of the resource follows the servlet path ('rest'), like /rest/aQute.service.library.Program[/...]*[?...]"); - rsp.setStatus(HttpServletResponse.SC_BAD_REQUEST); - return; - } - if (corsEnabled) { addCorsHeaders(rsp); } From 0218e57c7f102cf0956391153938d966819b3b67 Mon Sep 17 00:00:00 2001 From: Elias N Vasylenko Date: Fri, 24 Mar 2017 10:15:08 +0000 Subject: [PATCH 2/2] Simple response to feature request #68, enable primary rest endpoint. If a method is named for the verb with no path given, provide from the root of the endpoint. Signed-off-by: Elias N Vasylenko --- .../src/osgi/enroute/rest/simple/provider/Function.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/Function.java b/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/Function.java index 0c67471..5d04047 100644 --- a/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/Function.java +++ b/osgi.enroute.rest.simple.provider/src/osgi/enroute/rest/simple/provider/Function.java @@ -122,6 +122,10 @@ public Object convert(Type dest, Object o) throws Exception { hasPayloadAsParameter = false; } + if (path.equals("/") && cardinality > 0) + throw new IllegalArgumentException("Invalid " + verb + + " method " + method.getName() + ". A method on the root path cannot have a non-zero cardinality."); + this.post = requestBody; this.cardinality = cardinality;