Saturday, March 19, 2016

Ubuntu cannot mount /boot/efi

Yesterday all of a sudden Ubuntu (14.04) refused to boot with a strange message that it could not mount /boot/efi.

Running fsck from GRUB (Advanced menu) revealed this error

mount: wrong fs type, bad option, bad superblock on /dev/sdb1,
       missing codepage or helper program, or other error
       In some cases useful info is found in syslog - try
       dmesg | tail or so

mountall: mount /boot/efi [771] terminated with status 32
mountall: filesystem could not be mounted: /boot/efi


Opened root shell and tried to find the boot partition
# parted
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print devices                                                    
/dev/sda (1000GB)
/dev/sdb (120GB)
(parted) select /dev/sdb
Using /dev/sdb
(parted) print                                                            
Model: ATA Samsung SSD 840 (scsi)
Disk /dev/sdb: 120GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End    Size    File system     Name  Flags
 1      1049kB  538MB  537MB   fat32                 boot
 2      538MB   112GB  111GB   ext4
 3      112GB   120GB  8466MB  linux-swap(v1)

Ok, so the boot partition is #1 on /dev/sdb, i.e. /dev/sdb1
Tried to mount it manually

mount /dev/sdb1 /boot/efi

Same error as above
Ok, let's see the syslog as suggested in the message

dmesg | tail

New error message

FAT-fs (sdb1): IO charset iso8859-1 not found

Searching for this error found this post that suggested it is a problem with the dependency database.
Tried running this as suggested in the post

sudo modprobe nls_iso8859-1

but it complained that it could not load /lib/modules/3.13.0-83-generic/modules.dep.bin. This file existed but it was empty. But the file of the same name was non-empty in an older kernel directory.

So back in GRUB and selected the previous kernel version and Ubuntu booted normally.
Then tried this in an attempt to fix the package database

sudo apt-get check

It complained with some error and suggested the following command

sudo dpkg --configure -a

This finally fixed the issue. Afterwards Ubuntu booted normally with the latest kernel.
Still not clear what caused this glitch. Didn't run any upgrades or so before it.
Luckily it resolved without reinstall.


Tuesday, November 10, 2015

Include mock

Although recently I write mostly in (server side) JavaScript, I still have to return to my old C++ project once in a while. The other day I got notified that one of our integration tests will stop working due to changes in the database layer. It was testing a fix for a crash caused by inconsistent data coming from the db. So this integration test was trying to reproduce the situation by manipulating the data in the db. Actually reproducing such special cases with an integration test might be very difficult and unit tests are usually a better tool for this task. In the end I wanted to test some safety checks in our code, not an end to end scenario.

Still the code under test was a big and hairy lump of C++ code with many hard-wired dependencies. It certainly was not written with testability in mind. While looking around for mocking solutions in C++, I came across the section about testing from Michael Feathers' book Working Effectively with Legacy Code. He describes several approaches to mocking in C/C++, as he calls them seams. I was most impressed by the preprocessing seams. As Michael says, the preprocessor in C/C++ is kind of compensation for its stiffness compared to dynamic languages. It turns out the preprocessing seams are very powerful. You can take a C/C++ source file and compile it in a different environment thus making it do something very different.

So inspired by preprocessing seams I derived my own mocking approach in C/C++ that allows mocking of any function or class (even static, global and non-virtual) without changing the source file where these are called.

Here is the overall structure of a test file:
  1. Disable the original header that defines the dependency to be mocked using include guards
  2. Provide alternative/mock definition of that dependency
  3. #include the source file to be tested
  4. Write the tests
Let's see a simple example.
First, the header that defines the dependency that we want to mock.
Notice that this header uses include guards.

store.h

#ifndef STORE_H
#define STORE_H

class Connection;

class Store
{
public:
    Store(Conection& conn);
    //...
    const char* fetch(const char* query);
    void store(const char* data);
    //...
};

#endif // STORE_H

Next, the code that we want to test.

consumer.cpp
#include <string.h>

#include "store.h"

int measure(Store& store, const char* query)
{
  const char* v = store.fetch(query);
  return strlen(v);
}

And now the test.
We want to mock Store. To do this, we disable the original header by defining its include guard STORE_H. Then we provide our mock implementation. Note the mock does not need to be compatible to the original class. We just need to provide the minimum so that the code under test can compile and execute. So we implement only the methods used during the test.
Then we include the code to be tested consumer.cpp so it will compile in our mocked environment.
Finally, we run our test.

