cafe.code

there's more to software than just bytes...

Logging HTTP Request Time in RESTEasy

I wanted to log the time it took to complete requests sent to our HTTP API. This particular API uses JBoss’ RESTEasy framework for REST-like request dispatching. I wanted to intercept the request prior to it being executed:

Alas, RESTEasy does not seem to provide the ability to insert your own code in the routing/dispatching process. However, it does have the ability to create pre and post filter classes (called Interceptors) that are executed before and after a request. The mechanism works as follows:

I ended up using the following hack to time my API requests:

I set a StopWatch on the request and start it inside the pre-filter. During post-filtering, I inject the request into the filter, grab the timer, stop it and log the duration. It’s totally hacky, but at least I don’t have to add timing code into the our RESTEasy classes and I don’t have to introduce an AOP framework into the codebase. Definitely not my favorite implementation, but it’ll do for now.

-B-

Searching for Printed Books via Amazon’s Product Advertising API

I’m using Amazon’s Product Advertising API (formerly known as ECS) in one of my projects. I use it to search for books; only printed books to be exact (no eBooks). Querying for said books proved more difficult than it should be.

The ItemSearch API allows search queries to specify the category (i.e. books) to search through. That was easy. Narrowing the search to only printed books was not apparent. After staring at API XML responses for a couple books, I noticed that each book had a <binding>. Examples of <binding>’s included “Hardcover”, “Kindle eBook” and “Paperback”.

Filtering search results by <binding> is available through the power request parameter. I filter my search for only hardcover and paperback bindings. There may be other bindings that qualify as “printed book”, but the two bindings that I filter with are good enough for me.

Here is an example query using the power request parameter. The example calls the Amazon API using the amazon-ecs ruby gem.

-B-

Setting the filepath for HTML input-file-fields with WebDriver

My coworker could not get Selenium’s WebDriver to select a file for upload for the following HTML input field:

1
<input type="file" name="filename"/>

The problem is that the clicking the input field opens a OS-specific file selection window that WebDriver cannot interact with. Her initial approach was to call an external program controls that file selection window and picks the file she wanted to upload. The approach is described here. The approach works, but it binds us to a Windows-only implementation.

After some searching, I notice that Watir-WebDriver is able to set the filepath on the input directly. If watir can do it directly, why can’t we? Watir would either have its own implementation or be wrapping the WebDriver ruby binding. Turns out it’s doing the latter, which is great for us. We can use the same implementation that watir uses, inside our choosen language, java.

Here are code snippets for both java and ruby:

-B-