01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.responders;
04:
05: import fitnesse.*;
06: import fitnesse.components.XmlWriter;
07: import fitnesse.authentication.*;
08: import fitnesse.wiki.*;
09: import fitnesse.http.*;
10:
11: import java.io.*;
12: import org.w3c.dom.Document;
13:
14: public class SerializedPageResponder implements SecureResponder {
15: public Response makeResponse(FitNesseContext context,
16: Request request) throws Exception {
17:
18: WikiPage page = getRequestedPage(request, context);
19: if (page == null)
20: return new NotFoundResponder().makeResponse(context,
21: request);
22:
23: if ("pages".equals(request.getInput("type"))) {
24: Document doc = new PageXmlizer().xmlize(page);
25: SimpleResponse response = makeResponseWithxml(doc);
26: return response;
27: } else if ("data".equals(request.getInput("type"))) {
28: Document doc = new PageXmlizer().xmlize(page.getData());
29: SimpleResponse response = makeResponseWithxml(doc);
30: return response;
31: } else {
32: Object object = getObjectToSerialize(request, page);
33: byte[] bytes = serializeToBytes(object);
34: return responseWith(bytes);
35: }
36: }
37:
38: private SimpleResponse makeResponseWithxml(Document doc)
39: throws Exception {
40: // TODO MdM Shoudl probably use a StreamedResponse
41: ByteArrayOutputStream output = new ByteArrayOutputStream();
42: XmlWriter xmlWriter = new XmlWriter(output);
43: xmlWriter.write(doc);
44: xmlWriter.flush();
45: xmlWriter.close();
46: SimpleResponse response = new SimpleResponse();
47: response.setContentType("text/xml");
48: response.setContent(output.toByteArray());
49: return response;
50: }
51:
52: private Object getObjectToSerialize(Request request, WikiPage page)
53: throws Exception {
54: Object object;
55: if ("bones".equals(request.getInput("type")))
56: object = new ProxyPage(page);
57: else if ("meat".equals(request.getInput("type"))) {
58: PageData originalData = page.getData();
59: if (request.hasInput("version"))
60: originalData = page.getDataVersion((String) request
61: .getInput("version"));
62: PageData data = new PageData(originalData);
63:
64: object = data;
65: } else
66: throw new Exception("Improper use of proxy retrieval");
67: return object;
68: }
69:
70: private WikiPage getRequestedPage(Request request,
71: FitNesseContext context) throws Exception {
72: String resource = request.getResource();
73: WikiPagePath path = PathParser.parse(resource);
74: WikiPage page = context.root.getPageCrawler().getPage(
75: context.root, path);
76: return page;
77: }
78:
79: private SimpleResponse responseWith(byte[] bytes) {
80: SimpleResponse response = new SimpleResponse();
81: response.setContentType("application/octet-stream");
82: response.setContent(bytes);
83: return response;
84: }
85:
86: private byte[] serializeToBytes(Object object) throws IOException {
87: ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
88: ObjectOutputStream os = new ObjectOutputStream(byteStream);
89: os.writeObject(object);
90: os.close();
91: byte[] bytes = byteStream.toByteArray();
92: return bytes;
93: }
94:
95: public SecureOperation getSecureOperation() {
96: return new SecureReadOperation();
97: }
98:
99: }
|