Nexus REST API Java tutorial and examples

nexus-small

Once again, a very good example of a good process very badly documented.

When you search for « nexus api example » in Google, the first result is: http://books.sonatype.com/nexus-book/reference/confignx-sect-plugins.html. Waw, for a single time, the first result is not Stackoverflow! I thought it was the beginning of a clear documentation. But it was not the case, at all.

When you follow the link, you have no further exaplaination than a screenshot leading you to your local Nexus installation (http://your.nexus.url/nexus/nexus-restlet1x-plugin/default/docs/index.html), the definition of a Maven dependency, and a link to a complicated GitHub project dealing with other things than the API itself. What? Seriously?


I had a simple requirement: being able to download an artifact that is on my Nexus, to the file system of a local machine. Maven does that evrytime for you, right? So, thanks to Google, Github and a few neurones, I came with a very small project that is hosted here: https://github.com/toni07/nexus-api-client

With these small sources, you will be able:

  • to find all the versions for a given artifact-id
  • to download a given Maven artifact to your local drive

(ps: do not try to search for a pure Java based API, I lost time with the « nexus-client-core » artifact, that contains deprecated methods, no Javadoc, and no examples. With this uncomplete API there is no method to download an artifact).

 

Intellij run error « source release X requires target X »

intellij-idea-logo

When you run projects from « main() » methods in Intellij, it compiles and runs your project, starting from your « main() » method.

If your project is not configured properly, Intellij can complain before running your « main() » method, with a message like that displayed in your console: « source release 1.7 requires target 1.7 ».

Solution: You must check the (Java) language level of your project, and the Java Compiler target version.

  • check the (Java) language level of your project:
  1. right click on the root of your project in the explorer
  2. click on « open module settings »
  3. in the dialog that’s been opened, go to the « Modules » tab
  4. you should see your settings as in the screenshot below:

intellij_module_settings

 

  • check the Java Compiler target version:
  1. Open the « File -> Settings » dialog
  2. The configuration is as in the screenshot below:

I figured out the last point thanks to Stackoverflow, once again. The solution was in this post: http://stackoverflow.com/questions/12900373/idea-javac-source-release-1-7-requires-target-release-1-7.

Typehead intercept selected value event

Typehead is a great script for html autocomplete components, is it quite pretty and easy to use. BUT it lacks of documentation about a very basic feature: how do you intercept the changes made by the user? How do you know which value the user has selected? This is almost the only thing you want to do with an autocoplete component. After some searches, I found this to be working well:

$('.typeahead').typeahead(/* pass in your datasets and initialize the typeahead */)
    .on('typeahead:selected', function($e, datum){
        console.log('the user has selected', datum, datum.value);
     }
);

Thanks to this link for the help: http://eric.sau.pe/using-twitter-typeahead-js-custom-event-triggers/

 

Edit after two hours working with this library: once again, a great library is missing basic functions! You cannot event manage your Ajax calls as you want! Let’s say you want to retreive a list of cities corresponding to a zip code, and autocomplete the cities according to a zip code, you cannot do the Ajax request yourself, you have to let the library do it! What that means is you cannot call the same function as everywhere else in your site, so you cannot put the same loading gif or whatever… Lame! Very lame!

Here’s a proof of how it’s hard to have a spinner: https://github.com/twitter/typeahead.js/issues/166. C’mon guys, a spinner! This issue really scares me. This SO post also scares me: http://stackoverflow.com/questions/18117476/bootstrap-3-typeahead-js-remote-url-attributes.

Okay, I stop using this kiddy library.

Hibernate SessionFactory building history

Hibernate

Hibernate changes fast, yesterday I upgraded from the version 4 to the version 5. To create a SessionFactory, I used this code:

sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());

But the ServiceRegistryBuilder object has been removed from Hibernate-core in the version 5. So here is how you should do now:

sessionFactory = configuration.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build());

Once again, many thanks to the Stackoverflow guys! In http://stackoverflow.com/questions/8621906/is-buildsessionfactory-deprecated-in-hibernate-4, you can see the way to create a SessionFactory has changed between v4.2 and 4.3, and then betweeen 4.3 and 5. As usual, not well documented (I consider when the first answer in Google is SO, then the official doc is lame).

Maven + Nexus « Return code is: 400, ReasonPhrase: Bad Request »

nexus-small

Today, I’m lame. I got this error while deploying an artifact in my Nexus from a Maven build. « What? It used to work and now it fails!? ».

The reason is: I tried to deploy a « snapshot » artifact in a « release » repository. Ok, I’m dummy, but the error could be a little clearer. « 400, bad request »: what does that mean? Once again, errors are really not well managed. Anyway, here is the modifications I made to my pom.xml:

<distributionManagement>
    <repository>
        <id>nexus-srv-releases</id>
        <name>Internal Releases</name>
        <url>http://nexus.local/nexus/content/repositories/nxs-releases</url>
    </repository>
    <snapshotRepository>
        <id>nexus-srv-snapshots</id>
        <name>Internal Snapshots</name>
        <url>http://nexus.local/nexus/content/repositories/nxs-snapshots</url>
    </snapshotRepository>
</distributionManagement>

I already had the « repository » tag, I added the « snapshotRepository » one.

Spring MVC – resources error

spring
There’s a bug in Spring MVC 4.2.1.RELEASE: https://jira.spring.io/browse/SPR-13420. I experienced it myself.
My spring-context-web.xml was as follows (sorry I had to delete the tags to display it here):

