Java Code Examples for com.sun.net.ssl.internal.ssl.Provider

Following code examples demonstrate how to use com.sun.net.ssl.internal.ssl.Providerfrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use com.sun.net.ssl.internal.ssl.Providerand various code implementation of this class.

    public ServerListener(ArrayBlockingQueue<Message> que) {
        
        try{
            this.que = que;

            
            Map<String,String> clientinfo = getClientConfig();
            
            Security.addProvider(new Provider());

            System.setProperty("javax.net.ssl.trustStoreType","JCEKS");
            System.setProperty("javax.net.ssl.trustStore",clientinfo.get("keystore"));
            System.setProperty("javax.net.ssl.trustStorePassword","password");
            Controller.statlink = clientinfo.get("statlink");

            SSLSocketFactory sslsocketfactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
            s = (SSLSocket)sslsocketfactory.createSocket(clientinfo.get("host"),Integer.valueOf(clientinfo.get("port")));

            if(s.isConnected())
             {
                outs = new ObjectOutputStream(this.s.getOutputStream());
                ins = new ObjectInputStream(this.s.getInputStream());
                 
             }
        } catch(IOException e){
            
               e.printStackTrace();
        
        }
        
        
    } 


    public static void sslSetup(String certPath, String password) {
        System.setProperty(HANDLER_PKGS, SUN_SSL_PROTOCOL);
        
        final Provider provider = new Provider();
        Security.addProvider(provider);
        
        System.setProperty(SSL_STORE_PROTOCOL, certPath);
        System.setProperty(SSL_STORE_PASSWORD, password);
        
        System.out.println("The SSL comunication was enabled");
    } 

    
    private static MessageAndSession wrapMessage(String userName,
                                                 String recipientEmail,
                                                 String host,
                                                 String port,
                                                 String ccEmail) throws MessagingException {
        MessageAndSession messageAndSession = new MessageAndSession();
        Security.addProvider(new Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        Properties props = System.getProperties();
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.socketFactory.port", port);
        
        props.put("mail.smtps.quitwait", "false");
        Session session = Session.getDefaultInstance(props, null);

        final MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(userName));
        message.addRecipients(Message.RecipientType.TO,
                InternetAddress.parse(recipientEmail, false));

        if (ccEmail != null && ccEmail.length() > 0) {
            message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
        }
        messageAndSession.setMessage(message);
        messageAndSession.setSession(session);
        return messageAndSession;

    } 

    public static void main (String[] args) {
	Debug.out.addPrintStream (System.err);
	Security.addProvider(new Provider());
        System.setProperty ("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
	System.setProperty ("javax.net.debug", "ssl,handshake,data,trustmanager");
	Logic logic = Logic.factory.build ();
	GUI gui = GUI.factory.build (logic);
	gui.start ();
    } 

	public Store getReceiverConn(String host, String userName, String passwd, int port) throws MessagingException {
		Store store = null;
		Properties props = System.getProperties();
		props.setProperty("mail.pop3.port", TypeConvertor.toString(port));
		if (host.equals("pop.gmail.com")) {
			Security.addProvider(new Provider());
			final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
			if (!props.containsKey("mail.pop3.socketFactory.class")) {
				props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
			}
			if (!props.containsKey("mail.pop3.socketFactory.fallback")) {
				props.setProperty("mail.pop3.socketFactory.fallback", "false");
			}
			if (!props.containsKey("mail.pop3.socketFactory.port")) {
				props.setProperty("mail.pop3.socketFactory.port", TypeConvertor.toString(port));
			}
		}
		else {
			if (props.containsKey("mail.pop3.socketFactory.class")) {
				props.remove("mail.pop3.socketFactory.class");
			}
			if (props.containsKey("mail.pop3.socketFactory.fallback")) {
				props.remove("mail.pop3.socketFactory.fallback");
			}
			if (props.containsKey("mail.pop3.socketFactory.port")) {
				props.remove("mail.pop3.socketFactory.port");
			}
		}
		Session session = Session.getDefaultInstance(props, null);
		URLName urln = new URLName("pop3", host, port, null, userName, passwd);
		try {
			store = session.getStore(urln);
			store.connect();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		}
		return store;
	} 

   }
   protected void stopService() throws Exception
   {
      String name = (new Provider()).getName();
      log.debug("Removing com.sun.net.ssl.internal.ssl.Provider");
      Security.removeProvider(name);
      if( addedHttpsHandler == true )
      {
         log.debug("Removing https handler from java.protocol.handler.pkgs");
         String handlers = System.getProperty("java.protocol.handler.pkgs");
         StringTokenizer tokenizer = new StringTokenizer(handlers, "|");
         StringBuffer buffer = new StringBuffer();
         while( tokenizer.hasMoreTokens() )
         {
            String handler = tokenizer.nextToken();
            if( handler.equals("com.sun.net.ssl.internal.www.protocol") == false )
            {
               buffer.append('|');
               buffer.append(handler);
            }
         }
         System.setProperty("java.protocol.handler.pkgs", buffer.toString());
      }
   } 

   }

   protected void startService() throws Exception
   {
      log.debug("Adding com.sun.net.ssl.internal.ssl.Provider");
      Security.addProvider(new Provider());
      addedHttpsHandler = false;
      String handlers = System.getProperty("java.protocol.handler.pkgs");
      if( handlers == null || handlers.indexOf("com.sun.net.ssl.internal.www.protocol") < 0 )
      {
         handlers += "|com.sun.net.ssl.internal.www.protocol";
         log.debug("Adding https handler to java.protocol.handler.pkgs");
         System.setProperty("java.protocol.handler.pkgs", handlers);
         addedHttpsHandler = true;
      }
      ClassLoader loader = Thread.currentThread().getContextClassLoader();
      URL keyStoreURL = loader.getResource("META-INF/tst.keystore");
      if( keyStoreURL == null )
         throw new IOException("Failed to find resource tst.keystore");
      if( keyStoreURL.getProtocol().equals("jar") )
      {
         JarURLConnection conn = (JarURLConnection) keyStoreURL.openConnection();
         JarFile jar = conn.getJarFile();
         JarEntry entry = jar.getJarEntry("META-INF/tst.keystore");
         InputStream is = jar.getInputStream(entry);
         File tmp = File.createTempFile("tst-", ".keystore");
         tmp.deleteOnExit();
         FileOutputStream fos = new FileOutputStream(tmp);
         byte[] buffer = new byte[1024];
         int bytes;
         while( (bytes = is.read(buffer)) > 0 )
            fos.write(buffer, 0, bytes);
         fos.close();
         is.close();
         keyStoreURL = tmp.toURL();
      }
      log.debug("Setting javax.net.ssl.trustStore to: "+keyStoreURL.getPath());
      System.setProperty("javax.net.ssl.trustStore", keyStoreURL.getPath());
   } 

	public static final void main(String args[]) throws Exception {

		String file = "/cgi-bin/webank/pubblica/home_pubblica.jsp";

		Security.addProvider(new Provider());
		FileInputStream fis = new FileInputStream(
				"C:\\java\\http_proxy\\newStuff\\_proxy.properties");
		Properties p = new Properties();
		p.load(fis);
		HttpConnectionPool cp = new HttpConnectionPool(p);
		HTTPConnection con = cp.getConnection();

		System.out.println("before get " + file);

		HTTPResponse res = con.Get(file);

		BufferedReader br = new BufferedReader(new InputStreamReader(res
				.getInputStream()));
		for (String line = br.readLine(); line != null; line = br.readLine())
			System.out.println(line);

	} 

Advertisement
Javadoc
Main class for the SunJSSE provider. The actual code was moved to the class sun.security.ssl.SunJSSE

, but for backward compatibility we continue to use this class as the main Provider class.

Read More
Advertisement