Java Cookies

Image should be self explanatory. In regards to my last post: I still don’t get it. Am I supposed to be sending each of those initial cookies back to the server? And if so… how is that done when they all share the same key name?

I guess I have a lot more reading to do in regards to cookies.

Processing (854) – Release 0.4

Bug: http://code.google.com/p/processing/issues/detail?id=854&sort=owner&colspec=Stars%20ID%20Type%20Status%20Priority%20Owner%20Summary&

Release 0.4: https://github.com/kmcdonald11/Send2SP

This release was all about cookies and Java connection objects. Best part? Completely self taught. I’ll try and go over what I’ve learned as best I can since it’s still new to me – and it’s possible I have the wrong idea.

Part of this bug/enhancement was to connect to a website through the tool for the program, and be able to interact with a server. This is something I’ve touched on before in other languages, and it wasn’t too difficult. Java, on the other hand, was all new to me with it’s connection objects. My experience with Java in the past has been mainly graphical, so switching to something like this was a bit of a challenge.

In order to get as far as I did on this release, I had to piece together a few guides. These were the most useful, from least to greatest:
http://www.cookiecentral.com/code/javacook2.htm
http://www.dreamincode.net/forums/blog/114/entry-2715-login-to-a-website-from-java/
http://www.hccp.org/java-net-cookie-how-to.html

I’d encourage you to go check out the GitHub link above and head into the “src” folder to see (note that the spacing/alignment that you see in GitHub is super messy – and not something I’d do. Not sure why it shows up like that). In short, you choose the tool option (I named it Send2SP) from the menu. It pops up with a confirmation box, asking if you’d like to send your current code to sketchpad.cc. You accept, and are prompted to enter your login credentials.

Then comes the tricky part. Cookies. I posted a photo of what I’ve got, with some easy to read debug info:

First line, “set cookie,” is run /after/ you’ve requested the headers from Sketchpad’s url, and received the cookie information. That cookie info that’s taken from the url is saved in my code (variable named cookie, how clever). The “set cookie” that you see happens after you take that saved cookie, and pass it back up to the server through the connection object to let it know that we’re still the same person/user. It was a check to see what exactly I was relaying back at the server.

The second line was a sanity check. You’ll notice in the console below, you can see that the webpage I was getting back was telling me I didn’t have cookies enabled. I added in the “get cookie” to run after I sent my own cookie to the server, and wanted to make sure I was sending the same thing as what I expected.

Also, good to note that in Java, the responses from using the connection objects are the actual html page. I printed our my responses to console, and that explains why you see some divs and things down there. It’s webpage that’s being sent back.

Because of this, I didn’t get any further. I don’t understand why it wouldn’t be recognizing my cookies. My only guess is that as I’m originally requesting the header information from the website, a total of 7 different key/value pairs come back. The part that confuses me is that the key for each of those 7 values remains the same “ES”. The values aren’t even all different from each other. Sometimes two cookies are sent back, exact same key/value pairs, and maximum of 3 in a row. One would think that if they all have the same key – it’s really only one cookie.. right? Again, this cookie stuff is completely new to me, so I’m not sure. Having multiple key/value responses all using the same key would lead me to make a logical conclusion that it’s really only one cookie.

Over the past few weeks, I have been in emailing Ari, the man behind Sketchpad, back and forth about this API information. I haven’t been on IRC too often because I don’t see much fit for myself. I’m not sure if this IDE has it’s own IRC chat – but lurking around Processing.js didn’t relate to me at all. My communication has been email based due to the fact that this API information I needed for Sketchpad isn’t anywhere on the web for the public.

While this tool isn’t completed, I’m still really keen on finishing it. I feel like I’ve made that commitment, and I do want to get it done. I want to contribute. I couldn’t just walk away from it now and have this effort be wasted, haha. Over the winter, I’m hoping to have some Java/cookie guidance and help to get this little part figured out. Once I can make that connection work – the rest is easy. It’s all app flow. I was looking forward to reformatting my computer over the winter break, but I’m gonna put it on hold because I really don’t want to try and have to set up that ANT thing again in Eclipse.

EDIT:

I totally forgot to bring this up, but feel it’s important since I spent a good 1-2h trying to solve it. So in my searches of trying to understand connection/cookie stuff in Java, I came across this library: http://hc.apache.org/httpclient-3.x/

I did get it compiling with ANT, and had some test code for it. Problem was that I couldn’t launch Processing’s IDE with the new tool/library. I kept getting some error every time I tried to run the program when it had the tool in it’s compile path. I was able to initialize/call a few things from the library, but once I reached a certain point, it would no longer run with the IDE. If it worked – it would’ve been a huge help because that library looked awesome and had a lot of recommendations.

