Archive

Archive for the ‘Web’ Category

Moving this blog to “evgeny-goldin.com”

June 27, 2010 1 comment

This blog has moved to a new address: http://evgeny-goldin.com/blog/

Two new posts are available already:

See you there!

Categories: Web

Uploading files – multipart HTTP POST and Apache HttpClient

May 1, 2010 60 comments

I had to implement a files transferring mechanism where one machine was sending files to another using "multipart/form-data" POST request. It can be done using Apache’s Commons FileUpload and HttpClient.

The receiving part was an easy one:

<dependency>
    <groupid>commons-fileupload</groupid>
    <artifactid>commons-fileupload</artifactid>
    <version>1.2.1</version>
</dependency>

We parse an incoming request with ServletFileUpload and get a list of FileItems in return. Each FileItem is either form’s input field or a file uploaded:

if ( ServletFileUpload.isMultipartContent( request ))
{
    List<fileitem> fileItems = 
        new ServletFileUpload( new DiskFileItemFactory( 1024 * 1024, DIR )).
        parseRequest( request );

    for ( FileItem item : fileItems )
    {
        String fieldName = item.getFieldName();

        if ( item.isFormField()) { item.getString()      } // Form's input field
        else                     { item.getInputStream() } // File uploaded
    }
}

In our case, we use DiskFileItemFactory to store files larger than 1Mb in a temporary DIR. After reading file’s InputStream and storing the data in a proper storage – we need to delete the temporary copy: item.delete().

It’s the sending part that came out to be a bit trickier. Initially, I was using a simple HTML form:

<form action="http://localhost" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="text" name="paramName">
    <input type="submit" name="Submit" value="Upload File">
</form>

But then I’ve switched back to Java and assumed HttpClient will do the job.

<dependency>
    <groupid>org.apache.httpcomponents</groupid>
    <artifactid>httpclient</artifactid>
    <version>4.0.1</version>
</dependency>

Eventually, it did but it took me some time to figure out how. The problem with HttpClient is that it provides a nice tutorial and various usage examples but none of them actually mentions a word about uploading files!

I’ve figured out I need to set an instance of HttpEntity to request but it seemed like it’s going to be either a StringEntity or FileEntity but not both. How come ?! Why is it so hard to send a usual POST request with String and file parameters?

Ok, it’s Google time.

Some examples and documentation were referring to an outdated version when HttpClient was part of Apache Commons and, therefore, were of no use for me – the API has changed dramatically. Until I’ve found this example that finally saved my day. Radomir, thank you!

The solution is to use an additional Apache component – HttpMime:

<dependency>
    <groupid>org.apache.httpcomponents</groupid>
    <artifactid>httpclient</artifactid>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupid>org.apache.httpcomponents</groupid>
    <artifactid>httpmime</artifactid>
    <version>4.0.1</version>
</dependency>

and then we finally get to use a magical MultipartEntity:

HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

HttpPost        post   = new HttpPost( url );
MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );

// For File parameters
entity.addPart( paramName, new FileBody((( File ) paramValue ), "application/zip" ));

// For usual String parameters
entity.addPart( paramName, new StringBody( paramValue.toString(), "text/plain", 
                                           Charset.forName( "UTF-8" )));

post.setEntity( entity );

// Here we go!
String response = EntityUtils.toString( client.execute( post ).getEntity(), "UTF-8" );

client.getConnectionManager().shutdown();

Note the use of EntityUtils for reading the response.

That’s it.

I only wish library authors were providing better support and examples for more common cases like files uploading in our case. I mean, come on, when people get to use HttpClient they either want to send a usual request or upload a file, same thing they do with browser. Am I wrong here?

Categories: Java, Web Tags: , , , , ,

10 Online Tools for Superb Productivity

April 17, 2010 25 comments

I love being productive.

I love it to the point where I actually hate being slowed down by an application or a resource. I don’t mind waiting but only for a good reason. Anything that makes me stare at the screen doing absolutely nothing will usually drive me into searching for a faster replacement right away.

