- throw a MediaException if script fails, and use the >> operator
[platform/upstream/libzypp.git] / zypp / repo / ServiceRepos.cc
1 #include <iostream>
2 #include <sstream>
3 #include "zypp/base/Logger.h"
4 #include "zypp/repo/ServiceRepos.h"
5 #include "zypp/media/MediaException.h"
6 #include "zypp/parser/RepoFileReader.h"
7 #include "zypp/media/MediaManager.h"
8 #include "zypp/parser/RepoindexFileReader.h"
9 #include "zypp/ExternalProgram.h"
10
11 using std::stringstream;
12
13 namespace zypp
14 {
15 namespace repo
16 {
17
18 class ServiceRepos::Impl
19 {
20 public:
21     Impl()
22     {
23     }
24     
25     virtual ~Impl()
26     {
27     }
28 };
29
30 class RIMServiceRepos : public ServiceRepos::Impl
31 {
32 public:
33     ServiceRepos::ProcessRepo _callback;
34     
35     RIMServiceRepos(const ServiceInfo &service,
36                     const ServiceRepos::ProcessRepo & callback,
37                     const ProgressData::ReceiverFnc &progress = ProgressData::ReceiverFnc() )
38         : _callback(callback)
39     {
40       // repoindex.xml must be fetched always without using cookies (bnc #573897)
41       Url serviceUrl( service.url() );
42       serviceUrl.setQueryParam( "cookies", "0" );
43       
44       // download the repo index file
45       media::MediaManager mediamanager;
46       media::MediaAccessId mid = mediamanager.open( serviceUrl );
47       mediamanager.attach( mid );
48       mediamanager.provideFile( mid, "repo/repoindex.xml" );
49       Pathname path = mediamanager.localPath(mid, "repo/repoindex.xml" );
50       parser::RepoindexFileReader reader(path, _callback);
51       mediamanager.release( mid );
52       mediamanager.close( mid );
53     }
54     
55     ~RIMServiceRepos()
56     {
57
58     }
59 };
60
61 class PluginServiceRepos : public ServiceRepos::Impl
62 {
63 public:
64     ServiceRepos::ProcessRepo _callback;
65     
66     PluginServiceRepos(const ServiceInfo &service,
67                       const ServiceRepos::ProcessRepo & callback,
68                       const ProgressData::ReceiverFnc &progress = ProgressData::ReceiverFnc() )
69         : _callback(callback)
70     {
71       Url serviceUrl( service.url() );
72       stringstream buffer;
73      
74       ExternalProgram prog(serviceUrl.getPathName(), ExternalProgram::Stderr_To_Stdout, false, -1, true);
75       prog >> buffer;
76
77       // Services code in zypper is not ready to handle other
78       // types of exceptions yet
79       if ( prog.close() != 0 )
80           ZYPP_THROW(media::MediaException(buffer.str()));
81
82       parser::RepoFileReader parser(buffer, _callback);
83     }
84     
85     ~PluginServiceRepos()
86     {
87
88     }
89 };
90
91     
92 ServiceRepos::ServiceRepos(const ServiceInfo &service,
93                            const ServiceRepos::ProcessRepo & callback,
94                            const ProgressData::ReceiverFnc &progress)
95     : _impl( (service.type() == ServiceType::PLUGIN) ? (ServiceRepos::Impl *)(new PluginServiceRepos(service, callback, progress)) : (ServiceRepos::Impl *)(new RIMServiceRepos(service, callback, progress)))
96 {
97 }
98     
99 ServiceRepos::~ServiceRepos()
100 {
101 }
102     
103
104 }
105 }