All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jetty.websocket.server.ServletWebSocketRequest Maven / Gradle / Ivy

//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.websocket.server;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
import org.eclipse.jetty.websocket.api.util.QuoteUtil;

public class ServletWebSocketRequest extends UpgradeRequest
{
    private Map cookieMap;
    private HttpServletRequest req;

    public ServletWebSocketRequest(HttpServletRequest request)
    {
        super(request.getRequestURI());
        this.req = request;

        // Copy Request Line Details
        setMethod(request.getMethod());
        setHttpVersion(request.getProtocol());

        // Copy parameters
        super.setParameterMap(request.getParameterMap());

        // Copy Cookies
        cookieMap = new HashMap();
        for (Cookie cookie : request.getCookies())
        {
            cookieMap.put(cookie.getName(),cookie.getValue());
        }

        // Copy Headers
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements())
        {
            String name = headerNames.nextElement();
            Enumeration valuesEnum = request.getHeaders(name);
            List values = new ArrayList<>();
            while (valuesEnum.hasMoreElements())
            {
                values.add(valuesEnum.nextElement());
            }
            setHeader(name,values);
        }

        // Parse Sub Protocols
        Enumeration protocols = request.getHeaders("Sec-WebSocket-Protocol");
        List subProtocols = new ArrayList<>();
        String protocol = null;
        while ((protocol == null) && (protocols != null) && protocols.hasMoreElements())
        {
            String candidate = protocols.nextElement();
            for (String p : parseProtocols(candidate))
            {
                subProtocols.add(p);
            }
        }
        setSubProtocols(subProtocols);

        // Parse Extension Configurations
        Enumeration e = request.getHeaders("Sec-WebSocket-Extensions");
        while (e.hasMoreElements())
        {
            Iterator extTokenIter = QuoteUtil.splitAt(e.nextElement(),",");
            while (extTokenIter.hasNext())
            {
                String extToken = extTokenIter.next();
                ExtensionConfig config = ExtensionConfig.parse(extToken);
                addExtensions(config);
            }
        }
    }

    public Principal getPrincipal()
    {
        return req.getUserPrincipal();
    }

    public StringBuffer getRequestURL()
    {
        return req.getRequestURL();
    }

    public Map getServletAttributes()
    {
        Map attributes = new HashMap();

        for (String name : Collections.list(req.getAttributeNames()))
        {
            attributes.put(name,req.getAttribute(name));
        }

        return attributes;
    }

    public Map> getServletParameters()
    {
        Map> parameters = new HashMap>();

        for (String name : Collections.list(req.getParameterNames()))
        {
            parameters.put(name,Collections.unmodifiableList(Arrays.asList(req.getParameterValues(name))));
        }

        return parameters;
    }

    /**
     * Return the HttpSession if it exists.
     * 

* Note: this is equivalent to {@link HttpServletRequest#getSession()} and will not create a new HttpSession. */ @Override public Object getSession() { return this.req.getSession(); } protected String[] parseProtocols(String protocol) { if (protocol == null) { return new String[] { null }; } protocol = protocol.trim(); if ((protocol == null) || (protocol.length() == 0)) { return new String[] { null }; } String[] passed = protocol.split("\\s*,\\s*"); String[] protocols = new String[passed.length + 1]; System.arraycopy(passed,0,protocols,0,passed.length); return protocols; } public void setServletAttribute(String name, Object o) { this.req.setAttribute(name,o); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy