Search in sources :

Example 1 with ServletInfo

use of org.apache.felix.http.base.internal.runtime.ServletInfo in project felix by apache.

the class ServletRegistry method getRuntimeInfo.

public void getRuntimeInfo(final ServletContextDTO servletContextDTO, final Collection<FailedServletDTO> allFailedServletDTOs, final Collection<FailedResourceDTO> allFailedResourceDTOs) {
    final Map<Long, ServletDTO> servletDTOs = new HashMap<Long, ServletDTO>();
    final Map<Long, ResourceDTO> resourceDTOs = new HashMap<Long, ResourceDTO>();
    final Map<Long, FailedServletDTO> failedServletDTOs = new HashMap<Long, FailedServletDTO>();
    final Map<Long, FailedResourceDTO> failedResourceDTOs = new HashMap<Long, FailedResourceDTO>();
    for (final Map.Entry<ServletInfo, RegistrationStatus> entry : mapping.entrySet()) {
        final long serviceId = entry.getKey().getServiceId();
        for (final Map.Entry<Integer, String[]> map : entry.getValue().statusToPath.entrySet()) {
            if (entry.getKey().isResource()) {
                final ResourceDTO state = ResourceDTOBuilder.build(entry.getValue().handler, map.getKey());
                if (map.getValue().length == 1 && NAMED_SERVLET_PATTERN == map.getValue()[0]) {
                    state.patterns = BuilderConstants.EMPTY_STRING_ARRAY;
                } else {
                    state.patterns = Arrays.copyOf(map.getValue(), map.getValue().length);
                }
                if (map.getKey() == -1) {
                    resourceDTOs.put(serviceId, state);
                } else {
                    failedResourceDTOs.put(serviceId, (FailedResourceDTO) state);
                }
            } else {
                final ServletDTO state = ServletDTOBuilder.build(entry.getValue().handler, map.getKey());
                if (map.getValue().length == 1 && NAMED_SERVLET_PATTERN == map.getValue()[0]) {
                    state.patterns = BuilderConstants.EMPTY_STRING_ARRAY;
                } else {
                    state.patterns = Arrays.copyOf(map.getValue(), map.getValue().length);
                }
                if (map.getKey() == -1) {
                    servletDTOs.put(serviceId, state);
                } else {
                    failedServletDTOs.put(serviceId, (FailedServletDTO) state);
                }
            }
        }
    }
    final Collection<ServletDTO> servletDTOsArray = servletDTOs.values();
    if (!servletDTOsArray.isEmpty()) {
        servletContextDTO.servletDTOs = servletDTOsArray.toArray(new ServletDTO[servletDTOsArray.size()]);
    }
    final Collection<ResourceDTO> resourceDTOsArray = resourceDTOs.values();
    if (!resourceDTOsArray.isEmpty()) {
        servletContextDTO.resourceDTOs = resourceDTOsArray.toArray(new ResourceDTO[resourceDTOsArray.size()]);
    }
    allFailedResourceDTOs.addAll(failedResourceDTOs.values());
    allFailedServletDTOs.addAll(failedServletDTOs.values());
}
Also used : FailedServletDTO(org.osgi.service.http.runtime.dto.FailedServletDTO) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) FailedResourceDTO(org.osgi.service.http.runtime.dto.FailedResourceDTO) ResourceDTO(org.osgi.service.http.runtime.dto.ResourceDTO) FailedResourceDTO(org.osgi.service.http.runtime.dto.FailedResourceDTO) ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) ServletDTO(org.osgi.service.http.runtime.dto.ServletDTO) FailedServletDTO(org.osgi.service.http.runtime.dto.FailedServletDTO)

Example 2 with ServletInfo

use of org.apache.felix.http.base.internal.runtime.ServletInfo in project felix by apache.

the class FailedDTOHolder method add.