test.cpp
#include <iostream>
#include <cassert>

using namespace std;

#define STORE_H
class Store
{
public:
    const char* fetch(const char* query)
    {
        return "ola";
    }
};

#include "consumer.cpp"

int main(int argc, char *argv[])
{
    Store mock_store;
    assert(measure(mock_store, "query") == 3);
    cout << "OK" << endl;
}

With this approach all the code related to the test is in one place. You don't need to tweak any additional compiler/linker configurations. Also notice that we did not change the original code consumer.cpp, still we changed its behavior by compiling it in a mock environment.

Also described briefly this technique on SO.

Sunday, March 22, 2015

Checking out GitHub pull requests locally

When working with GitHub you often need to checkout a pull request (PR) locally so you can load it in your favorite tools and run/test it.

GitHub help suggests you can use a command similar to:

git fetch origin pull/ID/head && git checkout FETCH_HEAD
(here ID is the number of the pull request)

While this will give you the original code of the PR, it might be different from what you will get if you actually merge the PR. The reason is that you might have parallel changes in your target (master) branch not yet merged in the PR. While you can do the merge also locally, it turns out this is not necessary as GitHub had alsready done it for you. All you need is to use this command instead:

git fetch origin pull/ID/merge && git checkout FETCH_HEAD
(notice the difference in the refspec 'head' vs. 'merge')

This will give you a merged version of the PR which contains all parallel commits in the target branch even those merged after the PR was created.

Sunday, January 11, 2015

Rip audio CDs on LINUX

I still use audio CDs sometimes. But they tend to get lost or damaged easily. So it is a good practice to convert them to MP3.
So far I used Asunder for CD ripping. It is very easy to use ... when it works. But with some discs, usually lower quality CD-R, it just hangs right from the start. So I searched for another tool to rip audio CDs on LINUX.
It turned out you can do this very quickly with two command line tools - cdparanoia & lame. As usual you can quickly install them on Ubuntu with a single command line:

$ sudo apt-get install cdparanoia lame

Assuming the CD is loaded in the CD drive, running this simple command will copy all audio tracks in WAV files in the current directory:

$ cdparanoia -B

Next to convert those WAV files to MP3, run this command:

$ ls -1 | xargs -L 1 lame --preset standard

This will compress the audio files about 10 times using VBR ~190kbps.
If you are satisfied with the result, you can delete all WAV files:

$ rm *.wav

This will leave only MP3 files named like track04.cdda.mp3.
Of course these tools have many more options so you can tweak them as much as you like. For example lame option --ta sets the artist and --tl the album in ID3 tags inside MP3 files.
You can also script and automate this process as you see fit, but these are the tools that do the job nice and quickly.

BTW did you know that "disk" refers to magnetic storage while "disc" refers to optical storage? See Wikipedia.

Wednesday, September 3, 2014

npm - first encounters

Playing with node.js at work I hit an issue in the very beginning as I was unable to install any package using npm (the convenient package manager of node).

$ npm install nodemon
npm ERR! network tunneling socket could not be established, cause=connect EINVAL
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

Ok, I really use a proxy so I run npm config edit and uncomment these lines:

; proxy=proxy:8080
; https-proxy=proxy:8080

Same result

$ npm install nodemon
npm ERR! network tunneling socket could not be established, 

The error EINVAL suggests that connect was called with an invalid argument. What that might be? Let's see the system calls of npm:

