14
While YouTube has an API to get thumbnails of its videos, there is also the possibility to construct the URLs of the thumbnails out of the URL of the video.
While YouTube has an API to get thumbnails of its videos, there is also the possibility to construct the URLs of the thumbnails out of the URL of the video.
If you are getting the following exception on a migration on Darwin / OS X:
SQLite3::SQLException: near "ADD": syntax error: ALTER TABLE "xxx" ADD "yyy" varchar(255)
then you can try the following:
For those of you who want to display a progress panel or a progress bar whenever some loading on your page is required, the JBoss Wiki contains a really good tip: RichFacesPleaseWaitBox.
This sample can be enhanced very easily to bring in some dynamic: within the rich:modalPanel, just use an animated gif, for instance from this cool page.
This has been originally published on tomcat.ranta.info on May 21, 2008.
I know I should be hard at work, but… it took only 10 minutes!
And sometimes I should really scratch that programming itch when it comes along…
Yesterday I did some reading and talking on the Debian OpenSSL desaster. guruz brought in an interesting website on random number visualization.
I don’t know about you, but I couldn’t help but think that PRNGs can’t be that bad. Or at least they wouldn’t be this bad in Linux, where timing and attributes of mouse and keyboard activity, disk I/O operations and specific interrupts are used as entropy sources.
There are many scenarios where a web application must serve a file instead of a response containing HTML output to be rendered by the browser.
This little tip illustrates how to cause the browser to display a file download dialog instead of trying to display the data directly (which fails in most cases, especially with binary data).
As you will see, the whole magic concentrates on a special HTTP header, so this tip can be used with any technology and framework, ranging from Java Server Faces (JSF) and the Seam framework to PHP and even ASP or ASP.NET.
A common way to realize file download is to simply write the file data into the HTTP response.
However, if you have to handle arbitrary files, you are not able to set a specific Content-Type header like text/plain or something else.
So the client (namely the browser) has no idea what kind of data it actually receives and what to do with it.
Here is where the HTTP header Content-Disposition comes into play. Setting this header tells the client that an attachment is received and therefore causes the browser to display a file download dialog.
Now it’s the user’s responsibility to save the file and to open it with a corresponding application.
The Content-Disposition header consists of two values: the string “attachment” indicating an attachment and a “filename” property specifying a filename for the attachment. Usually, the browser takes this value as proposed filename in the download dialog.
The following line is an example extract from an HTTP response containing a Content-Disposition header:
Content-Disposition: attachment; filename="myFile.exe"
Let’s consider having two functions:
increment(x) which increments a value x
retrieve(x) which retrieves a value x
Now let’s have two subjects (processes, threads, users) which use both functions
in order to increment the value and then retrieve it. I will give a real-life example
afterwards.
1. no race condition:
x=5
1. A: increment(x) //x=6
2. A: retieve(x) //A.x = 6
3. B: increment(x) //x=7
4. B: retieve(x) //B.x = 7
Let x be 5 before the first operation. After step 2 x holds the value 6 and
A has the correct value. After step 4 x holds the value 7 and B has also the correct
value. However let’s look at another scenario now:
2. race condition:
x=5
1. A: increment(x) //x=6
2. B: increment(x) //x=7
3. A: retrieve(x) //A.x=7
4. B: retrieve(x) //B.x=7
Now both subjects have the value 7. This problem is called a race condition as the result
depends on what subject is first and both do not know about the operations of the other.
You might think of that as a very theoretical problem, but it’s not:
Consider the following simple PHP code snippet. Let’s have
a table ‘table’ with the fields id, val1, val2. Let us INSERT a row into db and retrieve
the given id (using MySQL’s ‘auto_increment’ option) afterwards (we might use it for data
referencing).
You may have identified the possible race condition immediately: What happens if two subjects
(i.e. two users/threads) are using the script at exactly the same time?
You guessed it - if another INSERT happens to be executed right after the first INSERT, the
retrieved id will be wrong for the first subject.
However there is a very easy workaround for this problem (we will have to disable the auto_increment
option here):
The while-loop is executed until $success will be ‘true’. It is set to ‘true’ whenever the
INSERT INTO table (id, val1,val2) succedes and it only succedes, when the id, that was ‘guessed’
by the SELECT MAX(id) query, was not used before (of course considering the id being a unique primary key).
Please notice, that mysql_result($result,0,0) might return NULL if the table is empty, however this is
no problem at all as PHP is everything else than a type safe language, so $id=NULL; $id++; leads to $id being 1.
One might think that these race conditions are less likely to happen. But even if they are unlikely we should
care about all those eventualities.
If you want to display your current Affilinet earnings in your Feedreader, you can use this PHP code:
Replace the Publisher ID and the Password in the URL. You can ask Affilinet for a Publisher Webservice Password in their webinterface in the account settings.
This code is based on code by Sascha Boege.
I have written a similar script for layer-ads.de earnings.
The following might be totally obvious, but to me it was a revelation, so why not publish it.
There are situations when you have methods returning a collection.
A Java collection can be a set (no sorting, no duplicates), a list (sorting, duplicates) or a map (association key/value).
In some cases, the method might fail.
To have robust code, you would still like to return an empty set, list or collection instead of null.
Strings in the D programming language (and in many other languages too) are stored as an array of characters. Sometimes you might forget that you are still working with an array. The following code for example might result in unexpected behaviour.