“Being productive” starts from “working fast” and “using the best tools” for me. So my favorite on-line tools is what I would like to write about today. Had I written this review several years ago – most definitely, I would have talked about “10 Windows Application for Superb Productivity”. But Web is where I/we spend most of the time today so it makes more sense to talk about Web applications rather than various Windows tweaks.

1. Google Chrome

Obviously, living on the Web starts with a browser. Like many others, I was a devoted Firefox user for the last 5 years. After all, it was the only choice that actually made sense on Windows. When Chrome has initially come out – I wasn’t impressed much but lot’s of things have changed since then.

So .. why Chrome today? For one reason, mostly – it’s fast and I’ve mentioned already how working fast is important to me. Chrome’s start-up time is light-years ahead of Firefox and no restart is required when extensions are (un)installed. Those two factors add up to a tremendous speed-ups when working on-line, as I wait much less now.

2. delicious

Keeping bookmarks online is an old idea and being able to tag them isn’t novel as well. Today I use delicious as my main storage of everything I ever read and find it useful for later reference. Cars, tablet PCs, video sessions and travelling – it’s all there, anytime, anywhere.

Using browser’s keyworded searches I access a tag by typing "d tag" ("d tag1 tag2" for combination) and search delicious with "ds search" – it works amazingly fast allowing me to pull almost anything from my last year of browsing in a matter of seconds. This "d(s) something" thing is what I believe I type the most in browser’s address bar today.

With it’s Chrome extension being supported in Chrome Dev channel (finally!) – I now enjoy it even more. But I still keep bookmarklet around, it’s in the left side of my bookmarks bar so I use either extension’s button or a bookmarklet to add a link, whichever my mouse is closer to.

3. Zoho Writer

Working online means keeping notes and documents. Zoho Writer is my #1 application of choice now – it’s fast and it looks really great. Ironically, I have heard of it when Microsoft’s “fake Office” has made its way into a blogosphere. So, yes, this “fake Office” works pretty well for me now – all my private summaries, notes and drafts are there.

I only wish:

  • I could export all documents at once, as a backup copy.
     
       Whatever they say – I never trust “the cloud” completely, making a backup copies even
       of my Gmail account.
     
  • “Google Sign In” would sign me in transparently.
     
       After opening “writer.zoho.com” I’m forced to click a “G” button to enter. This extra “G”
       click may sound not as a big deal to many but when one gets used to “Remember me”
       allowing to access resources and documents with a single click – this extra delay is quite
       painful, actually.
     
       It really defeats the way I believe the Web should work – one single click to get me “in”.

4. Zoho Notebook

It’s not hard to get lost in all my Zoho documents and sadly, I still don’t get its way of tagging. But I now use Zoho Notebook as a way to organize related docs as “books”, grouping them together. I can edit them in Writer or Notebook, it doesn’t matter. But working in Notebook is significantly slower, though.

Of course, it’s intended for OneNote-like documents but I mostly use it as my “tagging” mechanism. A real OneNote is something I use a lot in the office.

5. Mindomo

After getting used to on-line mode of working it doesn’t come natural to install any desktop mind-mapping application, like FreeMind. Searching for online solution brought me to Mindomo and I have to tell you .. it’s beautiful.

Surprisingly, it’s way, way better than mindmeister that I’ve heard of so much recently.

Too bad it suffers from the same “Sign in with Google” extra click, as Zoho does. How come there’s no “Remember me” option for those case?!

6. Dropbox

Keeping files online is pretty standard today, but lot’s of applications have failed on delivering a good upload process, relying on browser’s capability to upload files. Trying to upload a bigger file usually resulted in broken connections and lot’s of frustration. Few resources cared to provide desktop “uploader” dealing with slow and unreliable networks.

YouSendIt has one and it’s excellent, I was using it a lot for a number of years. But free YouSendIt version doesn’t keep files forever while Dropbox does. Also, Dropbox has a native service installed, monitoring and syncing a certain folder: all I need to do in order to upload a file to the cloud and sync it with all my machines is to copy it to "e:/Data/Dropbox/My Dropbox". That’s it! After copying a file at home I find it available on my office machine when I get there.

Can it be any simpler than that?!

