Example usage for java.net CookieHandler setDefault

List of usage examples for java.net CookieHandler setDefault

Introduction

In this page you can find the example usage for java.net CookieHandler setDefault.

Prototype

public static synchronized void setDefault(CookieHandler cHandler) 

Source Link

Document

Sets (or unsets) the system-wide cookie handler.

Usage

From source file:com.codename1.impl.android.AndroidImplementation.java

@Override
public void init(Object m) {
    if (m instanceof CodenameOneActivity) {
        setContext(null);//ww  w  . j av a2s . c  o m
        setActivity((CodenameOneActivity) m);
    } else {
        setActivity(null);
        setContext((Context) m);
    }

    instance = this;
    if (getActivity() != null && getActivity().hasUI()) {
        if (!hasActionBar()) {
            try {
                getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);
            } catch (Exception e) {
                //Log.d("Codename One", "No idea why this throws a Runtime Error", e);
            }
        } else {
            getActivity().invalidateOptionsMenu();
            try {
                getActivity().requestWindowFeature(Window.FEATURE_ACTION_BAR);
                getActivity().requestWindowFeature(Window.FEATURE_PROGRESS);

                if (android.os.Build.VERSION.SDK_INT >= 21) {
                    //WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
                    getActivity().getWindow().addFlags(-2147483648);
                }
            } catch (Exception e) {
                //Log.d("Codename One", "No idea why this throws a Runtime Error", e);
            }
            NotifyActionBar notify = new NotifyActionBar(getActivity(), false);
            notify.run();
        }

        if (statusBarHidden) {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            getActivity().getWindow().setStatusBarColor(android.graphics.Color.TRANSPARENT);
        }

        if (Display.getInstance().getProperty("StatusbarHidden", "").equals("true")) {
            getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        if (Display.getInstance().getProperty("KeepScreenOn", "").equals("true")) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }

        if (Display.getInstance().getProperty("DisableScreenshots", "").equals("true")) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        }

        if (m instanceof CodenameOneActivity) {
            ((CodenameOneActivity) m).setDefaultIntentResultListener(this);
            ((CodenameOneActivity) m).setIntentResultListener(this);
        }

        /**
         * translate our default font height depending on the screen density.
         * this is required for new high resolution devices. otherwise
         * everything looks awfully small.
         *
         * we use our default font height value of 16 and go from there. i
         * thought about using new Paint().getTextSize() for this value but if
         * some new version of android suddenly returns values already tranlated
         * to the screen then we might end up with too large fonts. the
         * documentation is not very precise on that.
         */
        final int defaultFontPixelHeight = 16;
        this.defaultFontHeight = this.translatePixelForDPI(defaultFontPixelHeight);

        this.defaultFont = (CodenameOneTextPaint) ((NativeFont) this.createFont(Font.FACE_SYSTEM,
                Font.STYLE_PLAIN, Font.SIZE_MEDIUM)).font;
        Display.getInstance().setTransitionYield(-1);

        initSurface();
        /**
         * devices are extremely sensitive so dragging should start a little
         * later than suggested by default implementation.
         */
        this.setDragStartPercentage(1);
        VirtualKeyboardInterface vkb = new AndroidKeyboard(this);
        Display.getInstance().registerVirtualKeyboard(vkb);
        Display.getInstance().setDefaultVirtualKeyboard(vkb);

        InPlaceEditView.endEdit();

        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        if (nativePeers.size() > 0) {
            for (int i = 0; i < nativePeers.size(); i++) {
                ((AndroidImplementation.AndroidPeer) nativePeers.elementAt(i)).init();
            }
        }
    } else {
        /**
         * translate our default font height depending on the screen density.
         * this is required for new high resolution devices. otherwise
         * everything looks awfully small.
         *
         * we use our default font height value of 16 and go from there. i
         * thought about using new Paint().getTextSize() for this value but if
         * some new version of android suddenly returns values already tranlated
         * to the screen then we might end up with too large fonts. the
         * documentation is not very precise on that.
         */
        final int defaultFontPixelHeight = 16;
        this.defaultFontHeight = this.translatePixelForDPI(defaultFontPixelHeight);

        this.defaultFont = (CodenameOneTextPaint) ((NativeFont) this.createFont(Font.FACE_SYSTEM,
                Font.STYLE_PLAIN, Font.SIZE_MEDIUM)).font;
    }
    HttpURLConnection.setFollowRedirects(false);
    CookieHandler.setDefault(null);
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_Sessions01() {
    final AtomicBoolean valueBound = new AtomicBoolean(false);
    final AtomicBoolean valueUnbound = new AtomicBoolean(false);
    final HttpSessionBindingListener bindingListener = new HttpSessionBindingListener() {

        @Override//ww  w. j ava  2 s . c o m
        public void valueUnbound(HttpSessionBindingEvent event) {
            valueUnbound.set(true);
        }

        @Override
        public void valueBound(HttpSessionBindingEvent event) {
            valueBound.set(true);
        }
    };
    final AtomicBoolean sessionCreated = new AtomicBoolean(false);
    final AtomicBoolean sessionDestroyed = new AtomicBoolean(false);
    HttpSessionListener sessionListener = new HttpSessionListener() {

        @Override
        public void sessionDestroyed(HttpSessionEvent se) {
            sessionDestroyed.set(true);
        }

        @Override
        public void sessionCreated(HttpSessionEvent se) {
            sessionCreated.set(true);
        }
    };
    HttpServlet sessionServlet = new HttpServlet() {
        private static final long serialVersionUID = 1L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            HttpSession session = request.getSession();
            if (session.getAttribute("test.attribute") == null) {
                session.setAttribute("test.attribute", bindingListener);
                response.getWriter().print("created");
            } else {
                session.invalidate();
                response.getWriter().print("invalidated");
            }
        }

    };
    ServiceRegistration<Servlet> servletReg = null;
    ServiceRegistration<HttpSessionListener> sessionListenerReg = null;
    Dictionary<String, Object> servletProps = new Hashtable<String, Object>();
    servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/sessions");
    String actual = null;
    CookieHandler previous = CookieHandler.getDefault();
    CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
    try {
        servletReg = getBundleContext().registerService(Servlet.class, sessionServlet, servletProps);
        Dictionary<String, String> listenerProps = new Hashtable<String, String>();
        listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true");
        sessionListenerReg = getBundleContext().registerService(HttpSessionListener.class, sessionListener,
                listenerProps);

        sessionCreated.set(false);
        valueBound.set(false);
        sessionDestroyed.set(false);
        valueUnbound.set(false);
        // first call will create the session
        actual = requestAdvisor.request("sessions");
        assertEquals("Wrong result", "created", actual);
        assertTrue("No sessionCreated called", sessionCreated.get());
        assertTrue("No valueBound called", valueBound.get());
        assertFalse("sessionDestroyed was called", sessionDestroyed.get());
        assertFalse("valueUnbound was called", valueUnbound.get());

        sessionCreated.set(false);
        valueBound.set(false);
        sessionDestroyed.set(false);
        valueUnbound.set(false);
        // second call will invalidate the session
        actual = requestAdvisor.request("sessions");
        assertEquals("Wrong result", "invalidated", actual);
        assertFalse("sessionCreated was called", sessionCreated.get());
        assertFalse("valueBound was called", valueBound.get());
        assertTrue("No sessionDestroyed called", sessionDestroyed.get());
        assertTrue("No valueUnbound called", valueUnbound.get());

        sessionCreated.set(false);
        sessionDestroyed.set(false);
        valueBound.set(false);
        valueUnbound.set(false);
        // calling again should create the session again
        actual = requestAdvisor.request("sessions");
        assertEquals("Wrong result", "created", actual);
        assertTrue("No sessionCreated called", sessionCreated.get());
        assertTrue("No valueBound called", valueBound.get());
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    } finally {
        if (servletReg != null) {
            servletReg.unregister();
        }
        if (sessionListenerReg != null) {
            sessionListenerReg.unregister();
        }
        CookieHandler.setDefault(previous);
    }
}

From source file:com.codename1.impl.android.AndroidImplementation.java

public Object connect(String url, boolean read, boolean write, int timeout) throws IOException {
    URL u = new URL(url);
    CookieHandler.setDefault(null);
    URLConnection con = u.openConnection();
    if (con instanceof HttpURLConnection) {
        HttpURLConnection c = (HttpURLConnection) con;
        c.setUseCaches(false);/*ww  w . j  a  v a  2s . c  o  m*/
        c.setDefaultUseCaches(false);
        c.setInstanceFollowRedirects(false);
        if (timeout > -1) {
            c.setConnectTimeout(timeout);
        }
        if (read) {
            if (timeout > -1) {
                c.setReadTimeout(timeout);
            } else {
                c.setReadTimeout(10000);
            }
        }
        if (android.os.Build.VERSION.SDK_INT > 13) {
            c.setRequestProperty("Connection", "close");
        }
    }
    con.setDoInput(read);
    con.setDoOutput(write);
    return con;
}