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.


jueves, 12 de julio de 2012

XML & JDom

Hi there!!!!
So todays subject is XML parsing.
Right, you'll say this is a common thing, who didn't work with XMLs.

And you are right, but, as it turns out my line of work didn't usually take me to work with xml files.

Thing is I needed to take an XML string and just add it a new node.
This example it's quite simple except in my case I needed to use name spaces, for the xml file used them.

So with out further due here it is:


SAXBuilder builder = new SAXBuilder();

Document document;

document = builder.build(new ByteArrayInputStream(xmlMessage.getBytes()));


Namespace atomNS = Namespace.getNamespace("atom", "http://www.w3.org/2005/Atom");
Namespace cmisraNS = Namespace.getNamespace("cmisra", "http://docs.oasis-open.org/ns/cmis/restatom/200908/");
Namespace cmisNS = Namespace.getNamespace("cmis", "http://docs.oasis-open.org/ns/cmis/core/200908/");

Element newElement = new Element("propertyString").setAttribute("propertyDefinitionId", "aNameSpace:uuid");
newElement.setNamespace(cmisNS);


Element newValue = new Element("value").setNamespace(cmisNS).addContent(uuid);
newElement.addContent(newValue);

Element rootElement = document.getRootElement();
rootElement.getChild("object", cmisraNS).getChild("properties", cmisNS).addContent(newElement);



As you can see there is no much new here, even more if you compare it to this site that I used as a starting point.
The only difference is that as my XML has namespaces you must use them in order to JDom parse the document correctly. This is, to get the actual node you are looking for.

In the same way when you create your new elements/nodes you must specify the name space that should be used.

martes, 3 de julio de 2012

EasyMock & Matchers

Hi there!

