Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Migrating 9 to 10: what replaces WebSocketServlet and JSON.parse() ?

Alexander,

You should only need to change your line with `JSON.toString(root)` to `new JSON().toJSON(root)`.

As for the deprecated header() method, it says in the javadoc to use the `headers(Consumer<HttpFields.Mutable>)` method instead.
So would look something like this:

HttpClient httpClient = new HttpClient();
httpClient.POST(uri)
    .headers(httpFields ->
    {
        httpFields.add(new HttpField(HttpHeader.ACCEPT, APPLICATION_JSON));
        httpFields.add(new HttpField(HttpHeader.CONTENT_TYPE, APPLICATION_URLENCODED));
    })
    .body(new StringRequestContent("content"))
    .send().getContentAsString();

Cheers,
Lachlan



On Wed, Sep 1, 2021 at 11:43 PM Alexander Farber <alexander.farber@xxxxxxxxx> wrote:
Hi Joakim and all -

On Tue, Aug 31, 2021 at 6:19 PM Joakim Erdfelt <joakim@xxxxxxxxxxx> wrote:
With that said, you appear to be using the Jetty implementation/apis.
So, switch to `org.eclipse.jetty.websocket.server.JettyWebSocketServlet` instead.
 
JSON json = new JSON();
// configure json here
Map<String, String> myMap = (Map<String, String>) json.fromJSON(str);

thank you for the helpful tips on JSON parsing.

Could you please help me with creating JSON? 

In Jetty 9.4.x I had this method:

    // this string is POSTed to Google FCM server
    public String toFcmBody(String boardUrl, String gameNumber) {
        Map<String, Object> root  = new HashMap<>();
        Map<String, Object> notif = new HashMap<>();
        Map<String, Object> data  = new HashMap<>();
        root.put(KEY_TO, mFcm);
        root.put(KEY_NOTIFICATION, notif);
        root.put(KEY_DATA, data);
        notif.put(KEY_TITLE, gameNumber + " " + mGid);
        notif.put(KEY_BODY, mBody);
        notif.put(KEY_IMAGE, boardUrl + mGid);
        data.put(KEY_GID, mGid);
        return JSON.toString(root);
    }

So now I am trying the following, but what to use as the first param to appendMap()?

    // this string is POSTed to Google FCM server
    public String toFcmBody(String boardUrl, String gameNumber) {
        Map<String, Object> root  = new HashMap<>();
        Map<String, Object> notif = new HashMap<>();
        Map<String, Object> data  = new HashMap<>();
        root.put(KEY_TO, mFcm);
        root.put(KEY_NOTIFICATION, notif);
        root.put(KEY_DATA, data);
        notif.put(KEY_TITLE, gameNumber + " " + mGid);
        notif.put(KEY_BODY, mBody);
        notif.put(KEY_IMAGE, boardUrl + mGid);
        data.put(KEY_GID, mGid);
        // new code
        JSON json = new JSON();
        json.appendMap(???, root);
        return json.toString();
    }

And also Jetty 10 is not happy with the deprecated header() method here:

            MultiMap<String> postParams = new MultiMap<>();
            postParams.put("code",          code);
            postParams.put("client_id",     mBundle.getString(STR_GOOGLE_ID));
            postParams.put("client_secret", mBundle.getString(STR_GOOGLE_SECRET));
            postParams.put("redirect_uri",  String.format(GOOGLE_REDIRECT_URI, mLanguage));
            postParams.put("grant_type",    "authorization_code");
            
            String tokenStr = mHttpClient.POST(GOOGLE_TOKEN_URL)
                .header(HttpHeader.ACCEPT, APPLICATION_JSON)                       // what to use here please?
                .header(HttpHeader.CONTENT_TYPE, APPLICATION_URLENCODED)
                .content(new StringContentProvider(UrlEncoded.encode(postParams, StandardCharsets.UTF_8, false)))
                .send().getContentAsString();
                Map<String, String> tokenMap = (Map<String, String>) mJson.fromJSON(tokenStr);

Best regards
Alex
_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users

Back to the top