| 1 | # ===================================================================== | ||
| 2 | # Copyright (c) 2002,2003 IBM Corporation | ||
| 3 | # All rights reserved. This program and the accompanying materials | ||
| 4 | # are made available under the terms of the Common Public License v1.0 | ||
| 5 | # which accompanies this distribution, and is available at | ||
| 6 | # http://www.opensource.org/licenses/cpl.php | ||
| 7 | # | ||
| 8 | # ===================================================================== | ||
| 9 | package LS::Client::FTP; | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use vars qw( @ISA ); | ||
| 15 | |||
| 16 | use File::Temp; | ||
| 17 | use Net::FTP; | ||
| 18 | |||
| 19 | use LS; | ||
| 20 | use LS::ID; | ||
| 21 | use LS::Client; | ||
| 22 | use LS::Service; | ||
| 23 | |||
| 24 | |||
| 25 | @ISA = ( 'LS::Client' ); | ||
| 26 | |||
| 27 | sub new { | ||
| 28 | |||
| 29 | my ($self, %options) = @_; | ||
| 30 | |||
| 31 | unless(ref $self) { | ||
| 32 | |||
| 33 | $self = bless { | ||
| 34 | |||
| 35 | %options, | ||
| 36 | |||
| 37 | }, $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | return $self; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub getContent { | ||
| 44 | |||
| 45 | my ($self, %options) = @_; | ||
| 46 | |||
| 47 | unless($options{'url'}) { | ||
| 48 | |||
| 49 | $self->recordError( 'Invalid FTP URL in sub getContent' ); | ||
| 50 | $self->addStackTrace() ; | ||
| 51 | |||
| 52 | return undef; | ||
| 53 | } | ||
| 54 | |||
| 55 | my $url = $options{'url'}; | ||
| 56 | |||
| 57 | my $host = $url->host; | ||
| 58 | my $port = $url->port; | ||
| 59 | my $path = $url->path; | ||
| 60 | $path =~ s|^/+||; | ||
| 61 | |||
| 62 | my $user = $url->user; | ||
| 63 | my $password = $url->password; | ||
| 64 | |||
| 65 | my $ftp = Net::FTP->new($host, Port=> $port); | ||
| 66 | |||
| 67 | unless ($ftp) { | ||
| 68 | |||
| 69 | $self->recordError('Error initializing FTP client'); | ||
| 70 | $self->addStackTrace(); | ||
| 71 | |||
| 72 | return undef; | ||
| 73 | } | ||
| 74 | |||
| 75 | unless ($ftp->login($user, $password)) { | ||
| 76 | |||
| 77 | $ftp->quit; | ||
| 78 | |||
| 79 | $self->recordError('Login failed in sub getContent'); | ||
| 80 | $self->addStackTrace(); | ||
| 81 | |||
| 82 | return undef; | ||
| 83 | } | ||
| 84 | |||
| 85 | # | ||
| 86 | # Store the data in a temporary file | ||
| 87 | # The file handle should be passed up the stack to the user | ||
| 88 | # in the form of a LS::Service::Response object | ||
| 89 | # | ||
| 90 | my ($fh, $local) = File::Temp::tempfile(); | ||
| 91 | |||
| 92 | unless ($fh) { | ||
| 93 | |||
| 94 | $ftp->quit; | ||
| 95 | |||
| 96 | $self->recordError('Unable to open temp file in sub getContent'); | ||
| 97 | $self->addStackTrace(); | ||
| 98 | |||
| 99 | return undef; | ||
| 100 | } | ||
| 101 | |||
| 102 | my $success = $ftp->get($path, $local); | ||
| 103 | $ftp->quit; | ||
| 104 | |||
| 105 | unless ($success) { | ||
| 106 | |||
| 107 | File::Temp::unlink0($fh, $local); | ||
| 108 | |||
| 109 | $self->recordError('Download failed in sub getContent'); | ||
| 110 | $self->addStackTrace(); | ||
| 111 | |||
| 112 | return undef; | ||
| 113 | } | ||
| 114 | |||
| 115 | local($/) = undef; | ||
| 116 | |||
| 117 | seek($fh, 0, 0); | ||
| 118 | binmode $fh; | ||
| 119 | |||
| 120 | my $data = <$fh>; | ||
| 121 | |||
| 122 | if (File::Temp::unlink0($fh, $local)) { | ||
| 123 | |||
| 124 | $self->recordError( undef ); | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | |||
| 128 | $self->recordError("Failed to delete temp file $local in sub getContent"); | ||
| 129 | $self->addStackTrace(); | ||
| 130 | } | ||
| 131 | |||
| 132 | return \$data; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub getMetadata { | ||
| 136 | |||
| 137 | my ($self, %options) = @_; | ||
| 138 | |||
| 139 | unless($options{'lsid'} && | ||
| 140 | $options{'url'}) { | ||
| 141 | |||
| 142 | return undef; | ||
| 143 | } | ||
| 144 | |||
| 145 | my $url = URI->new($options{'url'}); | ||
| 146 | |||
| 147 | unless ($url && $url->isa('URI::ftp')) { | ||
| 148 | |||
| 149 | $self->recordError('Invalid FTP URL in sub getMetadata'); | ||
| 150 | $self->addStackTrace(); | ||
| 151 | |||
| 152 | return undef; | ||
| 153 | } | ||
| 154 | |||
| 155 | my $data_ref = $self->getContent(url=> $url); | ||
| 156 | |||
| 157 | return undef unless($data_ref); | ||
| 158 | |||
| 159 | return LS::Service::Response->new(response=> ${ $data_ref } ); | ||
| 160 | } | ||
| 161 | |||
| 162 | sub getMetadataSubset { | ||
| 163 | |||
| 164 | my $self = shift; | ||
| 165 | |||
| 166 | return $self->getMetadata(@_); | ||
| 167 | } | ||
| 168 | |||
| 169 | sub getData { | ||
| 170 | |||
| 171 | my $self = shift; | ||
| 172 | |||
| 173 | return $self->getMetadata(@_); | ||
| 174 | } | ||
| 175 | |||
| 176 | sub getDataByRange { | ||
| 177 | |||
| 178 | my $self = shift; | ||
| 179 | |||
| 180 | return $self->getMetadata(@_); | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | __END__ |