Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] http classes not accessible

Nice, thanks.


On Mon, Dec 30, 2013 at 1:09 AM, Joakim Erdfelt <joakim@xxxxxxxxxxx> wrote:
https://bugs.eclipse.org/424734 - WebSocket / Expose Locale information from ServletUpgradeRequest


Also added: https://bugs.eclipse.org/424735 - WebSocket / Make ServletUpgradeRequest implement HttpServletRequest
Will work on that tomorrow.


--
Joakim Erdfelt <joakim@xxxxxxxxxxx>
Expert advice, services and support from from the Jetty & CometD experts


On Sun, Dec 29, 2013 at 9:14 AM, Nils Kilden-Pedersen <nilskp@xxxxxxxxx> wrote:
On Sun, Dec 29, 2013 at 12:47 PM, Nils Kilden-Pedersen <nilskp@xxxxxxxxx> wrote:
On Sun, Dec 29, 2013 at 12:54 AM, Joakim Erdfelt <joakim@xxxxxxxxxxx> wrote:
We fixed some of this with Jetty 9.1.x.

There is now a ServletUpgradeRequest that is based on HttpServletRequest.

Ok, this is great. I'll be upgrading to 9.1 soon anyway.

Just tried this. I guess I interpreted "based on" as "extends". I now see that it simply wraps an HttpServletRequest, but doesn't expose it. 

Any reason ServletUpgradeRequest doesn't either expose the underlying HttpServletRequest or is itself an HttpServletRequest? 

Or at least add the getLocales() method.

 
 

However, its not accessible from within the Session.getUpgradeRequest().
Know that UpgradeRequest is the exposed API, unwrapping and digging into the jetty specifics is not a long term, future-proof approach.

You could do something like this (notice that this only uses java classes + websocket api classes + junit)

package jetty.websocket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jetty.websocket.api.util.QuoteUtil;
import org.junit.Test;

public class AcceptLanguagesTest
{
    public static class QualityValue implements Comparable<QualityValue>
    {
        public String value;
        public double quality;

        public QualityValue(String value, double quality)
        {
            this.value = value;
            this.quality = quality;
        }

        @Override
        public int compareTo(QualityValue o)
        {
            return (int)((o.quality * 100) - (this.quality * 100));
        }

        @Override
        public String toString()
        {
            return String.format("%s [quality=%.1f]",value,quality);
        }
    }

    @Test
    public void testAcceptLanguages()
    {
        String acceptLangs = "tr-tr,tr;q=0.8,en-us;q=0.5,en;q=0.3";
        System.out.printf("--] %s%n", acceptLangs);
        List<QualityValue> values = parseQualityValues(acceptLangs);
        for (QualityValue value : values)
        {
            System.out.println(value);
        }
    }

    @Test
    public void testAcceptLanguagesOdd()
    {
        String acceptLangs = "tr-tr,tr;q=0.2,en-us;q=0.9,en;q=0.3";
        System.out.printf("--] %s%n", acceptLangs);
        List<QualityValue> values = parseQualityValues(acceptLangs);
        for (QualityValue value : values)
        {
            System.out.println(value);
        }
    }

    private List<QualityValue> parseQualityValues(String raw)
    {
        List<QualityValue> ret = new ArrayList<>();
        Iterator<String> parts = QuoteUtil.splitAt(raw,",");
        while (parts.hasNext())
        {
            String part = parts.next();
            String value = null;
            double quality = 1.0;
            Iterator<String> qs = QuoteUtil.splitAt(part,";");
            while (qs.hasNext())
            {
                String q = qs.next();
                if (q.startsWith("q"))
                {
                    int idx = q.indexOf('=');
                    if (idx > 0)
                    {
                        quality = Double.parseDouble(q.substring(idx + 1));
                    }
                }
                else
                {
                    value = q;
                }
            }
            if (value == null)
            {
                value = part;
            }
            ret.add(new QualityValue(value,quality));
        }
        Collections.sort(ret);
        return ret;
    }
}

Output from this testcase ...

--] tr-tr,tr;q=0.8,en-us;q=0.5,en;q=0.3
tr-tr [quality=1.0]
tr [quality=0.8]
en-us [quality=0.5]
en [quality=0.3]
--] tr-tr,tr;q=0.2,en-us;q=0.9,en;q=0.3
tr-tr [quality=1.0]
en-us [quality=0.9]
en [quality=0.3]
tr [quality=0.2]


Now, that being said, exposing either a ..

      List<String> UpgradeRequest.getHeaderValues(String name)
or   List<WeightedValue> UpgradeRequest.getWeightedHeaderValues(String name)
or   List<String> UpgradeRequest.getUserLocales()
or   List<WeightedValue> QuoteUtil.parseWeightedValues(String headervalue)

sounds like a reasonable feature / enhancement request.


--
Joakim Erdfelt <joakim@xxxxxxxxxxx>
Expert advice, services and support from from the Jetty & CometD experts


On Sat, Dec 28, 2013 at 12:57 PM, Nils Kilden-Pedersen <nilskp@xxxxxxxxx> wrote:
I wanted to reuse the methods for extracting weighted header values. More specifically, I'm trying to get the user locale(s) from the UpgradeRequest on websocket creation.


On Sat, Dec 28, 2013 at 3:22 PM, Joakim Erdfelt <joakim@xxxxxxxxxxx> wrote:
the org.eclipse.jetty.http.HttpHeader is not exposed by Jetty's WebSocketServlet, what are you trying to do?

--
Joakim Erdfelt <joakim@xxxxxxxxxxx>
Expert advice, services and support from from the Jetty & CometD experts


On Sat, Dec 28, 2013 at 5:20 AM, Nils Kilden-Pedersen <nilskp@xxxxxxxxx> wrote:

Using 9.0.6, I’m referencing HttpHeader in my web socket servlet, but apparently the http classes are not accessible to the servlet.

java.lang.ClassNotFoundException: org.eclipse.jetty.http.HttpHeader

How do I fix this?


_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users



_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users



_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users



_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users




_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users



_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/jetty-users



Back to the top