Thursday, December 17, 2009

Show all files in finder

I needed to restore some files in /private/etc after deleting them. I know I should have had a backup before the delete, but I didn't. Anyway, to restore with time machine, you need to see the files in finder.

Open terminal and execute:
  • defaults write com.apple.Finder AppleShowAllFiles YES
  • sudo killall Finder
That's it, you can now see your hidden files. To revert:

defaults write com.apple.Finder AppleShowAllFiles NO
sudo killall Finder

Creds goes to these folks.

cups pdf printing on osx

I admit it, I love printing to PDF, and I get more than a little annoyed when it breaks. So, I just went through with some headaches to get cups-pdf working again for me. I had cups-pdf working just fine under 10.5.8 - going to 10.6 is where things changed. At the end of the day, I am fairly certain that I only needed to make sure I had configured my cups to print outside of a user directory e.g.

/opt/local/var/spool/cups-pdf/${USER}

I learned this little tid bit by trying to install cups-pdf from mac ports "sudo port install cups-pdf" the instructions helped a lot!

###########
As of Mac OS X 10.6, cups can no longer write into user

directories, so the output directory for cups-pdf has been
updated to reflect this.  cups-pdf will now write PDF files
into /opt/local/var/spool/cups-pdf/$USER .  You can create a
symlink to this location from Desktop to have it behave as
before:
   ln -s /opt/local/var/spool/cups-pdf/$USER ~/Desktop/cups-pdf

###########

Do this by editing your cups-pdf.conf

sudo vi /etc/cups/cups-pdf.conf

My line is now:

Out /opt/local/var/spool/cups-pdf/${USER}

Then you can make a sym link wherever you like e.g.

ln -s Out /opt/local/var/spool/cups-pdf/russellsimpkins /Users/russellsimpkins/cups-pdf

Of interest to you may be the web interface to your cups system, its located here http://localhost:631/

I had help along the way from http://www.codepoetry.net/projects/cups-pdf-for-mosx and as already mentioned the ports install of cups-pdf.

Wednesday, December 2, 2009

Setting up DAV SVN on Apache

I had to set up another SVN dav for a client today, but I have to admit that I don't do this every day. I use svn for my own stuff, but its been a while since I last set this up. Here are the steps I follow

1. Decide if this location will be a single svn dav or will there be multiple repos. If you host many projects under one dav, then you will get version numbers that are shared and increment with any change. I had this happen to me at Bluefly - its not the end of the world, but it can be a little annoying.

2. Create the folder and then the repo with

svnadmin create /folder/repo

Again, if you were to make your dav had multiple repos, you might just create /folder/repos and then do

svnadmin create /folder/repos/repo-1

3. I'm guessing you aren't going to leave your repo to just anyone, so go ahead and create an htpasswd file to at least secure PUT requests.

htpasswd -bc .svnaccess-file svn-user s3cr3tPwd

4. Modify your apache config. I bet you could do this in an .htaccess file, but I didn't try that yet.


    <Directory "/folder/repo">
       Options Indexes FollowSymLinks
       Order allow,deny
       Allow from all
    </Directory>
    <Location /svn-repo>
     DAV svn
    # This is for a single repo
     SVNPath /folder/repo

     # Limit write permission to list of valid users.
     <Limit GET PUT PROPFIND OPTIONS REPORT>
        AuthType Basic
        AuthBasicProvider file
        AuthUserFile /home/axiomgroup/.svnaccess
        AuthName "Log into the SVN Repo"
        require user svn-user
     </Limit>
    </Location>


That's about it. If you don't secure PUT, then you won't know who made the repo changes. If you remove GET from the Limit option, then your repo will be open to anyone to downlaod from.

If you were to put multiple repos under this one DAV location, then you would want to use SVNParentPath over SVNPath

Tuesday, November 17, 2009

CGI vars and their JSP equivilant

I never seem to remember the exact syntax for getting CGI variables in JSP. So, I put what I found here so I would not have to look again.

SERVER_NAME request.getServerName();
SERVER_SOFTWARE request.getServletContext().getServerInfo();
SERVER_PROTOCOL request.getProtocol();
SERVER_PORT request.getServerPort();
REQUEST_METHOD request.getMethod();
PATH_INFO request.getPathInfo();
PATH_TRANSLATED request.getPathTranslated();
SCRIPT_NAME request.getServletPath();
DOCUMENT_ROOT request.getRealPath("/");
QUERY_STRING request.getQueryString();
REMOTE_HOST request.getRemoteHost();
REMOTE_ADDR request.getRemoteAddr();
AUTH_TYPE request.getAuthType();
REMOTE_USER request.getRemoteUser();
CONTENT_TYPE request.getContentType();
CONTENT_LENGTH request.getContentLength();
HTTP_ACCEPT request.getHeader("Accept");
HTTP_USER_AGENT request.getHeader("User-Agent");
HTTP_REFERER request.getHeader("Referer");

