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.wfs.response;
06:
07: import net.opengis.wfs.FeatureCollectionType;
08: import net.opengis.wfs.GetFeatureType;
09: import net.opengis.wfs.ResultTypeType;
10: import net.opengis.wfs.WfsFactory;
11: import org.geoserver.ows.Response;
12: import org.geoserver.ows.util.OwsUtils;
13: import org.geoserver.ows.util.ResponseUtils;
14: import org.geoserver.platform.Operation;
15: import org.geoserver.platform.ServiceException;
16: import org.geoserver.wfs.WFS;
17: import org.geoserver.wfs.xml.v1_1_0.WFSConfiguration;
18: import org.geotools.xml.Encoder;
19: import org.xml.sax.SAXException;
20: import java.io.IOException;
21: import java.io.OutputStream;
22:
23: /**
24: * WFS output format for a GetFeature operation in which the resultType is "hits".
25: *
26: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
27: *
28: */
29: public class HitsOutputFormat extends Response {
30: /**
31: * WFS configuration
32: */
33: WFS wfs;
34:
35: /**
36: * Xml configuration
37: */
38: WFSConfiguration configuration;
39:
40: public HitsOutputFormat(WFS wfs, WFSConfiguration configuration) {
41: super (FeatureCollectionType.class);
42:
43: this .wfs = wfs;
44: this .configuration = configuration;
45: }
46:
47: /**
48: * @return "text/xml";
49: */
50: public String getMimeType(Object value, Operation operation)
51: throws ServiceException {
52: return "text/xml";
53: }
54:
55: /**
56: * Checks that the resultType is of type "hits".
57: */
58: public boolean canHandle(Operation operation) {
59: GetFeatureType request = (GetFeatureType) OwsUtils.parameter(
60: operation.getParameters(), GetFeatureType.class);
61:
62: return (request != null)
63: && (request.getResultType() == ResultTypeType.HITS_LITERAL);
64: }
65:
66: public void write(Object value, OutputStream output,
67: Operation operation) throws IOException, ServiceException {
68: FeatureCollectionType featureCollection = (FeatureCollectionType) value;
69:
70: //create a new feautre collcetion type with just the numbers
71: FeatureCollectionType hits = WfsFactory.eINSTANCE
72: .createFeatureCollectionType();
73: hits.setNumberOfFeatures(featureCollection
74: .getNumberOfFeatures());
75: hits.setTimeStamp(featureCollection.getTimeStamp());
76:
77: Encoder encoder = new Encoder(configuration, configuration
78: .schema());
79: encoder.setSchemaLocation(
80: org.geoserver.wfs.xml.v1_1_0.WFS.NAMESPACE,
81: ResponseUtils.appendPath(wfs.getSchemaBaseURL(),
82: "wfs/1.1.0/wfs.xsd"));
83:
84: try {
85: encoder.encode(hits,
86: org.geoserver.wfs.xml.v1_1_0.WFS.FEATURECOLLECTION,
87: output);
88: } catch (SAXException e) {
89: //SAXException does not sets initCause(). Instead, it holds its own "exception" field.
90: if (e.getException() != null && e.getCause() == null) {
91: e.initCause(e.getException());
92: }
93: throw (IOException) new IOException("Encoding error ")
94: .initCause(e);
95: }
96: }
97: }
|