Search in sources :

Example 1 with PathSpec

use of org.eclipse.jetty.http.pathmap.PathSpec in project jetty.project by eclipse.

the class NativeWebSocketConfiguration method addMapping.

@Override
public void addMapping(String rawspec, WebSocketCreator creator) {
    PathSpec spec = toPathSpec(rawspec);
    addMapping(spec, creator);
}
Also used : RegexPathSpec(org.eclipse.jetty.http.pathmap.RegexPathSpec) ServletPathSpec(org.eclipse.jetty.http.pathmap.ServletPathSpec) UriTemplatePathSpec(org.eclipse.jetty.http.pathmap.UriTemplatePathSpec) PathSpec(org.eclipse.jetty.http.pathmap.PathSpec)

Example 2 with PathSpec

use of org.eclipse.jetty.http.pathmap.PathSpec in project jetty.project by eclipse.

the class WebSocketUpgradeFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if (configuration == null) {
        // no configuration, cannot operate
        LOG.debug("WebSocketUpgradeFilter is not operational - missing " + NativeWebSocketConfiguration.class.getName());
        chain.doFilter(request, response);
        return;
    }
    WebSocketServletFactory factory = configuration.getFactory();
    if (factory == null) {
        // no factory, cannot operate
        LOG.debug("WebSocketUpgradeFilter is not operational - no WebSocketServletFactory configured");
        chain.doFilter(request, response);
        return;
    }
    try {
        HttpServletRequest httpreq = (HttpServletRequest) request;
        HttpServletResponse httpresp = (HttpServletResponse) response;
        if (!factory.isUpgradeRequest(httpreq, httpresp)) {
            // Not an upgrade request, skip it
            chain.doFilter(request, response);
            return;
        }
        // Since this is a filter, we need to be smart about determining the target path
        String contextPath = httpreq.getContextPath();
        String target = httpreq.getRequestURI();
        if (target.startsWith(contextPath)) {
            target = target.substring(contextPath.length());
        }
        MappedResource<WebSocketCreator> resource = configuration.getMatch(target);
        if (resource == null) {
            // no match.
            chain.doFilter(request, response);
            return;
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("WebSocket Upgrade detected on {} for endpoint {}", target, resource);
        }
        WebSocketCreator creator = resource.getResource();
        // Store PathSpec resource mapping as request attribute
        httpreq.setAttribute(PathSpec.class.getName(), resource.getPathSpec());
        // We have an upgrade request
        if (factory.acceptWebSocket(creator, httpreq, httpresp)) {
            // We have a socket instance created
            return;
        }
        // due to incoming request constraints (controlled by WebSocketCreator)
        if (response.isCommitted()) {
            // not much we can do at this point.
            return;
        }
    } catch (ClassCastException e) {
        // We are in some kind of funky non-http environment.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Not a HttpServletRequest, skipping WebSocketUpgradeFilter");
        }
    }
    // not an Upgrade request
    chain.doFilter(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebSocketServletFactory(org.eclipse.jetty.websocket.servlet.WebSocketServletFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) PathSpec(org.eclipse.jetty.http.pathmap.PathSpec)

Example 3 with PathSpec

use of org.eclipse.jetty.http.pathmap.PathSpec in project jetty.project by eclipse.

the class NativeWebSocketConfiguration method addMapping.

/**
     * Manually add a WebSocket mapping.
     *
     * @param rawspec the pathspec to map to (see {@link MappedWebSocketCreator#addMapping(String, WebSocketCreator)} for syntax details)
     * @param endpointClass the endpoint class to use for new upgrade requests on the provided
     * pathspec (can be an {@link org.eclipse.jetty.websocket.api.annotations.WebSocket} annotated
     * POJO, or implementing {@link org.eclipse.jetty.websocket.api.WebSocketListener})
     */
public void addMapping(String rawspec, final Class<?> endpointClass) {
    PathSpec pathSpec = toPathSpec(rawspec);
    addMapping(pathSpec, endpointClass);
}
Also used : RegexPathSpec(org.eclipse.jetty.http.pathmap.RegexPathSpec) ServletPathSpec(org.eclipse.jetty.http.pathmap.ServletPathSpec) UriTemplatePathSpec(org.eclipse.jetty.http.pathmap.UriTemplatePathSpec) PathSpec(org.eclipse.jetty.http.pathmap.PathSpec)

Example 4 with PathSpec

use of org.eclipse.jetty.http.pathmap.PathSpec in project jetty.project by eclipse.

the class ServletHandler method doScope.

@Override
public void doScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    // Get the base requests
    final String old_servlet_path = baseRequest.getServletPath();
    final String old_path_info = baseRequest.getPathInfo();
    DispatcherType type = baseRequest.getDispatcherType();
    ServletHolder servlet_holder = null;
    UserIdentity.Scope old_scope = null;
    // find the servlet
    if (target.startsWith("/")) {
        // Look for the servlet by path
        MappedResource<ServletHolder> entry = getHolderEntry(target);
        if (entry != null) {
            PathSpec pathSpec = entry.getPathSpec();
            servlet_holder = entry.getResource();
            String servlet_path = pathSpec.getPathMatch(target);
            String path_info = pathSpec.getPathInfo(target);
            if (DispatcherType.INCLUDE.equals(type)) {
                baseRequest.setAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH, servlet_path);
                baseRequest.setAttribute(RequestDispatcher.INCLUDE_PATH_INFO, path_info);
            } else {
                baseRequest.setServletPath(servlet_path);
                baseRequest.setPathInfo(path_info);
            }
        }
    } else {
        // look for a servlet by name!
        servlet_holder = _servletNameMap.get(target);
    }
    if (LOG.isDebugEnabled())
        LOG.debug("servlet {}|{}|{} -> {}", baseRequest.getContextPath(), baseRequest.getServletPath(), baseRequest.getPathInfo(), servlet_holder);
    try {
        // Do the filter/handling thang
        old_scope = baseRequest.getUserIdentityScope();
        baseRequest.setUserIdentityScope(servlet_holder);
        nextScope(target, baseRequest, request, response);
    } finally {
        if (old_scope != null)
            baseRequest.setUserIdentityScope(old_scope);
        if (!(DispatcherType.INCLUDE.equals(type))) {
            baseRequest.setServletPath(old_servlet_path);
            baseRequest.setPathInfo(old_path_info);
        }
    }
}
Also used : UserIdentity(org.eclipse.jetty.server.UserIdentity) DispatcherType(javax.servlet.DispatcherType) PathSpec(org.eclipse.jetty.http.pathmap.PathSpec) ServletPathSpec(org.eclipse.jetty.http.pathmap.ServletPathSpec)

