Friday 21 December 2012

Set up x11vnc on ubuntu 12.10 as a startup item

I just installed a brand new instance of Ubuntu 12.10 and was trying to get my x11vnc to be a startup application.

In order to acheieve that, I first had to set up a password with the following command:

x11vnc -storepasswd

After that you enter a password.

Now click on the Windows icon and type 'startup'. From there enter a new command as a new task:

x11vnc -usepw

Voila!

Thursday 20 December 2012

Drupal 7: Not being able to see front page content

I'm new to Drupal and to be honest it's not my first choice of a CMS, but I have a low budget client so I have to use it. It sucks because in part I don't actually have to do too much coding and in part because there are so many configuration options.

For example, I was trying to enable the home page of the app so anyone can view it. However that didn't seem possible initially. Then I heard of the Access Control plugin, which I installed and had configured it correctly, but it still didn't work! What gives??

It turns out that there is a overriding flag that restricts the access of published content. To fix this, goto People -> Permission -> View Published Content, and check the Anonymous checkbox. Then the access control plugin will work.


Change your fonts in Intellij to make it look like Eclipse!

I've been working with Intellij for awhile now and I must say that for Groovy/Grails development it beats the default Eclipse or STS IDEs.

The one quirk (up until now) that I have is that the fonts look horrible. Well no more.

Thanks to http://722.kalaari.net/b/lang/fr/2012/09/30/improve-font-rendering-under-linux-for-your-favorite-java-ide/ we now can change the fonts to look like Eclipse.

Set GTK+ in appearance, and
export this: export _JAVA_OPTIONS="-Dawt.useSystemAAFontSettings=on -Dswing.aatext=true"

Ubuntu 12.10 - Kernel driver not install (rc=-1908)

I keep getting this error when I try to run virtual box Please install the kernel module by executing 'etc/init.d/vboxdrv setup' the solution: sudo apt-get install linux-headers-3.5.0-21-generic sudo apt-get remove virtualbox sudo apt-get install virtualbox sudo apt-get install virtualbox-ose-dkms

Wednesday 19 December 2012

Grails - Integration testing with the REST plugin using SSL (HTTPS)

I was writing a few integration tests in a Grails application which used the REST plugin to call out to an HTTPS (SSL) endpoint. In Config.groovy, we had this defined:

rest.https.truststore.path='truststore.jks'

rest.https.truststore.pass='asdf'

rest.https.keystore.path='keystore.jks'

rest.https.keystore.pass='asdf'

rest.https.cert.hostnameVerifier = 'ALLOW_ALL'

rest.https.sslSocketFactory.enforce = true



I knew for a fact that this works because when I run the test individual it is fine. However, when I run it via the command

grails test-app integration:



However, I keep getting errors while running the test.


Upon looking at the source of the plugin, I realized that the plugin is still using the ConfigurationHolder class for reading its configuration, which has been deprecated since version 2.0.0. It seems that the context inside the ConfigurationHolder object is not the same as what's in the grailApplication variable.

So to fix the issue, I had to declare the values declaratively (unfortunately).

   @Before
    public void setUp() {
        //need to override as the rest plugin isn't using the grailApplication variable.
        ConfigurationHolder.config.rest.https.truststore.path='truststore.jks'
        ConfigurationHolder.config.rest.https.truststore.pass='asdf'
        ConfigurationHolder.config.rest.https.keystore.path='keystore.jks'
        ConfigurationHolder.config.rest.https.keystore.pass='asdf'
        ConfigurationHolder.config.rest.https.cert.hostnameVerifier = 'ALLOW_ALL'
        ConfigurationHolder.config.rest.https.sslSocketFactory.enforce = true
        super.setUp()
    }





The ideal way to fix this is to monkey patch the rest plugin to use the grailsApplication. However I didn't want to deal with the headache of a custom patched plugin in our project.

Sunday 9 December 2012

Troubleshooting PHP XDebug

It's been awhile since I set up XDebug for PHP.

I wanted to do it so I can step through some code one of my clients have prepared.

I found that in general it was hard to set up debugging, but it sure saves you a ton of time while troubleshooting. Anyways it seems that the installation guide provides you with 80% of what you need, but leaves 20% out.

Note that the installation instructions are for Ubuntu 12.10

To get the first 80%: Follow install instruction located below
http://xdebug.org/docs/install

To get the last bit working: I came across a few issues.
First was that php cli didn't see xdebug. This isn't a problem if you're using the apache php, but to complete and not run into problems in the future, solve this problem paste the following a new file /etc/php5/conf.d/xdebug.ini

; xdebug debugger
zend_extension="/usr/lib/php5/20100525/xdebug.so"

xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
PHP will automatically scan that conf.d directory and pick up the config. Restart apache, and check that phpinfo() has the correct info.
Do a Ctrl + A and paste it into checker wizard: http://xdebug.org/wizard.php
Make sure there aren't any problems with it.

The second issue was related to my IDE. I was using PHPStorm and you actually have to create a server first by going to File -> Settings -> PHP -> Servers, and adding a new Server. Put localhost and port 80. After those two additions it now works like a charm!

Tutorial: Migrate Drupal app from GoDaddy to localhost on Ubuntu

I wanted to set up an exact copy of a hosted Drupal instance (on GoDaddy) without actually changing any code on production, which meant I had to set up my local environment on a fresh Ubuntu install. I've documented the process so hopefully whoever is doing the same thing won't run into the same issues I did. 
The versions I used for this tutorial:
Drupal 7.17
Apache2 2.2.22
Ubuntu 12.10
PHP 5.4.6-1ubuntu1.1
MySQL 5.5.28-0ubuntu0.12.10.1

The following procedures were done:

1.) Copy over the existing database. This can be done using the phpMyAdmin tool. It's pretty self-explanatory and I didn't encounter any problems with it.
2.) Get apache, mysql, php5. Just follow the instructions on the following page. Create the database user/password with the same as your hosted instance. https://help.ubuntu.com/community/ApacheMySQLPHP 
3.) Restore the database. Pretty self-explanatory
4.) FTP the files to your local.
5.) Create GIT repo (optional) I want to track my changes against the baseline, so I just created a local git repo
git init && git add . && git commit -m "init commit"
6.) Edit the drupal settings file Because the settings point to the hosted database, you need to change it.

 * @code
 * array(
 *   'driver' => 'mysql',
 *   'database' => 'databasename',
 *   'username' => 'username',
 *   'password' => 'password',
 *   'host' => 'localhost',
 *   'port' => 3306,
 *   'prefix' => 'myprefix_',
 *   'collation' => 'utf8_general_ci',
 * );
 * @endcode
7.) Change your apache configs My site was hosted on the root context, so to mimick the same behavior, edit your apache config file:
tchan@tchan-PC:/etc/apache2/sites-available$ cat default


        ServerAdmin webmaster@localhost

        DocumentRoot /home/tchan/repo/socialworkportal/
        
                Options FollowSymLinks Indexes
                AllowOverride All
        
        
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined


Note the DocumentRoot, the Directory, and the AllowOverride (set to All)
8.) Set the correct permission to your directory For ubuntu the group it uses it root, so do something like this:
cd ~/repo && chgrp root socialworkportal
9.) Enable mod_rewrite This is so that Drupal can enable the .htaccess files for cleaner urls
sudo a2enmod rewrite && sudo service apache2 restart
10.) Check for any problems in status report. Login using your admin account, then click Reports -> Status Reports. Fix any issues that come up. For me cTools and File System had permission issues. Just change it to the correct permission. For me the following fixed it:

tchan@tchan-PC:~/repo/socialworkportal/sites/default$ chmod 777 files/
tchan@tchan-PC:~/repo/socialworkportal/sites/default/files$ chmod 777 ctools/

12.10 Hanging at checking battery state

Solved! Took me a bit of time to find out why. I just install a new superfast Samsung SSD and apparently the checking order is incorrect, leading to issues with Ubuntu. Solution: sleep 1 && service lightdm restart /etc/rc.local http://ubuntuforums.org/showthread.php?t=2073483&page=3

Make Ccumber fail if step is missing

make cucumber fail if step is missing in your runner, just use --strict

Setting up a symlink on apache and Ubuntu

I'm starting a client project which uses LAMP stack. However, instead of putting the source file in /var/www, I wanted to create a symlink in that directory that brings me to the actual source directory. So first thing I did was:
ln -s ~/repo/socialworkportal /var/www
Pretty standard stuff. However when I tried to hit the path localhost/socialworkportal it gave me a forbidden error. Then I thought it was an issue with the permission -- after all the dir /var/www had root:root permissions. Okay, then I did this:
cd ~/repo && chgrp root socialworkportal
Still didn't work! Solution: So apparently Apache needs execute rights to the path of your directory, so something like this was needed: chmod o+rx /home/me /home/me/repo Hope that helps! Reference for chmod: http://www.washington.edu/doit/Newsletters/Jul97/15.html

Wednesday 28 November 2012

Grails: Use of @Mixins in Controller/Services and unit testing methods that uses the mixin methods

So I spent an hour today trying to see why my tests are failing. I have a @Mixin in my service class and I was writing a unit test for a method that calls the mixin method. However, it was giving errors like these:
testPaymentTypes_exception_in_getting_payment_types(PaymentServiceTests)
|  groovy.lang.MissingMethodException: No signature of method: com.originfunds.service.PaymentService.extractExceptionMessage() is applicable for argument types: (java.lang.Exception) values: [java.lang.Exception: exception]
Turns out the problem is that in Grails 2.0, the unit testing infrastructure of Grails cleans up the meta classes between test runs. Therefore, to solve the problem, put this in your setUp clause: void setUp() { MyController.metaClass.mixin(LogTrait) }

Saturday 24 November 2012

Caution: extending Knockout's default bindings

So I'm working on a feature that requires that every input be sent to the server after a user types in the data. What a better way than to decorate the default "value" binding. My initial binding looks like this:
                     ko.bindingHandlers.trackedInput = {  
                          init : function(element, valueAccessor, allBindingsAccessor) {  
                               ko.bindingHandlers.value.init(element, valueAccessor, allBindingsAccessor);  
                          },  
                          update : function(element, valueAccessor) {  
                               track($(element));  
                               ko.bindingHandlers.value.update(element, valueAccessor);  
                          }  
                     }  
However, when I ran this, I got this error:
Uncaught TypeError: undefined is not a function
Upon digging into the source code, I found that the default 'value' binding actually requires a 3rd param that needs to passed in. So the correct way is this:
 ko.bindingHandlers.trackedInput = {  
                          init : function(element, valueAccessor, allBindingsAccessor) {  
                               ko.bindingHandlers.value.init(element, valueAccessor, allBindingsAccessor);  
                          },  
                          update : function(element, valueAccessor) {  
                               track($(element));  
                               ko.bindingHandlers.value.update(element, valueAccessor);  
                          }  
                     }  
                     vm = {choice : ko.observable()};  
                     ko.applyBindings(vm);  
                });  

Tuesday 20 November 2012

Grails unit test: | java.lang.NullPointerException: Cannot get property 'principal' on null object

So I have a controller that uses a command object as the param and the controller also uses the springSecurityService and am writing a test to cover the flow. For the life of me, if I don't call .validate() on the mocked command object, I get an error stating that the injected springSecurityService is null:
|  java.lang.NullPointerException: Cannot get property 'principal' on null object
Somebody else had a similar problem and their solution is to to an outright mock: http://stackoverflow.com/questions/10275803/how-to-mock-a-service-injected-in-a-domain-class-from-a-controller-test-class However, the springSecurityService seems to be populated when I call the mockCommandObject().validate()

Friday 16 November 2012

Knockout.js - Calling default foreach in a custom binding

So I created a binding that handles loading images asychroniously and I wanted to have a spinner to load while it's waiting. Initially I came up with something like this:

 ko.bindingHandlers.asyncImgs = {
  init: function (element, valueAccessor) {
   $(element).spin();
  },
  update: function (element, valueAccessor) {
      var value = ko.utils.unwrapObservable(valueAccessor());
      if (value) {
          $(element).spin(false);
                ko.bindingHandlers.foreach.update(element.firstElementChild, valueAccessor);           
         }
  }
 }
