viernes, 14 de septiembre de 2012

Once upon a time CURL

Have you had the chance to work with REST???

Something that I've come to learn working with REST is that people working with it are fond of the CURL command line.

Wikipedia defines CURL as:

"is a computer software project providing a library and command-line tool for transferring data using various protocols."

Thing is, I really don't like it. Not because it's a bad tool but because I just don't want to learn yet another command line, let's face it I'm lazy. Any way this people I was talking about have so much faith in CURL that may come to demand you to post the output of a CURL execution to validate what ever you're saying about a REST call.

Providing all this I thought it may be worth to write a small post about the really basic things that you can do with CRUL.

Now the prototype of the command it's quite simple:


curl [options] [URL...]


The options portion is the one that really matters.
Here is a list that I found useful, but keep an eye on the post for I may keep updating it:

 -H  this is for header. Any valid HTTP may be used here.

It's useful  for things like Authorization.

-X this can be used for for HTTP verbs

Like so -X DELETE, -X POST


So this would be how a valid call looks like:

curl  -H "Authorization: Basic sdaqwrdsfdsfafdszadsafdsafdsfad3" -X DELETE http://somehost.com/something?queryparam=somethingelese

Any way, for now this suits me but would you need more data please refer to:


Of course over there you'll find all there is to find about CURL, but I just like to post here things that I tend to forget.

Enjoy





viernes, 7 de septiembre de 2012

Retry Strategy Implementation

Hi, this small portion of code is to be used when ever you need to perform a task that may fail and in which event you should retry a predefined amount of times.

I won't be explaining the code in detail but if you have questions please to comment :D.

RetriableTask Class:


public class RetriableTask<V> implements Callable<V> {

    private static final int DEFAULT_RETRY_ATTEMPTS = 3;
    private static final int DEFAULT_TIME_BETWEEN_RETRIES = 1000;

    private Callable<V> task;
    private int retryAttempts;
    private int timeBetweenRetries;

    private int retryCount;

    public RetriableTask(Callable<V> task) {
        this(task, DEFAULT_RETRY_ATTEMPTS, DEFAULT_TIME_BETWEEN_RETRIES);
    }

    public RetriableTask(Callable<V> task, int retryAttempts, int timeBetweenRetries) {
        this.task = task;
        this.retryAttempts = retryAttempts;
        this.timeBetweenRetries = timeBetweenRetries;
        this.retryCount = retryAttempts;
    }

    @Override
    public V call() throws Exception {

        while (true) {
            try {
                return task.call();
            } catch (InterruptedException e) {
                throw e;
            } catch (CancellationException e) {
                throw e;
            } catch (Exception e) {
                retryCount--;
                if (retryCount == 0) {
                    throw new RetryException(retryAttempts + " attempts to retry failed at " + timeBetweenRetries
                            + "ms interval", e);
                }
                Thread.sleep(timeBetweenRetries);
            }
        }
    }

}


How to use the class above:

Put this code in a method of your choosing. Of course this can be perfected and used in such way as to change what ever task you want to perform in runtime.

Callable<T> task = new Callable<T>() {
@Override
public T call() throws Exception {
    return // WHAT EVER YOU NEED TO ACTUALLY DO;
        }
};
       
       
String response = "";
try {
    response = new RetriableTask<String>(task).call();
} catch (RetryException e) {
    // THIS WILL BE CATCH WHEN:
    // The call to the task fails and all the retries were exausted

    // So do what ever you need to do when your operation fails:
    // HERE
} catch (Exception e) {
    // If you reach this point something else
    // besides the actuall retry failed
    throw new RuntimeException(e);
}

Attach existing directory to a new GIT Hub Repo

So........

This one may sound familiar to you right?

You started working on something and at some point some one tells you: "oh!!!! brilliant upload it Git Hub so I can take a look at it"

And ad that point is when the problem starts.

So how do you update code you are working on (this is an existing project) to a repo you are about to create.

Well this is basically solved in StackOverflow BUT allow me to replicate it here ok?



1. Create the remote repository, and get the URL such asgit://github.com/youruser/somename.git

If your local GIT repo is already set up, skips steps 2 and 3

2. Locally, at the root directory of your source, git init

3. Locally, add and commit what you want in your initial repo (for everything, git add . git commit -m 'initial commit comment')to attach your remote repo with the name 'origin' (like cloning would do)

4. to attach your remote repo with the name 'origin' (like cloning would do) git remote add origin [URL From Step 1]

5. git pull origin master

6. to push up your master branch (change master to something else for a different branch): git push origin master

So this worked pretty well for me to have a look at it.

I case you wander this is the StackOverflow link

martes, 14 de agosto de 2012

Complex types & Cloud connectors with Mule ESB

Hi there, have you ever worked with Mule ESB?

Quite nice, free and open source ESB that you should really check!

Now they have this concept of CloudConnector, which is basically an easy plug and play way to make you ESB talk to other service's API. Here is a list of all of them as of today.

Now this post is not about how to use nor create a  CloudConnector. This is just a short post about how to deal with a  CloudConnector when its methods returns Complex Types.

A Complex Type it's nothing else than a POJO, BUT the fact that it's a POJO make it some how a little more complex to use it the first time.

Why????

Well because the output of any call to a  CloudConnector it's something that you can not use out of the box just because you don't know what does it expose, I mean the getters of the object.
And the problem here is that when you are modifying a Mule configuration file (which is the way to program Mule ESB) there is no autocomplete like in Eclipse to help you.


So, now that I've stablished the problem I shall offer you the solution.
Mule ESB has native support for Groovy scripts, which allows you to access any object.

So let's take for instance a call to the LinkedIn Cloud Connector they offer:


<linkedin:get-profile-for-current-user config-ref="LinkedinConf"/>

When set up properly this should return you data from the profile of the currenent user. And it does but it does so by returning a POJO.
So if you want any useful information from it you should access its getter methods. BUT WAIT I DON'T KNOW THE CODE OF IT, even more important I don't have it.

And that's where Groovy comes in handy, for this will tell you what you can ask for this POJO returned:

<linkedin:get-profile-for-current-user config-ref="LinkedinConf"/>
<logger message="#[groovy:payload.getClass().getDeclaredMethods().toString()]" level="INFO" doc:name="Logger"/>


When put together this to tags in you Mule config file you'll be able to check all the methods exposed by this POJO and hopefully find something you can use.

Of course this is nothing more than the use of java reflection but for a newbie in both Java and Mule ESB this post my help you.


So that's it, have fun.

martes, 7 de agosto de 2012

Working with ZIP files in memory

Long time no see right?

Today's topic is ZIP files. I'm going to tell you how to work with ZIP files in memory.

The use case comes handy when your code has no access to the actual file persisted in the file system. The most common scenario, I think, it's when a file it's being uploaded or transferred  through any end point.

As it turns out Java has quite a lot of library build in in its java.util.zip packages to make this magic happens (kind of logical it's in the zip package eh? I just DIDN'T see it before :P ).

These are the main classes we are going to be using:

  • java.util.zip.ZipEntry
  • java.util.zip.ZipInputStream
  • java.util.zip.ZipOutputStream
As you may already notice, if we can access the actual file we should have access to something right?
Well that something it's an InputStream, not any input stream of course but a ZipInputStream, so lets see how to create one out of any common InputStream:

InputStream zipFileInputStream;
ZipInputStream inputZipFile = new ZipInputStream(zipFileInputStream);

Pretty easy, right? 
Of course, bear in mind that the use case here assumes that you have access to an input stream, could be the actual file, an http etc. The point is that ones you have the input stream you won't have to use anything else to mange the ZIP file.

Now we are going to see two use cases that may come handy, first to read a particular file/s inside a ZIP file and second how to add content to the ZIP file.

Let us begin with the first use case how to get a particular file from inside the ZIP file:

ZipEntry entryFile;
try {
  entryFile = inputZipFile.getNextEntry();
  while (entryFile != null) {
    String[] nameAndExtention = StringUtils.split(entryFile.getName(), ".");
    if (nameAndExtention.length >= 2) {
      String extension = nameAndExtention[nameAndExtention.length - 1].toLowerCase()
      if (extension.equals(FILE_EXTENTION)) {        

        int n;
byte[] buf = new byte[1024];         

        ByteArrayOutputStream tmpOutStream = new ByteArrayOutputStream(1024);

while ((n = inputZipFile.read(buf, 0, 1024)) > -1) {
          tmpOutStream.write(buf, 0, n);
}

fileList.add(new String(tmpOutStream.toString()));
    }
  }
  entryFile = inputZipFile.getNextEntry();
 }
} catch (IOException e) {
  e.printStackTrace();
}

