01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.wfsv.response.v1_1_0;
06:
07: import freemarker.template.Configuration;
08: import freemarker.template.Template;
09: import freemarker.template.TemplateException;
10: import net.opengis.wfs.FeatureCollectionType;
11: import net.opengis.wfs.ResultTypeType;
12: import net.opengis.wfsv.GetLogType;
13: import org.geoserver.ows.Response;
14: import org.geoserver.ows.util.OwsUtils;
15: import org.geoserver.platform.Operation;
16: import org.geoserver.platform.ServiceException;
17: import org.geoserver.template.GeoServerTemplateLoader;
18: import org.geotools.feature.FeatureCollection;
19: import java.io.IOException;
20: import java.io.OutputStream;
21: import java.io.OutputStreamWriter;
22: import java.nio.charset.Charset;
23:
24: /**
25: * Html, templatized output format for the GetLog response
26: * @author Andrea Aime, TOPP
27: *
28: */
29: public class GetLogHtmlOutputFormat extends Response {
30: private static Configuration templateConfig;
31:
32: static {
33: //initialize the template engine, this is static to maintain a cache
34: // over instantiations of kml writer
35: templateConfig = new Configuration();
36: templateConfig.setObjectWrapper(new FeatureISODateWrapper());
37: }
38:
39: public GetLogHtmlOutputFormat() {
40: super (FeatureCollectionType.class, "HTML");
41: }
42:
43: public String getMimeType(Object value, Operation operation)
44: throws ServiceException {
45: return "text/html";
46: }
47:
48: public boolean canHandle(Operation operation) {
49: if ("GetLog".equalsIgnoreCase(operation.getId())) {
50: //also check that the resultType is "results"
51: GetLogType request = (GetLogType) OwsUtils.parameter(
52: operation.getParameters(), GetLogType.class);
53:
54: return request.getResultType() == ResultTypeType.RESULTS_LITERAL;
55: }
56:
57: return false;
58: }
59:
60: public void write(Object value, OutputStream output,
61: Operation operation) throws IOException, ServiceException {
62: FeatureCollectionType fct = (FeatureCollectionType) value;
63: FeatureCollection fc = (FeatureCollection) fct.getFeature()
64: .get(0);
65:
66: // setup template subsystem
67: GeoServerTemplateLoader templateLoader = new GeoServerTemplateLoader(
68: getClass());
69: templateLoader.setFeatureType(fc.getSchema());
70:
71: Template template = null;
72:
73: synchronized (templateConfig) {
74: templateConfig.setTemplateLoader(templateLoader);
75: template = templateConfig.getTemplate("wfsvGetLog.ftl");
76: }
77:
78: try {
79: template.setOutputEncoding("UTF-8");
80: template.process(fc, new OutputStreamWriter(output, Charset
81: .forName("UTF-8")));
82: } catch (TemplateException e) {
83: String msg = "Error occured processing template.";
84: throw (IOException) new IOException(msg).initCause(e);
85: }
86: }
87: }
|