How do I connect to an https URL to download a file?

I am writing a java program to download a daily file from an https url.
i.e. https://example.net/Downloads/guid.zip

I have a truststore and keystore configured correctly.
I have also been given a username and password.

My problem at this point is with the connection.  When I attempt to connect, I get the following error:
Access denied (java.util.PropertyPermission javax.net.ssl.keyStore write)

I'm not exactly sure where to incorporate my username and password. Also, not sure what the setRequestProperty to look like.   My code is below.

System.setProperty( "javax.net.ssl.keyStore", "/is/domains/domain48000/config/clientkeystore.jks" );
            System.setProperty( "javax.net.ssl.keyStorePassword", "kspass" );
            System.setProperty( "javax.net.ssl.trustStore", "/is/domains/domain48000/config/cacerts.jks" );
            System.setProperty( "javax.net.ssl.trustStorePassword", "tspass" );
            java.net.URL url = new java.net.URL( https://example.net/Downloads/guid.zip );
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setDoInput( true );
            conn.setDoOutput( true );
            conn.setUseCaches( false );
            conn.setRequestProperty( "Content-Type", "text/xml" );
            InputStream stream = conn.getInputStream();
            BufferedInputStream in = new BufferedInputStream( stream );
            FileOutputStream file = new FileOutputStream( "/home/myFile.zip" );
            BufferedOutputStream out = new BufferedOutputStream( file );
            int i;
            while ((i = in.read()) != -1) {
                out.write( i );
            }
            out.flush();

Open in new window

cindymccartneyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bryan ButlerCommented:
So is the error happening on line 2 then?
0
CEHJCommented:
You're complicating the issue: there should be no need to manipulate SSL properties. All you need to do is authenticate:

http://www.exampledepot.com/egs/java.net/Auth.html

and copy the stream:


IOUtils.copyStream(conn.getInputStream(), new FileOutputStream( "/home/myFile.zip" ));
 
// At http://www.technojeeves.com/joomla/index.php/free/51-copying-streams

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marchentCommented:
Firstly install Openssl to support HTTPS communication. Download OpenSSL exe from web and install, then can use below code.
import java.io.*;
import java.net.*;
 
class Test{
    
    public void getZip(String link, String file) throws Exception{
        URL url = new URL(link);
        URLConnection urlConnection = url.openConnection();
        DataInputStream dis = new DataInputStream(urlConnection.getInputStream());
        DataOutputStream out = new DataOutputStream(new FileOutputStream( file  ) );
        
        int i;
        while ((i = dis.read()) != -1) {
            out.write( i );
        }
        out.flush();
        dis.close();
    }
    
    public static void main(String []argv) throws Exception{
        new Test().getZip("https://code.bandit-project.org/trac/export/3/trunk/RoleEngine/doc/rejavadoc.zip", "out.zip");
    }
}

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

cindymccartneyAuthor Commented:
CEHJ -

I attempted to your solution, but am having a problem.  I created a new package to hold the MyAuthenticator class and imported that package into my main java program.  

The first line in my code (as per the example) is this:

java.net.Authenticator.setDefault( new com.dva.Authenticator.MyAuthenticator() );

However, I am getting a "class com.dva.Authenticator.MyAuthenticator() cannot be resolved".
It seems it doesn't like "MyAuthenticator()" when attempted to create the object.

Below is the java file that I put into a package called com.dva.Authenticator:



package com.dva.Authenticator;
 
import java.net.*;
 
    public class MyAuthenticator extends Authenticator {
        // This method is called when a password-protected URL is accessed
        protected PasswordAuthentication getPasswordAuthentication() {
            // Get information about the request
            String promptString = getRequestingPrompt();
            String hostname = getRequestingHost();
            InetAddress ipaddr = getRequestingSite();
            int port = getRequestingPort();
 
            // Get the username from the user...
            String username = "myusername";
 
            // Get the password from the user...
            String password = "mypassword";
 
            // Return the information
            return new PasswordAuthentication(username, password.toCharArray());
        }
    }

Open in new window

0
cindymccartneyAuthor Commented:
ok, just an update.  I got the classes to work correctly per CEHJ's example.  

However, I am getting a different error:

Access denied (java.net.NetPermission setDefaultAuthenticator)|#]
0
CEHJCommented:
You've got a SecurityManager in place? Are you doing this inside a servlet container?
0
cindymccartneyAuthor Commented:
I'm writing the code inside Sun's JCAPS application.

I'm not sure if there is a SecurityMangager in place or not.

I'm also not sure if the java collaboration inside of JCAPS is considered a servlet container.
0
CEHJCommented:
Check out

http://java.sun.com/javase/6/docs/api/java/lang/SecurityManager.html

since you need to know whether you've got one or not. My guess is that you have. If so, you have two basic alternatives:

a. give the code the permission required
b. disable it
0
cindymccartneyAuthor Commented:
Thanks CEHJ for your continued help.

I do have a java.policy file (pasted below)  in one of the application server directories.  Do I need to add a permission entry into this file?

If so, can you give me an example of what it would look like?

Or maybe I'm not on the right track at all.

I'm not explicitly enabling the security manager, but it might be something the App server is enabling by default somehow.  Otherwise, how would I go about disabling it?

//
// @(#)src/security/sov/config/java.policy, security, asdev, 20050521 1.4
// ===========================================================================
// Licensed Materials - Property of IBM
// "Restricted Materials of IBM"
//
// IBM SDK, Java(tm) 2 Technology Edition, v1.4.2
// (C) Copyright IBM Corp. 1998, 2002. All Rights Reserved
// ===========================================================================
//
 
 
// Standard extensions get all permissions by default
 
grant codeBase "file:${java.home}/lib/ext/*" {
        permission java.security.AllPermission;
};
 
// default permissions granted to all domains
 
grant { 
        // Allows any thread to stop itself using the java.lang.Thread.stop()
        // method that takes no argument.
        // Note that this permission is granted by default only to remain
        // backwards compatible.
        // It is strongly recommended that you either remove this permission
        // from this policy file or further restrict it to code sources
        // that you specify, because Thread.stop() is potentially unsafe.
        // See "http://java.sun.com/notes" for more information.
        permission java.lang.RuntimePermission "stopThread";
 
        // allows anyone to listen on un-privileged ports
        permission java.net.SocketPermission "localhost:1024-", "listen";
 
        // "standard" properies that can be read by anyone
 
        permission java.util.PropertyPermission "java.version", "read";
        permission java.util.PropertyPermission "java.vendor", "read";
        permission java.util.PropertyPermission "java.vendor.url", "read";
        permission java.util.PropertyPermission "java.class.version", "read";
        permission java.util.PropertyPermission "os.name", "read";
        permission java.util.PropertyPermission "os.version", "read";
        permission java.util.PropertyPermission "os.arch", "read";
        permission java.util.PropertyPermission "file.separator", "read";
        permission java.util.PropertyPermission "path.separator", "read";
        permission java.util.PropertyPermission "line.separator", "read";
 
        permission java.util.PropertyPermission "java.specification.version", "read";
        permission java.util.PropertyPermission "java.specification.vendor", "read";
        permission java.util.PropertyPermission "java.specification.name", "read";
 
        permission java.util.PropertyPermission "java.vm.specification.version", "read";
        permission java.util.PropertyPermission "java.vm.specification.vendor", "read";
        permission java.util.PropertyPermission "java.vm.specification.name", "read";
        permission java.util.PropertyPermission "java.vm.version", "read";
        permission java.util.PropertyPermission "java.vm.vendor", "read";
        permission java.util.PropertyPermission "java.vm.name", "read";
 
};

Open in new window

0
cindymccartneyAuthor Commented:
Just an update, I was able to get it working.  Here is what I needed to add to my server.policy file:

Replace the following line:
   permission java.util.PropertyPermission "*", "read"; with
   permission java.util.PropertyPermission "*", "read,write";

And then within the same grant clause as the above add the following line:

permission java.net.NetPermission "setDefaultAuthenticator"
0
cindymccartneyAuthor Commented:
Thanks CEHJ for your help!
0
CEHJCommented:
Good - glad it's working :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.