| 1 | # Tridion::BusinessConnector | ||||||||||||||||||||
| 2 | # written by Toby Corkindale (perl (at) corkindale.net) | ||||||||||||||||||||
| 3 | # Copyright (c) 2004 Toby Corkindale, All rights reserved. | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # $Id: BusinessConnector.pm 18 2005-12-21 16:38:11Z tjc $ | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | # This Perl module is distributed under the terms of the LGPL: | ||||||||||||||||||||
| 8 | # This library is free software; you can redistribute it and/or | ||||||||||||||||||||
| 9 | # modify it under the terms of the GNU Lesser General Public | ||||||||||||||||||||
| 10 | # License as published by the Free Software Foundation; either | ||||||||||||||||||||
| 11 | # version 2.1 of the License, or (at your option) any later version. | ||||||||||||||||||||
| 12 | # | ||||||||||||||||||||
| 13 | # This license can be found at http://www.gnu.org/licenses/lgpl.html | ||||||||||||||||||||
| 14 | # | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | # This module will help you interface Perl programs to Tridion(tm)'s | ||||||||||||||||||||
| 17 | # content management system. | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | package Tridion::BusinessConnector; | ||||||||||||||||||||
| 20 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 2 | ▶ | use XML::LibXML; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 2 | our $VERSION = '0.04'; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | # Tridion namespaces: (Current as of 2004 - may need to be updated one day?) | ||||||||||||||||||||
| 28 | 2 | our $TCM_NS = 'http://www.tridion.com/ContentManager/5.0'; | |||||||||||||||||||
| 29 | 2 | our $TCMAPI_NS = 'http://www.tridion.com/ContentManager/5.0/TCMAPI'; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # Initialise the SOAP subsystem: | ||||||||||||||||||||
| 33 | use SOAP::Lite | ||||||||||||||||||||
| 34 | 2 | ▶ | on_fault => sub { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.2
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 35 | 0 | my $soap = shift; | |||||||||||||||||||
| 36 | 0 | my $res = shift; | |||||||||||||||||||
| 37 | 0 | ▶ | ref $res ? die(join "\n", "--- SOAP FAULT ---", $res->faultcode, | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | $res->faultstring, '') | ||||||||||||||||||||
| 39 | : die(join "\n", "--- TRANSPORT ERROR ---", | ||||||||||||||||||||
| 40 | $soap->transport->status, ''); | ||||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | 2 | ; | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 46 | 0 | my $proto = shift; | |||||||||||||||||||
| 47 | 0 | my %args = @_; | |||||||||||||||||||
| 48 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 49 | 0 | my $self = {}; | |||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | $self->{hostname} = $args{hostname}; | |||||||||||||||||||
| 52 | 0 | $self->{username} = $args{username}; | |||||||||||||||||||
| 53 | 0 | $self->{password} = $args{password}; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | $self->{_parser} = new XML::LibXML; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | $self->{_soaplite} = new SOAP::Lite | ||||||||||||||||||||
| 58 | uri => 'http://schemas.xmlsoap.org/soap/encoding/', | ||||||||||||||||||||
| 59 | 0 | proxy => 'http://' . $self->{hostname} . '/BCListener/services/urn:bc' | |||||||||||||||||||
| 60 | ; | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | $self->{_soaplite}->transport->credentials( | ||||||||||||||||||||
| 63 | $self->{hostname} . ':80', | ||||||||||||||||||||
| 64 | $self->{hostname}, | ||||||||||||||||||||
| 65 | $self->{username}, | ||||||||||||||||||||
| 66 | $self->{password} | ||||||||||||||||||||
| 67 | 0 | ); | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | bless($self, $class); | |||||||||||||||||||
| 70 | 0 | return $self; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | sub execute | ||||||||||||||||||||
| 75 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
execute: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 76 | 0 | my $self = shift; | |||||||||||||||||||
| 77 | 0 | my $requestXML = shift; | |||||||||||||||||||
| 78 | 0 | my $method = SOAP::Data->name('execute') | |||||||||||||||||||
| 79 | ->attr({xmlns => 'urn:bc'}); | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | my $obj = $self->{_soaplite}->call($method => ('default', $requestXML)); | |||||||||||||||||||
| 82 | 0 | return $obj->result; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | sub GetItem | ||||||||||||||||||||
| 87 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
GetItem: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 88 | 0 | my $self = shift; | |||||||||||||||||||
| 89 | 0 | my $uri = shift; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | ▶ | die("Invalid TCM URI: $uri\n") unless ($uri =~ /^tcm:\d+(\-\d+)*$/); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | my $requestXML =<<EOM; | |||||||||||||||||||
| 94 | <tcmapi:Message xmlns:tcmapi="http://www.tridion.com/ContentManager/5.0/TCMAPI" | ||||||||||||||||||||
| 95 | version="5.0" from="SOAPMod" failOnError="true"> | ||||||||||||||||||||
| 96 | <tcmapi:Request ID="Request1" preserve="false"> | ||||||||||||||||||||
| 97 | <tcmapi:GetItem itemURI="$uri" /> | ||||||||||||||||||||
| 98 | </tcmapi:Request> | ||||||||||||||||||||
| 99 | </tcmapi:Message> | ||||||||||||||||||||
| 100 | EOM | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | my $result = $self->execute($requestXML); | |||||||||||||||||||
| 103 | 0 | my $xml = $self->{_parser}->parse_string($result); | |||||||||||||||||||
| 104 | 0 | ▶ | if ($xml->findvalue('/tcmapi:Message/tcmapi:Response/@success') ne 'true') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | 0 | die("---- Request Failed, Dumping output ----\n$result\n"); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | # get the first, and only, child of <Result>, and make that the new | ||||||||||||||||||||
| 109 | # document root | ||||||||||||||||||||
| 110 | 0 | my ($data) = $xml->documentElement()->findnodes('/tcmapi:Message/tcmapi:Response/tcmapi:Result/*'); | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 0 | my $newdoc = new XML::LibXML::Document; | |||||||||||||||||||
| 113 | 0 | $newdoc->setDocumentElement($data); | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 0 | return $newdoc; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | sub SaveItem | ||||||||||||||||||||
| 120 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
SaveItem: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 121 | 0 | my $self = shift; | |||||||||||||||||||
| 122 | 0 | my $xml = shift; | |||||||||||||||||||
| 123 | 0 | my $uri = shift; | |||||||||||||||||||
| 124 | 0 | my $context_uri = shift; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | ▶ | die("Invalid TCM URI: $uri\n") unless ($uri =~ /^tcm:\d+(\-\d+)*$/); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 0 | ▶ | die("Invalid TCM URI: $context_uri\n") unless ($context_uri =~ /^tcm:\d+(\-\d+)*$/); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 0 | my $requestDoc = new XML::LibXML::Document; | |||||||||||||||||||
| 130 | 0 | my $root = $requestDoc->createElement('Message'); | |||||||||||||||||||
| 131 | 0 | $root->setNamespace($TCMAPI_NS, 'tcmapi', 1); | |||||||||||||||||||
| 132 | 0 | $root->setAttribute('version', '5.0'); | |||||||||||||||||||
| 133 | 0 | $root->setAttribute('from', 'SOAPMod'); | |||||||||||||||||||
| 134 | 0 | $root->setAttribute('failOnError', 'true'); | |||||||||||||||||||
| 135 | 0 | $requestDoc->setDocumentElement($root); | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | my $node = $requestDoc->createElement('tcmapi:Request'); | |||||||||||||||||||
| 138 | 0 | $node->setAttribute('ID', 'Request1'); | |||||||||||||||||||
| 139 | 0 | $node->setAttribute('preserve', 'false'); | |||||||||||||||||||
| 140 | 0 | $root->addChild($node); | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 0 | my $savenode = $requestDoc->createElement('tcmapi:SaveItem'); | |||||||||||||||||||
| 143 | 0 | $savenode->setAttribute('itemURI', $uri); | |||||||||||||||||||
| 144 | 0 | $savenode->setAttribute('contextURI', $context_uri); | |||||||||||||||||||
| 145 | 0 | $savenode->setAttribute('doneEditing', 'true'); | |||||||||||||||||||
| 146 | 0 | $node->addChild($savenode); | |||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | # set 'itemType' attr to correct type | ||||||||||||||||||||
| 149 | 0 | my $type = $xml->documentElement->nodeName; | |||||||||||||||||||
| 150 | 0 | $type =~ s/^\w+://; | |||||||||||||||||||
| 151 | 0 | $savenode->setAttribute('itemType', $type); | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | # And add the provided component or whatever. | ||||||||||||||||||||
| 154 | 0 | $savenode->addChild($xml->documentElement); | |||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 0 | my $result = $self->execute($requestDoc->toString); | |||||||||||||||||||
| 157 | 0 | $xml = $self->{_parser}->parse_string($result); | |||||||||||||||||||
| 158 | 0 | ▶ | if ($xml->findvalue('/tcmapi:Message/tcmapi:Response/@success') ne 'true') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 0 | die("---- Request Failed, Dumping output ----\n$result\n"); | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | # get the first, and only, child of <Result>, and make that the new | ||||||||||||||||||||
| 163 | # document root | ||||||||||||||||||||
| 164 | 0 | my ($data) = $xml->documentElement()->findnodes('/tcmapi:Message/tcmapi:Response/tcmapi:Result/*'); | |||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 0 | my $newdoc = new XML::LibXML::Document; | |||||||||||||||||||
| 167 | 0 | $newdoc->setDocumentElement($data); | |||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 0 | return $newdoc; | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | # In case you need to discover the proper values for realm or netloc for | ||||||||||||||||||||
| 174 | # your own server, uncomment these: | ||||||||||||||||||||
| 175 | #sub SOAP::Transport::HTTP::Client::get_basic_credentials | ||||||||||||||||||||
| 176 | #{ | ||||||||||||||||||||
| 177 | # my($self, $realm, $uri, $proxy) = @_; | ||||||||||||||||||||
| 178 | # warn "Realm[$realm]\nURI[$uri]\nproxy[$proxy]\n"; | ||||||||||||||||||||
| 179 | # warn "host_port[" . $uri->host_port . "]\n"; | ||||||||||||||||||||
| 180 | #} | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 2 | 1; | |||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||