$ strace npm install nodemon 1> npm.strace 2>&1
$ grep EINVAL npm.strace
ioctl(9, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7fff5f9a8330) = -1 EINVAL (Invalid argument)
connect(10, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr("0.0.31.144")}, 16) = -1 EINVAL (Invalid argument)
connect(10, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr("0.0.31.144")}, 16) = -1 EINVAL (Invalid argument)
connect(10, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr("0.0.31.144")}, 16) = -1 EINVAL (Invalid argument)
write(2, " tunneling socket could not be e"..., 65 tunneling socket could not be established, cause=connect EINVAL

Ahaa, we see 3 calls to connect with IP 0.0.31.144 and port 443 (default HTTPS port) and all of these returned EINVAL (Invalid argument).
What is this strange IP? Asking Google for it, revealed this post according to which, environment variable http_proxy should be given with a protocol, e.g.

http_proxy=http://proxy:8080

So setting https-proxy=http://proxy:8080 in npm config did solve the problem!

On my Linux both of these are set:

https_proxy=http://proxy:8080
HTTPS_PROXY=proxy:8080

and it seems npm takes the upper case variable to set the default values in npm config.

Wednesday, May 28, 2014

Dynamic auto-complete in Android

Auto-complete text input is very common in modern UI. It makes it easy to select an item from a large list or just provide some hint on matching items. Often the whole list of available items is not available, so you need to lookup matching items in some external source.
In this example we use auto-complete to select a stock, similar to the search field on finance.yahoo.com.

Here we use a web API from Yahoo to lookup matching stocks, but you can use the same approach with any way of populating the auto-complete drop-down dynamically. For example you could search in a database.

These are the major objects involved:
AutoCompleteTextView -> Adapter -> Filter



android.widget.AutoCompleteTextView is the standard Android widget for this purpose. We will use it as it is but will implement custom Adpter and Filter.

 symbolText = new AutoCompleteTextView(getActivity());
 symbolText.setAdapter(new StockLookupAdapter(getActivity()));

Here is our custom Adapter:

public class StockLookupAdapter extends
        ArrayAdapter<StockLookupAdapter.StockInfo> {

    private static final String LOG_TAG = StockLookupAdapter.class
            .getSimpleName();

    class StockInfo {
        public String symbol;
        public String name;
        public String exchange;

        @Override
        public String toString() {
            // text to display in the auto-complete dropdown
            return symbol + " (" + name + ")";
        }
    }

    private final StockLookupFilter filter = new StockLookupFilter();

    public StockLookupAdapter(Context context) {
        super(context, android.R.layout.simple_list_item_1);
    }

    @Override
    public Filter getFilter() {
        return filter;
    }

    private class StockLookupFilter extends Filter {
        ...
    }
}

Here StockInfo carries the data for each item in the drop down. Here we store the properties of each stock like symbol (a.k.a. ticker) and name. We override getFilter to return the custom Filter - StockLookupFilter. This is the essential part.
Here is what android.widget.Filter docu says:

Filtering operations performed by calling filter(CharSequence) or filter(CharSequence, android.widget.Filter.FilterListener) are performed asynchronously. When these methods are called, a filtering request is posted in a request queue and processed later. Any call to one of these methods will cancel any previous non-executed filtering request.

This is exactly what we need as calling a web API usually takes some time so we should not do it in the UI thread. Also the user may type faster than the web API can return the results. This could result in the hints shown in the drop-down lagging considerably behind the current text state. The queuing described above helps avoid this effect.

So here is our custom filter (nested inside StockLookupAdapter):
private class StockLookupFilter extends Filter {

    // Invoked in a worker thread to filter the data according to the
    // constraint.
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if (constraint != null) {
            ArrayList<StockInfo> list = lookupStock(constraint);
            results.values = list;
            results.count = list.size();
        }
        return results;
    }

    private ArrayList<StockInfo> lookupStock(CharSequence constraint) {
        ...
    }

    // Invoked in the UI thread to publish the filtering results in the user
    // interface.
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        setNotifyOnChange(false);
        clear();
        if (results.count > 0) {
            addAll((ArrayList<StockInfo>) results.values);
            notifyDataSetChanged();
        } else {
            notifyDataSetInvalidated();
        }

    }

    @Override
    public CharSequence convertResultToString(Object resultValue) {
        if (resultValue instanceof StockInfo) {
            // text to set in the text view when an item from the dropdown
            // is selected
            return ((StockInfo) resultValue).symbol;
        }
        return null;
    }

}

perfromFiltering is executed in a background thread and it finds the items to be shown in the drop-down based on the current text in the text field.
publishResults is executed on the UI thread and it is given the FilterResults returned by perfromFiltering. Here we just reset the ArrayAdapter contents and notify the UI to update.
convertResultToString returns the string to be substituted in the text field when a given item from the drop-down is selected. In our case we display both stock symbol and name in the drop-down but want only the symbol in the text field.

So as we can see simple text navigation can be very efficient. Probably this is the reason why it is so popular these days.

P.S.
Still there is one glitch that irritates me. It seems part of the the drop-down is covered by the on-screen keyboard. If I try to close the keyboard, the drop-down is closed first.

Friday, April 4, 2014

Does destruction change anything?

class C{};

// consider this

void foo(const C* p)
{
    delete p;
}

// does it work? should it work?
// after all destroyng the object very much changes it 
// and you are not allowed to change a const object, right?
// ...
// now consider this
// (you can substitute here auto_ptr 
// with your favorite smart pointer)

