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

org.eclipse.jetty.websocket.api.UpgradeResponse Maven / Gradle / Ivy

The newest version!
//
//  ========================================================================
//  Copyright (c) 1995-2012 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.api;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.net.websocket.HandshakeResponse;

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

public class UpgradeResponse implements HandshakeResponse
{
    public static final String SEC_WEBSOCKET_PROTOCOL = "Sec-WebSocket-Protocol";
    private int statusCode;
    private String statusReason;
    private Map> headers = new HashMap<>();
    private List extensions = new ArrayList<>();
    private boolean success = false;

    public void addHeader(String name, String value)
    {
        String key = name.toLowerCase();
        List values = headers.get(key);
        if (values == null)
        {
            values = new ArrayList<>();
        }
        values.add(value);
        headers.put(key,values);
    }

    /**
     * Get the accepted WebSocket protocol.
     * 
     * @return the accepted WebSocket protocol.
     */
    public String getAcceptedSubProtocol()
    {
        return getHeader(SEC_WEBSOCKET_PROTOCOL);
    }

    /**
     * Get the list of extensions that should be used for the websocket.
     * 
     * @return the list of negotiated extensions to use.
     */
    public List getExtensions()
    {
        return extensions;
    }

    public String getHeader(String name)
    {
        List values = getHeaders(name);
        // no value list
        if (values == null)
        {
            return null;
        }
        int size = values.size();
        // empty value list
        if (size <= 0)
        {
            return null;
        }
        // simple return
        if (size == 1)
        {
            return values.get(0);
        }
        // join it with commas
        boolean needsDelim = false;
        StringBuilder ret = new StringBuilder();
        for (String value : values)
        {
            if (needsDelim)
            {
                ret.append(", ");
            }
            QuoteUtil.quoteIfNeeded(ret,value,QuoteUtil.ABNF_REQUIRED_QUOTING);
            needsDelim = true;
        }
        return ret.toString();
    }

    public Set getHeaderNames()
    {
        return headers.keySet();
    }

    @Override
    public Map> getHeaders()
    {
        return headers;
    }

    public List getHeaders(String name)
    {
        return headers.get(name.toLowerCase());
    }

    public int getStatusCode()
    {
        return statusCode;
    }

    public String getStatusReason()
    {
        return statusReason;
    }

    public boolean isSuccess()
    {
        return success;
    }

    /**
     * Issue a forbidden upgrade response.
     * 

* This means that the websocket endpoint was valid, but the conditions to use a WebSocket resulted in a forbidden access. *

* Use this when the origin or authentication is invalid. * * @param message * the short 1 line detail message about the forbidden response * @throws IOException */ public void sendForbidden(String message) throws IOException { } /** * Set the accepted WebSocket Protocol. * * @param protocol * the protocol to list as accepted */ public void setAcceptedSubProtocol(String protocol) { setHeader(SEC_WEBSOCKET_PROTOCOL,protocol); } /** * Set the list of extensions that are approved for use with this websocket. *

* This is Advanced usage of the {@link WebSocketCreator} to allow for a custom set of negotiated extensions. *

* Notes: *

    *
  • Per the spec you cannot add extensions that have not been seen in the {@link UpgradeRequest}, just remove entries you don't want to use
  • *
  • If this is unused, or a null is passed, then the list negotiation will follow default behavior and use the complete list of extensions that are * available in this WebSocket server implementation.
  • *
* * @param extensions * the list of extensions to use. */ public void setExtensions(List extensions) { this.extensions.clear(); if (extensions != null) { this.extensions.addAll(extensions); } } public void setHeader(String name, String value) { List values = new ArrayList<>(); values.add(value); headers.put(name.toLowerCase(),values); } public void setStatusCode(int statusCode) { this.statusCode = statusCode; } public void setStatusReason(String statusReason) { this.statusReason = statusReason; } public void setSuccess(boolean success) { this.success = success; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy