Managing your BlueMix applications in Slack with Cloudbot

bluemix-slack

Slack has become a very popular collaboration tool for developers and designers. Think of LinkedIn groups combined with a Facebook or Twitter feed that is isolated to only the team members participating. It is equivalent to an on going persistent eMeeting chat that never ends, is searchable, and most of all, captures all conversations around a topic in a single place. Some argue Slack is the end of email and other collaboration techniques, specifically around projects.

In this video Yu Cao demonstrates how to setup Cloudbot for slack where you can then chat with the bot to get information about your your BlueMix applications.

“Cloudbot is a ChatOps bot platform built to be deployed on IBM Bluemix that integrates services and tools into a development or operations team’s workflow in a collaborative chat environment.” – link

 

Advertisements

Enabling CORS for WebSphere Commerce REST

Commerce-CORSI have two servers, one application server (JSP based) and one WebSphere Commerce server. I want to be able to call the REST API’s on the Commerce server via JavaScript served up by the application server. So my JavaScript would look like this:

$.ajax({
      url: myCommerceServerRestURL,
      method: "GET",
      crossDomain: true,
      contentType: "application/json",
      dataType: "json"
 })

The problem is, if CORS is not enabled on the WebSphere Commerce server you will receive the infamous No ‘Access-Control-Allow-Origin’ header is present on the requested resource. And the call fails.

Now WebSphere Commerce can be a little tricky if your server has a web server in front of it. You essentially have to enable it in both places – or at least that is what I had to do. If I only had to do one of these please let me know – I got tired of testing the different tips from the netverse.

I have IHS in front of WebSphere Commerce, so, after hours of playing around with different configurations I came up with these steps:

First configure httpd.conf in the IHS/conf directory and add these lines  if you want all domains to be able to access your data:

 <IfModule mod_headers.c>
        Header set Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Methods "GET, PUT, OPTIONS"
        Header set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
 </IfModule>

Remember, you can always override the Access-Control-Origin with a domain or multiple domains to prevent access for everyone.

Next, you have to update the WebSphere Commerce server. You can actually see the Commerce Insights enabling documentation for this (my colleague pointed me to this, thanks J).  You will be editing the WC configuration file and the WC Search configuration file.

 <_config:configgrouping name="HttpSecuritySettings_Rest">
          <_config:property name="CORSAccessControlAllowOrigin" value="*"/>
          <_config:property name="CORSAccessControlAllowMethods" value="GET, HEAD, POST, OPTIONS, PUT, DELETE"/>
          <_config:property name="CORSAccessControlAllowCredentials" value="true"/>
          <_config:property name="CORSAccessControlAllowHeaders" value="Origin,X-Requested-With,Content-Type,Accept,Authorization,cache-control,expires,pragma,wclogonid,wctoken,wctrustedtoken,wcuserid,X-RequestId"/>
          <_config:property name="CORSAccessControlExposeHeaders" value="Access-Control-Allow-Headers"/>
          <_config:property name="CORSAccessControlMaxAge" value="3600"/> 
 </_config:configgrouping>

 

If you found this useful follow this site! I also have a YouTube channel for IBM Commerce.

Using Watson Translation service in an Eclipse SWT application Part 2

The next follow-up to the last article is the code I used to communicate with the Watson Translation service. But first I want to show the flow of the data:

So basically the SWT client sends a JSON string to the server in the body of the message. You can do this as long as you specify application/json as the Content-Type. For JSON, I use the JSON library for Java over at json.org and its been perfect. I have even begun using this library for all of my in memory objects because then I can easily serialize it out for storage or over the network.

Below you will see the primary code to send this message:

JSONObject payload = new JSONObject();
                	
payload.put("toLang", obj.get("to"));
payload.put("text", obj.get("text"));
                	                	
URL url = new URL(pet_bluemix_translation_service);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(payload.toString());
out.close();
                         
//Now get the response
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                	
String n = in.readLine();
StringBuffer sbValue = new StringBuffer();
                	
while(n != null){
	sbValue.append(n);
	n = in.readLine();
}
in.close();
                	
final String value = sbValue.toString();

Allowing CORS from a Node-Red Bluemix application

I wrote a rating application some time ago in Node-Red and have since needed another similar application but with slightly different behavior. So I went ahead and created a new Node-Red application on Bluemix and decided to start from scratch since it was so simple to set up. This application has three requests – Get Rating, Post Rating, and Post Comment. Here is the current flow:

Image (1) rate-flow-680x422.png for post 5391

The problem I had was the requests actually respond with JSON and I could not figure out how to enable CORS!

The first thing I did was in the Image (2) cors.png for post 5391 node I added the following to the response:

Image (3) cors2.png for post 5391

And that did not work. Then, I stumbled upon this in my Cloudant dashboard in the Account Section under CORS:

 

Image (4) cloudant_admin-680x212.png for post 5391

Once I checked all domains it worked fine! I am sure I can restrict the domains appropriately but I guess I don’t understand why the CORS options are under the Cloudant section and not the base application somewhere. I would think this is a specific thing to hitting the Cloudant service directly, not through a Node-Red Node. I am very interested in hearing others opinions on this.

Update:

After using the Cloudant UI to update my profile I went back and checked the bluemix-settings.js saw it indeed added the httpNodeCors line to the profile:

httpNodeCors: { origin: “*”, methods: [‘GET’,’PUT’,’POST’,’DELETE’] },

So this is clearly the issue as I saw a similar question on developerworks but saw no example of the syntax.

Searching a Cloudant DB search index with Node-Red

After much searching the internet and seeing multiple posts about this subject I can only think that many others are greatly confused with the use of the Cloudant In node in Node-Red. I think my problem was terminology and not being familiar enough with the Cloudant nomenclature more than anything and the user interface in Node needs a little more work.

I basically want to return all documents where name=XXXXX. My document is pretty simple, here is an example:

review-doc

So lets start by looking at my index definition:

cloudant_index

Now you will notice I have three references to “namesearch”:

  1. The design document name is “_design/namesearch”
  2. The Index name is “nameSearch”
  3. The index in the search index function is also named “nameSearch”

My flow takes in an HTTP request, formats it with the Selector node, and passes the query on to the Cloudant In node:

flow

The selector node is pretty basic, after following many different instructions I finally resolved the problems with this function:

selector

This results in a payload that looks like this:

nameSearch:Create a category layout

The Cloudant In node now has the following configuration (which seemed to be the primary problem, I will explain later:

cloudantin

The problem I had was I could not future out the search index fields. Mainly because this is the form when no fields are filled in:

designdoc

Notice it specifically says “design document“. Now, anyone that is not familiar with this, based on my configuration, would most likely fill it out like this:

node-red cloudant in

But after scouring many documents on the internet a suggestion was to just remove the “_design/” from the first field. Which resulted in the correct version I have above:

cloudantin

Needless to say this actually took me hours to figure out because I thought it was something with the query being passed in!

If you are not familiar with Node-Red then click this link.

BlueMix URL Rating widget on WebSphere Commerce for product ratings

bluemix-ratings-wc

Ok, this is really just a teaser post as I will divulge the process used to get my Blue Mix URL rating widget into the WebSphere Commerce Composer framework a bit later.

I will say, now that the widget is in a real application I was able to test out some more use cases and fix a few bugs to make sure the widget is re-usable across a site. In this use case I am using it to rate products. I made a few changes to the NodeRED flow including adding more data saved for some future ideas I have. Here is the current block of data saved in the MongoDB for each rating received:

{
 "url": "http://mytestserver.com/webapp/wcs/stores/servlet/en/aurora1/electronics/tablets-10229--1/budget-tablet",
 "rating": 4,
 "host": "mytestserver.com",
 "cookie": "1406562990550",
 "date": "Wed, 30 Jul 2014 18:31:35 GMT",
 "_id": {
 "$oid": "53d93a07cb353621005aaadf"
 }
}

Notice I added “date“, “host” and “cookie” to the output. The date is actually filled in by the server for consistency and the cookie is a time stamp saved in the shopper browser for the page using HTML5 storage. I figure I will be able to use this data for reporting later and possibly as a WebSphere Commerce precision marketing action to get me a list of “top products” for a given domain.

The other change I had to make was to allow my service to respond with cross domain calls. I just put a little JavaScript between the post request and response nodes and added “Access-Control-Allow-Origin” to the call. I could probably isolate this to the specific domain eventually.

Cross Domain Setting

The next addition I made was if the browser already rated a product (cookie + URL) is already stored into the database send back a piece of data in the response of the “getRating” call to notify the browser to not let that person vote again. I really like how I can just connect nodes together conditionally and essentially introduce some complex decision making flows into the mix. Here you can see my decision “Check for cookie” and then ultimately append the flag in the “Check for already voted” function:

already voted logicHere is the Check for already voted function:

already voted code

Continue reading

URL Rating application on BlueMix – Part 2 – the user interface

starsThe next step in my Node-RED application on BlueMix is to create some user interface that calls the services I defined in my Node-RED flow sheet. If you recall from the last post, I created two services:

  • /postrating – which will post a rating for a given url
  • /rating – which retrieves the average rating for the url

For now, I will use the index.html to test the user interface. I want to make it dead simple for someone to use this in the end so the only need is to include the javascript file and define a div with an id of “urlRate”, like so:

[codesyntax lang=”html4strict”]

</div> </div>

[/codesyntax]

In this next video I create a basic jQuery user interface that allows the user to click on a star and register their rating for the given page. There are no checks and balances in the application as of yet – ie. I can click as many times as I want so I can test my services.

Continue reading

Creating a URL Rating Application in Node-RED on BlueMix – Part 1

starsThis is the first part of a new application I am creating using Node-RED on BlueMix. With very little programming, I show how you can get a basic URL rating application up and running very quickly using Node-RED. In this first part I focus on the two primary web services, posting a rating and getting a rating for a URL.

Continue reading

Today’s Bluemix Virtual Meetup Live On YouTube and Google+ starts in 5 minutes!

Originally from Ryan Baxters blog here.

You can watch today’s Bluemix Virtual Meetup on mobile cloud services live at 10 AM EST on YouTube.  There are 3 options for watching.

  1. Watch out on the YouTube page.
  2. Watch on the Google+ page.  If you watch on Google+ you will be able to ask questions.
  3. Watch right here

Node-RED boilerplate on #BlueMix now uses Cloudant NoSQL DB

Not sure if anyone noticed this but the Node-RED boilerplate application on BlueMix now uses the Cloudant NoSQL database service for storage. The beta version used Mongo:

CLOUDANT