Tuesday, November 10, 2009

Country codes for UPS

I'm not a big fan of the UPS site, so I try not to go there unless I have to - so I may be re-inventing the wheel here. I was wanting to do international shipping. While doing that, I found that UPS was kind enough to give a list of 2 Digit country codes with the full names inside their PDF. When you copy and paste, you get something like this:

Afghanistan AF
Åland Islands AX
Albania AL
Algeria DZ
American Samoa AS
Andorra AD
Angola AO
Anguilla AI
Antarctica AQ
Antigua and Barbuda AG
Argentina AR

....

This isn't the most helpful format - not really easy to parse with a bash script. I decided to continue my ruby education and create a short little Ruby script to turn this data into very basic database data. My needs were simple, so I created:

create table country (
 code char(2) not null primary key,
 country varchar(255) not null,
 enabled tinyint(1) default 0
);

I created a flat file with the data from the PDF - similar to above, but a full listing in a file and then wrote this ruby script:


#!/usr/bin/env ruby
  
# the file we want to create
writer = File.open("build-countries.sql", "w+")

# read in all the ups data and write it out in a format we want.
File.open("ups-country-codes.txt", "r") do |reader|
  while (!reader.eof)
   
    # read in a line
    line = reader.gets().chomp()

   
    # split into the 2 parts
    first = line.slice(0,line.length-3)
    second = line.slice(line.length-2,line.length )

    # ruby's string power makes formatting the output a snap
    out = "insert into country (country,code,enabled) values('#{first}','#{second}',0);"
    writer.puts(out);
  end
end

writer.close
#######################################

The result creates some nice SQL for me:

insert into country (country,code,enabled) values('Afghanistan','AF',0);
insert into country (country,code,enabled) values('Åland Islands','AX',0);
insert into country (country,code,enabled) values('Albania','AL',0);
insert into country (country,code,enabled) values('Algeria','DZ',0);
insert into country (country,code,enabled) values('American Samoa','AS',0);
insert into country (country,code,enabled) values('Andorra','AD',0);
insert into country (country,code,enabled) values('Angola','AO',0);
insert into country (country,code,enabled) values('Anguilla','AI',0);
insert into country (country,code,enabled) values('Antarctica','AQ',0);
insert into country (country,code,enabled) values('Antigua and Barbuda','AG',0);
insert into country (country,code,enabled) values('Argentina','AR',0);
......

Thursday, November 5, 2009

Using wget to check if files exist

I had a set of xml files, each file contained a url in <url></url> and I need a way to check if the URL was valid. I came up with the following bash script


#!/bin/bash
for e in `find . -type f`; do

#strip the the junk before and after to get a clean url
url=`grep url $e|sed 's/.*<url>//'|sed 's/.<\/url>//'`

# i put wget output to a file - stdout and stderr to a temp file
wget -nv --spider $url > tmp.file 2>&1

# if it was good, then a grep count should return 0
# bad files will get added to a new delete script
# good files go to a good list - this way we can count the
# results with wc -l

if [ "`grep -c 404 tmp.file`" != "0" ]; then
  echo "rm -f $e" >> delete.lst
else
  echo "$e" >> good.lst
fi

Russ

Sunday, November 1, 2009

Getting raw XML when testing Axis clients

I'm sure there's probably a really easy way to get the XML using code generated by Axis, but it wasn't so easy for me to find. I was using Axis 1.5 to do UPS's Tradablility Web Services. The web services are on https, so I didn't have a lot of luck with tcpmon to watch the data. The wsdl2java tool created a Stub and a unit test. I traced the unit test and found that the stub was making the web services calls. If you are looking, find your stub and look for where the code uses the org.apache.axis2.client.OperationClient. That code block should create a org.apache.axiom.soap.SOAPEnvelope. The SOAPEnvelope will have your XML

org.apache.axiom.soap.SOAPEnvelope env = ....

To get the XML:

String xml = env.getBody().toString();

After the web service call is made, you should see the code that gets the web service response e.g.:

org.apache.axis2.context.MessageContext _returnMessageContext = _operationClient.getMessageContext(org.apache.axis2.wsdl.WSDLConstants.MESSAGE_LABEL_IN_VALUE);

Then you can get the response XML:

org.apache.axiom.soap.SOAPEnvelope _returnEnv = _returnMessageContext.getEnvelope();

String xml = _returnEnv.getBody().toString();

I hope that makes sense.