Archive for the ‘Quick Tips’ Category

Use a DVCS to Track Anything Instantly

Sunday, August 16th, 2009

I recently stumbled across the essay In brief praise of DVCSs by Aristotle Pagaltzis, and it was a revelation for me. I’d been wanting to try a DVCS for a while, but hadn’t really gotten around to it — the perceived benefit hadn’t justified the perceived effort. But when Aristotle pointed out that Subversion makes the mental overhead of creating a repository very much greater than any DVCS, something clicked. I saw an immediate benefit I’d gain from even simplistic usage of a DVCS, and so I immediately gave it a try.

I had a text file that I wanted to explicitly track versions of — I use Time Machine for incremental backup, which is great, but doesn’t always give me precise enough control of the snapshots, and I don’t always have the snapshots available. Once I read Aristotle’s essay, it only took me a few minutes to download and install Mercurial, and then I started tracking my file with three simple commands: hg init; hg add *; hg commit -m "initial". Quick, easy, and effective. From that point on, my folder was also a repository, and I could track specific revisions of my files by simply typing hg commit -m "update" at any point.

What I think is the killer realization here is that it is now super easy to make any folder into a repository, and instantly start tracking versions of its contents. I’ve used Subversion to make local repositories, and track local files in the past — and it really is many more steps, and much more work. It’s also messier, because the repository and the working copy must be separate entities — you have to think about where the repository should live, as opposed to the actual stuff, the working copy. With a DVSC, no such dichotomy exists. The repository and the actual stuff are one and the same. You can move them, zip them, email them — as long as the metadata directory is preserved (for example, .hg or .git), the folder remains a repository.

I’m thankful to Aristotle for helping me get past the initial barrier to entry, and start actually using a DVCS. I’m looking forward to learning more about them, and using them even more going forward.

(Bonus tip: on OS X, some applications save their data using a special type of file called a “package”, which is actually a directory with a special flag. One example is OmniGraffle, which I use frequently. Because these “files” are actually directories, they can be easily made into self-versioning files with their own self-contained repositories. Just navigate to the directory and run the commands above! From that point on, the file will be a repository and can track its own versions. Pretty useful!)

(I can’t explain exactly why I decided to try Mercurial first, before Git or Bazaar. I’ve read many comparisons of them, and something about those comparisons just made me want to try it. I’m not sure exactly how, but I had built up the impression that Mercurial was somehow simpler, cleaner, more elegant, and more approachable than Git or Bazaar. I don’t know if that’s actually true, but the impression was enough that that’s where I started.)

(Aristotle’s mention of “mental overhead” as a decisive factor in using one system over another is something I’ve been thinking about lately as well, although I’ve used the more unwieldy “cognitive overhead.”)

SQL Server Stored Procedure Introspection

Thursday, February 12th, 2009

Here at Arc90, a hallmark of our work is extremely dynamic, flexible systems. In the process of building one of our platforms, I found myself calling some ever-evolving stored procedures with large numbers of parameters. Hard coding these calls became tedious, buggy, and generally maddening. I really needed to be able to dynamically discover all the pertinent info about the parameters for each sproc, and then spin up the call on the fly. As part of the solution, I created a sproc that brings back a recordset of all input and output parameters and metadata for each. Basically this sproc looks into the system tables and extracts all of the needed info.

This turns out to be particularly useful if you have large sets of data in which the names can be dynamically mapped to the param names. It’s suddenly a concise and simple matter to dynamically make large sproc calls.This procedure is simply called by passing in the name of the sproc that you want to examine. The sproc code is commented with an explanation of each return field.

This is compatible with Sql Server 2005 and 2008.

Example call:

EXEC    spStoredProcParamLookup @ipSprocName = 'spTestSproc'

sql.png

Continue reading»

Regexing to extract HTTP status codes

Wednesday, February 4th, 2009

Today I was trying to figure out why a library I called was throwing unhandled exceptions when it called a REST web service that was (correctly) returning 401 Unauthorized. It turns out that the library, which does nothing but handle HTTP requests, throws WebExceptions, not the HttpExceptions I expected. All I had to go on was the exception message text The remote server returned an error: (401) Unauthorized.

Since HttpException exposes a GetHttpCode() method and WebException does not, the HTTP status code from the underlying service was being discarded by this library, which made it impossible for me to automate error handling by status code. The right way to solve this problem is to fix the library to throw HttpExceptions to preserve status codes. But the right now way was to use regular expressions to see when these exception messages contained a three-digit number starting with 4 or 5 (e.g. 401, 404, 500) and wrap it in my own HttpException so that the caller further up the chain can process it automatically.

Note that an exception message like 404 is not a valid credit card number would get erroneously treated as a 404 File Not Found exception. However, in this case where I know the library and the service involved I think it’s safe to do. But I’m still going to fix the offending code ASAP!

    // C#
