Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Monday, January 25, 2016

SOAP/WSDL on Perl, Ruby, PHP or Python (anything but .NET/Java)

SOAP was designed to provide cross platform RPC but the reality is that the only usable implementations of the spec are on .NET and Java.  Yes there are several available for other programming languages but they are incomplete or broken (they may or may not work).  Want to know what ALWAYS works?  Read on.

My recommendation is to avoid them completely and download SoapUI (which is written in Java).  This desktop software will allow you to create a new project using the WSDL (file or URL) and view and manually create requests against a service.  You can see the raw data exchange (HINT: There can be additional required HTTP headers you need to pass.  SOAPAction is one that is almost ALWAYS used) and then using whatever language you want create a web client that POSTs the XML (templated so you can change various parameters) and then parse the resulting XML response to get the data you want.  Every programming language has a decent XML parser so that shouldn't be a problem (although sometimes you have "massage" the resulting XML).

Here is a simple example of a client in PHP:

        $soap_url = "https://blahblah.com:1234/Service";
        $soap_act = '"/SomeCall"'; // Use SoapUI raw view of request to find this
        $soap_xml = <<<ENDOFFILE
        <soapenv .......... </soapenv ...............
        ENDOFFILE;

        $resp = \Httpful\Request::post($soap_url)
            ->body($soap_xml)
            ->sends('text/xml')
            ->addHeader('SOAPAction', $soap_act)
            ->timeoutIn(10)
            ->send();

        // There are ways to make the PHP XML parser handle namespaces but usually
        // we don't need them so make them simple XML elements instead
        // (eg. s_Envelope)

        $xml_str = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '',
                               $resp->body);
        $xml_str = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2_$3", $xml_str);
        $xml_str = preg_replace("/ \w+(:\w+)?=\"[^\"]*\"/", "", $xml_str);

        $xml = new \SimpleXMLElement($xml_str);
        $body = $xml->xpath('//s_Envelope'); // Can use this to narrow it down

        $array = json_decode(json_encode((array)$body), TRUE);

        // Now you can access the data using $array['key_name'][1] etc.. etc..

        print_r($array);

Good luck and hopefully this will save you some time! :)

Thursday, April 7, 2011

Checking URL HTTP response codes from the command line.

I was recently looking for a good way to check a specific url for response codes using curl. I came across the following blog post. Kudos to the author for posting the solution.

Friday, October 8, 2010

HTML::Template CACHE LOAD, HIT and MISS

If you are sure you have mod_perl running and are using cache_debug to diagnose issues with repeated cache LOAD and cache MISS with HTML::Template then the following information might be useful.

Digging through the HTML::Template source I discovered the following:

my @key = ($options->{filepath});
push(@key, @{$options->{path}});
push(@key, $options->{search_path_on_include} || 0);
push(@key, $options->{loop_context_vars} || 0);
push(@key, $options->{global_vars} || 0);


The key under which the cached template is stored is a md5_hex of the previous values passed to the HTML::Template->new() method.  If you are pre-loading your templates in your startup with one set of any of these parameters but are later loading the same template with another set then you will see another CACHE LOAD.

For example if you pre-cache in your startup with:

HTML::Template->new( filename => '/my/path/templates/t.html', cache => 1);

and later in a Registry cgi you create a template object with:

my $tmpl = HTML::Template->new( filename => '/my/path/templates/t.html',  cache => 1, global_vars => 1);

you will see a CACHE LOAD rather than a CACHE HIT when /my/path/templates/t.html is loaded.  The end result is that you maybe loading and caching multiple copies of the same template unnecessarily and wasting memory.

Wednesday, September 2, 2009

Finding perl packages with APT

If you are attempting to satisfy dependencies for perl software that was NOT available via your favourite repository (this is sometimes unavoidable in enterprise environments) you should AT LEAST attempt to satisfy those dependencies using said repository. The easiest solution is to install 'apt-file' from your repo and for any perl modules you can simply call:
apt-file search "LWP::UserAgent"

This will return the following list on Debian Lenny:
liblwp-useragent-determined-perl: /usr/share/man/man3/LWP::UserAgent::Determined.3pm.gz
libtest-mock-lwp-perl: /usr/share/man/man3/Test::Mock::LWP::UserAgent.3pm.gz
libwww-perl: /usr/share/man/man3/LWP::UserAgent.3pm.gz

Now you know that in order to satisfy a dependency on "LWP::UserAgent" you would need to install the 'libwww-perl' module from the repo.

If the module is not available via the repo you should next consider installing the package into a custom perl lib directory specifically for the software (and/or user account) in question. To do this read the documentation for the local::lib module in CPAN.

Ultimately your goal should be to not install non-packaged software to /usr/local and by doing so increase the maintainability of your systems.

Wednesday, August 12, 2009

perl MIME::Lite sendmail Return-Path fix/override

I'm not sure this affects the newest version of MIME::Lite; however, if you find that you are having issues with your messages being rejected or marked as spam, due to an internal host being added to the Return-Path, the following fix might work for you.

Rather than using:
my $ml = MIME::Lite-new(
    From => ...
To => ...
...
);

$ml->send();

Use the following:
$ml->send_by_sendmail(SetSender => 1);

Thursday, August 6, 2009

Find page count in TIFF or PDF with perl

The perl module you'll want to install is called PerlMagick (Image::Magick) and rather than loading the image into memory you can just use the Ping method. The Ping method returns an array with 4 elements of information for each page of a multi-page TIFF/PDF. If we divide the number of elements by 4 we know how many pages there are in the TIFF/PDF.
use Image::Magick;

my $im = Image::Magick->new();

my @ping_info = $im->Ping('fax.tif');

## If we access an @rray in a scalar context we get the element count
my $count = @ping_info / 4;

print $count;

There you have it, good luck.

UPDATE:

The example above may or may not work properly with PDF's.  Even if it does work it appears that ImageMagick makes an external call to the ghostscript (gs) executable on the system to determine the properties of the pdf.  Here is an example that will work for PDFs:
use PDF::API2;

my $pdf = PDF::API2->open('2.pdf');
print $pdf->pages();

I'm guessing that eventually ImageMagick won't be "broken" but you can use the above in the meantime.

UPDATE:

We recently updated to Debian 6.0 and GhostScript seems to have problems with PDFs rendered by various software (it takes 5 minutes for it to get a page count on a 4 page document).  My first line of defense for checking page counts in a pdf is the following for PDFs:
open(FH, "$temp_file");
for my $line (<FH>) {
    if ($line =~ m/\/Count\s+(\d+)/) {
        $page_count = $1;
    }
}
close(FH);

Don't be tempted to stop searching for the Count after the first match. Some PDFs will list a count for every page (like a page number). You'll want to use whatever the last match is for your actual count. If this code doesn't produce a page count I do the Image::Magick Ping. That appears to cover all scenarios I've come across.