If you are down this rode chances are you were looking for something related to EasyMock right??
In short (for those who don't know) EasyMock is a mocking framework, makes sense ah?

I suggest you check mocking in Wikipedia, you are lost at this point.

For the rest of you here is the actual point of this post.
Haven you ever find your self mocking a method call where you know ALMOST ALL the values of the parameters that the method would be receiving. Please note that I set almost.

Well thing is, for those values we don't know Easy Mock provide us with matchers. It is a way to say "hey you're going to be receiving, let's say, a String".
Don't really know which one, don't know the content of it and I JUST DON'T CARE.

Here is how you can do that:

The method firm: public void myMetod(String firstStr, String secondStr)


Easy mock call: EasyMock.expects(obj.myMethod("", EasyMock.anyObject(String.class)); 

Now this makes sense right!!!!
BUT IT DOESN'T

You see, although I'm not laying to you, and Easy Mock does allow you to do such things the above it's not the correct way.
EasyMock allow you to use matchers in your method call BUT
Either all the parameters are passed through Matchers or not. You can't have mixed things as before.

So here are two valid calls:

EasyMock.expects(obj.myMethod(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class)); EasyMock.expects(obj.myMethod("", ""); 
Now all this brings us to the main problem:

What if I know some of the value an I don't know some of them. More important I don't know some of the values but at least one must be a particular value.

Well Easy Mock does also provide you with a way to do that, by the use of the following matcher:

EasyMock.eq("");

So the invalid call above should turn into this:

EasyMock.expects(obj.myMethod(EasyMock.eq(""), EasyMock.anyObject(String.class)); 

In this way you can keep on mocking.

For those of you that may be googling this article the exception thrown when doing this is:


java.lang.IllegalStateException: 2 matchers expected, 1 recorded.

Any way, I hope you have enjoy this!
Please also check this article that help me to find out this

viernes, 15 de junio de 2012

Maven offline

Right so this one time at band camp.... (really don't get it??? American Pie 1 OMG!!!)

Ok moving on, I have this maven project which has like ALL THE DEPENDENCIES IN THE WORLD.

And every time I build it it start downloading all of them. Problem is 99.9999999% of the time they don't change.

How to avoid that overhead?
mvn -o
The -o modifier tells maven not to go look for the dependencies and to use the ones it already has.

Now we're on the subject I found this cool page that list all the modifiers. Nothing you could't do with a  mvn --help, never the less I need to keep posting you know.

So here it is the list of modifiers:


usage: mvn [options] [<goal(s)>] [<phase(s)>]

Options:
 -q,--quiet                        Quiet output - only show errors
 -C,--strict-checksums             Fail the build if checksums don't match
 -c,--lax-checksums                Warn if checksums don't match
 -P,--activate-profiles            Comma-delimited list of profiles to
                                   activate
 -amd,--also-make-dependents       If project list is specified, also
                                   build projects that depend on projects on the list
 -ff,--fail-fast                   Stop at first failure in reactorized
                                   builds
 -rf,--resume-from                 Resume reactor from specified project
 -fae,--fail-at-end                Only fail the build afterwards; allow
                                   all non-impacted builds to continue
 -B,--batch-mode                   Run in non-interactive (batch) mode
 -am,--also-make                   If project list is specified, also
                                   build projects required by the list
 -fn,--fail-never                  NEVER fail the build, regardless of
                                   project result
 -emp,--encrypt-master-password    Encrypt master security password
 -ep,--encrypt-password            Encrypt server password
 -up,--update-plugins              Synonym for cpu
 -N,--non-recursive                Do not recurse into sub-projects
 -npr,--no-plugin-registry         Don't use ~/.m2/plugin-registry.xml for
                                   plugin versions
 -gs,--global-settings             Alternate path for the global settings
                                   file
 -U,--update-snapshots             Forces a check for updated releases and
                                   snapshots on remote repositories
 -cpu,--check-plugin-updates       Force upToDate check for any relevant
                                   registered plugins
 -npu,--no-plugin-updates          Suppress upToDate check for any
                                   relevant registered plugins
 -V,--show-version                 Display version information WITHOUT
                                   stopping build
 -D,--define                       Define a system property
 -X,--debug                        Produce execution debug output
 -e,--errors                       Produce execution error messages
 -f,--file                         Force the use of an alternate POM file.
 -h,--help                         Display help information
 -o,--offline                      Work offline
 -pl,--projects                    Build specified reactor projects
                                   instead of all projects
 -r,--reactor                      Dynamically build reactor from
                                   subdirectories
 -s,--settings                     Alternate path for the user settings
                                   file
 -v,--version                      Display version information

jueves, 14 de junio de 2012

HTTP POST request with telnet

So I keep writing here all this things I'm force to do in order to tests things.

So this time I was require to reproduce an error in an environment other than local, and the only way was to send a HTTP request over telnet client.

Long story short this is the form of the request you should type after you are connected to the server:



POST /some-path-to-a-server-resource HTTP/1.1Cache-Control: no-cacheConnection: trueContent-Type: application/atom+xml;type=entryKeep-Alive: trueTransfer-Encoding: chunked
5;ext-name=ext-val012340
Now, the parts in red are the important ones.
Please note that this particular request is using HTTP 1.1. The HTTP 1.1 specification defines two ways to post information. The first one is the same as in HTTP 1.0 which require a content length parameter in the header.
The other one is Transfer Encoding chunked this tells the server that the content is going to be sent in waves of information.
Bear in mind this last thing otherwise the web server may answer your request with an error code other that the old and nice 200.

Not much this but, toke me a good half hour to find how to do it.
Enjoy it.

GIT stash

So have your tried GIT, it's superb!!!!!
Though some people have encounter feelings about it like this friend of mind that says "it's something they invented to make all the rest fell stupid".

Any way, one nice thing about GIT it's is stash command.
In short let's say you are working on one branch and you have to do a quick fix on your master branch, BUT you're not yet ready to commit your changes.

Stash to the rescue......
This functionality allows you to put your changes aside, WITHOUT loosing them and without needing to commit them.

How it works:

This will stash your changes
git stash save

This will list your stash changes

git stash list

This will recover your stash changes

git stash apply your_stash_name 

This will from your stash changes

git stash drom your_stash_name

where your_stash_name is listed when you run the stash list.

I understand that the concept is somehow weird but I can tell you it works just fine.
As usual I wasn't born with this knowledge so this is the brilliant page that I used when I run into this.

Hope you guys find it usefull.

miércoles, 6 de junio de 2012

How to define system variable in Mac OS

Another tricky business....
We all know how to define a system variable in Mac OS we just do:

set MY_VAR=varcontent
Now the down side of this is that when you close the console ( or the tab console), or just switch tabs the variable doesn't exists any more right????


Well you are free to google the explanation for that ( is not to hard to see though).
Any way here is how you should do it if you want the variable to survive you console:

+ Go you your home path
+ With you favorite text editor change (or create) the file ".profiles"
+ In there add you variable

And presto!!!!!
From now on the variable/s listed there will be available in each console you open (only for your user of course).

Skiping Test with Maven

This is another oldy but goldy that we always forget how to do and end up googling after the tenth run of our project:
how to stop Maven from runnig test each freaking time????


And here is the answer, just add the following parameter to your mvn command:

-DskipTests
And, as always be happy.

VM Arguments in MULE ESB

Right this is one of those things that many consider tricky, and why not I mean is not like they have a whole page about this.

Any way if you work with Mule ESB one of the thing you may find your self trying to do is to run it from the command line. No eclipse plugin no Studio no nothing.

How do you pass VM arguments to the app running inside you Mule instance?
Well look no more for here is the answer:

mule -M-Dyour.envvar=envvar_value

As you can see it's the same as running any Java app from the command line BUT the folks at Mule change it a little bit adding the "-M".

And that's it.

Jetty Remote Debug

So did you ever had to do that???? This is, you need to run you app in Jetty and debug the freaking code. Well this small snippet of code take it from this other great blog should do the trick.
export MAVEN_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n"
As you can see you just set an env property and you are good to go. Just run
mave jetty:run
And be happy. Bear in mind that, with this particular setup, your jetty will receive connections in port 4000. Feel free to change it at your convenience.