0

So here are the things I know. The java application is attached, there are two JPanels , one with a basic graph and one that is more detailed. These two graphs are hosted on an applet together. The applet reads a file with all the student survey data. This all hosted on an instance of Desire 2 Learn http://www.desire2learn.com/ called courselink https://courselink.uoguelph.ca/shared/login/login.html

So this all works. The aspect that doesn't is getting from courselink who is signed on so the appropriate graph can be shown. A guy that works on developing courselink gave me a php program that grabs that information and returns it in a JSON block.

The php code is hosted on a different serve then the java app (which is hosted on courselink. So here is what I have tried:

first just grabbing what the page returned from java

String name = null;
    URL php = null;
    try {
        php = new URL ("http://coles- vs250.cs.uoguelph.ca/whoami/index.php");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    URLConnection yc = php.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();
    return name;
}

Then I was going to parse out the string, parse out the users name and return it. When I run this on course link though I get a security error, and I don't really know anything about java security errors. here is the error:

Exception in thread "AWT-EventQueue-2" java.security.AccessControlException: 
access denied ("java.net.SocketPermission" 
"coles-vs250.cs.uoguelph.ca:80"  "connect,resolve")

So next I tried to use JQuery to call the php from the html page so then the string would be a variable on the right survey and then the java app could grab it. After some research this is what I put together. I do not know JQuery and am actually quite out of my element when it comes to this kind of programming altogether. The alerts are not showing and I don't know whats wrong.

<html>
    <head>
        <title>Java Example</title>
        <script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'></script>
        <script type='text/javascript'>
            $(document).ready(function() {
                alert("String from iframe: " + $('#whoami').contents().find('body').html());
                whoami();
            });

            function whoami() {
                $.ajax({
                    type: "GET",
                    data: {},
                    url: "http://coles-vs250.cs.uoguelph.ca/whoami/index.php",
                    success: function(data) {
                        alert("whoami complete: " + data);
                    }
                });
            }
        </script>
    </head>

    <body>
        <p>
            <iframe style="visibility: visible;" id="whoami" src="http://coles-vs250.cs.uoguelph.ca/whoami/index.php"></iframe>--&gt;</p>
        <p>
            <applet width="800" height="1000" code="graphRun.class"></applet>
        </p>
    </body>

</html>

Any and all help or suggestions would be greatly appreciated and if there is any more info I should get or forgot I will do my best.

Jeff Noel
  • 7,500
  • 4
  • 40
  • 66
  • btw, your java attempt returns `name` but you set `name=null` and then you don't set it from the URL. You just print the contents of the link, instead of appending it on the `name` variable. – ajon Jul 15 '13 at 16:48
  • I know this code was just for testing purposes so far – Josh Zachariah Jul 15 '13 at 17:24

3 Answers3

0

Updated

You say that the users will be logged in to the server on their browser. That means you need some code to run from that browser that will download the content from the remote server and save the content locally. I would address the problem in the following way:

  1. create a webpage and host it on a server (example.com/download.html).
  2. Using your java program open the default browser to your webpage:

    URI uri = new URI("http",null, "example.com/download.html","",null); Desktop.getDesktop().browse(uri);

  3. Use jquery to download the contents of the remote server

  4. Find a way to save the contents to a specific file location - one possibility is downloadify (flash required)
  5. In your java application have a while loop that checks for the presence of the specific file. If it is there then you have the content, if it is not, then you need to keep waiting.

  6. Use my code below to turn the file into a json object:

GetJsonFromWeb:

private JSONObject getJSONFromWeb(String param){
    try{
        FileInputStream is = new FileInputStream("file.json");
        BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        StringBuilder sb = new StringBuilder();
        int cp;
        while((cp = br.read()) != -1) {
            sb.append((char) cp);
        }
        String jsonText = sb.toString();
        return new JSONObject(jsonText);
    } catch (IOException e) {
        //Handle exception
    } catch (JSONException e) {
        //Handle exception
    }
    return null;
}

Then you can use this object in the following way:

JSONObject jsonObj = getJSONFromWeb("Param_value");
try {
    boolean val1 = jsonObj.getBoolean("some_Json_index");
    double val2 = jsonObj.getDouble("some_Json_index2");
    //etc.
ajon
  • 7,868
  • 11
  • 48
  • 86
  • Thank you so much for your quick response! So I don't have access to the actualy php code, just the link. It was just given to me from the people who develop the survey this is all hosted on. So when I run the code you gave me it doesn't find the JSON block I think because the code hasn't actually run yet. It also needs to be told to run **from** the courselink server because thats where its gettig login data. Any suggestions on running the code before Java tries to grab the JSON block? – Josh Zachariah Jul 15 '13 at 18:15
  • Can you go to the URL of the php script from a web browser? You don't need to touch the PHP script. PHP is a language that runs on a web server and "outputs" web documents. So in my code above calling `InputStream is = new URL("http://example.com/website.php?someparameters="+param).openStream();` ensures the php code is "executed". – ajon Jul 15 '13 at 18:20
  • Also it seems that what the page actually returns is html with the JSON block embedded. – Josh Zachariah Jul 15 '13 at 18:20
  • When i put the link into my browser because the link changes and my info is outputted. When I run from eclipse it doesn't return have JSON block because it doesn't know who I am. I'm not sure if it needs to be run from courselink... though i don't think so because it still knows who I am if I just put the link in my browser. – Josh Zachariah Jul 15 '13 at 18:22
  • I understand now... this problem got much more complex now. When you enter the link in the browser you are logged in automatically because of a stored session variable from another window, or via certs or cookies. When you try to access the link from Java, you don't have any of those things. This is still possible, but much more tricky. I can think of 2 options. 1) use Java to log into the server - which requires some work. Or 2) build a php webpage that logs you into the server and retrieves the original php page using curl then serves that data. Either way, this isn't trivial. – ajon Jul 15 '13 at 21:23
  • it might not actually be that hard, again I say that not knowing anything really. I'm pretty sure once you log in to courselink (which is the login page you are redirected to) that login get stored your browser. The people who are using this app will already be logged in, they have to be. So java shouldn't have to log in because we already are. I suppose the problem then is that the browser is not the one going to the php page. Java is. I assume its cookies storing this info, so is there anyway to access those? The reason I tried JQueary was that I thought accessing php from browser would help – Josh Zachariah Jul 16 '13 at 13:20
0

I belive you're having this issue because the form you're trying to reach need an authentication, I assumed that based on the redirecting to the login page. In my vision, you should have a different aproach. You should force an authentication reaching the login page. I don't know Java very well, but I've worked with screen scrape in .Net. You should share a session in the login form and then try to reach this URL. You will simulate a browser acess. I'm sorry but I can't help you with Java source code.

Markus Fantone
  • 341
  • 1
  • 8
  • I have used a python package called mechanize as a screen scraper which also behaves like a browser and would allow to do the same thing. You can build an executable with the python code and call the executable from java. But this is a lot of work for a seemingly straightforward problem. – ajon Jul 15 '13 at 21:26
  • Unfortunately this is .NET, but I was thinking in something more like this: http://blog.bitlinkit.com/loading-a-web-page-using-c-optional-use-of-cookies-screen-scraper/ – Markus Fantone Jul 16 '13 at 12:14
  • In this case you would only do 2 web requests sharing cookies between. I really don't know if there is some similarity in Java, I believe it does. – Markus Fantone Jul 16 '13 at 12:17
  • My comment above also applies to what you were saying I believe – Josh Zachariah Jul 16 '13 at 13:21
0

I think you're getting that exception because the default security policy for applets does not allow to perform socket operations (connect and resolve in your case).

So you should try to change the default policy (not sure you can do whatever you want though). So create a security.policy:

grant {
    permission java.security.AllPermission;
};

Note that this grants all permissions to your applet. You can grant only socket permissions but as it is always a bit tricky, I would start with this to see if it solves your problem first.

Then specify the security policy from the command line with:

-J-Djava.security.policy=security.policy

You can also set the property with System.setProperty().

Menthos
  • 330
  • 2
  • 5