Search in sources :

Example 6 with CookieHandler

use of java.net.CookieHandler in project robovm by robovm.

the class HttpEngine method prepareRawRequestHeaders.

/**
   * Populates requestHeaders with defaults and cookies.
   *
   * <p>This client doesn't specify a default {@code Accept} header because it
   * doesn't know what content types the application is interested in.
   */
private void prepareRawRequestHeaders() throws IOException {
    requestHeaders.getHeaders().setRequestLine(getRequestLine());
    if (requestHeaders.getUserAgent() == null) {
        requestHeaders.setUserAgent(getDefaultUserAgent());
    }
    if (requestHeaders.getHost() == null) {
        requestHeaders.setHost(getOriginAddress(policy.getURL()));
    }
    if ((connection == null || connection.getHttpMinorVersion() != 0) && requestHeaders.getConnection() == null) {
        requestHeaders.setConnection("Keep-Alive");
    }
    if (requestHeaders.getAcceptEncoding() == null) {
        transparentGzip = true;
        requestHeaders.setAcceptEncoding("gzip");
    }
    if (hasRequestBody() && requestHeaders.getContentType() == null) {
        requestHeaders.setContentType("application/x-www-form-urlencoded");
    }
    long ifModifiedSince = policy.getIfModifiedSince();
    if (ifModifiedSince != 0) {
        requestHeaders.setIfModifiedSince(new Date(ifModifiedSince));
    }
    CookieHandler cookieHandler = client.getCookieHandler();
    if (cookieHandler != null) {
        requestHeaders.addCookies(cookieHandler.get(uri, requestHeaders.getHeaders().toMultimap(false)));
    }
}
Also used : Date(java.util.Date) CookieHandler(java.net.CookieHandler)

Example 7 with CookieHandler

use of java.net.CookieHandler in project robovm by robovm.

the class OldCookieHandlerTest method test_get_put.

public void test_get_put() {
    MockCookieHandler mch = new MockCookieHandler();
    CookieHandler defaultHandler = CookieHandler.getDefault();
    CookieHandler.setDefault(mch);
    class TestThread extends Thread {

        public void run() {
            try {
                URL url = new URL(link);
                URLConnection conn = url.openConnection();
                conn.getContent();
                url = new URL(link);
                conn = url.openConnection();
                conn.getContent();
                completedSuccessfully = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    try {
        TestThread thread = new TestThread();
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            fail("InterruptedException was thrown.");
        }
        assertTrue(isGetCalled);
        assertTrue(isPutCalled);
        assertTrue(completedSuccessfully);
    } finally {
        CookieHandler.setDefault(defaultHandler);
    }
}
Also used : URL(java.net.URL) URLConnection(java.net.URLConnection) IOException(java.io.IOException) CookieHandler(java.net.CookieHandler)

Example 8 with CookieHandler

use of java.net.CookieHandler in project phonegap-facebook-plugin by Wizcorp.

the class HttpEngine method prepareRawRequestHeaders.

/**
   * Populates requestHeaders with defaults and cookies.
   *
   * <p>This client doesn't specify a default {@code Accept} header because it
   * doesn't know what content types the application is interested in.
   */
private void prepareRawRequestHeaders() throws IOException {
    requestHeaders.getHeaders().setRequestLine(getRequestLine());
    if (requestHeaders.getUserAgent() == null) {
        requestHeaders.setUserAgent(getDefaultUserAgent());
    }
    if (requestHeaders.getHost() == null) {
        requestHeaders.setHost(getOriginAddress(policy.getURL()));
    }
    if ((connection == null || connection.getHttpMinorVersion() != 0) && requestHeaders.getConnection() == null) {
        requestHeaders.setConnection("Keep-Alive");
    }
    if (requestHeaders.getAcceptEncoding() == null) {
        transparentGzip = true;
        requestHeaders.setAcceptEncoding("gzip");
    }
    if (hasRequestBody() && requestHeaders.getContentType() == null) {
        requestHeaders.setContentType("application/x-www-form-urlencoded");
    }
    long ifModifiedSince = policy.getIfModifiedSince();
    if (ifModifiedSince != 0) {
        requestHeaders.setIfModifiedSince(new Date(ifModifiedSince));
    }
    CookieHandler cookieHandler = policy.cookieHandler;
    if (cookieHandler != null) {
        requestHeaders.addCookies(cookieHandler.get(uri, requestHeaders.getHeaders().toMultimap(false)));
    }
}
Also used : Date(java.util.Date) CookieHandler(java.net.CookieHandler)

Example 9 with CookieHandler

use of java.net.CookieHandler in project LeafPic by HoraApps.

the class PlayerActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(org.horaapps.leafpic.R.layout.activity_player);
    FrameLayout root = (FrameLayout) findViewById(org.horaapps.leafpic.R.id.root);
    findViewById(R.id.video_frame).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            toggleControlsVisibility();
        }
    });
    root.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            return !(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_MENU) && mediaController.dispatchKeyEvent(event);
        }
    });
    root.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.md_black_1000));
    shutterView = findViewById(org.horaapps.leafpic.R.id.shutter);
    videoFrame = (AspectRatioFrameLayout) findViewById(org.horaapps.leafpic.R.id.video_frame);
    surfaceView = (SurfaceView) findViewById(org.horaapps.leafpic.R.id.surface_view);
    surfaceView.getHolder().addCallback(this);
    mediaController = new CustomMediaController(this, this);
    mediController_anchor = findViewById(org.horaapps.leafpic.R.id.media_player_anchor);
    mediaController.setAnchorView(root);
    //mediaController.setPaddingRelative(0,0,0,Measure.getNavBarHeight(PlayerActivity.this));
    toolbar = (Toolbar) findViewById(org.horaapps.leafpic.R.id.toolbar);
    initUI();
    CookieHandler currentHandler = CookieHandler.getDefault();
    if (currentHandler != defaultCookieManager)
        CookieHandler.setDefault(defaultCookieManager);
    audioCapabilitiesReceiver = new AudioCapabilitiesReceiver(this, this);
    audioCapabilitiesReceiver.register();
}
Also used : KeyEvent(android.view.KeyEvent) AudioCapabilitiesReceiver(com.google.android.exoplayer.audio.AudioCapabilitiesReceiver) FrameLayout(android.widget.FrameLayout) AspectRatioFrameLayout(com.google.android.exoplayer.AspectRatioFrameLayout) OnKeyListener(android.view.View.OnKeyListener) SurfaceView(android.view.SurfaceView) VideoControllerView(org.horaapps.leafpic.views.VideoControllerView) View(android.view.View) CookieHandler(java.net.CookieHandler)

