Example usage for java.net CookiePolicy ACCEPT_ALL

List of usage examples for java.net CookiePolicy ACCEPT_ALL

Introduction

In this page you can find the example usage for java.net CookiePolicy ACCEPT_ALL.

Prototype

CookiePolicy ACCEPT_ALL

To view the source code for java.net CookiePolicy ACCEPT_ALL.

Click Source Link

Document

One pre-defined policy which accepts all cookies.

Usage

From source file:com.hygenics.parser.GetImages.java

private void getImages() {
    // controls the web process from a removed method
    log.info("Setting Up Pull");
    String[] proxyarr = (proxies == null) ? null : proxies.split(",");
    // cleanup/*ww  w.ja  va 2  s . c  o m*/
    if (cleanup) {
        cleanupDir(fpath);
    }

    // image grab
    CookieManager cm = new CookieManager();
    cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);
    int numimages = 0;
    InputStream is;
    byte[] bytes;
    int iter = 0;
    int found = 0;

    // set proxy if needed
    if (proxyuser != null) {
        proxy(proxyhost, proxyport, https, proxyuser, proxypass);
    }

    int i = 0;
    ArrayList<String> postImages = new ArrayList<String>();
    ForkJoinPool fjp = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
    Set<Callable<String>> pulls = new HashSet<Callable<String>>();
    Set<Callable<ArrayList<String>>> sqls = new HashSet<Callable<ArrayList<String>>>();
    List<Future<String>> imageFutures;

    ArrayList<String> images;
    int chunksize = (int) Math.ceil(commitsize / numqueries);
    log.info("Chunksize: " + chunksize);
    if (baseurl != null || baseurlcolumn != null) {
        do {
            log.info("Offset: " + offset);
            log.info("Getting Images");
            images = new ArrayList<String>(commitsize);
            log.info("Getting Columns");
            for (int n = 0; n < numqueries; n++) {
                String tempsql = sql + " WHERE " + idString + " >= " + offset + " AND " + idString + " < "
                        + (offset + chunksize);

                if (conditions != null) {
                    tempsql += conditions;
                }

                sqls.add(new QueryDatabase(
                        ((extracondition != null) ? tempsql + " " + extracondition : tempsql)));

                offset += chunksize;
            }

            List<Future<ArrayList<String>>> futures = fjp.invokeAll(sqls);

            int w = 0;
            while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) {
                w++;
            }

            for (Future<ArrayList<String>> f : futures) {
                try {
                    ArrayList<String> fjson;
                    fjson = f.get();
                    if (fjson.size() > 0) {
                        images.addAll(fjson);
                    }

                    if (f.isDone() == false) {
                        f.cancel(true);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
            log.info(Integer.toString(images.size()) + " image links found. Pulling.");

            ArrayList<String> tempproxies = new ArrayList<String>();

            if (proxyarr != null) {
                for (String proxy : proxyarr) {
                    tempproxies.add(proxy.trim());
                }
            }

            if (maxproxies > 0) {
                maxproxies -= 1;// 0 and 1 should be equivalent conditions
                // --num is not like most 0 based still due
                // to >=
            }

            // get images
            for (int num = 0; num < images.size(); num++) {
                String icols = images.get(num);
                int proxnum = (int) Math.random() * (tempproxies.size() - 1);
                String proxy = (tempproxies.size() == 0) ? null : tempproxies.get(proxnum);

                // add grab
                pulls.add(new ImageGrabber(icols, proxy));

                if (proxy != null) {
                    tempproxies.remove(proxy);
                }

                // check for execution
                if (num + 1 == images.size() || pulls.size() >= commitsize || tempproxies.size() == 0) {
                    if (tempproxies.size() == 0 && proxies != null) {
                        tempproxies = new ArrayList<String>(proxyarr.length);

                        for (String p : proxyarr) {
                            tempproxies.add(p.trim());
                        }
                    }

                    imageFutures = fjp.invokeAll(pulls);
                    w = 0;

                    while (fjp.isQuiescent() == false && fjp.getActiveThreadCount() > 0) {
                        w++;
                    }

                    for (Future<String> f : imageFutures) {
                        String add;
                        try {
                            add = f.get();

                            if (add != null) {
                                postImages.add(add);
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        }
                    }
                    imageFutures = null;// garbage collect elligible
                    pulls = new HashSet<Callable<String>>(commitsize);
                }

                if (postImages.size() >= commitsize && addtoDB == true) {
                    if (addtoDB) {
                        log.info("Posting to Database");
                        log.info("Found " + postImages.size() + " images");
                        numimages += postImages.size();
                        int size = (int) Math.floor(postImages.size() / numqueries);
                        for (int n = 0; n < numqueries; n++) {
                            if (((n + 1) * size) < postImages.size() && (n + 1) < numqueries) {
                                fjp.execute(new ImagePost(postImages.subList(n * size, (n + 1) * size)));
                            } else {
                                fjp.execute(new ImagePost(postImages.subList(n * size, postImages.size() - 1)));
                            }
                        }

                        w = 0;
                        while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) {
                            w++;
                        }
                    }
                    found += postImages.size();
                    postImages.clear();
                }

            }

            if (postImages.size() > 0 && addtoDB == true) {
                log.info("Posting to Database");
                numimages += postImages.size();
                int size = (int) Math.floor(postImages.size() / numqueries);
                for (int n = 0; n < numqueries; n++) {
                    if (((n + 1) * size) < postImages.size()) {
                        fjp.execute(new ImagePost(postImages.subList(n * size, (n + 1) * size)));
                    } else {
                        fjp.execute(new ImagePost(postImages.subList(n * size, postImages.size())));
                    }
                }

                w = 0;
                while (fjp.isQuiescent() && fjp.getActiveThreadCount() > 0) {
                    w++;
                }

                found += postImages.size();
                postImages.clear();
            }

            // handle iterations specs
            iter += 1;
            log.info("Iteration: " + iter);
            if ((iter < iterations && found < images.size()) || tillfound == true) {
                log.info("Not All Images Obtained Trying Iteration " + iter + " of " + iterations);
                offset -= commitsize;
            } else if ((iter < iterations && found >= images.size()) && tillfound == false) {
                log.info("Images Obtained in " + iter + " iterations. Continuing.");
                iter = 0;
            } else {
                // precautionary
                log.info("Images Obtained in " + iter + " iterations. Continuing");
                iter = 0;
            }

        } while (images.size() > 0 && iter < iterations);

        if (fjp.isShutdown()) {
            fjp.shutdownNow();
        }
    }

    log.info("Complete. Check for Errors \n " + numimages + " Images Found");
}

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//from ww w . j  av a 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);
    }
}