]> git.llucax.com Git - z.facultad/75.42/plaqui.git/blob - Server/src/response.cpp
- Se completa un poco la doc del cliente.
[z.facultad/75.42/plaqui.git] / Server / src / response.cpp
1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
3 //                                  PlaQui
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
6 //
7 // PlaQui is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the Free Software
9 // Foundation; either version 2 of the License, or (at your option) any later
10 // version.
11 //
12 // PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 // details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
19 // Place, Suite 330, Boston, MA  02111-1307  USA
20 //----------------------------------------------------------------------------
21 // Creado:  lun nov 17 21:02:22 ART 2003
22 // Autores: Leandro Lucarella <llucare@fi.uba.ar>
23 //----------------------------------------------------------------------------
24 //
25 // $Id$
26 //
27
28 #include "plaqui/server/response.h"
29 #include "plaqui/server/string.h"
30 #include <sstream>
31 #ifdef DEBUG
32 #       include <iostream>
33 #endif // DEBUG
34
35 using namespace std;
36
37 namespace PlaQui {
38
39 namespace Server {
40
41 Response::~Response(void) {
42 #ifdef DEBUG
43         cerr << __FILE__ << "(" << __LINE__ << ")"
44                 << ": destructor." << endl;
45 #endif // DEBUG
46 }
47
48 Response::Response(const Code& code, const string& desc):
49                 xml_version("1.0"), xml_code(code), xml_description(desc) {
50 #ifdef DEBUG
51         cerr << __FILE__ << "(" << __LINE__ << ")"
52                 << ": constructor(code = " << code << ", desc = " << desc << ");"
53                 << endl;
54 #endif // DEBUG
55         headers["Content-Type"] = "text/xml; charset=iso-8859-1";
56         build();
57 }
58
59 Response::Response(const string& contents, const string& desc, const Code& code):
60                 xml_version("1.0"), xml_code(code), xml_description(desc),
61                 xml_contents(contents) {
62 #ifdef DEBUG
63         cerr << __FILE__ << "(" << __LINE__ << ")"
64                 << ": constructor(body.length = " << contents.length()
65                 << ", desc = " << desc << ", code = " << code << ");" << endl;
66 #endif // DEBUG
67         headers["Content-Type"] = "text/xml; charset=iso-8859-1";
68         build();
69 }
70
71 void Response::build(void) {
72         String b;
73         b.from(xml_code);
74         b = string("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n"
75                         "<plaqui-response code=\"") + b + "\" version=\""
76                         + xml_version + "\" ";
77         if (xml_description.length()) {
78                 b += "description=\"" + xml_description + "\" ";
79         }
80         if (xml_contents.length()) {
81                 b += ">\n" + xml_contents + "\n</plaqui-response>\n";
82         } else {
83                 b += "/>\n";
84         }
85         set_body(b);
86         switch (xml_code) {
87                 case OK:
88                         status_code = HTTPMessage::OK;
89                         break;
90                 case INVALID_TARGET:
91                 case INVALID_COMMAND:
92                 case CONNECTION_NOT_FOUND:
93                 case TRANSMISSION_NOT_FOUND:
94                 case PLANT_NOT_FOUND:
95                 case ELEMENT_NOT_FOUND:
96                 case ELEMENT_INPUT_NOT_FOUND:
97                         status_code = HTTPMessage::INTERNAL_SERVER_ERROR;
98                         break;
99                 case ALLREADY_EXISTS:
100                 case ARGUMENT_MISSING:
101                         status_code = HTTPMessage::CONFLICT;
102                         break;
103                 case UNKNOWN_ERROR:
104                 case ERROR_STARTING_TRANSMISSION:
105                 case ERROR_GETING_PLANT_XML:
106                 case ERROR_CHANGING_ELEMENT_INPUT:
107                         status_code = HTTPMessage::INTERNAL_SERVER_ERROR;
108                         break;
109                 default:
110                         status_code = HTTPMessage::INTERNAL_SERVER_ERROR;
111                         break;
112         }
113 }
114
115 const string& Response::get_version(void) const {
116         return xml_version;
117 }
118
119 const string& Response::set_version(const string& version_) {
120         xml_version = version_;
121         build();
122         return version_;
123 }
124
125 const Response::Code& Response::get_code(void) const {
126         return xml_code;
127 }
128
129 const Response::Code& Response::set_code(const Response::Code& code_) {
130         xml_code = code_;
131         build();
132         return code_;
133 }
134
135 const string& Response::get_description(void) const {
136         return xml_description;
137 }
138
139 const string& Response::set_description(const string& description_) {
140         xml_description = description_;
141         build();
142         return description_;
143 }
144
145 const string& Response::get_contents(void) const {
146         return xml_contents;
147 }
148
149 const string& Response::set_contents(const string& contents_) {
150         xml_contents = contents_;
151         build();
152         return contents_;
153 }
154
155 istream& operator>>(istream& is, Response& resp)
156                 throw (HTTPResponse::Error, ios::failure, sockerr,
157                                 xmlpp::parse_error) {
158 #ifdef DEBUG
159         cerr << __FILE__ << "(" << __LINE__ << ")" << ": operator>>()" << endl;
160 #endif // DEBUG
161         is >> static_cast<HTTPResponse&>(resp);
162         if (resp.get_body().length()) {
163                 Response::Parser(resp).parse_memory(resp.get_body());
164         }
165         resp.build();
166         return is;
167 }
168
169 ostream& operator<<(ostream& os, const Response& resp) {
170 #ifdef DEBUG
171         cerr << __FILE__ << "(" << __LINE__ << ")" << ": operator<<()" << endl;
172 #endif // DEBUG
173         os << static_cast<const HTTPResponse&>(resp);
174         return os;
175 }
176
177 } // namespace Server
178
179 } // namespace PlaQui
180