I even use Dropbox for transferring files from virtual to hosting machines until I get to making “shared folder” work.

7. HootSuite

Twitter is my main source of new information. Keeping an eye on what’s happening is a real “must” today. But being able to do so in 4 columns is an awesome thing!

8. Chrome – SendLink

I can send a quick mail containing current link with two clicks only (you do remember I always count clicks, right?), without having to actually type or copy anything. That’s fast.

9. Chrome – goo.gl URL Shortener

Another “one-click” favorite: URL shortened is copied to the clipboard when I hit extension’s button. Dropbox can be improved when doing the same – it’s a two-step process there:


Immediate social sharing and keyboard shortcuts are available.

Less is more and it’s nice to see how Googlers count clicks as well. I guess it’s bad we can’t go down any further from one click. Zero clicks! How about that ? 🙂

10. Chrome – Tweetings

While HootSuite is great for reading Twits – I use Tweetings for posting them. It’s quick and it remembers the text entered even if I switch the tab to grab a shortened URL. It changes color to notify me on Twitter “mentions” and “replies”, what a great little handy cute tool.

Update:
11. Online dictionaries: Yandex and Dictionary

It would be not fair to leave out on-line translators. As previously, keyworded searches are my friends here.

"tr anything":

"dic make":

That’s it!
Those were my 10 most favorite online tools making “living on the Web” very enjoyable and productive.

What are yours? I would love to hear.

Moving to a private WordPress hosting

March 22, 2010 Leave a comment

I think I will be moving to a private WordPress hosting within time.
The problem with WordPress.com is that it:

  • .. is annoyingly slow oftentimes.
       There were cases when “Update” operation just stuck for no reason and I had to repost
       the whole article.
  • .. doesn’t allow to install any custom plugin, theme or widget

I mean, it’s good as a free service but from all SaaS-es I’ve met recently – this one definitely needs to go more personal, so I’m giving DreamHost a try now. Any other recommendations may be?

But, of course, I wouldn’t like to lose WordPress.com community and auto-generated links service so I’m planning to repost everything here from my personal WordPress setup.

Categories: Web Tags:

evgeny-goldin.com reloaded

March 6, 2010 Leave a comment

Just uploaded a new design to evgeny-goldin.com.
Finally! (the previous one was just horrible)

Design was made by Inbar (http://imba.co.il/) and Sagit (http://www.sagit.co.il/) – a big THANK YOU, ladies!

Categories: Web

So .. Why Twitter?

January 23, 2010 Leave a comment

So .. Why Twitter? – my attempt to answer the question (surprisingly, still being asked!)
Please, vote up if you find it useful.

Categories: Web Tags:

Browser keyworded searches

January 9, 2010 2 comments

There’s one thing I’m (probably) using no less than a search in a browser and that is keyworded search. It surprised me one day that even some geeks are not familiar with it so here it goes …

The idea is simple – launch your favorite search using a keyword.

For example, to translate a word I’m typing “tr word” in browser’s address bar and go to translation site, “d git” takes me to my delicious “git” tag and with “you prodigy” I’m going straight to YouTube to watch some Prodigy videos.

It works amazingly fast and I usually have about a dozen of keywords set up.

You can set up a keyword in Firefox by right-clicking a search box and choosing “Add a Keyword for this Search”:

                  1

 

It’s a bit trickier to do in Chrome (which even made me think it’s not possible) – you right-click an address bar and choose “Edit search engines”:

          2

 

The way it works – one needs to specify a search URL in Chrome (for some sites Chrome is trying to guess and add a search engine automatically which I think is kind of stupid – the Firefox way is way better here, it doesn’t guess anything but provides a query URL for you so there’s no need to know it):

 

       3 

In order to know a query URL I typically search for “aaaa” and copy the resulting address (most searches are GET requests – keyworded searches don’t work with POST ones, why not having “Send POST request” checkbox?):

       4

 

                5

 

In the URL textbox I replace my lovely “aaaa” with “%s” and that’s it!

Some of my favorites:

Chrome is also displaying a search engine that is going to be used (after typing a keyword):

        6

    Categories: PC, Web Tags: ,