public void add(final AbstractInfo<?> info, final long contextId, final int failureCode) {
    if (info instanceof ServletContextHelperInfo) {
        final FailedServletContextDTO dto = (FailedServletContextDTO) ServletContextDTOBuilder.build((ServletContextHelperInfo) info, null, failureCode);
        this.failedServletContextDTOs.add(dto);
    } else if (info instanceof ServletInfo) {
        boolean isError = false;
        if (((ServletInfo) info).getErrorPage() != null) {
            isError = true;
            final FailedErrorPageDTO dto = (FailedErrorPageDTO) ErrorPageDTOBuilder.build((ServletInfo) info, true);
            dto.failureReason = failureCode;
            final ErrorPageRegistry.ErrorRegistration reg = ErrorPageRegistry.getErrorRegistration((ServletInfo) info);
            dto.errorCodes = reg.errorCodes;
            dto.exceptions = reg.exceptions;
            dto.servletContextId = contextId;
            this.failedErrorPageDTOs.add(dto);
        }
        if (((ServletInfo) info).getPatterns() != null || ((ServletInfo) info).getName() != null || !isError) {
            final FailedServletDTO dto = (FailedServletDTO) ServletDTOBuilder.build((ServletInfo) info, failureCode);
            if (((ServletInfo) info).getPatterns() != null) {
                dto.patterns = ((ServletInfo) info).getPatterns();
            } else {
                dto.patterns = BuilderConstants.EMPTY_STRING_ARRAY;
            }
            dto.name = ((ServletInfo) info).getName();
            dto.servletContextId = contextId;
            this.failedServletDTOs.add(dto);
        }
    } else if (info instanceof FilterInfo) {
        final FailedFilterDTO dto = (FailedFilterDTO) FilterDTOBuilder.build((FilterInfo) info, failureCode);
        dto.failureReason = failureCode;
        dto.servletContextId = contextId;
        this.failedFilterDTOs.add(dto);
    } else if (info instanceof ResourceInfo) {
        final FailedResourceDTO dto = (FailedResourceDTO) ResourceDTOBuilder.build((ResourceInfo) info, true);
        dto.failureReason = failureCode;
        dto.servletContextId = contextId;
        this.failedResourceDTOs.add(dto);
    } else if (info instanceof ListenerInfo) {
        final FailedListenerDTO dto = (FailedListenerDTO) ListenerDTOBuilder.build((ListenerInfo) info, failureCode);
        dto.servletContextId = contextId;
        this.failedListenerDTOs.add(dto);
    } else if (info instanceof PreprocessorInfo) {
        final FailedPreprocessorDTO dto = (FailedPreprocessorDTO) PreprocessorDTOBuilder.build((PreprocessorInfo) info, failureCode);
        this.failedPreprocessorDTOs.add(dto);
    } else {
        SystemLogger.error("Unsupported info type: " + info.getClass(), null);
    }
}
Also used : FailedFilterDTO(org.osgi.service.http.runtime.dto.FailedFilterDTO) ResourceInfo(org.apache.felix.http.base.internal.runtime.ResourceInfo) FailedErrorPageDTO(org.osgi.service.http.runtime.dto.FailedErrorPageDTO) FailedServletDTO(org.osgi.service.http.runtime.dto.FailedServletDTO) FailedResourceDTO(org.osgi.service.http.runtime.dto.FailedResourceDTO) PreprocessorInfo(org.apache.felix.http.base.internal.runtime.PreprocessorInfo) ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) ServletContextHelperInfo(org.apache.felix.http.base.internal.runtime.ServletContextHelperInfo) FailedListenerDTO(org.osgi.service.http.runtime.dto.FailedListenerDTO) FailedPreprocessorDTO(org.osgi.service.http.runtime.dto.FailedPreprocessorDTO) FailedServletContextDTO(org.osgi.service.http.runtime.dto.FailedServletContextDTO) FilterInfo(org.apache.felix.http.base.internal.runtime.FilterInfo)

Example 3 with ServletInfo

use of org.apache.felix.http.base.internal.runtime.ServletInfo 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 4 with ServletInfo

use of org.apache.felix.http.base.internal.runtime.ServletInfo 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 5 with ServletInfo

use of org.apache.felix.http.base.internal.runtime.ServletInfo in project felix by apache.

the class ErrorPageRegistryTest method createServletInfo.

private static ServletInfo createServletInfo(final long id, final int ranking, final String... codes) throws InvalidSyntaxException {
    final BundleContext bCtx = mock(BundleContext.class);
    when(bCtx.createFilter(Matchers.anyString())).thenReturn(null);
    final Bundle bundle = mock(Bundle.class);
    when(bundle.getBundleContext()).thenReturn(bCtx);
    @SuppressWarnings("unchecked") final ServiceReference<Servlet> ref = mock(ServiceReference.class);
    when(ref.getBundle()).thenReturn(bundle);
    when(ref.getProperty(Constants.SERVICE_ID)).thenReturn(id);
    when(ref.getProperty(Constants.SERVICE_RANKING)).thenReturn(ranking);
    when(ref.getProperty(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_ERROR_PAGE)).thenReturn(codes);
    when(ref.getPropertyKeys()).thenReturn(new String[0]);
    final ServletInfo si = new ServletInfo(ref);
    return si;
}
Also used : ServletInfo(org.apache.felix.http.base.internal.runtime.ServletInfo) Bundle(org.osgi.framework.Bundle) Servlet(javax.servlet.Servlet) BundleContext(org.osgi.framework.BundleContext)

Aggregations

ServletInfo (org.apache.felix.http.base.internal.runtime.ServletInfo)12 Servlet (javax.servlet.Servlet)5 ExtServletContext (org.apache.felix.http.base.internal.context.ExtServletContext)4 HttpServiceServletHandler (org.apache.felix.http.base.internal.handler.HttpServiceServletHandler)4 HashMap (java.util.HashMap)3 ServletHandler (org.apache.felix.http.base.internal.handler.ServletHandler)3 FailedDTOHolder (org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder)3 Test (org.junit.Test)3 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ServletException (javax.servlet.ServletException)2 FilterInfo (org.apache.felix.http.base.internal.runtime.FilterInfo)2 ListenerInfo (org.apache.felix.http.base.internal.runtime.ListenerInfo)2 ResourceInfo (org.apache.felix.http.base.internal.runtime.ResourceInfo)2 Bundle (org.osgi.framework.Bundle)2 BundleContext (org.osgi.framework.BundleContext)2 FailedResourceDTO (org.osgi.service.http.runtime.dto.FailedResourceDTO)2 FailedServletDTO (org.osgi.service.http.runtime.dto.FailedServletDTO)2 IOException (java.io.IOException)1