Search in sources :

Example 1 with ServletInfo

use of org.apache.karaf.http.core.ServletInfo in project karaf by apache.

the class ServletListCommand method execute.

@Override
public Object execute() throws Exception {
    ShellTable table = new ShellTable();
    table.column(new Col("ID"));
    table.column(new Col("Servlet"));
    table.column(new Col("Servlet-Name"));
    table.column(new Col("State"));
    table.column(new Col("Alias"));
    table.column(new Col("Url"));
    for (ServletInfo info : servletService.getServlets()) {
        table.addRow().addContent(info.getBundleId(), info.getClassName(), info.getName(), info.getStateString(), info.getAlias(), Arrays.toString(info.getUrls()));
    }
    table.print(System.out, !noFormat);
    return null;
}
Also used : ServletInfo(org.apache.karaf.http.core.ServletInfo) Col(org.apache.karaf.shell.support.table.Col) ShellTable(org.apache.karaf.shell.support.table.ShellTable)

Example 2 with ServletInfo

use of org.apache.karaf.http.core.ServletInfo in project karaf by apache.

the class ServletServiceImpl method getServlets.

@Override
public List<ServletInfo> getServlets() {
    List<ServletInfo> servletInfos = new ArrayList<>();
    List<ServletEvent> events = servletEventHandler.getServletEvents();
    events.sort(Comparator.<ServletEvent>comparingLong(s -> s.getBundle().getBundleId()).thenComparing(ServletEvent::getServletName));
    for (ServletEvent event : events) {
        Servlet servlet = event.getServlet();
        String servletClassName = " ";
        if (servlet != null) {
            servletClassName = servlet.getClass().getName();
            servletClassName = servletClassName.substring(servletClassName.lastIndexOf(".") + 1, servletClassName.length());
        }
        String servletName = event.getServletName() != null ? event.getServletName() : " ";
        if (servletName.contains(".")) {
            servletName = servletName.substring(servletName.lastIndexOf(".") + 1, servletName.length());
        }
        String alias = event.getAlias();
        String[] urls = event.getUrlParameter();
        String contextPath = event.getBundle().getHeaders().get("Web-ContextPath");
        if (contextPath == null) {
            // this one used by pax-web but is deprecated
            contextPath = event.getBundle().getHeaders().get("Webapp-Context");
        }
        if (contextPath != null) {
            contextPath = contextPath.trim();
            if (!contextPath.startsWith("/")) {
                contextPath = "/" + contextPath;
            }
            if (alias != null) {
                alias = contextPath + alias;
            }
            if (urls != null) {
                urls = urls.clone();
                for (int i = 0; i < urls.length; i++) {
                    if (urls[i].startsWith("/")) {
                        urls[i] = contextPath + urls[i];
                    } else {
                        urls[i] = contextPath + "/" + urls[i];
                    }
                }
            }
        }
        ServletInfo info = new ServletInfo();
        info.setBundleId(event.getBundle().getBundleId());
        info.setName(servletName);
        info.setClassName(servletClassName);
        info.setState(event.getType());
        info.setAlias(alias != null ? alias : " ");
        info.setUrls(urls != null ? urls : new String[] { "" });
        servletInfos.add(info);
    }
    return servletInfos;
}
Also used : ServletInfo(org.apache.karaf.http.core.ServletInfo) ArrayList(java.util.ArrayList) ServletEvent(org.ops4j.pax.web.service.spi.ServletEvent) Servlet(javax.servlet.Servlet)

Aggregations

ServletInfo (org.apache.karaf.http.core.ServletInfo)2 ArrayList (java.util.ArrayList)1 Servlet (javax.servlet.Servlet)1 Col (org.apache.karaf.shell.support.table.Col)1 ShellTable (org.apache.karaf.shell.support.table.ShellTable)1 ServletEvent (org.ops4j.pax.web.service.spi.ServletEvent)1