Example 5 with PathSpec

use of org.eclipse.jetty.http.pathmap.PathSpec in project jetty.project by eclipse.

the class JsrCreator method createWebSocket.

@Override
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
    JsrHandshakeRequest jsrHandshakeRequest = new JsrHandshakeRequest(req);
    JsrHandshakeResponse jsrHandshakeResponse = new JsrHandshakeResponse(resp);
    // Get raw config, as defined when the endpoint was added to the container
    ServerEndpointConfig config = metadata.getConfig();
    // Establish a copy of the config, so that the UserProperties are unique
    // per upgrade request.
    config = new BasicServerEndpointConfig(containerScope, config);
    // Bug 444617 - Expose localAddress and remoteAddress for jsr modify handshake to use
    // This is being implemented as an optional set of userProperties so that
    // it is not JSR api breaking.  A few users on #jetty and a few from cometd
    // have asked for access to this information.
    Map<String, Object> userProperties = config.getUserProperties();
    userProperties.put(PROP_LOCAL_ADDRESS, req.getLocalSocketAddress());
    userProperties.put(PROP_REMOTE_ADDRESS, req.getRemoteSocketAddress());
    userProperties.put(PROP_LOCALES, Collections.list(req.getLocales()));
    // Get Configurator from config object (not guaranteed to be unique per endpoint upgrade)
    ServerEndpointConfig.Configurator configurator = config.getConfigurator();
    // [JSR] Step 1: check origin
    if (!configurator.checkOrigin(req.getOrigin())) {
        try {
            resp.sendForbidden("Origin mismatch");
        } catch (IOException e) {
            if (LOG.isDebugEnabled())
                LOG.debug("Unable to send error response", e);
        }
        return null;
    }
    // [JSR] Step 2: deal with sub protocols
    List<String> supported = config.getSubprotocols();
    List<String> requested = req.getSubProtocols();
    String subprotocol = configurator.getNegotiatedSubprotocol(supported, requested);
    if (StringUtil.isNotBlank(subprotocol)) {
        resp.setAcceptedSubProtocol(subprotocol);
    }
    // [JSR] Step 3: deal with extensions
    List<Extension> installedExtensions = new ArrayList<>();
    for (String extName : extensionFactory.getAvailableExtensions().keySet()) {
        installedExtensions.add(new JsrExtension(extName));
    }
    List<Extension> requestedExts = new ArrayList<>();
    for (ExtensionConfig reqCfg : req.getExtensions()) {
        requestedExts.add(new JsrExtension(reqCfg));
    }
    List<Extension> usedExtensions = configurator.getNegotiatedExtensions(installedExtensions, requestedExts);
    List<ExtensionConfig> configs = new ArrayList<>();
    if (usedExtensions != null) {
        for (Extension used : usedExtensions) {
            ExtensionConfig ecfg = new ExtensionConfig(used.getName());
            for (Parameter param : used.getParameters()) {
                ecfg.setParameter(param.getName(), param.getValue());
            }
            configs.add(ecfg);
        }
    }
    resp.setExtensions(configs);
    // [JSR] Step 4: build out new ServerEndpointConfig
    PathSpec pathSpec = jsrHandshakeRequest.getRequestPathSpec();
    if (pathSpec instanceof UriTemplatePathSpec) {
        // We have a PathParam path spec
        UriTemplatePathSpec wspathSpec = (UriTemplatePathSpec) pathSpec;
        String requestPath = req.getRequestPath();
        // Wrap the config with the path spec information
        config = new PathParamServerEndpointConfig(containerScope, config, wspathSpec, requestPath);
    }
    // [JSR] Step 5: Call modifyHandshake
    configurator.modifyHandshake(config, jsrHandshakeRequest, jsrHandshakeResponse);
    try {
        // [JSR] Step 6: create endpoint class
        Class<?> endpointClass = config.getEndpointClass();
        Object endpoint = config.getConfigurator().getEndpointInstance(endpointClass);
        // This will allow CDI to see Session for injection into Endpoint classes.
        return new EndpointInstance(endpoint, config, metadata);
    } catch (InstantiationException e) {
        if (LOG.isDebugEnabled())
            LOG.debug("Unable to create websocket: " + config.getEndpointClass().getName(), e);
        return null;
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ArrayList(java.util.ArrayList) EndpointInstance(org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance) IOException(java.io.IOException) UriTemplatePathSpec(org.eclipse.jetty.http.pathmap.UriTemplatePathSpec) PathSpec(org.eclipse.jetty.http.pathmap.PathSpec) JsrExtension(org.eclipse.jetty.websocket.jsr356.JsrExtension) Extension(javax.websocket.Extension) JsrExtension(org.eclipse.jetty.websocket.jsr356.JsrExtension) UriTemplatePathSpec(org.eclipse.jetty.http.pathmap.UriTemplatePathSpec) ExtensionConfig(org.eclipse.jetty.websocket.api.extensions.ExtensionConfig) Parameter(javax.websocket.Extension.Parameter)

Aggregations

PathSpec (org.eclipse.jetty.http.pathmap.PathSpec)7 ServletPathSpec (org.eclipse.jetty.http.pathmap.ServletPathSpec)4 UriTemplatePathSpec (org.eclipse.jetty.http.pathmap.UriTemplatePathSpec)4 RegexPathSpec (org.eclipse.jetty.http.pathmap.RegexPathSpec)3 WebSocketCreator (org.eclipse.jetty.websocket.servlet.WebSocketCreator)3 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DispatcherType (javax.servlet.DispatcherType)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Extension (javax.websocket.Extension)1 Parameter (javax.websocket.Extension.Parameter)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1 MappedResource (org.eclipse.jetty.http.pathmap.MappedResource)1 UserIdentity (org.eclipse.jetty.server.UserIdentity)1 ExtensionConfig (org.eclipse.jetty.websocket.api.extensions.ExtensionConfig)1 JsrExtension (org.eclipse.jetty.websocket.jsr356.JsrExtension)1 EndpointInstance (org.eclipse.jetty.websocket.jsr356.endpoints.EndpointInstance)1 WebSocketServletFactory (org.eclipse.jetty.websocket.servlet.WebSocketServletFactory)1