Advertisement
rplantiko

Connecting TWiki with SAP

Feb 10th, 2013
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.24 KB | None | 0 0
  1. # SapConnectPlugin
  2. # Get documentation or code from an SAP system
  3. # The information can be made available as a link, or directly embedded into the topic
  4. # See Plugin topic for history and plugin information
  5.  
  6. package TWiki::Plugins::SapConnectPlugin;
  7.  
  8. use strict;
  9. use warnings;
  10.  
  11. # Can be activated for development if running Twiki on Apache w/ mod_perl
  12. # use Apache2::Reload;  
  13.  
  14. # Exports an API function for access in scripts or other modules
  15. use base qw(Exporter);
  16. our @ISA = qw(Exporter);
  17. our @EXPORT_OK = qw(get_data_from_sap);
  18.  
  19. # For building URLs
  20. use URI;
  21. use URI::QueryParam;
  22.  
  23. # For performing the HTTP request
  24. use LWP::UserAgent;
  25.  
  26. use TWiki::Func;
  27.  
  28. our ($VERSION,$pluginName);
  29.  
  30. # Data for the SAP connections
  31. our ($scheme,%sapDest,$sapPath,$defaultDest);
  32.  
  33. # Parameters which will be routed to SAP
  34. my @ROUTED_PARAMETERS;
  35.  
  36. BEGIN {
  37.  
  38. # Version number, to avoid conflicts in initPlugin()  
  39.   $VERSION = '1.000';
  40.  
  41. # Plugin name should always be defined
  42.   $pluginName = 'SapConnectPlugin';
  43.  
  44. # Parameters which will be routed to SAP
  45.   @ROUTED_PARAMETERS = qw/level noheader expand/;
  46.  
  47.   }
  48.  
  49. # API function for usage in other modules or scripts
  50. sub get_data_from_sap {
  51.   my %params = @_;
  52.  
  53. # External calls have to load the system settings first
  54.   readSapDestinations() unless $defaultDest;
  55.  
  56.   my $dest   = $params{dest} || $defaultDest;
  57.   my $id     = $params{id}   || "";
  58.   my $args   = $params{args} || {};
  59.   my $type   = $params{type} or die "Please specify request type\n";  
  60.  
  61.   $args->{_DEFAULT} = $id ? "$type.$id" : $type;
  62.    
  63.   my $url = get_url_from_dest($params{dest});
  64.   enrich_url_from_tag_args( $url, 'addi', $args );    
  65.   return get_request( $url );  
  66.  
  67.   }
  68.  
  69. # TWiki hook "initPlugin"
  70. sub initPlugin {
  71.  
  72. # Read the SAP destinations from Main::TWikiPreferences
  73.   readSapDestinations( );
  74.  
  75. # No version conflicts: Everything OK
  76.   return 1;
  77.  
  78.   }
  79.  
  80. # TWiki hook "commonTagsHandler"
  81. sub commonTagsHandler {
  82.  
  83. # Common Syntax of all tags handled by this plugin
  84.  
  85. # INCLSAPDOCU - Include SAP documentation into TWiki topic  
  86. # INCLSAPCODE - Include SAP code into TWiki topic
  87. # URLSAPDOCU  - Only provide a hyperlink to SAP docu topic
  88. # URLSAPCODE  - Only provide a hyperlink to SAP code topic
  89. # INCLSAPADDI - Additional function
  90.   $_[0] =~ s/%(INCL|URL)SAP(DOCU|CODE|ADDI)\{(.*?)\}%/handle_single_tag($1,$2,$3)/ge ;
  91.    
  92.   }
  93.  
  94.  
  95. sub handle_single_tag {
  96.  
  97.   my $handler  = ( $_[0] eq 'INCL' ) ? \&incl_from_sap : \&url_for_sap;
  98.   my $type     = lc $_[1];
  99.   my %tag_args = TWiki::Func::extractParameters( $_[2] );
  100.  
  101.   my $result = eval {
  102.     local $SIG{__DIE__};   # avoiding interference with CGI::Carp
  103.     &$handler( $type, \%tag_args );
  104.     };
  105.   return errorMsg($@) if $@;
  106.   return $result;
  107.  
  108.   }
  109.  
  110.  
  111. # Include TWiki content from SAP
  112. sub incl_from_sap {
  113.  
  114.   my ($type,$tag_args) = @_;
  115.    
  116.   my $url = get_url_from_dest($tag_args->{dest});
  117.  
  118.   enrich_url_from_tag_args( $url, $type, $tag_args );  
  119.  
  120.   my $result = get_request( $url );  
  121.   preformat( \$result ) if $type eq 'code';
  122.  
  123.   return $result;
  124.  
  125.   }
  126.  
  127. # Generates an URL for docu or code display  
  128. sub url_for_sap {  
  129.  
  130.   my ($type,$tag_args) = @_;
  131.  
  132.   my $url = get_topic_url_for_link( $type );
  133.   enrich_url_from_tag_args( $url, '', $tag_args);
  134.  
  135.   return $url->as_string;
  136.  
  137.   }  
  138.  
  139.  
  140. # Add parameters from TWiki tag args to URL
  141. sub enrich_url_from_tag_args {
  142.   my ($url,$type,$args) = @_;
  143. # Handle the default arg first  
  144.   enrich_url_from_default_arg( $url, $type, $args->{_DEFAULT} );
  145. # Handle further arguments    
  146.   my @param = $type eq 'addi' ? get_addi_parameters($args) : @ROUTED_PARAMETERS;
  147.   foreach my $name (@param) {
  148.     next unless my $value = $args->{$name};
  149.     for ($value) {
  150.        s/^true$/X/;
  151.        s/^false$/ /;
  152.        };
  153.     $url->query_param( $name, $value );
  154.     }
  155.   }
  156.  
  157.  
  158. # Analyze the default argument of the TWiki tag
  159. sub enrich_url_from_default_arg {
  160.   my ($url,$type,$default_arg) = @_;
  161.   my ($id,$path_ext);
  162.  
  163.   if ($type ~~ ['docu','code','']) {
  164.     check_docu_key($default_arg);
  165.     ($path_ext,$id) = ($type,$default_arg);
  166.     }
  167.   else {
  168.    return unless $default_arg;
  169.    ($path_ext,$id) =  $default_arg =~ /(.*?)\.(.*)/ ?
  170.       ($1,$2) : ($default_arg,"");
  171.     }  
  172.   $url->path( $url->path . lc $path_ext );
  173.   $url->query_param('id',$id) if $id;    
  174.   }
  175.  
  176.  
  177. # Returns an URI object for the connection to SAP,
  178. # Ready for adding path extension and further query params
  179. sub get_url_from_dest {
  180.   my $dest = uc ( shift || $defaultDest );
  181.   my $domainAndPort = $sapDest{$dest} or die "Unknown system $dest\n";
  182.   return URI->new("$scheme://$domainAndPort$sapPath");
  183.   }
  184.  
  185.  
  186. # Create URL for Topic names
  187. # These topics should be general-purpose,
  188. # containing an %INCLSAPDOCU% or #INCLSAPCODE# tag, respectively
  189. sub get_topic_url_for_link {  
  190.   my $type = shift;  
  191. # Map :
  192. #  'docu' to TWiki.SapDocu
  193. #  'code' to TWiki.SapCode
  194.   my @topic = ('TWiki','Sap'.ucfirst lc $type);
  195.   return URI->new( TWiki::Func::getScriptUrl(@topic,"view") );    
  196.   }  
  197.  
  198.  
  199. # Is the argument a valid docu key, like e.g. "RE.ZZ_REPORT"?
  200. sub check_docu_key {
  201.   my $arg = shift;
  202.   $arg =~ /^(\w\w)\.([^.]+)/i or
  203.     die "Illegal key for docu object: $arg\n";
  204.   }
  205.  
  206.  
  207. # The parameters of an INCLSAPADDI tag which are to be routed to SAP
  208. sub get_addi_parameters {
  209.   my $args = shift;
  210.   my @param;
  211.   for my $name (keys %$args) {
  212.     next if $name eq '_DEFAULT' or $name eq 'dest';
  213.     push @param, $name;
  214.     }
  215.   return @param;  
  216.   }  
  217.  
  218.  
  219. # Perform the actual HTTP request
  220. sub get_request {
  221.    
  222.   my $url = shift;
  223.      
  224.   my $client = LWP::UserAgent->new;
  225.   $client->timeout(10);
  226.   $client->env_proxy;
  227.  
  228.   my $response = $client->get($url);
  229.    
  230.   if ($response->is_success) {
  231.    return $response->decoded_content;
  232.    }
  233.   else {
  234.     die "HTTP request error: ".$response->status_line."\n";
  235.     }
  236.    
  237.   }
  238.  
  239.  
  240. # For code display: Preformats argument
  241. sub preformat {
  242.   my $content = shift;
  243.   for ($$content) {
  244. # Encode HTML-Tags and escape symbol
  245.     s/\&/\&/sg;
  246.     s/</\&lt;/sg;
  247. # Different style for ABAP comments
  248.     s/^(\*.*)$/<span style="color:blue">$1<\/span>/sg;
  249. # Wrap everything in a <PRE> ... </PRE>
  250.     $_ = "<pre>\n$_\n</pre>";
  251.     }  
  252.   }
  253.  
  254. # Read the available SAP systems and the request path (once, early)
  255. sub readSapDestinations {
  256.  
  257. # We constantly use the scheme http (dynamize here if necessary)
  258.   $scheme = 'http';
  259.  
  260.   my $systemsParameter = TWiki::Func::getPluginPreferencesValue( "SYSTEMS" );
  261.  
  262. # First system in parameter =: default
  263.   ($defaultDest) = $systemsParameter =~ /(\w+)/;  
  264.  
  265. # %sapDest: Hash containing system infos: domain and port
  266.   %sapDest = $systemsParameter =~ /(\w+):([\w.]+:\d{4})\s*(?:,|$)\s*/g;
  267.  
  268. # Get request path for SAP system
  269.   $sapPath = TWiki::Func::getPluginPreferencesValue( "PATH" )  || "/sap/bc/twiki/";
  270.  
  271. # Patch path with / if necessary
  272.   $sapPath .= "/" unless $sapPath =~ /\/$/;
  273.   $sapPath = "/" . $sapPath unless $sapPath =~ /^\//;
  274.  
  275.   }
  276.  
  277.  
  278. # Produce an HTML-formatted error string
  279. sub errorMsg {
  280.  
  281.   my $theMsg = shift;
  282.  
  283.   $theMsg =~ s/&/&amp;/g;
  284.   $theMsg =~ s/</&lt;/g;
  285.  
  286.   $theMsg = '<pre style="color:red;font-weight:bold">' .
  287.          $theMsg .
  288.          '</pre>' ;
  289.   return $theMsg;
  290.  
  291.   }
  292.  
  293.  
  294. 1;
  295.  
  296. # EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement