Search in sources :

Example 1 with ExtServletContext

use of org.apache.felix.http.base.internal.context.ExtServletContext in project felix by apache.

the class PerBundleHttpServiceImpl method registerServlet.

/**
 * @see org.osgi.service.http.HttpService#registerServlet(java.lang.String, javax.servlet.Servlet, java.util.Dictionary, org.osgi.service.http.HttpContext)
 */
@Override
public void registerServlet(String alias, Servlet servlet, Dictionary initParams, HttpContext context) throws ServletException, NamespaceException {
    if (servlet == null) {
        throw new IllegalArgumentException("Servlet must not be null");
    }
    if (!PatternUtil.isValidPattern(alias) || !alias.startsWith("/")) {
        throw new IllegalArgumentException("Malformed servlet alias [" + alias + "]");
    }
    final Map<String, String> paramMap = new HashMap<>();
    if (initParams != null && initParams.size() > 0) {
        Enumeration e = initParams.keys();
        while (e.hasMoreElements()) {
            Object key = e.nextElement();
            Object value = initParams.get(key);
            if ((key instanceof String) && (value instanceof String)) {
                paramMap.put((String) key, (String) value);
            }
        }
    }
    synchronized (this.localServlets) {
        if (this.localServlets.contains(servlet)) {
            throw new ServletException("Servlet instance " + servlet + " already registered");
        }
        this.localServlets.add(servlet);
    }
    final ServletInfo servletInfo = new ServletInfo(String.format("%s_%d", servlet.getClass(), this.hashCode()), alias, paramMap);
    final ExtServletContext httpContext = getServletContext(context);
    boolean success = false;
    try {
        this.sharedHttpService.registerServlet(alias, httpContext, servlet, servletInfo);
        success = true;
    } finally {
        if (!success) {
            synchronized (this.localServlets) {
                this.localServlets.remove(servlet);
            }
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) Enumeration(java.util.Enumeration) ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) HashMap(java.util.HashMap)

Example 2 with ExtServletContext

use of org.apache.felix.http.base.internal.context.ExtServletContext in project felix by apache.

the class WhiteboardManager method sessionDestroyed.

public void sessionDestroyed(@Nonnull final HttpSession session, final Set<String> contextNames) {
    for (final String contextName : contextNames) {
        final WhiteboardContextHandler handler = this.getContextHandler(contextName);
        if (handler != null) {
            final ExtServletContext context = handler.getServletContext(this.httpBundleContext.getBundle());
            new HttpSessionWrapper(session, context, true).invalidate();
            handler.ungetServletContext(this.httpBundleContext.getBundle());
        }
    }
}
Also used : ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) HttpSessionWrapper(org.apache.felix.http.base.internal.handler.HttpSessionWrapper)

Example 3 with ExtServletContext

use of org.apache.felix.http.base.internal.context.ExtServletContext in project felix by apache.

the class WhiteboardManager method registerWhiteboardService.

/**
 * Register whiteboard service in the http service
 * @param handler Context handler
 * @param info Whiteboard service info
 */