try
{
// call the library that might throw the exception
// that ought to be an HttpException
}
catch(System.Web.HttpException) // it's already an HttpException, so throw it as-is
{
throw;
}
catch(Exception ex) // any other exception
{
// If the exception message contains a 3-digit number starting
// with 4 or 5, assume it's the correct HTTP status code.
// NOTE! Know your library to know if that's a safe assumption!
Regex httpErrorRegex = new Regex("((4|5)\\d{2})(?!\\d)");
if(httpErrorRegex.IsMatch(ex.Message))
{
int httpStatusCode = int.Parse(httpErrorRegex.Match(ex.Message)
.Captures[0].Value);
throw new HttpException(httpStatusCode, ex.Message, ex);
}
// No 3-digit HTTP error code found in the message, so default to a 500 error
throw new HttpException(500, ex.Message, ex);
}

Trac Bookmarklet

Tuesday, January 27th, 2009

Already know your Trac ticket number and don’t want to type a URL, scroll through your browser history, or have to search your Trac tickets? Customize server and path and save this bookmarklet for any Trac you use regularly:

If you save the bookmarklet to your browser’s toolbar you simply click once, enter the ticket number, and you’re in your ticket.

Encoding and XML

Tuesday, January 6th, 2009

In speccing a project last month, we discussed the best way to attach PDFs to the XML documents we send between very distributed systems. We quickly decided it would avoid a whole host of atomicity, reliability, and redesign issues to simply update our XML schema to include Base64 encoded documents inside the body of the XML document itself.

For my half of our application ecosystem I researched .NET’s Base64 encoding/decoding support, got curious about character set encoding, and set out to write a universal encoder to make it simple, easy, and guaranteed safe to insert any kind of binary or text data into an XML document.

But first, a quick nano-refresher: Character encoding specifies how a string of bytes should be mapped to specific text characters. In the simplest case, ASCII, one byte maps to one of 256 possible characters. 65=A, 66=B, …. 90=Z, etc. In Unicode, two (or even four) bytes map to thousands or even (theoretically) billions of characters. So when my program reads four bytes from a text file, I need to know if it represents four 1-byte characters, two 2-byte characters, or one 4-byte character. It’s actually even more complicated than that, but the basic problem is making sure I don’t accidentally turn a 400-byte ASCII text file into 200 Japanese characters. Or vice versa. Or garbage.

Fortunately, .NET has robust support for character encoding, so all I have to do is load the correct encoding class and ask it to take care of this for me. If I know I will only ever need to deal with Unicode, that class is Encoding.Unicode, but for maximum flexibility I can call Encoding.GetEncoding(encodingName) and get any encoding by name. Like so:

public string GetStringFromFile(string myFilename, string encodingName)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(myFilename);
System.Text.Encoding myEncoding = System.Text.Encoding.GetEncoding(encodingName);
return myEncoding.GetString(fileBytes);
}

After I’m done modifying that string I can easily convert it back into a byte array and save it, preserving the original character encoding:

public void SaveStringToFile(string myFilename, string encodingName, string myString)
{
System.Text.Encoding myEncoding = System.Text.Encoding.GetEncoding(encodingName);
byte[] fileBytes = myEncoding.GetBytes(myString);
System.IO.File.WriteAllBytes(myFilename, fileBytes);
}

But now let’s get back to my actual business problem, storing a PDF or other binary data in my XML document. Because the bytes I encounter are not supposed to represent character data, attempting to map them to characters may result in nonsense. For example, whether I decode the byte sequence 00 00 00 00 as ASCII, Unicode, or UTF-32, I get either one, two, or four null characters that will screw up string processing. Note that use of CDATA sections doesn’t help. Only being very lucky about which byte sequences I encounter would avert disaster, and I don’t like writing lucky code.

Enter Base64, which is designed for exactly this purpose: encoding arbitrary binary data into a string guaranteed to consist of only “safe” ASCII characters and decoding that string back to bytes with 100% fidelity later. Microsoft places Base64 functionality under the System.Convert class, not System.Text.Encoding because it’s more of a conversion and translation process, not a direct byte-to-character encoding like those described above.

To read a file into a Base64 string:

public string GetBase64StringFromFile(string myFilename)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(myFilename);
return Convert.ToBase64String(fileBytes, Base64FormattingOptions.InsertLineBreaks);
}

And to decode it and save it back to the filesystem:

public void SaveBase64StringToFile(string myFilename, string myString)
{
byte[] fileBytes = Convert.FromBase64String(myString);
System.IO.File.WriteAllBytes(myFilename, fileBytes);
}

I combined both “real” character encodings and Base64 encoding in my XmlFileEncoder class (attached) to provide unified access to both encodings when working with XML documents. If you know you’re dealing with Unicode, simply call XmlFileEncoder.InsertFileIntoXmlDocument with the encoding UTF-16 and the file will safely be inserted as text. If you don’t always know the file format, or you are dealing with binary files, simply call the same method with the encoding Base64. In either case, a new node will be added to contain your file data and the encoding attribute will record the encoding method so XmlFileEncoder.ExtractFileFromXmlDocument will use the correct character/Base64 decoding automatically.