Example 10 with CookieHandler

use of java.net.CookieHandler in project okhttp by square.

the class CookiesTest method cookieHandlerLikeAndroid.

@Test
public void cookieHandlerLikeAndroid() throws Exception {
    final MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse());
    server.start();
    final HttpUrl serverUrl = urlWithIpAddress(server, "/");
    CookieHandler androidCookieHandler = new CookieHandler() {

        @Override
        public Map<String, List<String>> get(URI uri, Map<String, List<String>> map) throws IOException {
            return Collections.singletonMap("Cookie", Collections.singletonList("$Version=\"1\"; " + "a=\"android\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\"; " + "b=\"banana\";$Path=\"/\";$Domain=\"" + serverUrl.host() + "\""));
        }

        @Override
        public void put(URI uri, Map<String, List<String>> map) throws IOException {
        }
    };
    client = client.newBuilder().cookieJar(new JavaNetCookieJar(androidCookieHandler)).build();
    get(serverUrl);
    RecordedRequest request = server.takeRequest();
    assertEquals("a=android; b=banana", request.getHeader("Cookie"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) List(java.util.List) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CookieHandler(java.net.CookieHandler) Test(org.junit.Test)

Aggregations

CookieHandler (java.net.CookieHandler)13 URI (java.net.URI)4 Date (java.util.Date)4 IOException (java.io.IOException)3 List (java.util.List)3 URISyntaxException (java.net.URISyntaxException)2 Map (java.util.Map)2 KeyEvent (android.view.KeyEvent)1 SurfaceView (android.view.SurfaceView)1 View (android.view.View)1 OnKeyListener (android.view.View.OnKeyListener)1 FrameLayout (android.widget.FrameLayout)1 AspectRatioFrameLayout (com.google.android.exoplayer.AspectRatioFrameLayout)1 AudioCapabilitiesReceiver (com.google.android.exoplayer.audio.AudioCapabilitiesReceiver)1 Request (com.squareup.okhttp.Request)1 HttpServer (com.sun.net.httpserver.HttpServer)1 InetSocketAddress (java.net.InetSocketAddress)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 ArrayList (java.util.ArrayList)1