And my html looks like this:
      <div class="gifts" style="height: 220px; border: 1px solid" data-bind="asyncImgs: offer_${standardProduct.productIdIndirect}_gifts" data-for="${standardProduct.productIdIndirect}">  
           <div class="gift" data-bind="foreach: $data">  
                <img data-bind="attr: {src: image}"/>  
                <span data-bind="text: description"/>  
           </div>  
      </div>  
But the above doesn't work! The reason for this is because of the fact that we're not passing in the full context that the foreach binding needs. To fix it and to get it working, I had to pass in the following params (note this is only for v2.0.0+ of Knockout).

 ko.bindingHandlers.asyncImgs = {
  init: function (element, valueAccessor) {
   $(element).spin();
  },
  update: function (element, valueAccessor, allBindings, viewModel, context) {
      var value = ko.utils.unwrapObservable(valueAccessor());
      if (value) {
          $(element).spin(false);
                ko.bindingHandlers.foreach.update(element.firstElementChild, valueAccessor, allBindings, viewModel, context);           
         }
  }
 };

Problem installing ruby on Ubuntu server: "It seems your ruby installation is missing psych (for YAML output). To eliminate this warning, please install libyaml and reinstall your ruby."

I'm getting this issue when I'm trying to install ruby:

It seems your ruby installation is missing psych (for YAML output). To eliminate this warning, please install libyaml and reinstall your ruby.
The solution is to completely remove ruby, then reinstall what is necessary and reinstall it:

rvm uninstall all

sudo apt-get gcc install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion pkg-config

rvm install 1.9.3
There you have it!

Friday 2 November 2012

Ruby/Watir/Cucumber: Taking a screenshot error: No connection could be made because the target machine actively refused it. - connect(2)

I was tasked with investigating a framework that lets QAs and Devs work together so we can get a functional test suite up. One of the tasks is to ensure we can capture screenshots on error so we can troubleshoot it. Here's my original code:
 After do |scenario|  
  if scenario.failed?  
   Dir::mkdir('screenshots') if not File.directory?('screenshots')  
   screenshot = "./screenshots/FAILED_#{scenario.name.gsub(' ','_').gsub(/[^0-9A-Za-z_]/, '')}.png"  
   @browser.driver.save_screenshot(screenshot)  
   embed screenshot, 'image/png'  
  end  
  @browser.close  
 end  
But it will create the file but nothing gets populated, and this exception comes up: No connection could be made because the target machine actively refused it. - connect(2) Well I pinpointed the error - it's the fact that the browser is closed before I made the call to take the screenshot. Put the browser close in a finally (ensure) block to make sure it's closed off.

Monday 29 October 2012

Ubuntu 12.10 - Unable to suspend

I'm having an issue where after I upgraded to the latest version 12.10, the suspend function stopped working. Upon looking at the /var/log/syslog, this is what I found: libupower-glib-CRITICAL: up_client_get_can_suspend: assertion `UP_IS_CLIENT (client)' failed And it looks like there's a bug filed for it: https://bugs.launchpad.net/ubuntu/+source/gnome-session/+bug/1066057 I guess the guys are still trying to solve it...

Wednesday 24 October 2012

Knockoutjs - using the same variable with the 'with' binding and nested 'text' binding

I'm working on a demo for a meetup presentation next week, and initially I had something like this:
 <!DOCTYPE html>  
 <html>  
 <head>  
 <script type="text/javascript" src="js/knockout-2.1.0.js"></script>  
 </head>  
      <body>  
      <p>Choose your costume:   
           <select data-bind="options: availableCostumes, optionsCaption: 'Choose one...', value: selectedCostume"></select>  
      </p>  
      <div data-bind="with: selectedCostume">  
           On Halloween this year, I will be a <span data-bind="text: makeScaryCostume"></span>.  
      </div>  
      </body>  
      <script type="text/javascript">  
        function CostumeViewModel() {  
           var self = this;  
           self.selectedCostume = ko.observable(null);  
           self.availableCostumes = ko.observableArray(['Pirate', 'Ghost', 'Banana']);  
             self.makeScaryCostume = ko.computed(function() {  
                    return "very scary " + self.selectedCostume();  
             }, self);  
        };  
      ko.applyBindings(new CostumeViewModel());  
      </script>  
 </html>  
However, whenever I run this, the following error message happens:
 Uncaught Error: Unable to parse bindings.  
 Message: ReferenceError: makeScaryCostume is not defined;  
 Bindings value: text: makeScaryCostume knockout-2.1.0.js:48  
 a.a.extend.parseBindingsString knockout-2.1.0.js:48  
 a.a.extend.getBindings knockout-2.1.0.js:48  
 a.h.disposeWhenNodeIsRemoved  
The problem is that the 'with' binding creates a context for the nested elements, so within that inner div, it doesn't have access to the view model. The solution is to use the built-in $root reference, which refers to the view model. From there you can use the computed method.
      <div data-bind="with: selectedCostume">  
           On Halloween this year, I will be a <span data-bind="text: $root.makeScaryCostume"></span>.  
      </div  

Sunday 14 October 2012

Grails UrlMappings.groovy - Mapping a top level controller excludes dbconsole fix

One of the requirements in my app that I'm working on the side involves adding a rewriting rule that maps any top level context to the ProfileController. Initially, it looks like this:
 class UrlMappings {  
      static mappings = {  
           "/${screenName}" (controller: 'profile', action: 'profile')  
      }  
 }  
So far so good. Unfortunately this poses a problem when I wanted to goto the dbconsole, which is a tool exposed by Grails in development mode to check the contents of the H2 in-mem database. Since everything maps to that controller, even the dbconsole went there. There is a solution, and that involves the use of the `constraints` param. We simply just have to add a notEqual validator of 'dbconsole' so that it skips the controller mapping. Since the /dbconsole is defined as a servlet-mapping in web.xml, it will filter to that context after Grails decides it cannot handle that uri.
So here is the solution:
 class UrlMappings {  
      static mappings = {  
           "/${screenName}" {  
                controller = 'profile'  
                action = 'profile'  
                constraints { screenName(notEqual: 'dbconsole') }}  
      }  
 }  

