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:no.eris.applet.AppletViewer.java

private void overrideCookieHandler(CookieManager manager) {
    manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    final CookieHandler handler = CookieHandler.getDefault();

    LOGGER.debug("CookieStore: size {}", manager.getCookieStore().getCookies().size());
    if (cookies != null) {
        for (UriAndCookie uriAndCookie : cookies) {
            URI uri = uriAndCookie.getUri();
            HttpCookie cookie = uriAndCookie.getCookie();
            LOGGER.debug("Adding cookies: <{}> value={} secure={}",
                    new Object[] { uri, cookie, cookie.getSecure() });
            manager.getCookieStore().add(uri, cookie);
        }//  w  w w. ja v a  2  s.  c  o m
    }
    LOGGER.debug("CookieStore: size {}", manager.getCookieStore().getCookies().size());
    LOGGER.debug("Overriding cookie handler: {}", (handler == null ? null : handler.getClass().getName()));
    // FIXME because we depend on the system-wide cookie manager, we probably cannot run multiple applets at the time
    // we also maybe have some security issues lurking here...
    // I could maybe partition the callers based on the ThreadGroup ?? 
    // FIXME theres also some cleanup to do somewhere
    CookieHandler.setDefault(new LoggingCookieHandler(manager));
}

From source file:io.kodokojo.brick.gitlab.GitlabConfigurer.java

public static OkHttpClient provideDefaultOkHttpClient() {
    OkHttpClient httpClient = new OkHttpClient();
    final TrustManager[] certs = new TrustManager[] { new X509TrustManager() {

        @Override// w  ww  .  jav  a 2 s  . c  om
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkServerTrusted(final X509Certificate[] chain, final String authType)
                throws CertificateException {
        }

        @Override
        public void checkClientTrusted(final X509Certificate[] chain, final String authType)
                throws CertificateException {
        }
    } };

    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
    } catch (final java.security.GeneralSecurityException ex) {
        //
    }
    httpClient.setHostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }
    });
    httpClient.setSslSocketFactory(ctx.getSocketFactory());
    CookieManager cookieManager = new CookieManager(new GitlabCookieStore(), CookiePolicy.ACCEPT_ALL);
    httpClient.setCookieHandler(cookieManager);
    httpClient.setReadTimeout(2, TimeUnit.MINUTES);
    httpClient.setConnectTimeout(1, TimeUnit.MINUTES);
    httpClient.setWriteTimeout(1, TimeUnit.MINUTES);
    return httpClient;
}

From source file:test.be.fedict.eid.applet.ControllerTest.java

@Test
public void controllerIdentification() throws Exception {
    // setup/*from  ww w  .  ja  v  a2 s  .  c o m*/
    Messages messages = new Messages(Locale.getDefault());
    Runtime runtime = new TestRuntime();
    View view = new TestView();
    Controller controller = new Controller(view, runtime, messages);

    // make sure that the session cookies are passed during conversations
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);

    // operate
    controller.run();

    // verify
    LOG.debug("verify...");
    SessionHandler sessionHandler = this.servletTester.getContext().getSessionHandler();
    SessionManager sessionManager = sessionHandler.getSessionManager();
    LOG.debug("session manager type: " + sessionManager.getClass().getName());
    HashSessionManager hashSessionManager = (HashSessionManager) sessionManager;
    LOG.debug("# sessions: " + hashSessionManager.getSessions());
    assertEquals(1, hashSessionManager.getSessions());
    Map<String, HttpSession> sessionMap = hashSessionManager.getSessionMap();
    LOG.debug("session map: " + sessionMap);
    Entry<String, HttpSession> sessionEntry = sessionMap.entrySet().iterator().next();
    HttpSession httpSession = sessionEntry.getValue();
    assertNotNull(httpSession.getAttribute("eid"));
    Identity identity = (Identity) httpSession.getAttribute("eid.identity");
    assertNotNull(identity);
    assertNotNull(identity.name);
    LOG.debug("name: " + identity.name);
    LOG.debug("document type: " + identity.getDocumentType());
    LOG.debug("duplicate: " + identity.getDuplicate());
    assertNull(httpSession.getAttribute("eid.identifier"));
    assertNull(httpSession.getAttribute("eid.address"));
    assertNull(httpSession.getAttribute("eid.photo"));
}

From source file:org.sakaiproject.commons.tool.entityprovider.CommonsEntityProvider.java

