releasing/cbrtools/perl/RemoteSite/FTP/Proxy.pm
author lorewang
Mon, 22 Nov 2010 10:56:31 +0800
changeset 700 c22eff170fac
parent 602 3145852acc89
permissions -rw-r--r--
update from trunk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     1
# Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     2
# All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     3
# This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     4
# under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     5
# which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     6
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     7
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     8
# Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     9
# Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    10
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    11
# Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    12
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    13
# Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    14
# 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    15
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    16
# Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    17
# RemoteSite::FTP::Proxy.pm
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    18
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    19
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    20
package RemoteSite::FTP::Proxy;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    21
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    22
use strict;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    23
use Net::FTP;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    24
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    25
use RemoteSite::FTP;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    26
use vars qw(@ISA);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    27
@ISA=("RemoteSite::FTP");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    28
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    29
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    30
# Initialization 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    31
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    32
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    33
sub Initialize {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    34
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    35
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    36
  my %args = @_;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    37
  $self->{proxy} = $args{proxy};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    38
  $self->{proxyUsername} = $args{proxy_username};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    39
  $self->{proxyPassword} = $args{proxy_password};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    40
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    41
  #if proxy username or password not defined ask for them interactively
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    42
  unless ($self->ProxyUsername()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    43
    print 'Proxy FTP username: ';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    44
    my $userName = <STDIN>;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    45
    if ($userName) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    46
      chomp ($userName);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    47
      $self->ProxyUsername($userName);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    48
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    49
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    50
  unless ($self->ProxyPassword()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    51
    print 'Proxy FTP password: ';
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    52
    $self->ProxyPassword(Utils::QueryPassword());
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    53
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    54
  
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    55
  #call base class initialization
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    56
  $self->SUPER::Initialize(@_);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    57
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    58
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    59
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    60
# Public getters/setters
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    61
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    62
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    63
sub Proxy {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    64
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    65
  if (defined $_[0]) {$self->{proxy} = shift;}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    66
  return $self->{proxy};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    67
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    68
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    69
sub ProxyUsername {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    70
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    71
  if (defined $_[0]) {$self->{proxyUsername} = shift;}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    72
  return $self->{proxyUsername};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    73
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    74
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    75
sub ProxyPassword {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    76
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    77
  if (defined $_[0]) {$self->{proxyPassword} = shift;}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    78
  return $self->{proxyPassword};
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    79
}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    80
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    81
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    82
# Private
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    83
#
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    84
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    85
sub Connect {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    86
  my $self = shift;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    87
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    88
  unless ($self->Proxy()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    89
    $self->HandleError("Cannot connect to proxy, host name not defined");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    90
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    91
  unless ($self->Host()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    92
    $self->HandleError("Cannot connect to FTP site from proxy, host name not defined");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    93
  } 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    94
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    95
  my $debug = (($self->{verbose} > 1) ? 1 : 0);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    96
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    97
  #Attempt to connect (or reconnect of connection fails)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    98
  for (1..$self->Reconnects()) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    99
    $self->{ftp} = undef;    
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   100
    if ($self->{verbose}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   101
      print "Connecting to proxy server ".$self->Proxy()."...\n";
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   102
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   103
    $self->{ftp} = Net::FTP->new($self->Proxy(),
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   104
				 Passive => $self->PassiveMode(),
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   105
				 Debug => $debug,
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   106
				 Timeout => $self->Timeout());
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   107
    if (defined $self->{ftp}) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   108
      # code to support Blue Coat proxy ftp server
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   109
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   110
      if ($self->{ftp}->message =~ /Blue Coat Ftp Service/) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   111
      # do BC login
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   112
      $self->{ftp}->login($self->Username().'@'.$self->Host()." ".$self->ProxyUsername(),
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   113
        $self->Password(),
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   114
        $self->ProxyPassword())
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   115
        or $self->HandleError("FTP via Blue Coat proxy login failed");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   116
      }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   117
      else {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   118
        #login to proxy server
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   119
        $self->{ftp}->login($self->ProxyUsername(), $self->ProxyPassword())
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   120
          or $self->HandleError("Proxy server login failed");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   121
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   122
        #login to ftp site from proxy server
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   123
        $self->{ftp}->login($self->Username().'@'.$self->Host(), $self->Password())
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   124
          or $self->HandleError("FTP login failed");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   125
        }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   126
      #change transfer mode to binary
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   127
      $self->{ftp}->binary()
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   128
        or $self->HandleError("Failed to set FTP server to binary transfer mode");
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   129
      return; 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   130
    }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   131
  }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   132
  $self->HandleError("Cannot connect to proxy server ".$self->Proxy());
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   133
}  
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   134
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   135
1;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   136
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   137
=head1 NAME
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   138
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   139
RemoteSite::FTP::Proxy.pm - Access a remote FTP site via a proxy.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   140
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   141
=head1 SYNOPSIS
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   142
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   143
 use RemoteSite::FTP::Proxy;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   144
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   145
 $ftp = RemoteSite::FTP::Proxy->New(host => 'ftp.somehost.com',
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   146
				    username => 'myusername',
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   147
				    password => 'mypassword',
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   148
				    proxy => 'ftp.proxyhost.com',
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   149
				    proxy_username => 'myproxyuser',
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   150
				    proxy_password => 'myproxypass',
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   151
				    verbose => 1);
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   152
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   153
 if ($ftp->FileExists('/somedir/someremotefile')) {
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   154
   do something...
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   155
 }
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   156
 $ftp->SendFile('somelocalfile', 'someremotefile');
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   157
 $ftp->GetFile('someremotefile', 'somelocalfile'); 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   158
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   159
=head1 DESCRIPTION
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   160
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   161
C<RemoteSite::FTP::Proxy> is inherited from C<RemoteSite::FTP>, it modifies base module methods to implement accessing an FTP site via a proxy server
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   162
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   163
=head1 INTERFACE
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   164
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   165
=head2 New
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   166
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   167
Passed an argument list in the form of hash key value pairs. The supported arguments are...
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   168
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   169
  host           => $host_address_string
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   170
  username       => $user_name_string
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   171
  password       => $pass_word_string
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   172
  proxy          => $proxy_address_string
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   173
  proxy_username => $proxy_username_string
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   174
  proxy_password => $proxy_password_string
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   175
  verbose        => $verbosity_integer
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   176
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   177
Returns a reference to a C<RemoteSite::FTP::Proxy> object
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   178
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   179
=head2 Proxy
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   180
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   181
Returns the current value of the C<proxy> attribute which contains the proxy FTP address. If passed an argument sets the attribute to this new value.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   182
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   183
=head2 ProxyUsername
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   184
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   185
Returns the current value of the C<proxyUsername> attribute which stores the user name required to access the proxy FTP site. If passed an argument sets the attribute to this new value.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   186
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   187
=head2 ProxyPassword
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   188
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   189
Returns the current value of the C<proxyPassword> attribute which stores the password required to access the proxy FTP site. If passed an argument sets the attribute to this new value.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   190
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   191
=head1 KNOWN BUGS
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   192
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   193
None
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   194
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   195
=head1 COPYRIGHT
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   196
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   197
 Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   198
 All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   199
 This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   200
 under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   201
 which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   202
 at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   203
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   204
 Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   205
 Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   206
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   207
 Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   208
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   209
 Description:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   210
 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   211
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   212
=cut