Friday 12 October 2012

HTML element: be careful of using element.value where there are newlines!

I'm working on a project where it uses a dirty check mechanism to warn the users on the page to save their data, and it uses element.value to retrieve the value for all form fields, and compare against the values at the time that the user navigates away from the page. This works great but unfortunately I found that element.value and $(element).val() does not always equal each other. You see, in OS specific systems, if there is a new line character, it will interpret it as \r\n, where as other OSes will interpret it as \n. By wrapping the element in jQuery, it will `normalize` the data for you so you don't have to worry about the subtle differences. For us it was essential that they have to be consistent, which is why one should use the jQuery implementation of .val() instead of element.value.

Monday 8 October 2012

Grails exception after changing UrlMappings.groovy

I got this strange exception after changing the UrlMappings.groovy in one of my Grails applications today:


 plugins.AbstractGrailsPluginManager Plugin [controllers:2.1.0] could not reload changes to file [/home/tchan/repo/privategram/grails-app/controllers/com/privategram/DashboardController.groovy]: Unable to locate constructor with Class parameter for class org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass  
 java.lang.RuntimeException: Unable to locate constructor with Class parameter for class org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass  
      at java.lang.Thread.run(Thread.java:662)  
 Caused by: java.lang.reflect.InvocationTargetException  
      ... 1 more  
 Caused by: java.lang.NullPointerException  
      ... 1 more  
 [/privategram].[default] Servlet.service() for servlet [default] in context with path [/privategram] threw exception  
 java.lang.RuntimeException: java.lang.RuntimeException: Unable to locate constructor with Class parameter for class org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass  
      at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.checkDevelopmentReloadingState(UrlMappingsFilter.java:298)  
      at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:199)  
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)  
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)  
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
      at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.obtainContent(GrailsPageFilter.java:200)  
      at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.doFilter(GrailsPageFilter.java:151)  
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)  
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
      at javax.servlet.FilterChain$doFilter.call(Unknown Source)  
      at org.grails.plugin.resource.DevModeSanityFilter.doFilter(DevModeSanityFilter.groovy:44)  
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)  
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369)  
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)  
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)  
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)  
      at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)  
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)  
      at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)  
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)  
      at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:112)  
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)  
      at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)  
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)  
      at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)  
      at org.codehaus.groovy.grails.plugins.springsecurity.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:40)  
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)  
      at org.codehaus.groovy.grails.plugins.springsecurity.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:79)  
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)  
      at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)  
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)  
      at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)  
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)  
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)  
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)  
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
      at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:69)  
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)  
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)  
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
      at org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:69)  
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)  
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)  
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)  
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)  
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)  
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)  
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)  
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)  
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)  
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)  
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)  
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)  
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)  
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)  
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)  
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)  
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)  
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)  
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)  
      at java.lang.Thread.run(Thread.java:662)  
 Caused by: java.lang.RuntimeException: Unable to locate constructor with Class parameter for class org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass  
      ... 1 more  
 Caused by: java.lang.reflect.InvocationTargetException  
      ... 1 more  
 Caused by: java.lang.NullPointerException  
      ... 1 more  
I doubled checked my UrlMappings again and they seemed fine, so I was pretty certain it was something to do with the reloading, and sure enough, after restarting the app, everything was okay. I guess they didn't tell you that not everything is reloadable?

Saturday 22 September 2012

Troubleshooting performance problems Apache HTTPD with PHP

I was tasked with troubleshooting some of the performance issues we were encountering for one of my projects. In a nutshell, the project is taking an board exam from a desktop-based solution to a web-based one. The test scenario was constructed using Apache JMeter, and it simulated 120 concurrent students taking the exams at the same time. While there were nothing wrong with the average response times, we were able to consistently reproduce a max response time of over 80 seconds, which is very high. It was suspicious to me because even querying the static resources took that long for the max response, which indicated to me that this might not be because of a code issue. After a couple of weeks of investigation, I looked at the logs and found this:

[Sat Sep 15 14:46:57 2012] [warn] Server ran out of threads to serve requests. Consider raising the ThreadsPerChild setting.

Right there that indicated to me that perhaps we were not allocating enough threads, and my suspicions were right....so I looked at the config under %httpd_home%/conf/extras/httpd-mpm.conf, but I could find any problems with the settings. Turns out, by default it actually disabled the inclusion of that config file in httpd.conf.

I just uncommented it out and restarted.

I then ran the test again, and voila the max time decreased dramatically to 7 seconds) which is acceptable by the client.

Disabling / turning off the bundling of js and css resources in the Grails resources plugin