void foo(auto_ptr<const C> p) { }

// is this possible at all?
// ...
// how about this

void foo(const C x) { }

// hmm... this is pretty common code
// if const objects exist (and we know they do) 
// then they must come to an end somehow

// so it is possible and completely normal to destroy 
// a constant object and all of the above is valid code

// so now my interpretation of const is this:
// if the object still exists, 
// its observable state should be the same as before

Based on this post on SO

Saturday, December 21, 2013

Back to native

Usually the end of the year is time to take a step back, reflect on the past and plan for the future. For me it is also a time for a change. After 9 years working in Java (SE) I will switch back to C++. So I'll use the opportunity to share some thoughts I have accumulated in recent years.
Generally I avoid being a language zealot (like some of my colleagues ;). For me it is more important to create simple and beautiful solutions leveraging whatever technology is available.

Some say Java tends to be too ceremonial and I would agree on that.
Nowadays the industry has accepted that checked exceptions are not so useful after all.
Proper resource management with all this exception handling feels very clumsy and is definitely inferior compared to the good old RIIA mechanism available in C++.
Some design mistakes from the yearly day of Java are still present for the sake of compatibility. These are things like decorator pattern overuse in stream handling and all these unnecessary methods in Object most of which you rarely use but constantly pay the price for.

It was interesting for me to see how C++ has advanced for the last decade. I see there are many new and useful features C++11. For example lambdas are now part of the standard while in Java they have been delayed for years and are still not present.
The preprocessor and the necessity to use it has always been for me one of the major issues in C++. So I was hoping that this was addressed somehow in the recent evolution of the language. Alas! All these #include and #define directives and the involved text substitution without any awareness of the language feel like a stone age hack. And it is still there.
Even before I started working in Java I thought it would be great if C++ could be compiled to some standard intermediate format. This could improve both compile-time and runtime performance. When you compile to machine instructions ahead-of-time (AOT) you rarely know the exact CPU where the code will run, so you cannot take advantage of new CPU features. I was glad to find recently that there is already a considerable effort in that direction - namely LLVM project even though it is not C/C++ specific.

Wednesday, May 22, 2013

Java Serialization Performance

The Java program I work on in my day job maintains its data model in a tree-like structure of Java beans. The data volume is not big but still it is significant - about 500K property values in about 30K bean objects.
To ensure our program can restart from point of failure we often have to save this data model to disk. We use custom XML serialization. As this takes quite some time (several seconds) I started researching different alternatives.

It turned out that JAXB is not appropriate for the job as it does not cope well with cyclic dependencies and cross-references. It also requires adding a lot of annotations and our data model employs about 50 classes.

Next I tried standard Java object serialization but it turned out to preform even worse - it saved the data model in 15s in a 10MB file.

A quick search about Java serialization showed an open source library Kryo. I was able to test it very easily as it requires no annotations and almost no code changes. Kryo turned out to be lightning fast! It saved the same data model in just 400ms in a 5MB file. At first I did not believe all our data was saved, so I loaded it back and compared it to the original data but there were no differences.
One of the main reasons for this performance is that instead of reflection Kryo uses dynamic byte code generation via ReflectASM.
I also found another project run by Nate - Spine to be very interesting. It is related to game development. Obviously a very skilled developer.
Kudos Nate, great job!

Wednesday, May 15, 2013

Batch convert MOV to MP4 in Ubuntu

for i in *.MOV; do avconv -i $i -b:v 3000k ${i%.MOV}.MP4; done

Video: mjpeg -> libx264
Audio: pcm_s16be -> libvo_aacenc
Compression: 10 -> 1

These are video clips from my Panasonic photo camera

Wednesday, May 8, 2013

Saturday, February 23, 2013

Function or method

Lately I have been reading about Python. Here is an example that shows some interesting properties of functions and methods.
Given this code in methods.py

def f(a):
    print a
 
class C:
    def __init__(self, x):
        self.x = x
 
    def __str__(self):
        return "C(%d)" % self.x


Let's try some statements in the Python console

>>> from methods import *
>>> f("Hello")
Hello
>>> c = C(5)
>>> print c
C(5)
>>> c.f = f
>>> c.f("bye")
bye
>>> C.g = f
>>> c.g()
C(5)
>>> g = c.g
>>> g()
C(5)



Friday, October 26, 2012

Beware of Premature Generalization