The demo WinForms app starts up displaying an XML document with some UTF-16 data already encoded into it. Use the controls along the bottom to experiment with inserting differently encoded files (provided, or use your own) using different application character encodings. Some files will clearly look wrong in the XML when you select the wrong encoding. Others may look correct, or almost correct, but when you press the SaveEncodedFile to File button it will report Copy accuracy FAILED when it verifies against the source file. However, the files encoded and decoded using Base64 will always copy accurately. The only downside is that Base64 encoded data is always 1/3 bigger and much less human readable than the source.

You can find the source code here: XmlEncodingDemo.zip

Have a happy, healthy, and correctly encoded 2009!

Gotcha: Adding Metadata to a Flex Component

Monday, December 8th, 2008

Recently, I noticed that as I was adding event metadata to my custom Flex components, Flex Builder wasn’t giving me code hints for those configured events. For example, I would do the following:

[Event(name='complete', type='flash.events.Event')]

Since I wasn’t seeing the event in the code hints list I looked at some inner Flex framework components like Button or TextInput to see what I was doing wrong. Everything matched but then I realized the only difference was that they used double quotes. Aha! Once I switched the above metadata example to the following, everything worked out:

[Event(name="complete", type="flash.events.Event")]

The complete event showed up in the code hints list when trying to add an event listener for my custom component. This isn’t a complaint on Flex Builder, as I normally use double quotes–so I blame this on laziness and copying and pasting the metadata line but to others be careful and note to see the event (or any other metadata type e.g. styles) show up in the code hints list always use double quotes!

Java Quick Tip: Parameterized Logging

Tuesday, December 2nd, 2008

I hate writing ugly, concatenated logging statements like this:

package com.arc90.example;
import java.util.logging.Logger;
public class LoggingExample1
{
public static void main(String[] args)
{
Logger logger = Logger.getLogger(LoggingExample1.class.getName());
String user = "dougb";
int logins = 17;
int purchases = 2;
logger.info("User " + user + " has logged in " + logins +
" times and made " + purchases + " purchases.");
}
}

Continue reading»

Enabling SSL on Windows with PHP and the Zend Framework

Tuesday, November 11th, 2008

Ever tried to use HTTPS with PHP and the Zend Framework on Windows, but only received an error for all your troubles? Perhaps an error like:

Unable to Connect to ssl://( your host name):443.
Error #175113224: Unable to find the socket transport "ssl" -
did you forget to enable it when you configured PHP?

The problem lies in your version of OpenSSL and certain configuration parameters.

HTTPS relies on SSL to transport your data securely and PHP usually uses the OpenSSL implementation of SSL. Problems occur when your versions of OpenSSL are out of sync with each other.

The best option is to always keep ALL of your software 100% up-to-date at all times–but sometimes you are not able to do a full software update for one reason or another.

Continue reading»

Ignore Double Slashes in URLs in Restlet Applications

Wednesday, November 5th, 2008

Every once in a while, people accidentally type two slashes instead of one in their URLs.

The Apache httpd server will automatically ignore the double slashes, and treat the URL as if the double slash was a single slash. This is helpful; the server is giving the user what they want, even though they technically didn’t ask for it correctly.

Reslet’s Router class, however, isn’t so forgiving. If you accidentally include a double-slash in the URL of a request, a Restlet application will probably return a 404 Not Found status code.

Thankfully, Restlet’s request handling process is so flexible, we can easily add a Filter which will cause the application to ignore double slashes, just like httpd.

If your application uses a subclass of Restlet’s Application class, then it is overriding the default getRoot() method to provide your own request handling Restlets.

A standard getRoot() might look something like this:

@Override
public Restlet getRoot()
{
Router router = new Router();
// Attach all the routes
router.attach("path", ResourceClass.class);
return router;
}

In order to remove double slashes from request URLs before they reach the router, we can create an anonymous Filter class and return it as
the root of the application:

@Override
public Restlet getRoot()
{
Filter doubleSlashFilter = new Filter() {
@Override
protected int beforeHandle(Request request, Response response) {
Reference ref = request.getResourceRef();
String originalPath = ref.getPath();
if (originalPath.contains("//"))
{
String newPath = originalPath.replaceAll("//", "/");
ref.setPath(newPath);
}
return Filter.CONTINUE;
}
};
Router router = new Router();
// Attach all the routes
router.attach("path", ResourceClass.class);
doubleSlashFilter.setNext(router);
return doubleSlashFilter;
}

And that’s it! The application will now ignore double slashes, and will successfully serve requests that accidentally include them.

Instant Web Sharing via Python

Monday, October 27th, 2008

This is a neat trick I saw from ‘mrlawlsome’ on reddit and didn’t want to pass up.

Ever been on an internal network and wanted to share a file or two quickly? This little *nix script will let you do so in a snap. (And it works on Mac OS X, too!)

Continue reading»