private void registerWhiteboardService(final WhiteboardContextHandler handler, final WhiteboardServiceInfo<?> info) {
    try {
        int failureCode = -1;
        if (info instanceof ServletInfo) {
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final ServletHandler servletHandler = new WhiteboardServletHandler(handler.getContextInfo().getServiceId(), servletContext, (ServletInfo) info, handler.getBundleContext(), info.getServiceReference().getBundle(), this.httpBundleContext.getBundle());
                handler.getRegistry().registerServlet(servletHandler);
            }
        } else if (info instanceof FilterInfo) {
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final FilterHandler filterHandler = new WhiteboardFilterHandler(handler.getContextInfo().getServiceId(), servletContext, (FilterInfo) info, handler.getBundleContext());
                handler.getRegistry().registerFilter(filterHandler);
            }
        } else if (info instanceof ResourceInfo) {
            final ServletInfo servletInfo = ((ResourceInfo) info).getServletInfo();
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final ServletHandler servleHandler = new HttpServiceServletHandler(handler.getContextInfo().getServiceId(), servletContext, servletInfo, new ResourceServlet(servletInfo.getPrefix()));
                handler.getRegistry().registerServlet(servleHandler);
            }
        } else if (info instanceof ListenerInfo) {
            final ExtServletContext servletContext = handler.getServletContext(info.getServiceReference().getBundle());
            if (servletContext == null) {
                failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
            } else {
                final ListenerHandler listenerHandler = new WhiteboardListenerHandler(handler.getContextInfo().getServiceId(), servletContext, (ListenerInfo) info, handler.getBundleContext());
                handler.getRegistry().registerListeners(listenerHandler);
            }
        } else {
            // This should never happen, but we log anyway
            SystemLogger.error("Unknown whiteboard service " + info.getServiceReference(), null);
        }
        if (failureCode != -1) {
            this.failureStateHandler.addFailure(info, handler.getContextInfo().getServiceId(), failureCode);
        }
    } catch (final Exception e) {
        this.failureStateHandler.addFailure(info, handler.getContextInfo().getServiceId(), FAILURE_REASON_UNKNOWN, e);
    }
}
Also used : WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ResourceInfo(org.apache.felix.http.base.internal.runtime.ResourceInfo) WhiteboardServletHandler(org.apache.felix.http.base.internal.handler.WhiteboardServletHandler) ServletHandler(org.apache.felix.http.base.internal.handler.ServletHandler) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) WhiteboardFilterHandler(org.apache.felix.http.base.internal.handler.WhiteboardFilterHandler) WhiteboardFilterHandler(org.apache.felix.http.base.internal.handler.WhiteboardFilterHandler) FilterHandler(org.apache.felix.http.base.internal.handler.FilterHandler) ServletException(javax.servlet.ServletException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) ResourceServlet(org.apache.felix.http.base.internal.service.ResourceServlet) WhiteboardServletHandler(org.apache.felix.http.base.internal.handler.WhiteboardServletHandler) FilterInfo(org.apache.felix.http.base.internal.runtime.FilterInfo) WhiteboardListenerHandler(org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler)

Example 4 with ExtServletContext

use of org.apache.felix.http.base.internal.context.ExtServletContext in project felix by apache.

the class ErrorPageRegistryTest method createServletHandler.

private static ServletHandler createServletHandler(final long id, final int ranking, final String... codes) throws InvalidSyntaxException {
    final ServletInfo si = createServletInfo(id, ranking, codes);
    final ExtServletContext ctx = mock(ExtServletContext.class);
    final Servlet servlet = mock(Servlet.class);
    return new HttpServiceServletHandler(ctx, si, servlet);
}
Also used : ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) HttpServiceServletHandler(org.apache.felix.http.base.internal.handler.HttpServiceServletHandler) Servlet(javax.servlet.Servlet)

Example 5 with ExtServletContext

use of org.apache.felix.http.base.internal.context.ExtServletContext in project felix by apache.

the class EventListenerRegistryTest method createFilterHandler.

private static FilterHandler createFilterHandler(final long id, final int ranking, final String... paths) throws InvalidSyntaxException {
    final FilterInfo si = createFilterInfo(id, ranking, paths);
    final ExtServletContext ctx = mock(ExtServletContext.class);
    final Filter filter = mock(Filter.class);
    return new HttpServiceFilterHandler(ctx, si, filter);
}
Also used : ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) Filter(javax.servlet.Filter) HttpServiceFilterHandler(org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler) FilterInfo(org.apache.felix.http.base.internal.runtime.FilterInfo)

Aggregations

ExtServletContext (org.apache.felix.http.base.internal.context.ExtServletContext)10 ServletException (javax.servlet.ServletException)4 ServletInfo (org.apache.felix.http.base.internal.runtime.ServletInfo)4 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)3 FilterInfo (org.apache.felix.http.base.internal.runtime.FilterInfo)3 IOException (java.io.IOException)2 Enumeration (java.util.Enumeration)2 HashMap (java.util.HashMap)2 Servlet (javax.servlet.Servlet)2 FilterHandler (org.apache.felix.http.base.internal.handler.FilterHandler)2 HttpServiceFilterHandler (org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler)2 WhiteboardListenerHandler (org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler)2 ListenerInfo (org.apache.felix.http.base.internal.runtime.ListenerInfo)2 Filter (javax.servlet.Filter)1 FilterChain (javax.servlet.FilterChain)1 FilterConfig (javax.servlet.FilterConfig)1 ServletRequest (javax.servlet.ServletRequest)1 ServletRequestEvent (javax.servlet.ServletRequestEvent)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1