When people start writing some new code they often feel so enthusiastic that they try to make it reusable right from the beginning. Or some do it out of some pretended sophistication. The end result is usually layers of unnecessary abstractions that are actually not used anywhere else. In the end such code takes more effort to write and more effort to maintain afterwards.
The reality is that you cannot make something reusable if it is used in a single place/scenario. To make it reusable you do need at least two real use cases, so you can abstract the common traits. The more use cases you have, the better you can refine the abstraction.
You may think you know what new use cases will come, but in reality these either don't come at all or are quite different from what you have expected.
So don't do this, unless you can tell the future.

I thought premature generalization would be a good term for such situations. Then after searching the net it turned out this term is well established already - Premature Generalization Is Evil. It is even listed as #1 in Seven deadly sins of programming.

Thursday, August 30, 2012

Remove class inheritance from Java

Introduction

We often hear the advice that we should prefer composition over inheritance. Then naturally come the questions: Are there valid cases at all to use inheritance? Is it possible that inheritance, one of the fundamental OOP principles, is actually an anti-pattern and unnecessary language feature?
By inheritance here I mean only class inheritance and not interface extension.

Goodbye Class Inheritance

The most common reason for class inheritance found in practice is code reuse. When people realize they need some common code in two or more classes, they pull it up in a common base class (usually an abstract one). But what would you do if you need to reuse code from several other classes? There is no multiple inheritance in Java. The right way to reuse some common code is via composition, i.e. extract the common code in a separate class and use it via a reference.

The next reason for inheritance is method overriding, i.e. template method pattern. When people realize they need some variability in behavior, they define a method for it (often abstract) and implement it in different ways in different subclasses. This is also wrong for several reasons. You cannot change the variable method implementation dynamically without changing the whole object. Also if you have several axes of variability, i.e. several abstract methods, how many subclasses do you need to cover all possible combinations? Again the right way to do that is via composition. Each independently variable part is extracted in a separate interface. Different implementations of these interfaces are injected from outisde, i.e. strategy pattern.

Can you suggest cases where it is still best to use class inheritance? Post a comment.

Generally, inheritance violates other OOP principles like encapsulation as it creates tight coupling between involved classes.

The conclusion is that the availability of this feature (implementation inheritance) leads people into wrong designs. So the Java language can be both improved and simplified if classes are not allowed to extend other classes. Still an interface can extend other interfaces and a class can implement a number of interfaces.
Removing this feature can simplify the language significantly. Additional language features can also be removed as they are no longer necessary.
  • abstract keyword  is no longer necessary and can be removed.  Abstract coding can get you only abstract money. :)
  • protected keyword  is no longer necessary and can be removed
  • final keyword on classes and methods  is no longer necessary
  • (Probably this list can be extended further. Drop your suggestions in the comments.)
So this is a nice example how taking away can improve the result, i.e. less is more principle. Or as Antoine de Saint-Exupery has put it

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away."

Welcome Auto-binding

Still when your class implements an interface you often do not write the code from scratch but want to reuse some existing implementation. As we saw already the proper way to do that is via composition. Something like this

class A implements I
{
    private I anI;
    ...
    public void foo() { anI.foo(); }
    public void moo() { anI.moo(); }
    ...
}

Unfortunately we have no way to tell that anI provides the implementation of interface I, so we have to implement all the methods of I in A just by delegating to anI. Boring boilerplate! This is where the second proposal for the language improvement kicks in. You can call it auto-wiring or auto-binding but it does just that, specify that an interface is implemented by a specific class member. Something like this:

class A implements I via anI
{
    private I anI;
    ...
}

The compiler can automatically generate the necessary delegation methods or an optimized implementation could even skip this overhead.

If a class needs to customize the interface implementation it can still implement some of the interface methods, thus overriding these methods from the external implementation.

Instead of a field also a method could be specified in case the interface implementation should be calculated dynamically. Something like this:

class A implements I via getI
{
    private I getI() { ... }
}

I find these two proposals for Java language improvement nicely complement each other and naturally lead developers in the right direction.
Until these improvements are implemented in Java (version 15 probably :) you can try to avoid class inheritance in your code.

Sunday, December 11, 2011

Ubuntu price

Ubuntu costs no money but it costs time to search for solutions for various issues. And recent releases are full of issues most of which are regressions.
The latest one is that file transfer over Bluetooth does not work - Bug 897995.

Saturday, May 21, 2011

Natty Upgrade

