CAMEL-7691: camel-serlvet - Potential NPE if no servlet name configured for osgi
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/f070a83f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/f070a83f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/f070a83f Branch: refs/heads/master Commit: f070a83fa2f95ffa345b48b555cb7c8d7b6b8da7 Parents: fb77527 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Aug 13 10:25:17 2014 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Aug 13 10:31:39 2014 +0200 ---------------------------------------------------------------------- .../servlet/osgi/OsgiServletRegisterer.java | 28 ++++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/f070a83f/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/osgi/OsgiServletRegisterer.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/osgi/OsgiServletRegisterer.java b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/osgi/OsgiServletRegisterer.java index 6c786ad..429f9e5 100644 --- a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/osgi/OsgiServletRegisterer.java +++ b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/osgi/OsgiServletRegisterer.java @@ -20,6 +20,7 @@ import java.util.Dictionary; import java.util.Hashtable; import javax.servlet.http.HttpServlet; +import org.apache.camel.util.ObjectHelper; import org.osgi.service.http.HttpContext; import org.osgi.service.http.HttpService; @@ -38,8 +39,11 @@ public class OsgiServletRegisterer { * of the form "/" is used to denote the root alias. */ private String alias; - - private String servletName; + + /** + * The servlet name. + */ + private String servletName = "CamelServlet"; /** * Servlet to be registered @@ -47,14 +51,16 @@ public class OsgiServletRegisterer { private HttpServlet servlet; /** - * HttpService to register with. Get this with osgi:reference in the spring - * context + * HttpService to register with. Get this with osgi:reference in the blueprint/spring-dm file */ private HttpService httpService; private HttpContext httpContext; private boolean alreadyRegistered; + + // The servlet will default have to match on uri prefix as some endpoints may do so + private volatile boolean matchOnUriPrefix = true; public void setHttpService(HttpService httpService) { this.httpService = httpService; @@ -75,14 +81,20 @@ public class OsgiServletRegisterer { public void setHttpContext(HttpContext httpContext) { this.httpContext = httpContext; } - + + public void setMatchOnUriPrefix(boolean matchOnUriPrefix) { + this.matchOnUriPrefix = matchOnUriPrefix; + } + public void register() throws Exception { - HttpContext actualHttpContext = (httpContext == null) + ObjectHelper.notEmpty(alias, "alias", this); + ObjectHelper.notEmpty(servletName, "servletName", this); + + HttpContext actualHttpContext = (httpContext == null) ? httpService.createDefaultHttpContext() : httpContext; final Dictionary<String, String> initParams = new Hashtable<String, String>(); - // The servlet will always have to match on uri prefix as some endpoints may do so - initParams.put("matchOnUriPrefix", "true"); + initParams.put("matchOnUriPrefix", matchOnUriPrefix ? "true" : "false"); initParams.put("servlet-name", servletName); httpService.registerServlet(alias, servlet, initParams, actualHttpContext); alreadyRegistered = true;