Search in sources :

Example 1 with Cookie

use of org.apache.apex.shaded.ning19.com.ning.http.client.cookie.Cookie in project apex-core by apache.

the class PubSubWebSocketClient method openConnectionAsync.

public void openConnectionAsync() throws IOException {
    throwable.set(null);
    if (loginUrl != null && userName != null && password != null) {
        // get the session key first before attempting web socket
        JSONObject json = new JSONObject();
        try {
            json.put("userName", userName);
            json.put("password", password);
        } catch (JSONException ex) {
            throw new RuntimeException(ex);
        }
        client.preparePost(loginUrl).setHeader("Content-Type", "application/json").setBody(json.toString()).execute(new AsyncCompletionHandler<Response>() {

            @Override
            public Response onCompleted(Response response) throws Exception {
                List<Cookie> cookies = response.getCookies();
                BoundRequestBuilder brb = client.prepareGet(uri.toString());
                if (cookies != null) {
                    for (Cookie cookie : cookies) {
                        brb.addCookie(cookie);
                    }
                }
                connection = brb.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new PubSubWebSocket()).build()).get();
                return response;
            }
        });
    } else {
        final PubSubWebSocket webSocket = new PubSubWebSocket() {

            @Override
            public void onOpen(WebSocket ws) {
                connection = ws;
                super.onOpen(ws);
            }
        };
        client.prepareGet(uri.toString()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(webSocket).build());
    }
}
Also used : Cookie(org.apache.apex.shaded.ning19.com.ning.http.client.cookie.Cookie) BoundRequestBuilder(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) JSONException(org.codehaus.jettison.json.JSONException) WebSocketUpgradeHandler(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketUpgradeHandler) TimeoutException(java.util.concurrent.TimeoutException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JsonParseException(org.codehaus.jackson.JsonParseException) JSONException(org.codehaus.jettison.json.JSONException) WebSocket(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocket) Response(org.apache.apex.shaded.ning19.com.ning.http.client.Response) BoundRequestBuilder(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) JSONObject(org.codehaus.jettison.json.JSONObject) List(java.util.List)

Example 2 with Cookie

use of org.apache.apex.shaded.ning19.com.ning.http.client.cookie.Cookie in project apex-core by apache.

the class PubSubWebSocketClient method openConnection.

/**
   * <p>openConnection.</p>
   *
   * @param timeoutMillis
   * @throws IOException
   * @throws ExecutionException
   * @throws InterruptedException
   * @throws TimeoutException
   */
public void openConnection(long timeoutMillis) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    throwable.set(null);
    List<Cookie> cookies = null;
    if (loginUrl != null && userName != null && password != null) {
        // get the session key first before attempting web socket
        JSONObject json = new JSONObject();
        try {
            json.put("userName", userName);
            json.put("password", password);
        } catch (JSONException ex) {
            throw new RuntimeException(ex);
        }
        Response response = client.preparePost(loginUrl).setHeader("Content-Type", "application/json").setBody(json.toString()).execute().get();
        cookies = response.getCookies();
    }
    BoundRequestBuilder brb = client.prepareGet(uri.toString());
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            brb.addCookie(cookie);
        }
    }
    connection = brb.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new PubSubWebSocket()).build()).get(timeoutMillis, TimeUnit.MILLISECONDS);
}
Also used : Cookie(org.apache.apex.shaded.ning19.com.ning.http.client.cookie.Cookie) Response(org.apache.apex.shaded.ning19.com.ning.http.client.Response) BoundRequestBuilder(org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient.BoundRequestBuilder) JSONObject(org.codehaus.jettison.json.JSONObject) JSONException(org.codehaus.jettison.json.JSONException) WebSocketUpgradeHandler(org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketUpgradeHandler)

Example 3 with Cookie

use of org.apache.apex.shaded.ning19.com.ning.http.client.cookie.Cookie in project pinpoint by naver.

the class ExecuteRequestInterceptor method recordCookie.

protected void recordCookie(com.ning.http.client.Request httpRequest, SpanEventRecorder recorder) {
    if (cookieSampler.isSampling()) {
        Collection<Cookie> cookies = httpRequest.getCookies();
        if (cookies.isEmpty()) {
            return;
        }
        StringBuilder sb = new StringBuilder(config.getCookieDumpSize() * 2);
        Iterator<Cookie> iterator = cookies.iterator();
        while (iterator.hasNext()) {
            Cookie cookie = iterator.next();
            sb.append(cookie.getName()).append("=").append(cookie.getValue());
            if (iterator.hasNext()) {
                sb.append(",");
            }
        }
        recorder.recordAttribute(AnnotationKey.HTTP_COOKIE, StringUtils.abbreviate(sb.toString(), config.getCookieDumpSize()));
    }
}
Also used : Cookie(com.ning.http.client.cookie.Cookie)

Aggregations

BoundRequestBuilder (org.apache.apex.shaded.ning19.com.ning.http.client.AsyncHttpClient.BoundRequestBuilder)2 Response (org.apache.apex.shaded.ning19.com.ning.http.client.Response)2 Cookie (org.apache.apex.shaded.ning19.com.ning.http.client.cookie.Cookie)2 WebSocketUpgradeHandler (org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocketUpgradeHandler)2 JSONException (org.codehaus.jettison.json.JSONException)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 Cookie (com.ning.http.client.cookie.Cookie)1 IOException (java.io.IOException)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 WebSocket (org.apache.apex.shaded.ning19.com.ning.http.client.ws.WebSocket)1 JsonParseException (org.codehaus.jackson.JsonParseException)1 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)1