Finally upgraded to Ubuntu 11.04 Natty Narwhal. The upgrade went smooth. Download was faster than the actual package update. It is interesting to see the upgrade running while the old system is still usable. I guess they do the actual switch during the reboot. And Ubuntu upgrade takes only one reboot.

But still there was a glitch after the upgrade. It said something that my display mode is not supported and switched it from 1920x1080 to 1024x768. Even if I try to switch it back, the image gets distorted. Then I noticed that Login screen used the right resolution and the same also for the other account on my PC. But when I login to my account the screen switches to 1024x768. Obviously it is some account-specific configuration. After couple of hours searching around I found the guilty file ~/.config/monitors.xml. After deleting it and re-login the screen resolution is back to normal.

Release 10.10 was pretty buggy. As Tom said once there seems to be a tradition of .10 releases being more unstable. I hope that 11.04 sticks to this tradition and is more reliable than the previous one.

Sunday, April 10, 2011

Queries in Sling

Here is how I use JCR queries in my .esp pages.
The sample below lists all child nodes of the current node sorted by price property.


<%
var sql = "select * from [nt:base] where ischildnode('" + currentNode + "') order by price asc";
var query = currentSession.workspace.queryManager.createQuery(sql, Packages.javax.jcr.query.Query.JCR_SQL2);
var result = query.execute();
var iter = result.nodes;
while (iter.hasNext()) {
var node = iter.nextNode();
%>

// render each node from the result

<% } %>


Notice that when a Node is converted to string its path is used.
Also here you can see how Packages object is used to access static members of Java classes. This is a Rhino feature. You can read more about it here.

Resizing windows in Ubuntu

A major usability paint point in Ubuntu's default theme is that it is very difficult to grab the window border to resize it. The reason is that the borders are very thin, I think just one pixel.
So I have been using Alt-F8 shortcut (see System > Preferences > Keyboard Shortcuts) to resize windows. But this is not very convenient either since you can resize in only one direction. To resize in another direction you have to press Alt-F8 again. Also the direction is chosen from the initial mouse movement after Alt-F8. Well, just try it.
Now I got irritated by this obvious malfunction and did a quick search and found this thread. It turns out you can also resize windows with Alt-(middle mouse button). Well, this is much easier. There is also a link to a bug report where they say this will be addressed in the coming 11.04 release. It seems like there will be 3 pixel wide invisible area around the window edges where you can grab and resize.

Some other useful shortcuts:
  • Alt-F7 - move active window (very convenient to get back a window from a second display/TV which is currently turned off)
  • Alt-(left mouse button) - move window

Friday, March 25, 2011

Log in Sling

The default Sling user is anonymous and it has read-only access to the repository. So if this user attempts to change anything in the repository he gets an error like this

javax.jcr.AccessDeniedException: <path>: not allowed to add or modify item

So in order to change the data they have to login with a user that has proper permissions. But we still want to allow anonymous visitors to read the content of our site (like search engines). We ask them to log in only if they choose to edit the content. One way to do this in Sling is like this:

<%
if (!currentSession.hasPermission(currentNode.path, "set_property"))
    response.sendRedirect(request.requestURL + "?sling:authRequestLogin=BASIC");

%>

Put this code at the beginning of an ESP page that performs content changes.

Wednesday, February 9, 2011

Posting non-ASCII characters in web forms

I just hit this issue. You write some non-ASCII text (e.g. in Cyrillic) in a form and when submitted the text appears garbled on the server side.

It turns out this is a well known glitch in web development especially when done in Java. The main reasons for the mess are
  1. Web browsers do not specify the encoding of posted data
  2. Java Servlet specification says that default request encoding should be ISO-8859-1 in contrast to UTF-8 which is universally used nowadays
You can find a good description of this issue here HTTP Form Character Sets and Related Problems.

Tomcat FAQ recommends creating a filter to set the request encoding.

But when using Wicket there is no such issue as they fix the request encoding to UTF-8 as described in How to change the character encoding.

Unfortunately Sling guys did it in another way (SLING-508) which requires me to put a hidden input named _charset_ with the value UTF-8 in all my forms. So they have adopted the ugly IE hack. :(
This is also described in Sling documentation.

I wish Sling had a way to set this to UTF-8 in one place and get rid of it.

Sometimes web development is so frustrating.

Update: Mar 2nd
After picking up this discussion on Sling mailing list the guys there decided after all to make this configurable, see SLING-1998.
Great! My forms now work without the _charset_ hack.