001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.wfsv.response.v1_1_0;
006:
007: import java.io.IOException;
008: import java.io.OutputStream;
009: import java.util.Arrays;
010: import java.util.HashMap;
011: import java.util.HashSet;
012: import java.util.Iterator;
013: import java.util.List;
014: import java.util.Map;
015: import java.util.Set;
016:
017: import net.opengis.wfs.BaseRequestType;
018: import net.opengis.wfs.GetFeatureType;
019: import net.opengis.wfs.ResultTypeType;
020: import net.opengis.wfsv.VersionedFeatureCollectionType;
021:
022: import org.geoserver.ows.Response;
023: import org.geoserver.ows.util.OwsUtils;
024: import org.geoserver.ows.util.RequestUtils;
025: import org.geoserver.ows.util.ResponseUtils;
026: import org.geoserver.platform.Operation;
027: import org.geoserver.platform.ServiceException;
028: import org.geoserver.wfs.WFS;
029: import org.geoserver.wfs.WFSException;
030: import org.geoserver.wfs.xml.GML3OutputFormat;
031: import org.geoserver.wfs.xml.v1_1_0.WFSConfiguration;
032: import org.geotools.feature.FeatureCollection;
033: import org.geotools.feature.FeatureType;
034: import org.geotools.xml.Encoder;
035: import org.vfny.geoserver.global.Data;
036: import org.vfny.geoserver.global.FeatureTypeInfo;
037: import org.xml.sax.SAXException;
038:
039: /**
040: * Works just like {@link GML3OutputFormat}, but refers to DescribeVersionedFeatureType and encodes
041: * a VersionedFeatureCollection
042: *
043: * @author Andrea Aime
044: *
045: */
046: public class VersionedGML3OutputFormat extends Response {
047:
048: private WFS wfs;
049:
050: private Data catalog;
051:
052: private WFSConfiguration configuration;
053:
054: public VersionedGML3OutputFormat(WFS wfs, Data catalog,
055: WFSConfiguration configuration) {
056: super (VersionedFeatureCollectionType.class, new HashSet(Arrays
057: .asList(new Object[] { "gml3",
058: "text/xml; subtype=gml/3.1.1" })));
059:
060: this .wfs = wfs;
061: this .catalog = catalog;
062: this .configuration = configuration;
063: }
064:
065: public String getMimeType(Object value, Operation operation) {
066: return "text/xml; subtype=gml/3.1.1";
067: }
068:
069: public void write(Object value, OutputStream output,
070: Operation getFeature) throws ServiceException, IOException {
071: VersionedFeatureCollectionType results = (VersionedFeatureCollectionType) value;
072: List featureCollections = results.getFeature();
073:
074: // round up the info objects for each feature collection
075: HashMap /* <String,Set> */ns2metas = new HashMap();
076:
077: for (Iterator fc = featureCollections.iterator(); fc.hasNext();) {
078: FeatureCollection features = (FeatureCollection) fc.next();
079: FeatureType featureType = features.getSchema();
080:
081: // load the metadata for the feature type
082: String namespaceURI = featureType.getNamespace().toString();
083: FeatureTypeInfo meta = catalog.getFeatureTypeInfo(
084: featureType.getTypeName(), namespaceURI);
085:
086: if (meta == null)
087: throw new WFSException("Could not find feature type "
088: + namespaceURI + ":"
089: + featureType.getTypeName()
090: + " in the GeoServer catalog");
091:
092: // add it to the map
093: Set metas = (Set) ns2metas.get(namespaceURI);
094:
095: if (metas == null) {
096: metas = new HashSet();
097: ns2metas.put(namespaceURI, metas);
098: }
099:
100: metas.add(meta);
101: }
102:
103: Encoder encoder = new Encoder(configuration, configuration
104: .schema());
105:
106: // declare wfs schema location
107: BaseRequestType gft = (BaseRequestType) getFeature
108: .getParameters()[0];
109:
110: String proxifiedBaseUrl = RequestUtils.proxifiedBaseURL(gft
111: .getBaseUrl(), wfs.getGeoServer().getProxyBaseUrl());
112: encoder.setSchemaLocation(
113: org.geoserver.wfsv.xml.v1_1_0.WFSV.NAMESPACE,
114: ResponseUtils.appendPath(proxifiedBaseUrl,
115: "schemas/wfs/1.1.0/wfs.xsd"));
116:
117: // declare application schema namespaces
118: for (Iterator i = ns2metas.entrySet().iterator(); i.hasNext();) {
119: Map.Entry entry = (Map.Entry) i.next();
120:
121: String namespaceURI = (String) entry.getKey();
122: Set metas = (Set) entry.getValue();
123:
124: StringBuffer typeNames = new StringBuffer();
125:
126: for (Iterator m = metas.iterator(); m.hasNext();) {
127: FeatureTypeInfo meta = (FeatureTypeInfo) m.next();
128: typeNames.append(meta.getName());
129:
130: if (m.hasNext()) {
131: typeNames.append(",");
132: }
133: }
134:
135: // set the schema location
136: encoder.setSchemaLocation(namespaceURI, ResponseUtils
137: .appendQueryString(proxifiedBaseUrl + "wfs",
138: "service=WFSV&version=1.1.0&request=DescribeVersionedFeatureType&typeName="
139: + typeNames.toString()));
140: }
141:
142: try {
143: encoder.encode(results,
144: org.geoserver.wfs.xml.v1_1_0.WFS.FEATURECOLLECTION,
145: output);
146: } catch (SAXException e) {
147: //SAXException does not sets initCause(). Instead, it holds its own "exception" field.
148: if (e.getException() != null && e.getCause() == null) {
149: e.initCause(e.getException());
150: }
151: String msg = "Error occurred encoding features";
152: throw (IOException) new IOException(msg).initCause(e);
153: }
154: }
155:
156: public boolean canHandle(Operation operation) {
157: // GetVersionedFeature operation?
158: if ("GetVersionedFeature".equalsIgnoreCase(operation.getId())) {
159: // also check that the resultType is "results"
160: GetFeatureType request = (GetFeatureType) OwsUtils
161: .parameter(operation.getParameters(),
162: GetFeatureType.class);
163:
164: if (request.getResultType() == ResultTypeType.RESULTS_LITERAL) {
165: return true;
166: }
167: }
168:
169: return false;
170: }
171:
172: }
|