I'm using the Grails resources plugin, which is great because you don't have to worry about including it in every page. However, by default it bundles all of your javascript into one big file. If you are using a whole bunch of plugins, troubleshooting that big file can be a daunting task. There is an easy to do this actually:


 modules = {  
      application {  
           defaultBundle false  
           dependsOn 'jquery'  
           resource url: "css/bootstrap/bootstrap.css"  
 ...  
Using the defaultBundle method will allow you to disable the auto-bundling feature.

Friday 21 September 2012

Grails RabbitMQ rabbitSend not working

So I'm setting up my second app for logging purposes, and I had a little bit of an issue sending messages to RabbitMQ using Grail's rabbitmq plugin. Here was my setup:

In Config.groovy:


rabbitmq {
connectionfactory {
         ...
}

concurrentConsumers = 5

queues = {
exchange name: 'privategram.email', type: fanout, durable: true
exchange name: 'privategram.error', type: fanout, durable: true
exchange name: 'privategram.comment.created', type: fanout, durable: true
}

retryPolicy.maxAttempts = 3 //bug workaround -- see http://jira.grails.org/browse/GPRABBITMQ-34
}

As you can see, I am using an exchange with a fanout (for now).

In my BaseController.groovy:


def exception = { message ->
log.error(message)
rabbitSend('privategram.error', message)
}

In my ReceiverService:

class ErrorReceieverService {
static rabbitSubscribe = 'privategram.error'

void handleMessage(message) {
log.error "subscribed message received."
}
}

With the following set up, I was never able to send any messages. I was able to receive messages after I sent them using the RabbitMQ management console, so I knew it was something wrong with the rabbitSend method. Upon looking at the documentation, I realized that the method signature was actually wrong! Using 2 parameters means it will be sent to a queue, while using 3 parameter means it will be sent to the exchange. I personally think this api method is flawed because it is not explicit in where it is directing the messages to. But anyways this is how I fixed it:

In my BaseController.groovy:


def exception = { message ->
log.error(message)
rabbitSend('privategram.error', '', message) //notice the additional param.
}

Thursday 20 September 2012

Grails messages not working in the gsp views

So I was having this annoying issue whenever there was a validation error on one of my forms. I have configured the message key correctly and I know the feature works because I used it extensively before.

Error Field error in object 'com.privategram.User' on field 'email': rejected value [tommytcchan@gmail.com]; codes [com.privategram.User.email.unique.error.com.privategram.User.email,com.privategram.User.email.unique.error.email,com.privategram.User.email.unique.error.java.lang.String,com.privategram.User.email.unique.error,user.email.unique.error.com.privategram.User.email,user.email.unique.error.email,user.email.unique.error.java.lang.String,user.email.unique.error,com.privategram.User.email.unique.com.privategram.User.email,com.privategram.User.email.unique.email,com.privategram.User.email.unique.java.lang.String,com.privategram.User.email.unique,user.email.unique.com.privategram.User.email,user.email.unique.email,user.email.unique.java.lang.String,user.email.unique,unique.com.privategram.User.email,unique.email,unique.java.lang.String,unique]; arguments [email,class com.privategram.User,tommytcchan@gmail.com]; default message [Property [{0}] of class [{1}] with value [{2}] must be unique]

This was what I had originally.


 <g:haserrors bean="${userInstance}">  
 </g:haserrors>  
 <br />  
 <ul class="errors" role="alert">  
      <g:eacherror bean="${userInstance}" var="error">  
           <div class="alert alert-error">  
                <button class="close" data-dismiss="alert" type="button">×</button>  
                <strong>Error</strong>  
                ${error}  
           </div>  
      </g:eacherror>  
 </ul>  



...it seemed fair to me, which made me think it could be because I upgraded the version of Grails for this current project. However, upon comparing this to my other project, I realized it was an user (*cough* `me`) error. You have to wrap the error param in a g:message tag!


 <g:haserrors bean="${userInstance}">  
      <ul class="errors" role="alert">  
           <g:eacherror bean="${userInstance}" var="error">  
                <div class="alert alert-error">  
                     <button class="close" data-dismiss="alert" type="button">×</button>  
                     <strong>Error</strong>  
                     <g:message error="${error}">  
                     </g:message>  
                </div>  
           </g:eacherror>  
      </ul>  
 </g:haserrors>  


Wednesday 19 September 2012

Grails: logging in user on success with Spring Security

I'm working on my next app, and the use case is that after a user signs up, he/she will be redirected to the dashboard "page". Spring security is great for adding out of the box functionality, but after combing through the documentation, it was unable to fulfill what I wanted to do. The problem is creating a user and a user role has nothing to do with the fact that the user is authenticated. I searched The Google and came across someone who had the same problem. Apparently he was directed to the helper class methods in the plugin. Turns out there is a poorly documented method, there, reauthenticate(). I didn't bother to completely read what the method did because of the name. I wanted something along the lines of authenticate(), not reauthenticate(). The documentation and the sample use case they described didn't make it apparent that it was the method to use either.

So, anyways, to make it work, my controller method looks like this:
   def save() {  
     def userInstance = new User(params)  
     if (!userInstance.save(flush: true)) {  
       render(view: "create", model: [userInstance: userInstance])  
       return  
     }  
           def userRole = Role.findByAuthority('ROLE_USER')  
           UserRole.create(userInstance, userRole, true)  
           // use this to set the context.  
           springSecurityService.reauthenticate(userInstance.email);  
     flash.message = message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])  
     redirect(controller: 'dashboard', id: userInstance.id)  
   }  

Monday 17 September 2012

Problem with Grails Spring Security Plugin - Cannot login

I'm starting a Grails project, and I am using Spring Security to implement the login/auth feature. However, it seemed like there was no way of getting it to work, even though I have created the current roles and assigned them to the user on creation. Frustrated, I then looked at the default properties specified in the DefaultSecurityConfig.groovy file, and what I found was this:


apf.postOnly = true

Once seeing this, I had the issue fixed because I had a custom form that I created and I forgot to set the
Once I set that then everything worked!

Tuesday 11 September 2012

5 easy steps to writing a Netbeans 7.x plugin

I was fed up with configuring one of the tools that we use at work. It's a data migration tool that takes data from a legacy data store and migrates it to a more modern RDBMS. Now this tool reads a whole bunch of properties from a properties file, and then uses them to configure the migration. This doesn't sound so bad on paper. The problem appears when a user has to work with multiple projects frequently. Each project requires a separate set of configuration (ie. the source/target dbs, along with a whole slew of other configurations specific for that project.) This properties file gets passed around, with people moving the keys within the file between runs. This presents a problem when someone is trying to find the differences for two given .properties files. The standard diff tool doesn't really know to sort the properties files by key first, then remove the any comments before actually performing the diff.

I was so fed up that I decided to make my own plugin in Netbeans to help with diffing two .properties files. In this post I will describe how I did it. Feel free to look at the source code locate at https://github.com/tommytcchan/properties-file-differ-netbeans

*UPDATE* - The plugin is approved and live. Check it out at: http://plugins.netbeans.org/plugin/44324/?show=true

Step 1: Create the Netbeans project.


First open Netbeans (version 7.2 as of this writing). Then click File -> New Project -> Netbeans Module -> Module. Follow the steps to create the project.

Step 2: Create the Action for someone to click to initialize the diff process.


Right click Source Packages -> New -> Action. Follow the steps to create the project. I won't go into details because this page explains it pretty well.

Step 3: Implement the logic for the diff process.


