title: Write a Java program that shows a simple CookiePolicy that blocks cookies from .gov domains, but allows others
body: <pre><code>import java.net.CookiePolicy;
  import java.net.HttpCookie;
  import java.net.URI;
  
  public class NoGovernmentCookies implements CookiePolicy {
      @Override
      public boolean shouldAccept(URI uri, HttpCookie cookie){
          if(uri.getAuthority().toLowerCase().endsWith(&quot;.gov&quot;)){
              return false;
          }
          return true;
      }
  }
  </code></pre>
  <p>I have made new CookieTest class to call NoGovernmentCookies.</p>
  <pre><code>import java.net.*;
  import java.util.List;
  
  public class CookieTest {
      public static void main(String[] args) {
  
          CookieManager manager = new CookieManager();
  
  
          HttpCookie c1 = new HttpCookie(&quot;user1&quot;, &quot;1&quot;);
          URI uri1 = URI.create(&quot;http://spm.gov&quot;);
  
  
  
          manager.setCookiePolicy(new NoGovernmentCookies());
  
        /*  manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
          manager.setCookiePolicy(CookiePolicy.ACCEPT_NONE);
          manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);*/
  
         // CookieHandler.setDefault(manager);
          CookieStore cs = manager.getCookieStore();
          cs. add(uri1, c1);
  
  
          //read stored cookies
          List cl = cs.getCookies();
          System.out.println(&quot;cookies list in cookiestore&quot; + cl + &quot;\n&quot;);
      }
  
  }
  </code></pre>
  <p>it is showing output as value:</p>
  <pre><code>cookies list in cookiestore[user1=&quot;1&quot;]
  </code></pre>
  <p>I do not want to add it in cookiestore, because it is against our  NoGovernmentCookies policy for <code>.gov</code>.</p>