...
mvc:annotation-driven /
mvc:resources mapping="/app/**" location="/app/" /
mvc:resources mapping="/resources/css/**" location="/resources/css/" /
...

and when I tried to reach: http://localhost:8080/app/resources/css/common.css, I had a 500 response, with the error: java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getHeader.

Hopefully, Spring guys have fixed this bug in the 4.2.2.RELEASE (a few days ago, I’m feeling lucky!).

MongoDB authentication

mongodb-logo-large

Still learning MongoDB, today I want to stop connecting to my database without any security: no user, no password, no authentication… So I google « mongodb set password ». First answer: Stackoverflow. Wow, begins great! How to get the feeling the official MongoDB documentation is so weak that it is not even the first result in google for this search!

Anyway, here is the link for this question in stackoverflow: http://stackoverflow.com/questions/4881208/how-to-put-username-password-in-mongodb. I have to warn you, the answer is at least outdated, and maybe even wrong.

I found some documentation here that suits my version (3.0.5): http://docs.mongodb.org/manual/tutorial/enable-authentication-without-bypass/. It seems that once again, no one is able to put only the list of commands you should type to:

  • add a global user: the « super admin » of the all the DBs
  • create a DB
  • add a user to this DB, that will be able to CRUD it

So here is why I came with, seems to be working:

  • > (of course) start mongodb, with the command: cd [your_mongo_path]\bin\ && mongod –config mongo.config
  • start the command line client, in bin/mongo.exe
  • in the client console
    • > use admin;
    • > db.createUser({user: « superadmin », pwd: « superpassword », roles: [ { role: « userAdminAnyDatabase », db: « admin » } ]});
    • > db.shutdownServer();
    • > exit;
  • you have created the super admin, so disable all non logged connections when restarting you mongod:
    • $ cd [your_mongo_path]\bin\ && mongod –auth –config mongo.config
  • start the command line client again, in bin/mongo.exe
  • in the client console
    • > use admin;
    • > db.auth(‘superadmin’,’superpassword’);
    • it should print ‘1’ in the console if OK, otherwise it will throw you errors
  • then create a user for a given database
    • create the database: > use toni07db;
    • > use toni07db; /!\ don’t forget this, otherwise your user will be created in the admin DB, and even if you give his credentials for toni07db, he won’t be able to connect (wtf???)
    • > db.createUser({user: « test_user1 »,pwd: « test_password1 »,roles: [{ role: « read », db: « test » },{ role: « readWrite », db: « toni07db » }]})
    • > exit
  • then log in with this user => restart the client console
    • > db.auth(‘test_user1′,’test_password1’); -> fail, you are not in the right db
    • use toni07db;
    • db.auth(‘test_user1′,’test_password1’);
    • it should print ‘1’ (success)

 

 

Paypal tutorial

paypal

Back in the days (in 2012), I developped a website that used Paypal. It was quite a pain in the a**, as usual with this kind of site.

When I got back to it a few days ago, I realized things had changed a lot since 2012. First, to test the integration with Paypal, you need to play with the « Paypal sandbox ». Beforehand, you could register on this sandbox just by creating a developper account. Now, you need a REAL Paypal account to be able to connect to the Sandbox. Here we go…

  1. Go to https://www.paypal.com/singin
  2. Create a « presonal » account
  3. Don’t give your credit card numbers, just click on the Paypal logo on the page asking for your credit card numbers
  4. Go to the bottom of the page, click on the « developpers » link
  5. Your sandbox accounts are on this page (10 minutes to find this URL, what’s wrong with you, Paypal guys?): https://developer.paypal.com/developer/accounts
  6. BUT! You cannot login into the sandbox with these accounts (so what are they here for?)
  7. So, on the accounts page, click « create account »
  8. Create as many accounts as you want, then you can login in the sandbox (https://www.sandbox.paypal.com/) with those accounts

Now, to test payment on your website, you have to create 2 sandbox accounts:

  • one « buisness »
  • one « personnal »

Create those accounts. You can login to your fake business account on this link: https://www.sandbox.paypal.com/fr/cgi-bin/webscr?cmd=_account&nav=0, and see you have no money for the moment!

Grunt tutorial for Windows

  1. Install Grunt:
    1. update your npm: > npm update -g npm
    2. > npm install -g grunt-cli (This will put the grunt command in your system path, allowing it to be run from any directory. Note that installing grunt-cli does not install the Grunt task runner.)
  2. Prepare a new Grunt project:
    1. create a file called « package.json »
      1. this can be created automatically by « cd » to your working directory and then « npm init »
    2. create a file called « Gruntfile.js », populate it as in the website example (TODO)
    3. > npm install grunt –save-dev
    4. > npm install grunt-contrib-jshint –save-dev
    5. > npm install grunt-contrib-nodeunit –save-dev
    6. > npm install grunt-contrib-uglify –save-dev
    7. > npm install grunt-contrib-concat –save-dev
    8. > grunt

Install node.js on Windows

If you just want the Node Runtime (no npm for example):

  1. Go to https://nodejs.org/download/
  2. Download the 64-bits version of the .exe (or 32bits if your system is in 32bits)

If you want the full packages:

  1. Go to https://nodejs.org/download/
  2. Download the 64-bits version of the .msi (or 32bits if your system is in 32bits)
  3. Double click on the .msi file, it will install all the packages on your computer
  4. If you want to play with npm, you have to go to start panel -> Node.js -> « Node.js command prompt »