The logic for this is pretty simple. When someone clicks on the properties diff button, we pop up a file chooser dialog for the user to select the left file to compare. After they have selected a file, then we display another file chooser for the right file. After we have the two input files, we then read in the properties file, sort them by key, and then pass it into the Diff api.

I won't show the code here: if you are interested in the code, please go to project src repo:

https://github.com/tommytcchan/properties-file-differ-netbeans

One gotcha here that took me half an hour to figure out was how to include and use the various Netbeans api. To include the ones you need, you will want to right click on the project root -> Properties -> Libraries -> Module Dependencies -> Add.. and add the ones you want to use.

Step 4: Deploy the plugin so a NPM file is created.


For this part, I needed to set up the signing process before the system can build the signed NPM file.

Create a .keystore file by doing the following in a command line:


keytool -genkey -alias propertiesDiff -keypass password1

Create a LICENSE file in your project root that contains the license type of your project.


In your Module Manifest (aka manifest.mf), add in the 'OpenIDE-Module-Short-Description' key
In your Project Properties (aka proeject.properties), add in the following keys (obviously changing the values to suit your project).


keystore=~/.keystore
license.file=LICENSE
nbm.homepage=https://github.com/tommytcchan/properties-file-differ-netbeans
nbm.module.author=Tommy Chan
storepass=password1
nbm_alias=propertiesDiff

Finally, right click on the project root and click on Create NBM. In will build a *.nbm file in your `build` directory.


Step 5: Upload the plugin to the Netbeans repository.


The final step involves uploading the plugin at the Netbeans site. It's pretty self-explanatory, so I won't go into details. Go to the following url to start the process.

http://plugins.netbeans.org/plugin-publish-step1

That's it! It's pretty easy once you get the steps. I did find myself scratching my head on occasions, and searching via Google doesn't usually give me much. I hope this how-to helps somebody out there.

Saturday 8 September 2012

node-mongodb-native - Node.js - TypeError: Cannot read property 'safe' of undefined mongodb

One of the most annoying things that I found when building an app with Node.js is that you never know what's going to happen when you update a 3rd party library. Because Node is pretty new and everything is in active development, things can change dramatically. Take the noe-mongodb-native library for example. I upgraded to 1.1.x version of the library, and I started getting errors like these when I'm doing an insert:

TypeError: Cannot read property 'safe' of undefined mongodb


I looked at the implementing code and realized they changed how some of the stuff was implemented, so I locked it now to the latest version 1.1.6 and did and `npm install` and re-ran my app. Problem solved afterwards. The lesson of the day...try to lock down your apps to the specific version, or else risk debugging for half an hour trying to solve your problem!


Friday 31 August 2012

Grails -- unable to download groovy 1.7.12


So I recently ran into a problem with my Grails build. The problem seem to stem from the fact that one of my dependencies (an older Groovy snapshot jar?!) can not be fetched from the main grails site. So I wanted to run `grails dependency-report` to see who's using that rouge jar...unfortunately it didn't build and gave me the same message.

This is the message that I got:


[NOT FOUND  ] org.codehaus.groovy#groovy;1.7.12-SNAPSHOT!groovy.jar (226ms)
==== http://repo.grails.org/grails/core: tried
  http://repo.grails.org/grails/core/org/codehaus/groovy/groovy/1.7.12-SNAPSHOT/groovy-1.7.12-SNAPSHOT.jar
::::::::::::::::::::::::::::::::::::::::::::::
::          UNRESOLVED DEPENDENCIES         ::
::::::::::::::::::::::::::::::::::::::::::::::
:: org.grails.plugins#cloud-support;[1.0.7,): not found
::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::
::              FAILED DOWNLOADS            ::
:: ^ see resolution messages for details  ^ ::
::::::::::::::::::::::::::::::::::::::::::::::
:: org.codehaus.groovy#groovy;1.7.12-SNAPSHOT!groovy.jar
::::::::::::::::::::::::::::::::::::::::::::::
| Downloading: groovy-1.7.12-SNAPSHOT.jar.sha1
| Error Failed to resolve dependencies (Set log level to 'warn' in BuildConfig.groovy for more information):
- org.codehaus.groovy:groovy:1.7.12-SNAPSHOT


So, to fix it (so that I can troubleshoot the problem and exclude the old Groovy snapshot jar). All I did was to add the `http://snapshots.repository.codehaus.org` repository before the grails default repositories in BuildConfig.groovy.

So it looks something like this:



repositories {
inherits true // Whether to inherit repository definitions from plugins
mavenRepo "http://snapshots.repository.codehaus.org" //comes first
grailsPlugins()
grailsHome()
grailsCentral()
mavenCentral()
mavenRepo "http://twitter4j.org/maven2"
// uncomment these to enable remote dependency resolution from public Maven repositories
mavenLocal()
mavenRepo "http://repository.codehaus.org"
mavenRepo "http://download.java.net/maven/2/"
mavenRepo "http://repository.jboss.com/maven2/"
}






Tuesday 28 August 2012

Using Twitter Bootstrap's modal from an ajax loaded page.

So I'm working on one of my apps and had an issue with modal dialogs and Bootstrap. According to Bootstrap, you can do something like this:
Activate a modal without writing JavaScript. Set data-toggle="modal" on a controller element, like a button, along with a data-target="#foo" or href="#foo" to target a specific modal to toggle. 
Therefore, initially, this is what I had:

 <a data-toggle="modal" href="#help-with-url" >Content not showing up?</a>

However, what I found was that on pages that are loaded via ajax which the contents are put on a separate dialog, one cannot use what they described above for reasons I do not know why. I checked to see if there were any errors in the bootstrap js, but couldn't find any. I did solve it by just calling the modal using the javascript way, like so:

 <a onclick="$('#help-with-url').modal()" data-toggle="modal" >Content not showing up?</a>





Friday 24 August 2012

Use Heroku, MongoLab and Node.js and mongo node-mongodb-native

So I am trying out MongoLab for their database, and it uses a connection string, where in the node native drivers examples they instantiate a new Server() instance. I got it to work with the connection string, so I thought I would share:

My previous code:

SiteService.prototype.save = function (site, callback) {
    var db = new Db(mongods.datasource.db, new Server(mongods.datasource.hostname, mongods.datasource.port, {auto_reconnect:true}, {}));
    db.open(function () {
        self.getCollection(db, function (error, collection) {
            if (error) {
                callback(error);
            } else {
                site.created_at = new Date();
                collection.insert(site, function () {
                    callback(null, site);
                    db.close();
                });
            }
        });
    });
};


The code that uses the connection string:

SiteService.prototype.save = function (site, callback) {  
   mongodb.connect(mongods.datasource, {auto_reconnect: true}, function(error, db)  
   {  
     console.log("connected, db: " + db);  
     self.getCollection(db, function (error, collection) {  
       if (error) {  
         callback(error);  
       } else {  
         site.created_at = new Date();  
         collection.insert(site, function () {  
           callback(null, site);  
           db.close();  
         });  
       }  
     });  
   });  
 };  


Notice also that I changed how the `mongods` variable is defined. Instead of return a json with the individual properties, the connection string is now embedded into the datasource property.

Tuesday 21 August 2012

Heroku: specifying node / npm version doesn't work

node js heroku specify version not working

I'm deploying my app on Heroku today and because I use some libraries that require the latest version of Node, the deploy to Heroku fails because I haven't declare which version of Node to use. The docs says to create an "engines" property as Json and add in the version, so initially this is what I added (based on their sample) in my package.json:

  "engines": {  

   "node": "0.8.x",
   "npm": "1.1.x"
  }


When I pushed again, I saw the same error message? What gives?

Well, stupid me. Turned out that I was working on my develop branch and I was following the tutorials, which issues the command `git push heroku master`. Once I merge my changes from develop to master and retried pushing to master, everything worked.


Friday 17 August 2012

Node js - set your content type if you are returning JSON objects



So I'm working on my app which at times return XHR json responses, and I was a little curious when the client interpreted the json as a string. Of course, stupid me forgot to set the content-type to be 'text/json' in the headers using Express. DUHHHH.

The correct way of doing it should be like this (note: `text/json`).

 app.post("/submitNew", function(req, res) {  

   var siteService = new site.SiteService();
   res.writeHead(200, {'Content-Type':'text/html'});
   var siteObj = req.body;
   if (siteService.isUnique(siteObj.url)) {
     siteService.save(siteObj, function(error, site) {
       rabbitmq.publish(site);
       res.write(JSON.stringify(site));
     });
   } else {
     res.write(JSON.stringify({error: true, field: 'url'}));
   }
   res.end('\n');
 });


Thursday 16 August 2012

I'm using Bootstrap and knockout for my upcoming app, 404hound, and I had a bit of an issue with the enable binding from knockout when the element is an anchor.

Problem #1: I was using the anchor as a button (by annotating the class as `btn`) and unfortunately the disabled doesn't really work. Fortunately though, I was able to fix that problem easily by changing the type from to

Problem #2: I wanted to be able to set the `disabled` class (to disable the button) when the preconditions have not been met. I managed to fix that by using the css binding like so:


       
            changes


Thursday 19 July 2012

Node js: TypeError: Cannot read property 'prototype' of undefined at Object. (/http.js:49:35)

So I'm deploying my node app to the OpenStack framework using AppFog, but unfortunately even if you supply a package.json specifying your dependencies, you still need to manually copy over the files from your .npm directory
TypeError: Cannot read property 'prototype' of undefined at Object. (/http.js:49:35)

So I started copying over the dependencies manually, ie.


tchan@tommy:~/repo/afbirthday$ cp -a ~/.npm/cssom/0.2.4/package node_modules/cssom


I got to copying over express and its dependencies, and I had two versions of connect I could choose from. Naturally I chose the latest one, which turned out to be a bad idea when I tried to deploy the app.

I searched through forums, and eventually I came across a niffy feature of npm, which allows you to see the dependent versions of your dependencies.

npm ls

You will then see what version of connect is required by express. Turns out I used the newer version, which was causing issues.

Lesson of the day...use npm ls to see what versions your dependencies are before copying shiet over!

Monday 16 July 2012

Node and ampq plugin: /amqp/amqp.js:1229 this.channels[channel] = exchange; ^ TypeError: Cannot set property '1' of undefined

This is the error I got initially:



 /home/repo/404hound/node_modules/amqp/amqp.js:1229 
  this.channels[channel] = exchange;
              ^
 TypeError: Cannot set property '1' of undefined  


The solution is to wait until the connection is ready before invoking any start functions:




   var conn = createConnection(); 
   conn.on('ready', function () {
     conn.exchange(config.exchangeName, function (exchange) {
       console.log("Starting...");
       conn.queue(config.queueName, {durable:true, exclusive:true},
         function (q) {
           q.bind(config.exchangeName, '#'); //subscribe to all messages.
           q.subscribe(function (msg) {
             console.log(msg);
           });
         });
     });
   });  



Saturday 14 July 2012

Problems with Express js 3.x and EJS for node

So I upgraded to the latest version of Express, and to my dismay, they dropped support by the register() method in favor of the engine() method. Furthermore, they dropped partial view rendering (among others), which means one needs another plugin to restore the old functionality.

My solution:

 app.engine('.html', 

   require('ejs-locals') 

 );  

Saving to package.json and installing a node plugin in one step

I just found this from someone on Github:

$ npm install  --save
(--save automatically writes to your package.json file, tell your friends)
Pretty cool!

Using Node and RabbitMQ step-by-step tutorial

I spend a few hours today trying to configure the messaging part of my app using Rabbitmq on node js. I wanted to make sure it can be deployed to CloudFoundry. Anyways here is the finished module:



 var amqp = require('amqp'); 

 function rabbitUrl() { 

   if (process.env.VCAP_SERVICES) { 

     conf = JSON.parse(process.env.VCAP_SERVICES); 

     return conf['rabbitmq-2.4'][0].credentials.url; 

   } 

   else { 

     return "amqp://guest:@localhost";  

   } 

 } 



 var config = { 

   exchangeName:'404hound', 

   queueName:'ping.job' 

 }; 

 function init() { 

   var conn = createConnection(); 

   conn.on('ready', function () { 

     console.log("Starting..."); 

     conn.queue(config.queueName, {durable:true, exclusive:true}, 

       function (q) { 

         q.bind(config.exchangeName, '#'); //subscribe to all messages. 

         q.subscribe(function (msg) { 

           console.log(msg); 

         }); 

       }); 

   }); 

 } 

 function publish(msg, conn) { 

   if (conn === undefined) { 

     conn = createConnection(); 

   } 

   conn.on('ready', function () { 

     console.log("Sending message..."); 

     conn.publish(config.queueName, {body:msg}); 

   }); 

 } 

 function createConnection() { 

   return amqp.createConnection({url:rabbitUrl()}, {defaultExchangeName:config.exchangeName}); 

 } 

 module.exports.init = init; 

 module.exports.publish = publish; 

 module.exports.createConnection = createConnection;  