Processing (854) – Release 0.3

Bug: http://code.google.com/p/processing/issues/detail?id=854&sort=owner&colspec=Stars%20ID%20Type%20Status%20Priority%20Owner%20Summary&

Release 0.3: https://github.com/kmcdonald11/Send2SP

Okaaaay, so.

0.3 Was meant to be my API implementation, and while it is – it’s not complete. I’ve spent numerous hours trying to find a /good/ JSON in Java tutorial or example, or just something.. and I’m falling short. For one – I don’t know JSON, and have never used it. I used Ajax from PHP once.. once. While it’s kind of similar, it’s not really, lol. My weekend was spent researching this new technique and trying to understand how to use it alongside Java (I miss jQuery).

You’ll see in my commit that I managed to find (I think?) suitable Java libraries for this. There’s one that translates POJOs to JSON objects, and then there’s Jackson – which I believe is the last missing piece. I’m still unsure if I’ll need Jackson or not. I did manage to find Google’s version, GSON, but I found easier-to-read examples with Jackson, so I went that route. Again, I’m not even entirely sure I need it.

Basically, I was following these steps:

For a Java program to interact with a server-side process
it simply must be able to write to a URL, thus providing
data to the server.
1. Create a URL.
2. Retrieve the URLConnection object.
3. Set output capability on the URLConnection.
4. Open a connection to the resource.
5. Get an output stream from the connection.
6. Write to the output stream.
7. Close the output stream.

Alright, that’s fine and dandy. It shows a simple HTTP request, but doesn’t really interweave JSON in there. For someone who’s trying to teach themself – there is a lack of good instructions on how to do this. Maybe I’m just retarded or something, but I need a good guide.

So, that rant aside, you will see some JSON action in my commit. There’s JSON objects in there for a user to sign into sketchpad.cc, and as well as uploading a sketch once they’re already logged in. I’ve been in contact with the creator of Sketchpad, and this API isn’t entirely finished yet. The two requests for signing in, and then uploading, are to be merged into one. There’s also a sign up request which I haven’t implemented yet because of the numerous return value possibilities – which aren’t in the API yet. I’m hoping it’s finished soon, or I may have to hop onto another bug for 0.4.

Either way – I learned a ton this weekend. Trying to teach myself JSON with little resource was fun I guess. Trying to them implement this newly thought up JSON code was also fun with an unfinished API.

Goal for 0.4? Have this tool completed for the Processing IDE. I really hope I am the one to finish it.

Last words? I feel like my mind was melting today.

PS. While messing around with libraries and build paths – I accidently deleted some Java environment library thing from my project? Basically, whatever I did broke all of my code, and of course.. Eclipse. There’s never an easy fix with Eclipse. I won’t go into detail on the one hour fix because of my stupid mis-click, but I was nearly in tears from frustration. Cool story. That was a nightmare.

Next release due in a week from today!

Fairly excited about it since this will be the release where I actually start integrating my code with the API for sketchpad.cc. I’m hoping this release will be finished over the weekend, since today and tomorrow are more PRJ/GAM/APD days.. yay.

PRJ – Thursday. Typed up a bunch of notes from client meetings, organized/found all of our weekly status reports for the website, and uploaded some old SYS references including a template of my SYS366 PID, and another that was mostly finished. Probably discuss what else should be done tomorrow with group members.

GAM – Thursday/Friday. Today I’m going through the framework and playing around with it. Tomorrow we have a team meeting to discuss a timeline, and hopefully get started on this crazy Goose Hunt game. I have no doubt getting used to the framework is going to take me a few hours. Yay, C++… not.

APD – Friday. Meeting with professor about my iPod app idea after GAM meeting, and then probably going to start a lot of the design work. There’s a ton of graphics that are going to have to be made, and I need to refresh my drawing skills.

That leaves all weekend for OSD release 0.3, which is due on the 17th of November (next Thursday). I’m not too worried about this release… as long as I can find the API documentation. A month with no response thus far. I’m going to have to either email or bug someone on IRC, and I hate bothering people. I just can’t seem to find any information on it anywhere. I suppose if I don’t get any help locating it, or find it myself, over the weekend, I’ll still have a few days to quickly pick up another bug to work on.

</endpost>

Processing (854) – Release 0.2

Bug: http://code.google.com/p/processing/issues/detail?id=854&sort=owner&colspec=Stars%20ID%20Type%20Status%20Priority%20Owner%20Summary&

Release 0.2: https://github.com/kmcdonald11/Send2SP
Note! My update on the Wiki was done around 2:30am simple because I forgot to copy my work there. Use repo commit time, as well as this blog post time, to verify that it was finished prior to midnight😦

For any information regarding how to use this tool or questions on why I have my own repo, please see 0.1 release’s post below.

Release 0.3: This tool should be completed. As of now, the only thing left to do is integrate API logic into the existing functions and GUI.

For the 0.2 release, this was simply GUI work. My Java skills were a little outdated (by about 6 months), so it was nice to go refresh myself. I had a little trouble getting the “sign in” button selected by default (so when the user types their login and password, and hits enter, it’ll select the sign in option rather than cancel), but once I researched it and figured it out.. I’m now reminded why I love Java.

int login = JOptionPane.showOptionDialog(editor, panel, "Sign in using your sketchpad.cc account",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, loginOptions, loginOptions[0]);

The bolded variable = the default selected button in a showOptionDialog window. How simple. Yay Java.

I’m really excited to be working with the sketchpad.cc API for my next release. Now all I have to do is find the documentation..

PS. I’m considering uploading my Java project from last year, a “coloring book,” for laughs. It had a nice fancy start screen logo, but only one stencil you could color in. I chose a flying pig. The color pallet was nice, and save feature worked (depending on your definition of working). It might be a project I revisit after this and hopefully make it a little more practical.

Processing (854) – Release 0.1

Bug: http://code.google.com/p/processing/issues/detail?id=854&sort=owner&colspec=Stars%20ID%20Type%20Status%20Priority%20Owner%20Summary&

Release 0.1: https://github.com/kmcdonald11/Send2SP

You’re probably asking why I created a new repo on GitHub for this. The IDE code that I was working with comes from: http://code.google.com/p/processing/, not GitHub (that I know of, anyways). Since OSD is all about GitHub, I figured it’d be best if I kept with that and hosted my project there rather than Google.

This release adds a menu item to the IDE toolbar under the “Tools” heading called “Send2SP” (stands for send to sketchpad. Open to other naming ideas). When selected, an option box appears asking if you wish to upload the current code you have in the editor to sketchpad.cc, or to cancel. If you hit cancel, it goes away. If you hit upload, a new message box appears saying something like “Yay! Send to sketchpad.cc”.

To test this, you must already have the Processing IDE on your computer. If not, download the latest stable version from the link above for whichever OS you’re using. You’ll also want to get a copy of my Send2SP tool. Once cloned, you should have a folder named “Send2SP” with a bin, src, and tool folder inside there. Next, go find your copy of the Processing IDE that you downloaded from Google Code, and open the folder. You should see a tools directory in there as well. You need to copy/move my Send2SP folder into the IDE’s tool folder. It’ll look something like this:

C:\Users\Kaitlyn\Desktop\processing-1.5.1\tools\Send2SP

After you’ve added the new tool to the IDE’s file hierarchy,  back out and simply run the Processing application. The Send2SP option should now appear under the tool menu!

Javadoc failed: java.io.IOException: Cannot run program “javadoc.exe”

I had a huge article typed up surrounding this issue, but it’s just me rambling. Basically, if you have this coming from Ant:

Javadoc failed: java.io.IOException: Cannot run program
"javadoc.exe": CreateProcess error=2, The system cannot find the file
specified

Read this: http://blog.darevay.com/2008/12/running-javadoc-ant-task-from-eclipse/

 

For someone like me who has never really stepped foot into editing/reading XML, and has never heard of this “Ant” tool within Eclipse – it makes fixing the issue pretty straight forward. Even offers a little guide on how to save the settings and changes you’ve made to a file so the next person who tries to run this code won’t have to set up Ant’s environment.. things.

OOC, I’m usually someone who likes to have things done a few days in advance. So far this semester, I’ve been slacking, and it’s catching up with me. I was pretty worried about my OSD 0.1 release due later tonight – but after following some steps on Processing’s Wiki about tool creation (http://code.google.com/p/processing/wiki/ToolTemplate), I’ve gotta say.. wow. Once I had finished that tutorial and it was working (without even really understanding what was going on. Darn XML), it was like magic. A tool was created just like that. I copied the results over to p5’s tool folder, and ran the IDE. Guess what? The tool was there in the menu, working as expected. What just happened? I don’t even. Could it be any simpler? I’m near speechless due to how easy that was (Eclipse issues aside). I don’t even know what to say, but that was awesome.

Now that I’ve got a template to work off of, I’m going to go ahead and make my tool. I’m still in shock. Hats off.

Follow

Get every new post delivered to your Inbox.