@EntityCustomAction(action = "getUrlMarkup", viewKey = EntityView.VIEW_LIST)
public ActionReturn getUrlMarkup(OutputStream outputStream, EntityView view, Map<String, Object> params) {

    String userId = getCheckedUser();

    String urlString = (String) params.get("url");

    if (StringUtils.isBlank(urlString)) {
        throw new EntityException("No url supplied", "", HttpServletResponse.SC_BAD_REQUEST);
    }/* ww  w  .  ja v a2 s . co m*/

    try {
        CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
        URL url = new URL(urlString);
        URLConnection c = url.openConnection();

        if (c instanceof HttpURLConnection) {
            HttpURLConnection conn = (HttpURLConnection) c;
            conn.setRequestProperty("User-Agent", USER_AGENT);
            conn.setInstanceFollowRedirects(false);
            conn.connect();
            String contentEncoding = conn.getContentEncoding();
            String contentType = conn.getContentType();
            int responseCode = conn.getResponseCode();
            log.debug("Response code: {}", responseCode);

            int redirectCounter = 1;
            while ((responseCode == HttpURLConnection.HTTP_MOVED_PERM
                    || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
                    || responseCode == HttpURLConnection.HTTP_SEE_OTHER) && redirectCounter < 20) {
                String newUri = conn.getHeaderField("Location");
                log.debug("{}. New URI: {}", responseCode, newUri);
                String cookies = conn.getHeaderField("Set-Cookie");
                url = new URL(newUri);
                c = url.openConnection();
                conn = (HttpURLConnection) c;
                conn.setInstanceFollowRedirects(false);
                conn.setRequestProperty("User-Agent", USER_AGENT);
                conn.setRequestProperty("Cookie", cookies);
                conn.connect();
                contentEncoding = conn.getContentEncoding();
                contentType = conn.getContentType();
                responseCode = conn.getResponseCode();
                log.debug("Redirect counter: {}", redirectCounter);
                log.debug("Response code: {}", responseCode);
                redirectCounter += 1;
            }

            if (contentType != null
                    && (contentType.startsWith("text/html") || contentType.startsWith("application/xhtml+xml")
                            || contentType.startsWith("application/xml"))) {
                String mimeType = contentType.split(";")[0].trim();
                log.debug("mimeType: {}", mimeType);
                log.debug("encoding: {}", contentEncoding);

                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));

                String line = null;
                while ((line = reader.readLine()) != null) {
                    writer.write(line);
                }

                return new ActionReturn(contentEncoding, mimeType, outputStream);
            } else {
                log.debug("Invalid content type {}. Throwing bad request ...", contentType);
                throw new EntityException("Url content type not supported", "",
                        HttpServletResponse.SC_BAD_REQUEST);
            }
        } else {
            throw new EntityException("Url content type not supported", "", HttpServletResponse.SC_BAD_REQUEST);
        }
    } catch (MalformedURLException mue) {
        throw new EntityException("Invalid url supplied", "", HttpServletResponse.SC_BAD_REQUEST);
    } catch (IOException ioe) {
        throw new EntityException("Failed to download url contents", "",
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:test.be.fedict.eid.applet.ControllerTest.java

@Test
public void controllerIdentificationWithAddressAndPhoto() throws Exception {
    // setup//from w  ww  .j  a  va  2  s.  c o  m
    Messages messages = new Messages(Locale.getDefault());
    Runtime runtime = new TestRuntime();
    View view = new TestView();
    Controller controller = new Controller(view, runtime, messages);

    // make sure that the session cookies are passed during conversations
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);

    this.servletHolder.setInitParameter("IncludeAddress", "true");
    this.servletHolder.setInitParameter("IncludePhoto", "true");

    // operate
    controller.run();

    // verify
    LOG.debug("verify...");
    SessionHandler sessionHandler = this.servletTester.getContext().getSessionHandler();
    SessionManager sessionManager = sessionHandler.getSessionManager();
    LOG.debug("session manager type: " + sessionManager.getClass().getName());
    HashSessionManager hashSessionManager = (HashSessionManager) sessionManager;
    LOG.debug("# sessions: " + hashSessionManager.getSessions());
    assertEquals(1, hashSessionManager.getSessions());
    Map<String, HttpSession> sessionMap = hashSessionManager.getSessionMap();
    LOG.debug("session map: " + sessionMap);
    Entry<String, HttpSession> sessionEntry = sessionMap.entrySet().iterator().next();
    HttpSession httpSession = sessionEntry.getValue();
    assertNotNull(httpSession.getAttribute("eid"));
    Identity identity = (Identity) httpSession.getAttribute("eid.identity");
    assertNotNull(identity);
    assertNotNull(identity.name);
    LOG.debug("name: " + identity.name);
    LOG.debug("nationality: " + identity.getNationality());
    LOG.debug("national number: " + identity.getNationalNumber());
    assertNull(httpSession.getAttribute("eid.identifier"));
    assertNotNull(httpSession.getAttribute("eid.address"));
    assertNotNull(httpSession.getAttribute("eid.photo"));
}

From source file:test.be.fedict.eid.applet.ControllerTest.java

@Test
public void controllerKioskMode() throws Exception {
    // setup//from  www  .  jav  a 2s.c o  m
    Messages messages = new Messages(Locale.getDefault());
    Runtime runtime = new TestRuntime();
    View view = new TestView();
    Controller controller = new Controller(view, runtime, messages);

    // make sure that the session cookies are passed during conversations
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);

    this.servletHolder.setInitParameter("Kiosk", "true");

    // operate
    controller.run();

    // verify
    LOG.debug("verify...");
}