So as you can see the first thing we use here is the ZipEntry class. This is what the ZipFileInputStream returns when we iterate through it. It represents a file inside the the ZIP file and its related metadata.
So from this point onwards it's quite simple you'll j just need to play with the ZipEntry API to get what you need.
In my case I was looking for files with a certain extension(word of advise though the getName method returns the canonical name of the file).

So once you selected the files you needed you may want to read them right?
As you may have guessed by now this is achieved by this code:
        int n;
byte[] buf = new byte[1024];         

        ByteArrayOutputStream tmpOutStream = new ByteArrayOutputStream(1024);

while ((n = inputZipFile.read(buf, 0, 1024)) > -1) {
          tmpOutStream.write(buf, 0, n);
}

fileList.add(new String(tmpOutStream.toString()));

And the first question should be but how does the ZipFileInpuStream which file to read and the answer it's easy. 
When you do:

inputZipFile
.getNextEntry()

This works as a pointer for the ZipFileInpuStream, and internally position the pointer to the beginning of the entry it just returned to you. So when you do getNextEntry the pointer moves forward to the next entry. 
Thus when you do:
inputZipFile.read

You are going to be reading until the end of the entry you are on, this is the end of the file you selected.
Finally I just do a tmpOutStream.toString() but because the files I work with are text based files.

In this way you can read the content of any file inside a ZIP file.

Now for the second use case,  add content to the ZIP file.
The idea it's pretty much the same BUT you'll have to do something like this:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

ZipOutputStream zipOutputFile = new ZipOutputStream(outputStream);


ZipEntry newZipentry = new ZipEntry("some_name");
zipOutputFile.putNextEntry(newZipentry);
zipOutputFile.write(newFileContent.getBytes());


As you can see you first need to create an output file first.
So you'll say "BUT YOU SAY ADD CONTENT TO A ZIP FILE" well we ARE doing that it's just a matter of creating your zipOutpuFile from an original content.


Any way I hope you've enjoy it.
Also take a look at the following web sites I used to code this:







miércoles, 18 de julio de 2012

Run Single Test in Maven

If you guys read the reason I started this blog, you'll recall  that it was just to summarize stupid stuff one always forget when coding.
And so here is one of them, how to run just a single test from the command line with maven.
Well let me just C&P the data from the Apache Maven site


To run this through Maven, set the test property to a specific test case.
mvn -Dtest=TestCircle test
The value for the test parameter is the name of the test class (without the extension; we'll strip off the extension if you accidentally provide one).
You may also use patterns to run a number of tests:
mvn -Dtest=TestCi*le test
And you may use multiple names/patterns, separated by commas:
mvn -Dtest=TestSquare,TestCi*le test
I know I know this has not actual merit but hey think of this blog as place were you can find this kind of things because, except from one or two original things this is what you'll find.

martes, 17 de julio de 2012

Stream of bytes

Today's post is a short one.

So I was playing with streams, and by playing I mean I had to deal with them at work :P

Any way, I do know that unless you are absolutely sure about the content of the stream you should treat them as bytes always, but I made the mistake to handle them as string.
Any way that lead me to find Apache Commons IO, in particular IOUtils.

As it turns out I needed to read all the content from an HTTPInputStream and then returned as byte array.
For those of you who don't see the problem here, the thing is that it's a pain to read something of an input stream at place it in an array. Just to much code for something so simple. Even more if it's a byte array.

Any way a good friend of mine introduce me to this portion of the Apache common library.
(Do take a look at his blog it's just awesome)

And as a result here are the two lines of code that you'll need to do just that i.e. read from an input stream and place the content in a byte array:


import org.apache.commons.io.IOUtils;

return IOUtils.toByteArray((InputStream) yourObjecStream.getStream());


So that's it, please note the casting of the yourObjectStream is just a copy paste hehe.
Hope you enjoy it.