CSRBuilder { public void sign(KeyPair keypair) throws IOException { Objects.requireNonNull(keypair, "keypair"); if (namelist.isEmpty() && iplist.isEmpty()) { throw new IllegalStateException("No domain or IP address was set"); } try { int ix = 0; GeneralName[] gns = new GeneralName[namelist.size() + iplist.size()]; for (String name : namelist) { gns[ix++] = new GeneralName(GeneralName.dNSName, name); } for (InetAddress ip : iplist) { gns[ix++] = new GeneralName(GeneralName.iPAddress, ip.getHostAddress()); } GeneralNames subjectAltName = new GeneralNames(gns); PKCS10CertificationRequestBuilder p10Builder = new JcaPKCS10CertificationRequestBuilder(namebuilder.build(), keypair.getPublic()); ExtensionsGenerator extensionsGenerator = new ExtensionsGenerator(); extensionsGenerator.addExtension(Extension.subjectAlternativeName, false, subjectAltName); p10Builder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, extensionsGenerator.generate()); PrivateKey pk = keypair.getPrivate(); JcaContentSignerBuilder csBuilder = new JcaContentSignerBuilder( pk instanceof ECKey ? EC_SIGNATURE_ALG : SIGNATURE_ALG); ContentSigner signer = csBuilder.build(pk); csr = p10Builder.build(signer); } catch (OperatorCreationException ex) { throw new IOException("Could not generate CSR", ex); } }  void addDomain(String domain); void addDomains(Collection<String> domains); void addDomains(String... domains); void addIP(InetAddress address); void addIPs(Collection<InetAddress> ips); void addIPs(InetAddress... ips); void addIdentifier(Identifier id); void addIdentifiers(Collection<Identifier> ids); void addIdentifiers(Identifier... ids); void setOrganization(String o); void setOrganizationalUnit(String ou); void setLocality(String l); void setState(String st); void setCountry(String c); void sign(KeyPair keypair); PKCS10CertificationRequest getCSR(); byte[] getEncoded(); void write(Writer w); void write(OutputStream out); @Override String toString();  }
@Test(expected = IllegalStateException.class) public void testNoDomain() throws IOException { CSRBuilder builder = new CSRBuilder(); builder.sign(testKey); }