From source file:test.be.fedict.eid.applet.ControllerTest.java

@Test
public void controllerAuthentication() throws Exception {
    // setup/*from  w  ww  .  jav  a 2s  .c o m*/
    Messages messages = new Messages(Locale.getDefault());
    Runtime runtime = new TestRuntime();
    View view = new TestView();
    Controller controller = new Controller(view, runtime, messages);

    // make sure that the session cookies are passed during conversations
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);

    this.servletHolder.setInitParameter("AuthenticationServiceClass",
            TestAuthenticationService.class.getName());
    this.servletHolder.setInitParameter("Logoff", "true");

    // operate
    controller.run();

    // verify
    LOG.debug("verify...");
    SessionHandler sessionHandler = this.servletTester.getContext().getSessionHandler();
    SessionManager sessionManager = sessionHandler.getSessionManager();
    LOG.debug("session manager type: " + sessionManager.getClass().getName());
    HashSessionManager hashSessionManager = (HashSessionManager) sessionManager;
    LOG.debug("# sessions: " + hashSessionManager.getSessions());
    assertEquals(1, hashSessionManager.getSessions());
    Map<String, HttpSession> sessionMap = hashSessionManager.getSessionMap();
    LOG.debug("session map: " + sessionMap);
    Entry<String, HttpSession> sessionEntry = sessionMap.entrySet().iterator().next();
    HttpSession httpSession = sessionEntry.getValue();
    assertNotNull(httpSession.getAttribute("eid"));
    assertNull(httpSession.getAttribute("eid.identity"));
    assertNull(httpSession.getAttribute("eid.address"));
    assertNull(httpSession.getAttribute("eid.photo"));
    String identifier = (String) httpSession.getAttribute("eid.identifier");
    assertNotNull(identifier);
    LOG.debug("identifier: " + identifier);
    assertTrue(TestAuthenticationService.called);
}

From source file:test.be.fedict.eid.applet.ControllerTest.java

@Test
public void testAuthnSessionIdChannelBinding() throws Exception {
    // setup// ww  w.  ja v a  2 s.  c  o  m
    Messages messages = new Messages(Locale.getDefault());
    Runtime runtime = new TestRuntime();
    View view = new TestView();
    Controller controller = new Controller(view, runtime, messages);

    // make sure that the session cookies are passed during conversations
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);

    this.servletHolder.setInitParameter("AuthenticationServiceClass",
            TestAuthenticationService.class.getName());
    this.servletHolder.setInitParameter("Logoff", "true");
    this.servletHolder.setInitParameter("SessionIdChannelBinding", "true");

    // operate
    controller.run();

    // verify
    LOG.debug("verify...");
    SessionHandler sessionHandler = this.servletTester.getContext().getSessionHandler();
    SessionManager sessionManager = sessionHandler.getSessionManager();
    LOG.debug("session manager type: " + sessionManager.getClass().getName());
    HashSessionManager hashSessionManager = (HashSessionManager) sessionManager;
    LOG.debug("# sessions: " + hashSessionManager.getSessions());
    assertEquals(1, hashSessionManager.getSessions());
    Map<String, HttpSession> sessionMap = hashSessionManager.getSessionMap();
    LOG.debug("session map: " + sessionMap);
    Entry<String, HttpSession> sessionEntry = sessionMap.entrySet().iterator().next();
    HttpSession httpSession = sessionEntry.getValue();
    assertNotNull(httpSession.getAttribute("eid"));
    assertNull(httpSession.getAttribute("eid.identity"));
    assertNull(httpSession.getAttribute("eid.address"));
    assertNull(httpSession.getAttribute("eid.photo"));
    String identifier = (String) httpSession.getAttribute("eid.identifier");
    assertNotNull(identifier);
    LOG.debug("identifier: " + identifier);
    assertTrue(TestAuthenticationService.called);
}