The first function defines the url value. Notice that we are saying if it's defined by CloudFoundry, then we use it...otherwise we use our local config.

We then define an init() function that will create the connection with the exchange it we want. After that we wait until the connection is ready. Once it's ready we can then bind it to the messages we want to read. A '#' means we want to subscribe to all messages. Then we define what should happen when a message comes in. In this simply case we just log it.

The second part of this is the publish function. There are times where we don't want to create a connection for each message we're sending, so we can opt to pass in the connection. Again it's simple as publishing to the queue you want to send to.

Here's some sample output:


 Starting... 

 Sending message... 

 { body: 'hello' }  


Notice my code above differs from the sample code from CloudFoundry...after reading the ampq plugin for Node I realized that the guys at CF are using a deprecated way to sending/receiving messages.

Note #1: You will need to create your exchange on RabbitMQ first. In my case I created the exchange 404hound. You can also use the ampq plugin to create the exchange.

Unable to remove the tipsy tooltip plugin with using the fallback option

So I am using the tipsy tooltip plugin to alert a user that the form input in invalid if a XHR request returns false. Now I wanted to add the error text dynamically, so I used the 'fallback' option of the tipsy config, which looks like this:


   self.showTipsy = function() { 

     $('#keywords').tipsy({gravity: 'w', fallback: "Whoops. Did not find matching keywords."}); 

   };  

This is what the manual said if I want to remove it:
Tipsy tooltips are 'live' - if the source attribute's value changes, the tooltip text will be updated the next time the tooltip is shown. There's one caveat - if you wish to remove the tooltip by setting the title attribute to an empty string, set theoriginal-title attribute instead (this only applies if you're using the title attribute).
Unfortunately though, when I tried that it did not work. I also try to try to hide the tipsy as mention also in the manual. Still no luck. I did find a solution for it after doing a search on google:


   self.hideTipsy = function() { 

     $("#keywords").unbind('mouseenter mouseleave'); //workaround 

   }  

Hope that helps someone!

Wednesday 4 July 2012

Knockout binding doesn't work when using jquery $.load() method

So I encountered a problem today with knockout binding apparently not working when I load it via ajax using the $.load() method. I had the content loaded into a modal body, and the html content had data-bind elements in it, like so:




    <form id="create-form" class="form-horizontal"> 

     <fieldset> 

      <div class="control-group success"> 

       <label class="control-label">Email address</label> 

       <div class="controls"> 

        <span class="input-xlarge uneditable-input"><%= email %></span> 

       </div> 

      </div> 

      </div> 

     </fieldset> 

    </form>  



This is my js file that comes with it:




 ko.applyBindings(new CreatePageViewModel()); 

 function CreatePageViewModel() { 

   var self = this; 

   self.fetchContents = function() { 

     alert('hello'); 

   } 

 }  


Everything should work right? Wrong. Because we have already loaded the knockout library previously and it was activated, we need to specify the context in which to apply bindings, like so:



 ko.applyBindings(new CreatePageViewModel(), document.getElementById('create-form')); 

 function CreatePageViewModel() { 

   var self = this; 

   self.fetchContents = function() { 

     alert('hello'); 

   } 

 }  



Monday 2 July 2012

Knockout calling view model method twice

So I have a pretty simple use case, which involves disabling a button until the person types something in the text field. Here is my view model setup:



 var pageViewModel = new PageViewModel(); 
 ko.applyBindings(pageViewModel);
 function PageViewModel() {
   var self = this;
   self.email = ko.observable("");
   self.hasEmail = ko.computed(function () {
     return self.email().length != 0;
   });
   //called after user clicks the create button.
   self.create = function () {
     $('#modal-body').load('http://localhost:1337/create', function () {
       $('#modal').modal();
     });
   }
 }  

In my view, this is what I have:


       <div id="search"> 
         <form name="input" action="" method="get">
           <input type="text" data-bind="value: email" value="The admin's email address" name="field"
               class="text"/>
           <input type="submit" data-bind="enable: hasEmail(), click: create()" value="Start" name="search" class="search btn"
               data-toggle="modal" href="#myModal"/>
         </form>
       </div>  

Now for some reason before I found out my problem, it was also calling the create() method whenever the ko.applyBindings() method is called.

Upon looking at the docs, I realized that I have an extra bracket on the create, which causes KO to evaluate the create in the beginning. So the correct code should look like this:


       <div id="search"> 
         <form name="input" action="" method="get">
           <input type="text" data-bind="value: email" value="The admin's email address" name="field"
               class="text"/>
           <input type="submit" data-bind="enable: hasEmail(), click: create" value="Start" name="search" class="search btn"
               data-toggle="modal" href="#myModal"/>
         </form>
       </div>  



XMLHttpRequest cannot load http://localhost:1337/create. Origin null is not allowed by Access-Control-Allow-Origin.

I've started to do some backend Node work today. While my index.html is served locally on the file system, the node server actually runs on localhost with a port. This presented a problem when I try to make an ajax call. Here is the response

XMLHttpRequest cannot load http://localhost:1337/create. Origin null is not allowed by Access-Control-Allow-Origin.

If you get this error, that means that you are not allowed to cross site request, unless the server says you are allowed. Well how do you do this? Simple:


    res.header('Access-Control-Allow-Origin', '*');

For production though, depending on how secure you want your web service to be, you might not want to do '*' (allow everything from all domains) but rather restrict it to your own domain.