| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | OSGi::Osgish::Upload - Upload a bundle to the agent upload directory | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | package OSGi::Osgish::Agent::Upload; | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | use warnings; | ||
| 17 | use LWP::UserAgent; | ||
| 18 | use HTTP::Request::Common; | ||
| 19 | use OSGi::Osgish; | ||
| 20 | use JMX::Jmx4Perl::Agent::UserAgent; | ||
| 21 | use Data::Dumper; | ||
| 22 | use vars qw($HAS_PROGRESS_BAR); | ||
| 23 | |||
| 24 | my $UPLOAD_SERVICE_NAME = "osgish:type=Upload"; | ||
| 25 | |||
| 26 | BEGIN { | ||
| 27 | eval { | ||
| 28 | require "Term/ProgressBar.pm"; | ||
| 29 | $HAS_PROGRESS_BAR = 1; | ||
| 30 | }; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub new { | ||
| 34 | my $class = shift; | ||
| 35 | my $agent = shift || die "No OSGi::Osgish object given"; | ||
| 36 | my $ua = new JMX::Jmx4Perl::Agent::UserAgent(); | ||
| 37 | $ua->jjagent_config($agent->cfg()); | ||
| 38 | my $self = { | ||
| 39 | url => $agent->cfg('url') . "-upload", | ||
| 40 | agent => $agent, | ||
| 41 | ua => $ua | ||
| 42 | }; | ||
| 43 | bless $self,(ref($class) || $class); | ||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub list { | ||
| 48 | my $self = shift; | ||
| 49 | my $agent = $self->{agent}; | ||
| 50 | $self->{list} = $agent->execute($UPLOAD_SERVICE_NAME,"listUploadDirectory"); | ||
| 51 | return $self->{list}; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub cache_update { | ||
| 55 | # Refrehs internal list cache | ||
| 56 | shift->list; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub remove { | ||
| 60 | my $self = shift; | ||
| 61 | my $file = shift || die "No file given\n"; | ||
| 62 | my $agent = $self->{agent}; | ||
| 63 | return $agent->execute($UPLOAD_SERVICE_NAME,"deleteFile",$file); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub upload { | ||
| 67 | my $self = shift; | ||
| 68 | my $file = shift; | ||
| 69 | my $cfg = {}; | ||
| 70 | if (@_) { | ||
| 71 | $cfg = ref($_[0]) eq "HASH" ? $_[0] : { @_ }; | ||
| 72 | } | ||
| 73 | #$file = glob($file) if $file =~ /^~/; | ||
| 74 | die "No file $file\n" unless $file and -f $file; | ||
| 75 | my $ua = $self->{ua}; | ||
| 76 | |||
| 77 | { | ||
| 78 | local $HTTP::Request::Common::DYNAMIC_FILE_UPLOAD = 1; | ||
| 79 | |||
| 80 | my $req = | ||
| 81 | POST | ||
| 82 | $self->{url}, | ||
| 83 | 'Content_Type' => 'form-data', | ||
| 84 | 'Content' => { "upload" => [ $file ] }; | ||
| 85 | my $reader = $self->_content_reader($req->content(),$cfg,$req->header('Content_Length')); | ||
| 86 | $req->content($reader); | ||
| 87 | my $resp = $ua->request($req); | ||
| 88 | die "Error while uploading $file: ",$resp->message if $resp->is_error; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub complete_files_in_upload_dir { | ||
| 93 | my $self = shift; | ||
| 94 | my $term = shift; | ||
| 95 | my $cmpl = shift; | ||
| 96 | |||
| 97 | my $list = $self->{list} || $self->list; | ||
| 98 | my $file = $cmpl->{str} || ""; | ||
| 99 | my $flen = length($file); | ||
| 100 | |||
| 101 | my @files = grep { substr($_,0,$flen) eq $file } keys %$list; | ||
| 102 | return \@files; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _content_reader { | ||
| 106 | my $self = shift; | ||
| 107 | my $gen = shift; | ||
| 108 | my $cfg = shift; | ||
| 109 | my $len = shift; | ||
| 110 | if ($HAS_PROGRESS_BAR && $cfg->{progress_bar}) { | ||
| 111 | my $progress = new Term::ProgressBar({name => "Upload",count => $len,remove => 1,term_width => 65}); | ||
| 112 | $progress->minor(0); | ||
| 113 | my $size = 0; | ||
| 114 | my $next_update = 0; | ||
| 115 | sub { | ||
| 116 | my $chunk = &$gen(); | ||
| 117 | $size += length($chunk) if $chunk; | ||
| 118 | $next_update = $progress->update($size) | ||
| 119 | if $size >= $next_update; | ||
| 120 | return $chunk; | ||
| 121 | } | ||
| 122 | } else { | ||
| 123 | return sub { | ||
| 124 | return &$gen(); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | #my $u = new OSGi::Osgish(url => "http://localhost:8080/j4p-upload"); | ||
| 130 | #$u->upload("n",progress_bar => 1); | ||
| 131 | |||
| 132 | =head1 LICENSE | ||
| 133 | |||
| 134 | This file is part of osgish. | ||
| 135 | |||
| 136 | Osgish is free software: you can redistribute it and/or modify | ||
| 137 | it under the terms of the GNU General Public License as published by | ||
| 138 | the Free Software Foundation, either version 2 of the License, or | ||
| 139 | (at your option) any later version. | ||
| 140 | |||
| 141 | osgish is distributed in the hope that it will be useful, | ||
| 142 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 143 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 144 | GNU General Public License for more details. | ||
| 145 | |||
| 146 | You should have received a copy of the GNU General Public License | ||
| 147 | along with osgish. If not, see <http://www.gnu.org/licenses/>. | ||
| 148 | |||
| 149 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 150 | further details. | ||
| 151 | |||
| 152 | =head1 PROFESSIONAL SERVICES | ||
| 153 | |||
| 154 | Just in case you need professional support for this module (or JMX or OSGi in | ||
| 155 | general), you might want to have a look at www.consol.com Contact | ||
| 156 | roland.huss@consol.de for further information (or use the contact form at | ||
| 157 | http://www.consol.com/contact/) | ||
| 158 | |||
| 159 | =head1 AUTHOR | ||
| 160 | |||
| 161 | roland@cpan.org | ||
| 162 | |||
| 163 | =cut | ||
| 164 | |||
| 165 | 1; |