From source file:test.be.fedict.eid.applet.ControllerTest.java

@Test
public void testAuthnServerCertificateChannelBinding() throws Exception {
    // setup/*from w w  w.j  a v a 2 s.  com*/
    Messages messages = new Messages(Locale.getDefault());
    Runtime runtime = new TestRuntime();
    View view = new TestView();
    Controller controller = new Controller(view, runtime, messages);

    // make sure that the session cookies are passed during conversations
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);

    this.servletHolder.setInitParameter("AuthenticationServiceClass",
            TestAuthenticationService.class.getName());
    this.servletHolder.setInitParameter("Logoff", "true");
    File tmpCertFile = File.createTempFile("ssl-server-cert-", ".crt");
    FileUtils.writeByteArrayToFile(tmpCertFile, this.certificate.getEncoded());
    this.servletHolder.setInitParameter("ChannelBindingServerCertificate", tmpCertFile.toString());

    // operate
    controller.run();

    // verify
    LOG.debug("verify...");
    SessionHandler sessionHandler = this.servletTester.getContext().getSessionHandler();
    SessionManager sessionManager = sessionHandler.getSessionManager();
    LOG.debug("session manager type: " + sessionManager.getClass().getName());
    HashSessionManager hashSessionManager = (HashSessionManager) sessionManager;
    LOG.debug("# sessions: " + hashSessionManager.getSessions());
    assertEquals(1, hashSessionManager.getSessions());
    Map<String, HttpSession> sessionMap = hashSessionManager.getSessionMap();
    LOG.debug("session map: " + sessionMap);
    Entry<String, HttpSession> sessionEntry = sessionMap.entrySet().iterator().next();
    HttpSession httpSession = sessionEntry.getValue();
    assertNotNull(httpSession.getAttribute("eid"));
    assertNull(httpSession.getAttribute("eid.identity"));
    assertNull(httpSession.getAttribute("eid.address"));
    assertNull(httpSession.getAttribute("eid.photo"));
    String identifier = (String) httpSession.getAttribute("eid.identifier");
    assertNotNull(identifier);
    LOG.debug("identifier: " + identifier);
    assertTrue(TestAuthenticationService.called);
}

From source file:test.be.fedict.eid.applet.ControllerTest.java

@Test
public void testAuthnHybridChannelBinding() throws Exception {
    // setup/*  w w  w  . j ava2  s.  c o  m*/
    Messages messages = new Messages(Locale.getDefault());
    Runtime runtime = new TestRuntime();
    View view = new TestView();
    Controller controller = new Controller(view, runtime, messages);

    // make sure that the session cookies are passed during conversations
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);

    this.servletHolder.setInitParameter("AuthenticationServiceClass",
            TestAuthenticationService.class.getName());
    this.servletHolder.setInitParameter("Logoff", "true");
    File tmpCertFile = File.createTempFile("ssl-server-cert-", ".crt");
    FileUtils.writeByteArrayToFile(tmpCertFile, this.certificate.getEncoded());
    this.servletHolder.setInitParameter("ChannelBindingServerCertificate", tmpCertFile.toString());
    this.servletHolder.setInitParameter("SessionIdChannelBinding", "true");

    // operate
    controller.run();

    // verify
    LOG.debug("verify...");
    SessionHandler sessionHandler = this.servletTester.getContext().getSessionHandler();
    SessionManager sessionManager = sessionHandler.getSessionManager();
    LOG.debug("session manager type: " + sessionManager.getClass().getName());
    HashSessionManager hashSessionManager = (HashSessionManager) sessionManager;
    LOG.debug("# sessions: " + hashSessionManager.getSessions());
    assertEquals(1, hashSessionManager.getSessions());
    Map<String, HttpSession> sessionMap = hashSessionManager.getSessionMap();
    LOG.debug("session map: " + sessionMap);
    Entry<String, HttpSession> sessionEntry = sessionMap.entrySet().iterator().next();
    HttpSession httpSession = sessionEntry.getValue();
    assertNotNull(httpSession.getAttribute("eid"));
    assertNull(httpSession.getAttribute("eid.identity"));
    assertNull(httpSession.getAttribute("eid.address"));
    assertNull(httpSession.getAttribute("eid.photo"));
    String identifier = (String) httpSession.getAttribute("eid.identifier");
    assertNotNull(identifier);
    LOG.debug("identifier: " + identifier);
    assertTrue(TestAuthenticationService.called);
}