msgbartop
Code Musings and Such
msgbarbottom

09 Jun 09 How to issue a simple HTTPRequest in Java

I needed a way to issue an HTTPRequest in Java, and I don't care about the return value. I wasn't able to find a simple example so I thought I'd post my that part of my solution. I don't know that it's the best way, but it works.

import java.net.URL;

public class Request {
  public static void main(String[] args) throws Exception {
      if(args.length == 0) {
          System.out.println("URL required.");
          return;
      }
      URL url = new URL(args[0]);
      url.openStream().close();
  }
}

And to use it you just pass in the url via the command line

java Request http://joezack.com
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Tags:

21 May 09 When is a Struct not a Struct?

I've run into an annoying problem trying to get some older java code working under ColdFusion 8. The problem method accepts two arguments, java.util.Hashmap and java.util.Vector, but the signature isn't matching.

Here's the set up:

<cfset classpath = [
  CreateObject( "java","java.net.URL" ).init( "file:#cfusion#" ),
  CreateObject( "java","java.net.URL" ).init( "file:#testfile#" )
] />

<cfset loader  = CreateObject( "java","java.net.URLClassLoader" ).Init(
  classpath
) />
<cfset example = loader.LoadClass( "ProblemExample" ).NewInstance() />

<cfset cfarray  = ArrayNew(1) />
<cfset cfstruct = StructNew() />

<cfdump var="#example#" />

My sample method looks good...

public void originalMethod(java.util.Hashtable h,java.util.Vector v){}

The dump looks greatdump

But I'm getting a "method was not found" error

I did a bit of googling, and found a great post about the differences between CF7 and CF8 structs and arrays. Perfect...right?

I wrote a little test method to accept a coldfusion.util.FastHashtable:

public void takeFastHashtable(coldfusion.util.FastHashtable fh){}
method was not found"

...

I wrote a little script, to see what I was dealing with:

<cfoutput>
  #cfstruct.getClass().getName()# == #example.inspect(cfstruct)#
  => #cfstruct.getClass().getName() EQ example.inspect(cfstruct)#
</cfoutput>

using

public String inspect( Object o ) {
  return o.getClass().getName();
}

And my output was as expected:
coldfusion.runtime.Struct == coldfusion.runtime.Struct => YES

So I wrote a up a simple test method for coldfusion.runtime.Struct:

public void testStruct(coldfusion.runtime.Struct s){}
<cfset example.testStruct(s) />

You guessed it:

"method was not found"

Okay....let's try casting....

public coldfusion.runtime.Struct castAsStruct(Object o) {
  return (coldfusion.runtime.Struct) o;
}

This time the error's a little different...

coldfusion.runtime.Struct cannot be cast to coldfusion.runtime.Struct
W T F

Granted, I'm trying to use underlying undocumented code, and I'm no Java expert, but there's something here that's not adding up. Is this a bug? Am I missing something stupid? At this point I'll be moving on to alternative solutions, but any help would be still greatly appreciated.

You can download some sample code I've been using, just make sure to compile with the appropriate classpath for your environment.

Here's my command

javac ProblemExample.java -classpath /opt/coldfusion8/lib/cfusion.jar
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Tags: , ,