Groovy #1

Several years ago, when Java was still a toddler, probably around 2001, my brother had written a very functional downloader in Swing – it would read a text file of urls, and download each file one by one. It has been a while, that source code is gone, Swing is gone, Sun is gone, blah blah blah.

Sitting idly, I suddenly thought of that piece of code for no reason and said to myself why not rewrite that in groovy. And literally wrote this in a few minutes… The code is pretty self contained, it assumes defaults and there is even a test mode… Its been long journey with Java, yeah definitely feels like one.

Invoking is pretty easy:

groovy Download.groovy -fileName [urlsPerLineFile.txt]

import java.io.*;

class DownloadFile {

    def args = [:]
    def list2map = { list -> def map = [:]; 0.step(list.size(),2) { i -> map[list[i] - "-"] = list[i+1] }; return map }
    def download(url) {
            def file = new FileOutputStream(url.tokenize("/")[-1])
            def out = new BufferedOutputStream(args.targetDir + file)
            out << new URL(url).openStream()
            out.close()
    }

    public DownloadFile(String[] _args) {
        args = this.list2map(_args)
        args.mode = args.mode ?: "test"
        args.targetDir = args.targetDir ?: "c:/downloads/files"
        println args
    }

    public void process() {
        new File(args.fileName).readLines().each {
            println "downloading [$it]"
            if (args.mode != "test") {
                download(it)
            }
        }
    }
}

new DownloadFile(this.args).process()

While the code is simple, there are two things I really like about it. First is the closure for conversion from list to map.

def list2map = { list -> def map = [:]; 0.step(list.size(),2) { i -> map[list[i] – “-“] = list[i+1] }; return map }

This converts a command-line invoke with arguments with a hyphen character into a map of key-value. For eg:

Download.groovy -mode test -targetDir c:/downloads

will yield a map of [mode:test, targetDir:c:/downloads] Neat isn’t it? Think of how complicated a conversion of list to map in Java is.

Another is the expando, see the keys of the map are used like properties: args.mode, args.targetDir instead of args.getKey(“mode”) or args.getKey(“targetDir”).

The above feature comes very handy especially when trying to deserializing xml into objects. You just don’t do deserialization xml to java bean objects! Use directly. That way if xml changes, you do not have to generate code again (think jaxb!). Just modify your code to use the new xml tags!

Advertisements

SP 2007 to SP 2010 Migration #3 – Lanap.BotDetect

Problem

Lanap.BotDetect 2.0.15.0 does not work after migrating to SharePoint 2010 from SharePoint 2007. The image does not appear on the screen or any validation handling does not work.

Solution

This is a similar problem to Issue #2 post.

First up, check if you have followed the instructions as in http://captcha.biz/doc/asp.net/2.0/asp.net-captcha-sharepoint.html

I deployed and use Lanap in GAC, no issues there.

In classic app pool mode, the http handlers are expected in <system.web><httpHandlers> section. In Integrated app pool mode, the handlers are expected in <system.webServer><handlers> section.

If the handlers are not added in the new section for Integrated mode, you will get a 404 error for invoking the handlers.

Similar to the solution in Issue #2, add the Lanap.BotDetect handler using the IIS7 Manager. Restart IIS and Lanap.BotDetect should work now.

SP 2007 to SP 2010 Migration #2 – Custom http handlers

Problem

Custom ASP.Net http handlers do not work in SharePoint 2010 after migration from 2007.

Premises

There are several ways one can write a custom http handler for use within a sharepoint environment.

Create .ashx file and a corresponding code behind .cs code and place it in _layouts folder. However that would not work if you dont want to expose the _layouts or you want to use a custom url mapping too (eg MyCustomHttpHandler.logon instead of MyCustomHttpHandler.ashx). So an alternative way is to create a .cs (that implements IHttpHandler) and a custom url mapping and place it in the <httpHandlers> section in web.config.

Solution

  1. Open IIS 7 and select the SharePoint site
  2. Open Handler mappings
  3. Add Managed Handler
  4. Provide a name and use the same type as used in the web.config section for httpHandlers
  5. Restart IIS

Now the http handler will work.

Reason

When migrating to 2010, one of the changes was using the Integrated app pool instead of classic app pool. If your new site is using Integrated app pool, IIS 7 expects the handlers to be in a different section – specifically <system.webServer><handlers> section. In classic mode, the handlers are used from <system.web><httpHandlers> section.

Adding the custom http handlers within this section allowed IIS 7 to map the handler to the code.

SP 2007 to SP 2010 Migration #1 – System.Web.Extensions not found

Problem:

We were migrating a sp 2007 project to sp 2010. The web.config between the two environments were very different (mainly the version numbers). After the merging the two web.config, I was getting a plain Error 500 on the site, with no further details.

Solutions:

First up, open IIS, select the site, double click on the Configuration icon. If this opens, at least the web.config is fine. In my case even this did not open up, and gave an error 0x8007000D and very helpful error of empty string.

Scouting around on several websites, (the most helpful was stackoverflow.com) finally tried installing the IIS7 rewrite module (rewrite_2.0_rtw_x64.msi) and restarted IIS. Now I was able to open the Configuration Editor from IIS7.

Now hitting the site gave atleast a readable error “System.Web.Extensions not found”. The dll is installed in GAC for sure. This is for AJAX ASP.Net and apparently this has to be turned off for SharePoint 2010 which relies on .NET 3.5.

So commented out the following line:

<add name=”ScriptModule” type=”System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ />

The site is up and running now.

SharePoint Designer 2010 does not open site

Problem:

When attempting to open a site, SPD 2010 gives a plain error “Cannot open site” with details like

response code = “”, status = OK.

Solution:

Make sure the following tag is in the web.config of the relevant site:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled=”true” />
</system.serviceModel>

I had accidentally removed it and spent an hour figuring out what went wrong.